where.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package clause
  2. // Where where clause
  3. type Where struct {
  4. Exprs []Expression
  5. }
  6. // Name where clause name
  7. func (where Where) Name() string {
  8. return "WHERE"
  9. }
  10. // Build build where clause
  11. func (where Where) Build(builder Builder) {
  12. // Switch position if the first query expression is a single Or condition
  13. for idx, expr := range where.Exprs {
  14. if v, ok := expr.(OrConditions); (!ok && expr != nil) || len(v.Exprs) > 1 {
  15. if idx != 0 {
  16. where.Exprs[0], where.Exprs[idx] = where.Exprs[idx], where.Exprs[0]
  17. }
  18. break
  19. }
  20. }
  21. for idx, expr := range where.Exprs {
  22. if expr != nil {
  23. if idx > 0 {
  24. if v, ok := expr.(OrConditions); ok && len(v.Exprs) == 1 {
  25. builder.Write(" OR ")
  26. } else {
  27. builder.Write(" AND ")
  28. }
  29. }
  30. expr.Build(builder)
  31. }
  32. }
  33. return
  34. }
  35. // MergeClause merge where clauses
  36. func (where Where) MergeClause(clause *Clause) {
  37. if w, ok := clause.Expression.(Where); ok {
  38. where.Exprs = append(w.Exprs, where.Exprs...)
  39. }
  40. clause.Expression = where
  41. }
  42. func And(exprs ...Expression) Expression {
  43. if len(exprs) == 0 {
  44. return nil
  45. }
  46. return AndConditions{Exprs: exprs}
  47. }
  48. type AndConditions struct {
  49. Exprs []Expression
  50. }
  51. func (and AndConditions) Build(builder Builder) {
  52. if len(and.Exprs) > 1 {
  53. builder.WriteByte('(')
  54. }
  55. for idx, c := range and.Exprs {
  56. if idx > 0 {
  57. builder.Write(" AND ")
  58. }
  59. c.Build(builder)
  60. }
  61. if len(and.Exprs) > 1 {
  62. builder.WriteByte(')')
  63. }
  64. }
  65. func Or(exprs ...Expression) Expression {
  66. if len(exprs) == 0 {
  67. return nil
  68. }
  69. return OrConditions{Exprs: exprs}
  70. }
  71. type OrConditions struct {
  72. Exprs []Expression
  73. }
  74. func (or OrConditions) Build(builder Builder) {
  75. if len(or.Exprs) > 1 {
  76. builder.WriteByte('(')
  77. }
  78. for idx, c := range or.Exprs {
  79. if idx > 0 {
  80. builder.Write(" OR ")
  81. }
  82. c.Build(builder)
  83. }
  84. if len(or.Exprs) > 1 {
  85. builder.WriteByte(')')
  86. }
  87. }
  88. func Not(exprs ...Expression) Expression {
  89. if len(exprs) == 0 {
  90. return nil
  91. }
  92. return NotConditions{Exprs: exprs}
  93. }
  94. type NotConditions struct {
  95. Exprs []Expression
  96. }
  97. func (not NotConditions) Build(builder Builder) {
  98. if len(not.Exprs) > 1 {
  99. builder.WriteByte('(')
  100. }
  101. for idx, c := range not.Exprs {
  102. if idx > 0 {
  103. builder.Write(" AND ")
  104. }
  105. if negationBuilder, ok := c.(NegationExpressionBuilder); ok {
  106. negationBuilder.NegationBuild(builder)
  107. } else {
  108. builder.Write(" NOT ")
  109. c.Build(builder)
  110. }
  111. }
  112. if len(not.Exprs) > 1 {
  113. builder.WriteByte(')')
  114. }
  115. }