qb.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright 2014 beego Author. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package orm
  15. import "errors"
  16. // QueryBuilder is the Query builder interface
  17. type QueryBuilder interface {
  18. Select(fields ...string) QueryBuilder
  19. ForUpdate() QueryBuilder
  20. From(tables ...string) QueryBuilder
  21. InnerJoin(table string) QueryBuilder
  22. LeftJoin(table string) QueryBuilder
  23. RightJoin(table string) QueryBuilder
  24. On(cond string) QueryBuilder
  25. Where(cond string) QueryBuilder
  26. And(cond string) QueryBuilder
  27. Or(cond string) QueryBuilder
  28. In(vals ...string) QueryBuilder
  29. OrderBy(fields ...string) QueryBuilder
  30. Asc() QueryBuilder
  31. Desc() QueryBuilder
  32. Limit(limit int) QueryBuilder
  33. Offset(offset int) QueryBuilder
  34. GroupBy(fields ...string) QueryBuilder
  35. Having(cond string) QueryBuilder
  36. Update(tables ...string) QueryBuilder
  37. Set(kv ...string) QueryBuilder
  38. Delete(tables ...string) QueryBuilder
  39. InsertInto(table string, fields ...string) QueryBuilder
  40. Values(vals ...string) QueryBuilder
  41. Subquery(sub string, alias string) string
  42. String() string
  43. }
  44. // NewQueryBuilder return the QueryBuilder
  45. func NewQueryBuilder(driver string) (qb QueryBuilder, err error) {
  46. if driver == "mysql" {
  47. qb = new(MySQLQueryBuilder)
  48. } else if driver == "tidb" {
  49. qb = new(TiDBQueryBuilder)
  50. } else if driver == "postgres" {
  51. err = errors.New("postgres query builder is not supported yet")
  52. } else if driver == "sqlite" {
  53. err = errors.New("sqlite query builder is not supported yet")
  54. } else {
  55. err = errors.New("unknown driver for query builder")
  56. }
  57. return
  58. }