本站源代码
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.

159 lines
3.2KB

  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, unionType:
  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 _, err := fmt.Fprintf(w, ") ON "); err != nil {
  81. return err
  82. }
  83. } else {
  84. if _, err := fmt.Fprintf(w, " %s JOIN %s ON ", v.joinType, v.joinTable); err != nil {
  85. return err
  86. }
  87. }
  88. if err := v.joinCond.WriteTo(w); err != nil {
  89. return err
  90. }
  91. }
  92. if b.cond.IsValid() {
  93. if _, err := fmt.Fprint(w, " WHERE "); err != nil {
  94. return err
  95. }
  96. if err := b.cond.WriteTo(w); err != nil {
  97. return err
  98. }
  99. }
  100. if len(b.groupBy) > 0 {
  101. if _, err := fmt.Fprint(w, " GROUP BY ", b.groupBy); err != nil {
  102. return err
  103. }
  104. }
  105. if len(b.having) > 0 {
  106. if _, err := fmt.Fprint(w, " HAVING ", b.having); err != nil {
  107. return err
  108. }
  109. }
  110. if len(b.orderBy) > 0 {
  111. if _, err := fmt.Fprint(w, " ORDER BY ", b.orderBy); err != nil {
  112. return err
  113. }
  114. }
  115. if b.limitation != nil {
  116. if err := b.limitWriteTo(w); err != nil {
  117. return err
  118. }
  119. }
  120. return nil
  121. }
  122. // OrderBy orderBy SQL
  123. func (b *Builder) OrderBy(orderBy string) *Builder {
  124. b.orderBy = orderBy
  125. return b
  126. }
  127. // GroupBy groupby SQL
  128. func (b *Builder) GroupBy(groupby string) *Builder {
  129. b.groupBy = groupby
  130. return b
  131. }
  132. // Having having SQL
  133. func (b *Builder) Having(having string) *Builder {
  134. b.having = having
  135. return b
  136. }
上海开阖软件有限公司 沪ICP备12045867号-1