builder_select.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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.tableName) <= 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.tableName); err != nil {
  43. return err
  44. }
  45. } else {
  46. if b.cond.IsValid() && len(b.tableName) <= 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.tableName) == 0 {
  63. fmt.Fprintf(w, ")")
  64. } else {
  65. fmt.Fprintf(w, ") %v", b.tableName)
  66. }
  67. default:
  68. return ErrUnexpectedSubQuery
  69. }
  70. }
  71. for _, v := range b.joins {
  72. if _, err := fmt.Fprintf(w, " %s JOIN %s ON ", v.joinType, v.joinTable); err != nil {
  73. return err
  74. }
  75. if err := v.joinCond.WriteTo(w); err != nil {
  76. return err
  77. }
  78. }
  79. if b.cond.IsValid() {
  80. if _, err := fmt.Fprint(w, " WHERE "); err != nil {
  81. return err
  82. }
  83. if err := b.cond.WriteTo(w); err != nil {
  84. return err
  85. }
  86. }
  87. if len(b.groupBy) > 0 {
  88. if _, err := fmt.Fprint(w, " GROUP BY ", b.groupBy); err != nil {
  89. return err
  90. }
  91. }
  92. if len(b.having) > 0 {
  93. if _, err := fmt.Fprint(w, " HAVING ", b.having); err != nil {
  94. return err
  95. }
  96. }
  97. if len(b.orderBy) > 0 {
  98. if _, err := fmt.Fprint(w, " ORDER BY ", b.orderBy); err != nil {
  99. return err
  100. }
  101. }
  102. if b.limitation != nil {
  103. if err := b.limitWriteTo(w); err != nil {
  104. return err
  105. }
  106. }
  107. return nil
  108. }
  109. // OrderBy orderBy SQL
  110. func (b *Builder) OrderBy(orderBy string) *Builder {
  111. b.orderBy = orderBy
  112. return b
  113. }
  114. // GroupBy groupby SQL
  115. func (b *Builder) GroupBy(groupby string) *Builder {
  116. b.groupBy = groupby
  117. return b
  118. }
  119. // Having having SQL
  120. func (b *Builder) Having(having string) *Builder {
  121. b.having = having
  122. return b
  123. }