1
0

qb_tidb.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Copyright 2015 TiDB 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 (
  16. "fmt"
  17. "strconv"
  18. "strings"
  19. )
  20. // TiDBQueryBuilder is the SQL build
  21. type TiDBQueryBuilder struct {
  22. Tokens []string
  23. }
  24. // Select will join the fields
  25. func (qb *TiDBQueryBuilder) Select(fields ...string) QueryBuilder {
  26. qb.Tokens = append(qb.Tokens, "SELECT", strings.Join(fields, CommaSpace))
  27. return qb
  28. }
  29. // ForUpdate add the FOR UPDATE clause
  30. func (qb *TiDBQueryBuilder) ForUpdate() QueryBuilder {
  31. qb.Tokens = append(qb.Tokens, "FOR UPDATE")
  32. return qb
  33. }
  34. // From join the tables
  35. func (qb *TiDBQueryBuilder) From(tables ...string) QueryBuilder {
  36. qb.Tokens = append(qb.Tokens, "FROM", strings.Join(tables, CommaSpace))
  37. return qb
  38. }
  39. // InnerJoin INNER JOIN the table
  40. func (qb *TiDBQueryBuilder) InnerJoin(table string) QueryBuilder {
  41. qb.Tokens = append(qb.Tokens, "INNER JOIN", table)
  42. return qb
  43. }
  44. // LeftJoin LEFT JOIN the table
  45. func (qb *TiDBQueryBuilder) LeftJoin(table string) QueryBuilder {
  46. qb.Tokens = append(qb.Tokens, "LEFT JOIN", table)
  47. return qb
  48. }
  49. // RightJoin RIGHT JOIN the table
  50. func (qb *TiDBQueryBuilder) RightJoin(table string) QueryBuilder {
  51. qb.Tokens = append(qb.Tokens, "RIGHT JOIN", table)
  52. return qb
  53. }
  54. // On join with on cond
  55. func (qb *TiDBQueryBuilder) On(cond string) QueryBuilder {
  56. qb.Tokens = append(qb.Tokens, "ON", cond)
  57. return qb
  58. }
  59. // Where join the Where cond
  60. func (qb *TiDBQueryBuilder) Where(cond string) QueryBuilder {
  61. qb.Tokens = append(qb.Tokens, "WHERE", cond)
  62. return qb
  63. }
  64. // And join the and cond
  65. func (qb *TiDBQueryBuilder) And(cond string) QueryBuilder {
  66. qb.Tokens = append(qb.Tokens, "AND", cond)
  67. return qb
  68. }
  69. // Or join the or cond
  70. func (qb *TiDBQueryBuilder) Or(cond string) QueryBuilder {
  71. qb.Tokens = append(qb.Tokens, "OR", cond)
  72. return qb
  73. }
  74. // In join the IN (vals)
  75. func (qb *TiDBQueryBuilder) In(vals ...string) QueryBuilder {
  76. qb.Tokens = append(qb.Tokens, "IN", "(", strings.Join(vals, CommaSpace), ")")
  77. return qb
  78. }
  79. // OrderBy join the Order by fields
  80. func (qb *TiDBQueryBuilder) OrderBy(fields ...string) QueryBuilder {
  81. qb.Tokens = append(qb.Tokens, "ORDER BY", strings.Join(fields, CommaSpace))
  82. return qb
  83. }
  84. // Asc join the asc
  85. func (qb *TiDBQueryBuilder) Asc() QueryBuilder {
  86. qb.Tokens = append(qb.Tokens, "ASC")
  87. return qb
  88. }
  89. // Desc join the desc
  90. func (qb *TiDBQueryBuilder) Desc() QueryBuilder {
  91. qb.Tokens = append(qb.Tokens, "DESC")
  92. return qb
  93. }
  94. // Limit join the limit num
  95. func (qb *TiDBQueryBuilder) Limit(limit int) QueryBuilder {
  96. qb.Tokens = append(qb.Tokens, "LIMIT", strconv.Itoa(limit))
  97. return qb
  98. }
  99. // Offset join the offset num
  100. func (qb *TiDBQueryBuilder) Offset(offset int) QueryBuilder {
  101. qb.Tokens = append(qb.Tokens, "OFFSET", strconv.Itoa(offset))
  102. return qb
  103. }
  104. // GroupBy join the Group by fields
  105. func (qb *TiDBQueryBuilder) GroupBy(fields ...string) QueryBuilder {
  106. qb.Tokens = append(qb.Tokens, "GROUP BY", strings.Join(fields, CommaSpace))
  107. return qb
  108. }
  109. // Having join the Having cond
  110. func (qb *TiDBQueryBuilder) Having(cond string) QueryBuilder {
  111. qb.Tokens = append(qb.Tokens, "HAVING", cond)
  112. return qb
  113. }
  114. // Update join the update table
  115. func (qb *TiDBQueryBuilder) Update(tables ...string) QueryBuilder {
  116. qb.Tokens = append(qb.Tokens, "UPDATE", strings.Join(tables, CommaSpace))
  117. return qb
  118. }
  119. // Set join the set kv
  120. func (qb *TiDBQueryBuilder) Set(kv ...string) QueryBuilder {
  121. qb.Tokens = append(qb.Tokens, "SET", strings.Join(kv, CommaSpace))
  122. return qb
  123. }
  124. // Delete join the Delete tables
  125. func (qb *TiDBQueryBuilder) Delete(tables ...string) QueryBuilder {
  126. qb.Tokens = append(qb.Tokens, "DELETE")
  127. if len(tables) != 0 {
  128. qb.Tokens = append(qb.Tokens, strings.Join(tables, CommaSpace))
  129. }
  130. return qb
  131. }
  132. // InsertInto join the insert SQL
  133. func (qb *TiDBQueryBuilder) InsertInto(table string, fields ...string) QueryBuilder {
  134. qb.Tokens = append(qb.Tokens, "INSERT INTO", table)
  135. if len(fields) != 0 {
  136. fieldsStr := strings.Join(fields, CommaSpace)
  137. qb.Tokens = append(qb.Tokens, "(", fieldsStr, ")")
  138. }
  139. return qb
  140. }
  141. // Values join the Values(vals)
  142. func (qb *TiDBQueryBuilder) Values(vals ...string) QueryBuilder {
  143. valsStr := strings.Join(vals, CommaSpace)
  144. qb.Tokens = append(qb.Tokens, "VALUES", "(", valsStr, ")")
  145. return qb
  146. }
  147. // Subquery join the sub as alias
  148. func (qb *TiDBQueryBuilder) Subquery(sub string, alias string) string {
  149. return fmt.Sprintf("(%s) AS %s", sub, alias)
  150. }
  151. // String join all Tokens
  152. func (qb *TiDBQueryBuilder) String() string {
  153. return strings.Join(qb.Tokens, " ")
  154. }