You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
3.5 KiB

2 years ago
  1. // Copyright 2016 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package builder
  5. import (
  6. "fmt"
  7. )
  8. // Select creates a select Builder
  9. func Select(cols ...string) *Builder {
  10. builder := &Builder{cond: NewCond()}
  11. return builder.Select(cols...)
  12. }
  13. func (b *Builder) selectWriteTo(w Writer) error {
  14. if len(b.from) <= 0 && !b.isNested {
  15. return ErrNoTableName
  16. }
  17. // perform limit before writing to writer when b.dialect between ORACLE and MSSQL
  18. // this avoid a duplicate writing problem in simple limit query
  19. if b.limitation != nil && (b.dialect == ORACLE || b.dialect == MSSQL) {
  20. return b.limitWriteTo(w)
  21. }
  22. if _, err := fmt.Fprint(w, "SELECT "); err != nil {
  23. return err
  24. }
  25. if len(b.selects) > 0 {
  26. for i, s := range b.selects {
  27. if _, err := fmt.Fprint(w, s); err != nil {
  28. return err
  29. }
  30. if i != len(b.selects)-1 {
  31. if _, err := fmt.Fprint(w, ","); err != nil {
  32. return err
  33. }
  34. }
  35. }
  36. } else {
  37. if _, err := fmt.Fprint(w, "*"); err != nil {
  38. return err
  39. }
  40. }
  41. if b.subQuery == nil {
  42. if _, err := fmt.Fprint(w, " FROM ", b.from); err != nil {
  43. return err
  44. }
  45. } else {
  46. if b.cond.IsValid() && len(b.from) <= 0 {
  47. return ErrUnnamedDerivedTable
  48. }
  49. if b.subQuery.dialect != "" && b.dialect != b.subQuery.dialect {
  50. return ErrInconsistentDialect
  51. }
  52. // dialect of sub-query will inherit from the main one (if not set up)
  53. if b.dialect != "" && b.subQuery.dialect == "" {
  54. b.subQuery.dialect = b.dialect
  55. }
  56. switch b.subQuery.optype {
  57. case selectType, setOpType:
  58. fmt.Fprint(w, " FROM (")
  59. if err := b.subQuery.WriteTo(w); err != nil {
  60. return err
  61. }
  62. if len(b.from) == 0 {
  63. fmt.Fprintf(w, ")")
  64. } else {
  65. fmt.Fprintf(w, ") %v", b.from)
  66. }
  67. default:
  68. return ErrUnexpectedSubQuery
  69. }
  70. }
  71. for _, v := range b.joins {
  72. b, ok := v.joinTable.(*Builder)
  73. if ok {
  74. if _, err := fmt.Fprintf(w, " %s JOIN (", v.joinType); err != nil {
  75. return err
  76. }
  77. if err := b.WriteTo(w); err != nil {
  78. return err
  79. }
  80. if v.joinAlias == "" {
  81. if _, err := fmt.Fprintf(w, ") ON "); err != nil {
  82. return err
  83. }
  84. } else {
  85. if _, err := fmt.Fprintf(w, ") AS %s ON ", v.joinAlias); err != nil {
  86. return err
  87. }
  88. }
  89. } else {
  90. if v.joinAlias == "" {
  91. if _, err := fmt.Fprintf(w, " %s JOIN %s ON ", v.joinType, v.joinTable); err != nil {
  92. return err
  93. }
  94. } else {
  95. if _, err := fmt.Fprintf(w, " %s JOIN %s AS %s ON ", v.joinType, v.joinTable, v.joinAlias); err != nil {
  96. return err
  97. }
  98. }
  99. }
  100. if err := v.joinCond.WriteTo(w); err != nil {
  101. return err
  102. }
  103. }
  104. if b.cond.IsValid() {
  105. if _, err := fmt.Fprint(w, " WHERE "); err != nil {
  106. return err
  107. }
  108. if err := b.cond.WriteTo(w); err != nil {
  109. return err
  110. }
  111. }
  112. if len(b.groupBy) > 0 {
  113. if _, err := fmt.Fprint(w, " GROUP BY ", b.groupBy); err != nil {
  114. return err
  115. }
  116. }
  117. if len(b.having) > 0 {
  118. if _, err := fmt.Fprint(w, " HAVING ", b.having); err != nil {
  119. return err
  120. }
  121. }
  122. if len(b.orderBy) > 0 {
  123. if _, err := fmt.Fprint(w, " ORDER BY ", b.orderBy); err != nil {
  124. return err
  125. }
  126. }
  127. if b.limitation != nil {
  128. if err := b.limitWriteTo(w); err != nil {
  129. return err
  130. }
  131. }
  132. return nil
  133. }
  134. // OrderBy orderBy SQL
  135. func (b *Builder) OrderBy(orderBy string) *Builder {
  136. b.orderBy = orderBy
  137. return b
  138. }
  139. // GroupBy groupby SQL
  140. func (b *Builder) GroupBy(groupby string) *Builder {
  141. b.groupBy = groupby
  142. return b
  143. }
  144. // Having having SQL
  145. func (b *Builder) Having(having string) *Builder {
  146. b.having = having
  147. return b
  148. }