case.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. "bytes"
  17. )
  18. type sCaseFieldBranch struct {
  19. whenCondition ICondition
  20. thenField IQueryField
  21. }
  22. // SCaseFunction represents function of case ... when ... branch
  23. type SCaseFunction struct {
  24. branches []sCaseFieldBranch
  25. elseField IQueryField
  26. }
  27. // Else adds else clause for case when function
  28. func (cf *SCaseFunction) Else(field IQueryField) *SCaseFunction {
  29. cf.elseField = field
  30. return cf
  31. }
  32. // When adds when clause for case when function
  33. func (cf *SCaseFunction) When(when ICondition, then IQueryField) *SCaseFunction {
  34. cf.branches = append(cf.branches, sCaseFieldBranch{
  35. whenCondition: when,
  36. thenField: then,
  37. })
  38. return cf
  39. }
  40. // NewCase creates a case... when...else... representation instance
  41. func NewCase() *SCaseFunction {
  42. return &SCaseFunction{}
  43. }
  44. func (cf *SCaseFunction) expression() string {
  45. var buf bytes.Buffer
  46. buf.WriteString("CASE ")
  47. for i := range cf.branches {
  48. buf.WriteString("WHEN ")
  49. buf.WriteString(cf.branches[i].whenCondition.WhereClause())
  50. buf.WriteString(" THEN ")
  51. buf.WriteString(cf.branches[i].thenField.Reference())
  52. }
  53. buf.WriteString(" ELSE ")
  54. buf.WriteString(cf.elseField.Reference())
  55. buf.WriteString(" END")
  56. return buf.String()
  57. }
  58. func (cf *SCaseFunction) variables() []interface{} {
  59. vars := make([]interface{}, 0)
  60. for i := range cf.branches {
  61. fromvars := cf.branches[i].whenCondition.Variables()
  62. vars = append(vars, fromvars...)
  63. fromvars = cf.branches[i].thenField.Variables()
  64. vars = append(vars, fromvars...)
  65. }
  66. fromvars := cf.elseField.Variables()
  67. vars = append(vars, fromvars...)
  68. return vars
  69. }
  70. func (cf *SCaseFunction) database() *SDatabase {
  71. for _, b := range cf.branches {
  72. db := b.whenCondition.database()
  73. if db != nil {
  74. return db
  75. }
  76. db = b.thenField.database()
  77. if db != nil {
  78. return db
  79. }
  80. }
  81. db := cf.elseField.database()
  82. if db != nil {
  83. return db
  84. }
  85. return nil
  86. }
  87. func (cf *SCaseFunction) queryFields() []IQueryField {
  88. return []IQueryField{
  89. cf.elseField,
  90. }
  91. }