union.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // Copyright 2019 Yunion
  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 sqlchemy
  15. import (
  16. "database/sql"
  17. "fmt"
  18. "strings"
  19. "yunion.io/x/log"
  20. "yunion.io/x/pkg/errors"
  21. )
  22. // SUnionQueryField represents a field of a union query
  23. type SUnionQueryField struct {
  24. union *SUnion
  25. name string
  26. alias string
  27. }
  28. // Expression implementation of SUnionQueryField for IQueryField
  29. func (sqf *SUnionQueryField) Expression() string {
  30. qChar := sqf.union.database().backend.QuoteChar()
  31. return fmt.Sprintf("%s%s%s.%s%s%s", qChar, sqf.union.Alias(), qChar, qChar, sqf.name, qChar)
  32. }
  33. // Name implementation of SUnionQueryField for IQueryField
  34. func (sqf *SUnionQueryField) Name() string {
  35. if len(sqf.alias) > 0 {
  36. return sqf.alias
  37. }
  38. return sqf.name
  39. }
  40. // Reference implementation of SUnionQueryField for IQueryField
  41. func (sqf *SUnionQueryField) Reference() string {
  42. qChar := sqf.union.database().backend.QuoteChar()
  43. return fmt.Sprintf("%s%s%s.%s%s%s", qChar, sqf.union.Alias(), qChar, qChar, sqf.Name(), qChar)
  44. }
  45. // Label implementation of SUnionQueryField for IQueryField
  46. func (sqf *SUnionQueryField) Label(label string) IQueryField {
  47. if len(label) > 0 {
  48. nsqf := *sqf
  49. nsqf.alias = label
  50. return &nsqf
  51. } else {
  52. return sqf
  53. }
  54. }
  55. // Variables implementation of SUnionQueryField for IQueryField
  56. func (sqf *SUnionQueryField) Variables() []interface{} {
  57. return nil
  58. }
  59. // ConvertFromValue implementation of SUnionQueryField for IQueryField
  60. func (sqf *SUnionQueryField) ConvertFromValue(val interface{}) interface{} {
  61. for _, query := range sqf.union.queries {
  62. field := query.Field(sqf.name)
  63. if field != nil {
  64. return field.ConvertFromValue(val)
  65. }
  66. }
  67. return val
  68. }
  69. func (sqf *SUnionQueryField) database() *SDatabase {
  70. return sqf.union.database()
  71. }
  72. // SUnion is the struct to store state of a Union query, which implementation the interface of IQuerySource
  73. type SUnion struct {
  74. alias string
  75. queries []IQuery
  76. fields []IQueryField
  77. // orderBy []sQueryOrder
  78. // limit int
  79. // offset int
  80. isAll bool
  81. }
  82. // Alias implementation of SUnion for IQuerySource
  83. func (uq *SUnion) Alias() string {
  84. return uq.alias
  85. }
  86. func (uq *SUnion) operator() string {
  87. if uq.isAll {
  88. return uq.database().backend.UnionAllString()
  89. } else {
  90. return uq.database().backend.UnionDistinctString()
  91. }
  92. }
  93. // Expression implementation of SUnion for IQuerySource
  94. func (uq *SUnion) Expression() string {
  95. var buf strings.Builder
  96. buf.WriteString("(")
  97. for i := range uq.queries {
  98. if i != 0 {
  99. buf.WriteByte(' ')
  100. buf.WriteString(uq.operator())
  101. buf.WriteByte(' ')
  102. }
  103. subQ := uq.queries[i].SubQuery()
  104. buf.WriteString(subQ.Query().String())
  105. }
  106. /*if uq.orderBy != nil && len(uq.orderBy) > 0 {
  107. buf.WriteString(" ORDER BY ")
  108. for i, f := range uq.orderBy {
  109. if i > 0 {
  110. buf.WriteString(", ")
  111. }
  112. buf.WriteString(fmt.Sprintf("%s %s", f.field.Reference(), f.order))
  113. }
  114. }
  115. if uq.limit > 0 {
  116. buf.WriteString(fmt.Sprintf(" LIMIT %d", uq.limit))
  117. }
  118. if uq.offset > 0 {
  119. buf.WriteString(fmt.Sprintf(" OFFSET %d", uq.offset))
  120. }*/
  121. buf.WriteByte(')')
  122. return buf.String()
  123. }
  124. /*func (tq *SUnion) _orderBy(order QueryOrderType, fields []IQueryField) *SUnion {
  125. if tq.orderBy == nil {
  126. tq.orderBy = make([]SQueryOrder, 0)
  127. }
  128. for _, f := range fields {
  129. tq.orderBy = append(tq.orderBy, SQueryOrder{field: f, order: order})
  130. }
  131. return tq
  132. }
  133. func (tq *SUnion) Asc(fields ...interface{}) *SUnion {
  134. return tq._orderBy(SQL_ORDER_ASC, convertQueryField(tq, fields))
  135. }
  136. func (tq *SUnion) Desc(fields ...interface{}) *SUnion {
  137. return tq._orderBy(SQL_ORDER_DESC, convertQueryField(tq, fields))
  138. }
  139. */
  140. // Limit adds limit to a union query
  141. // func (uq *SUnion) Limit(limit int) *SUnion {
  142. // uq.limit = limit
  143. // return uq
  144. // }
  145. // Offset adds offset to a union query
  146. // func (uq *SUnion) Offset(offset int) *SUnion {
  147. // uq.offset = offset
  148. // return uq
  149. // }
  150. // Fields implementation of SUnion for IQuerySource
  151. func (uq *SUnion) Fields() []IQueryField {
  152. return uq.fields
  153. }
  154. // Field implementation of SUnion for IQuerySource
  155. func (uq *SUnion) Field(name string, alias ...string) IQueryField {
  156. for i := range uq.fields {
  157. if name == uq.fields[i].Name() {
  158. if len(alias) > 0 {
  159. uq.fields[i].Label(alias[0])
  160. }
  161. return uq.fields[i]
  162. }
  163. }
  164. return nil
  165. }
  166. // Variables implementation of SUnion for IQuerySource
  167. func (uq *SUnion) Variables() []interface{} {
  168. ret := make([]interface{}, 0)
  169. for i := range uq.queries {
  170. ret = append(ret, uq.queries[i].Variables()...)
  171. }
  172. return ret
  173. }
  174. // Database implementation of SUnion for IQUerySource
  175. func (uq *SUnion) database() *SDatabase {
  176. for _, q := range uq.queries {
  177. db := q.database()
  178. if db != nil {
  179. return db
  180. }
  181. }
  182. return nil
  183. }
  184. // Union method returns union query of several queries.
  185. // Require the fields of all queries should exactly match
  186. // deprecated
  187. func Union(query ...IQuery) *SUnion {
  188. u, err := UnionWithError(query...)
  189. if err != nil {
  190. log.Fatalf("Fatal: %s", err.Error())
  191. }
  192. return u
  193. }
  194. // UnionWithError constructs union query of several Queries
  195. // Require the fields of all queries should exactly match
  196. func UnionWithError(query ...IQuery) (*SUnion, error) {
  197. return unionWithError(false, query...)
  198. }
  199. func UnionAllWithError(query ...IQuery) (*SUnion, error) {
  200. return unionWithError(true, query...)
  201. }
  202. func unionWithError(isAll bool, query ...IQuery) (*SUnion, error) {
  203. if len(query) == 0 {
  204. return nil, errors.Wrap(sql.ErrNoRows, "empty union query")
  205. }
  206. fieldNames := make([]string, 0)
  207. for _, f := range query[0].QueryFields() {
  208. fieldNames = append(fieldNames, f.Name())
  209. }
  210. var db *SDatabase
  211. for i := 1; i < len(query); i++ {
  212. if db == nil {
  213. db = query[i].database()
  214. } else if db != query[i].database() {
  215. panic(ErrUnionAcrossDatabases)
  216. }
  217. qfields := query[i].QueryFields()
  218. if len(fieldNames) != len(qfields) {
  219. return nil, errors.Wrap(ErrUnionFieldsNotMatch, "number not match")
  220. }
  221. for i := range qfields {
  222. if fieldNames[i] != qfields[i].Name() {
  223. return nil, errors.Wrapf(ErrUnionFieldsNotMatch, "name %s:%s not match", fieldNames[i], qfields[i].Name())
  224. }
  225. }
  226. }
  227. fields := make([]IQueryField, len(fieldNames))
  228. uq := &SUnion{
  229. alias: getTableAliasName(),
  230. queries: query,
  231. fields: fields,
  232. isAll: isAll,
  233. }
  234. for i := range fieldNames {
  235. fields[i] = &SUnionQueryField{name: fieldNames[i], union: uq}
  236. }
  237. return uq, nil
  238. }
  239. // Query of SUnion returns a SQuery of a union query
  240. func (uq *SUnion) Query(f ...IQueryField) *SQuery {
  241. return DoQuery(uq, f...)
  242. }