doc.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2016 The XORM Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD
  3. // license that can be found in the LICENSE file.
  4. /*
  5. Package builder is a simple and powerful sql builder for Go.
  6. Make sure you have installed Go 1.1+ and then:
  7. go get github.com/xormplus/builder
  8. WARNNING: Currently, only query conditions are supported. Below is the supported conditions.
  9. 1. Eq is a redefine of a map, you can give one or more conditions to Eq
  10. import . "github.com/xormplus/builder"
  11. sql, args, _ := ToSQL(Eq{"a":1})
  12. // a=? [1]
  13. sql, args, _ := ToSQL(Eq{"b":"c"}.And(Eq{"c": 0}))
  14. // b=? AND c=? ["c", 0]
  15. sql, args, _ := ToSQL(Eq{"b":"c", "c":0})
  16. // b=? AND c=? ["c", 0]
  17. sql, args, _ := ToSQL(Eq{"b":"c"}.Or(Eq{"b":"d"}))
  18. // b=? OR b=? ["c", "d"]
  19. sql, args, _ := ToSQL(Eq{"b": []string{"c", "d"}})
  20. // b IN (?,?) ["c", "d"]
  21. sql, args, _ := ToSQL(Eq{"b": 1, "c":[]int{2, 3}})
  22. // b=? AND c IN (?,?) [1, 2, 3]
  23. 2. Neq is the same to Eq
  24. import . "github.com/xormplus/builder"
  25. sql, args, _ := ToSQL(Neq{"a":1})
  26. // a<>? [1]
  27. sql, args, _ := ToSQL(Neq{"b":"c"}.And(Neq{"c": 0}))
  28. // b<>? AND c<>? ["c", 0]
  29. sql, args, _ := ToSQL(Neq{"b":"c", "c":0})
  30. // b<>? AND c<>? ["c", 0]
  31. sql, args, _ := ToSQL(Neq{"b":"c"}.Or(Neq{"b":"d"}))
  32. // b<>? OR b<>? ["c", "d"]
  33. sql, args, _ := ToSQL(Neq{"b": []string{"c", "d"}})
  34. // b NOT IN (?,?) ["c", "d"]
  35. sql, args, _ := ToSQL(Neq{"b": 1, "c":[]int{2, 3}})
  36. // b<>? AND c NOT IN (?,?) [1, 2, 3]
  37. 3. Gt, Gte, Lt, Lte
  38. import . "github.com/xormplus/builder"
  39. sql, args, _ := ToSQL(Gt{"a", 1}.And(Gte{"b", 2}))
  40. // a>? AND b>=? [1, 2]
  41. sql, args, _ := ToSQL(Lt{"a", 1}.Or(Lte{"b", 2}))
  42. // a<? OR b<=? [1, 2]
  43. 4. Like
  44. import . "github.com/xormplus/builder"
  45. sql, args, _ := ToSQL(Like{"a", "c"})
  46. // a LIKE ? [%c%]
  47. 5. Expr you can customerize your sql with Expr
  48. import . "github.com/xormplus/builder"
  49. sql, args, _ := ToSQL(Expr("a = ? ", 1))
  50. // a = ? [1]
  51. sql, args, _ := ToSQL(Eq{"a": Expr("select id from table where c = ?", 1)})
  52. // a=(select id from table where c = ?) [1]
  53. 6. In and NotIn
  54. import . "github.com/xormplus/builder"
  55. sql, args, _ := ToSQL(In("a", 1, 2, 3))
  56. // a IN (?,?,?) [1,2,3]
  57. sql, args, _ := ToSQL(In("a", []int{1, 2, 3}))
  58. // a IN (?,?,?) [1,2,3]
  59. sql, args, _ := ToSQL(In("a", Expr("select id from b where c = ?", 1))))
  60. // a IN (select id from b where c = ?) [1]
  61. 7. IsNull and NotNull
  62. import . "github.com/xormplus/builder"
  63. sql, args, _ := ToSQL(IsNull{"a"})
  64. // a IS NULL []
  65. sql, args, _ := ToSQL(NotNull{"b"})
  66. // b IS NOT NULL []
  67. 8. And(conds ...Cond), And can connect one or more condtions via AND
  68. import . "github.com/xormplus/builder"
  69. sql, args, _ := ToSQL(And(Eq{"a":1}, Like{"b", "c"}, Neq{"d", 2}))
  70. // a=? AND b LIKE ? AND d<>? [1, %c%, 2]
  71. 9. Or(conds ...Cond), Or can connect one or more conditions via Or
  72. import . "github.com/xormplus/builder"
  73. sql, args, _ := ToSQL(Or(Eq{"a":1}, Like{"b", "c"}, Neq{"d", 2}))
  74. // a=? OR b LIKE ? OR d<>? [1, %c%, 2]
  75. sql, args, _ := ToSQL(Or(Eq{"a":1}, And(Like{"b", "c"}, Neq{"d", 2})))
  76. // a=? OR (b LIKE ? AND d<>?) [1, %c%, 2]
  77. 10. Between
  78. import . "github.com/xormplus/builder"
  79. sql, args, _ := ToSQL(Between("a", 1, 2))
  80. // a BETWEEN 1 AND 2
  81. 11. define yourself conditions
  82. Since Cond is a interface, you can define yourself conditions and compare with them
  83. */
  84. package builder