select.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // Copyright 2018 Huan Du. All rights reserved.
  2. // Licensed under the MIT license that can be found in the LICENSE file.
  3. package sqlbuilder
  4. import (
  5. "bytes"
  6. "fmt"
  7. "strconv"
  8. "strings"
  9. )
  10. // NewSelectBuilder creates a new SELECT builder.
  11. func NewSelectBuilder() *SelectBuilder {
  12. return DefaultFlavor.NewSelectBuilder()
  13. }
  14. func newSelectBuilder() *SelectBuilder {
  15. args := &Args{}
  16. return &SelectBuilder{
  17. Cond: Cond{
  18. Args: args,
  19. },
  20. limit: -1,
  21. offset: -1,
  22. args: args,
  23. }
  24. }
  25. // SelectBuilder is a builder to build SELECT.
  26. type SelectBuilder struct {
  27. Cond
  28. distinct bool
  29. tables []string
  30. selectCols []string
  31. whereExprs []string
  32. havingExprs []string
  33. groupByCols []string
  34. orderByCols []string
  35. order string
  36. limit int
  37. offset int
  38. args *Args
  39. }
  40. // Distinct marks this SELECT as DISTINCT.
  41. func (sb *SelectBuilder) Distinct() *SelectBuilder {
  42. sb.distinct = true
  43. return sb
  44. }
  45. // Select sets columns in SELECT.
  46. func (sb *SelectBuilder) Select(col ...string) *SelectBuilder {
  47. sb.selectCols = EscapeAll(col...)
  48. return sb
  49. }
  50. // From sets table names in SELECT.
  51. func (sb *SelectBuilder) From(table ...string) *SelectBuilder {
  52. sb.tables = table
  53. return sb
  54. }
  55. // Where sets expressions of WHERE in SELECT.
  56. func (sb *SelectBuilder) Where(andExpr ...string) *SelectBuilder {
  57. sb.whereExprs = append(sb.whereExprs, andExpr...)
  58. return sb
  59. }
  60. // Having sets expressions of HAVING in SELECT.
  61. func (sb *SelectBuilder) Having(andExpr ...string) *SelectBuilder {
  62. sb.havingExprs = append(sb.havingExprs, andExpr...)
  63. return sb
  64. }
  65. // GroupBy sets columns of GROUP BY in SELECT.
  66. func (sb *SelectBuilder) GroupBy(col ...string) *SelectBuilder {
  67. sb.groupByCols = EscapeAll(col...)
  68. return sb
  69. }
  70. // OrderBy sets columns of ORDER BY in SELECT.
  71. func (sb *SelectBuilder) OrderBy(col ...string) *SelectBuilder {
  72. sb.orderByCols = EscapeAll(col...)
  73. return sb
  74. }
  75. // Asc sets order of ORDER BY to ASC.
  76. func (sb *SelectBuilder) Asc() *SelectBuilder {
  77. sb.order = "ASC"
  78. return sb
  79. }
  80. // Desc sets order of ORDER BY to DESC.
  81. func (sb *SelectBuilder) Desc() *SelectBuilder {
  82. sb.order = "DESC"
  83. return sb
  84. }
  85. // Limit sets the LIMIT in SELECT.
  86. func (sb *SelectBuilder) Limit(limit int) *SelectBuilder {
  87. sb.limit = limit
  88. return sb
  89. }
  90. // Offset sets the LIMIT offset in SELECT.
  91. func (sb *SelectBuilder) Offset(offset int) *SelectBuilder {
  92. sb.offset = offset
  93. return sb
  94. }
  95. // As returns an AS expression.
  96. func (sb *SelectBuilder) As(col, alias string) string {
  97. return fmt.Sprintf("%v AS %v", col, Escape(alias))
  98. }
  99. // BuilderAs returns an AS expression wrapping a complex SQL.
  100. // According to SQL syntax, SQL built by builder is surrounded by parens.
  101. func (sb *SelectBuilder) BuilderAs(builder Builder, alias string) string {
  102. return fmt.Sprintf("(%v) AS %v", sb.Var(builder), Escape(alias))
  103. }
  104. // String returns the compiled SELECT string.
  105. func (sb *SelectBuilder) String() string {
  106. s, _ := sb.Build()
  107. return s
  108. }
  109. // Build returns compiled SELECT string and args.
  110. // They can be used in `DB#Query` of package `database/sql` directly.
  111. func (sb *SelectBuilder) Build() (sql string, args []interface{}) {
  112. return sb.BuildWithFlavor(sb.args.Flavor)
  113. }
  114. // BuildWithFlavor returns compiled SELECT string and args with flavor and initial args.
  115. // They can be used in `DB#Query` of package `database/sql` directly.
  116. func (sb *SelectBuilder) BuildWithFlavor(flavor Flavor, initialArg ...interface{}) (sql string, args []interface{}) {
  117. buf := &bytes.Buffer{}
  118. buf.WriteString("SELECT ")
  119. if sb.distinct {
  120. buf.WriteString("DISTINCT ")
  121. }
  122. buf.WriteString(strings.Join(sb.selectCols, ", "))
  123. buf.WriteString(" FROM ")
  124. buf.WriteString(strings.Join(sb.tables, ", "))
  125. if len(sb.whereExprs) > 0 {
  126. buf.WriteString(" WHERE ")
  127. buf.WriteString(strings.Join(sb.whereExprs, " AND "))
  128. }
  129. if len(sb.groupByCols) > 0 {
  130. buf.WriteString(" GROUP BY ")
  131. buf.WriteString(strings.Join(sb.groupByCols, ", "))
  132. if len(sb.havingExprs) > 0 {
  133. buf.WriteString(" HAVING ")
  134. buf.WriteString(strings.Join(sb.havingExprs, " AND "))
  135. }
  136. }
  137. if len(sb.orderByCols) > 0 {
  138. buf.WriteString(" ORDER BY ")
  139. buf.WriteString(strings.Join(sb.orderByCols, ", "))
  140. if sb.order != "" {
  141. buf.WriteRune(' ')
  142. buf.WriteString(sb.order)
  143. }
  144. }
  145. if sb.limit >= 0 {
  146. buf.WriteString(" LIMIT ")
  147. buf.WriteString(strconv.Itoa(sb.limit))
  148. if sb.offset >= 0 {
  149. buf.WriteString(" OFFSET ")
  150. buf.WriteString(strconv.Itoa(sb.offset))
  151. }
  152. }
  153. return sb.Args.CompileWithFlavor(buf.String(), flavor, initialArg...)
  154. }
  155. // SetFlavor sets the flavor of compiled sql.
  156. func (sb *SelectBuilder) SetFlavor(flavor Flavor) (old Flavor) {
  157. old = sb.args.Flavor
  158. sb.args.Flavor = flavor
  159. return
  160. }
  161. // Copy the builder
  162. func (sb *SelectBuilder) Copy() *SelectBuilder {
  163. return &SelectBuilder{
  164. Cond: sb.Cond,
  165. distinct: sb.distinct,
  166. tables: sb.tables,
  167. selectCols: sb.selectCols,
  168. whereExprs: sb.whereExprs,
  169. havingExprs: sb.havingExprs,
  170. groupByCols: sb.groupByCols,
  171. orderByCols: sb.orderByCols,
  172. order: sb.order,
  173. limit: sb.limit,
  174. offset: sb.offset,
  175. args: sb.args.Copy(),
  176. }
  177. }