cond.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright 2018 Huan Du. All rights reserved.
  2. // Licensed under the MIT license that can be found in the LICENSE file.
  3. package sqlbuilder
  4. import (
  5. "fmt"
  6. "strings"
  7. )
  8. // Cond provides several helper methods to build conditions.
  9. type Cond struct {
  10. Args *Args
  11. }
  12. // Equal represents "field = value".
  13. func (c *Cond) Equal(field string, value interface{}) string {
  14. return fmt.Sprintf("%v = %v", Escape(field), c.Args.Add(value))
  15. }
  16. // E is an alias of Equal.
  17. func (c *Cond) E(field string, value interface{}) string {
  18. return c.Equal(field, value)
  19. }
  20. // NotEqual represents "field != value".
  21. func (c *Cond) NotEqual(field string, value interface{}) string {
  22. return fmt.Sprintf("%v <> %v", Escape(field), c.Args.Add(value))
  23. }
  24. // NE is an alias of NotEqual.
  25. func (c *Cond) NE(field string, value interface{}) string {
  26. return c.NotEqual(field, value)
  27. }
  28. // GreaterThan represents "field > value".
  29. func (c *Cond) GreaterThan(field string, value interface{}) string {
  30. return fmt.Sprintf("%v > %v", Escape(field), c.Args.Add(value))
  31. }
  32. // G is an alias of GreaterThan.
  33. func (c *Cond) G(field string, value interface{}) string {
  34. return c.GreaterThan(field, value)
  35. }
  36. // GreaterEqualThan represents "field >= value".
  37. func (c *Cond) GreaterEqualThan(field string, value interface{}) string {
  38. return fmt.Sprintf("%v >= %v", Escape(field), c.Args.Add(value))
  39. }
  40. // GE is an alias of GreaterEqualThan.
  41. func (c *Cond) GE(field string, value interface{}) string {
  42. return c.GreaterEqualThan(field, value)
  43. }
  44. // LessThan represents "field < value".
  45. func (c *Cond) LessThan(field string, value interface{}) string {
  46. return fmt.Sprintf("%v < %v", Escape(field), c.Args.Add(value))
  47. }
  48. // L is an alias of LessThan.
  49. func (c *Cond) L(field string, value interface{}) string {
  50. return c.LessThan(field, value)
  51. }
  52. // LessEqualThan represents "field <= value".
  53. func (c *Cond) LessEqualThan(field string, value interface{}) string {
  54. return fmt.Sprintf("%v <= %v", Escape(field), c.Args.Add(value))
  55. }
  56. // LE is an alias of LessEqualThan.
  57. func (c *Cond) LE(field string, value interface{}) string {
  58. return c.LessEqualThan(field, value)
  59. }
  60. // In represents "field IN (value...)".
  61. func (c *Cond) In(field string, value ...interface{}) string {
  62. vs := make([]string, 0, len(value))
  63. for _, v := range value {
  64. vs = append(vs, c.Args.Add(v))
  65. }
  66. return fmt.Sprintf("%v IN (%v)", Escape(field), strings.Join(vs, ", "))
  67. }
  68. // NotIn represents "field NOT IN (value...)".
  69. func (c *Cond) NotIn(field string, value ...interface{}) string {
  70. vs := make([]string, 0, len(value))
  71. for _, v := range value {
  72. vs = append(vs, c.Args.Add(v))
  73. }
  74. return fmt.Sprintf("%v NOT IN (%v)", Escape(field), strings.Join(vs, ", "))
  75. }
  76. // Like represents "field LIKE value".
  77. func (c *Cond) Like(field string, value interface{}) string {
  78. return fmt.Sprintf("%v LIKE %v", Escape(field), c.Args.Add(value))
  79. }
  80. // NotLike represents "field NOT LIKE value".
  81. func (c *Cond) NotLike(field string, value interface{}) string {
  82. return fmt.Sprintf("%v NOT LIKE %v", Escape(field), c.Args.Add(value))
  83. }
  84. // IsNull represents "field IS NULL".
  85. func (c *Cond) IsNull(field string) string {
  86. return fmt.Sprintf("%v IS NULL", Escape(field))
  87. }
  88. // IsNotNull represents "field IS NOT NULL".
  89. func (c *Cond) IsNotNull(field string) string {
  90. return fmt.Sprintf("%v IS NOT NULL", Escape(field))
  91. }
  92. // Between represents "field BETWEEN lower AND upper".
  93. func (c *Cond) Between(field string, lower, upper interface{}) string {
  94. return fmt.Sprintf("%v BETWEEN %v AND %v", Escape(field), c.Args.Add(lower), c.Args.Add(upper))
  95. }
  96. // NotBetween represents "field NOT BETWEEN lower AND upper".
  97. func (c *Cond) NotBetween(field string, lower, upper interface{}) string {
  98. return fmt.Sprintf("%v NOT BETWEEN %v AND %v", Escape(field), c.Args.Add(lower), c.Args.Add(upper))
  99. }
  100. // Or represents OR logic like "expr1 OR expr2 OR expr3".
  101. func (c *Cond) Or(orExpr ...string) string {
  102. return fmt.Sprintf("(%v)", strings.Join(orExpr, " OR "))
  103. }
  104. // Var returns a placeholder for value.
  105. func (c *Cond) Var(value interface{}) string {
  106. return c.Args.Add(value)
  107. }