qb_mysql.go 5.0 KB

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