github.com/yongjacky/phoenix-go-orm-builder@v0.3.5/doc.go (about)

     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  /*
     6  
     7  Package builder is a simple and powerful sql builder for Go.
     8  
     9  Make sure you have installed Go 1.1+ and then:
    10  
    11      go get xorm.io/builder
    12  
    13  WARNNING: Currently, only query conditions are supported. Below is the supported conditions.
    14  
    15  1. Eq is a redefine of a map, you can give one or more conditions to Eq
    16  
    17      import . "xorm.io/builder"
    18  
    19      sql, args, _ := ToSQL(Eq{"a":1})
    20      // a=? [1]
    21      sql, args, _ := ToSQL(Eq{"b":"c"}.And(Eq{"c": 0}))
    22      // b=? AND c=? ["c", 0]
    23      sql, args, _ := ToSQL(Eq{"b":"c", "c":0})
    24      // b=? AND c=? ["c", 0]
    25      sql, args, _ := ToSQL(Eq{"b":"c"}.Or(Eq{"b":"d"}))
    26      // b=? OR b=? ["c", "d"]
    27      sql, args, _ := ToSQL(Eq{"b": []string{"c", "d"}})
    28      // b IN (?,?) ["c", "d"]
    29      sql, args, _ := ToSQL(Eq{"b": 1, "c":[]int{2, 3}})
    30      // b=? AND c IN (?,?) [1, 2, 3]
    31  
    32  2. Neq is the same to Eq
    33  
    34      import . "xorm.io/builder"
    35  
    36      sql, args, _ := ToSQL(Neq{"a":1})
    37      // a<>? [1]
    38      sql, args, _ := ToSQL(Neq{"b":"c"}.And(Neq{"c": 0}))
    39      // b<>? AND c<>? ["c", 0]
    40      sql, args, _ := ToSQL(Neq{"b":"c", "c":0})
    41      // b<>? AND c<>? ["c", 0]
    42      sql, args, _ := ToSQL(Neq{"b":"c"}.Or(Neq{"b":"d"}))
    43      // b<>? OR b<>? ["c", "d"]
    44      sql, args, _ := ToSQL(Neq{"b": []string{"c", "d"}})
    45      // b NOT IN (?,?) ["c", "d"]
    46      sql, args, _ := ToSQL(Neq{"b": 1, "c":[]int{2, 3}})
    47      // b<>? AND c NOT IN (?,?) [1, 2, 3]
    48  
    49  3. Gt, Gte, Lt, Lte
    50  
    51      import . "xorm.io/builder"
    52  
    53      sql, args, _ := ToSQL(Gt{"a", 1}.And(Gte{"b", 2}))
    54      // a>? AND b>=? [1, 2]
    55      sql, args, _ := ToSQL(Lt{"a", 1}.Or(Lte{"b", 2}))
    56      // a<? OR b<=? [1, 2]
    57  
    58  4. Like
    59  
    60      import . "xorm.io/builder"
    61  
    62      sql, args, _ := ToSQL(Like{"a", "c"})
    63      // a LIKE ? [%c%]
    64  
    65  5. Expr you can customerize your sql with Expr
    66  
    67      import . "xorm.io/builder"
    68  
    69      sql, args, _ := ToSQL(Expr("a = ? ", 1))
    70      // a = ? [1]
    71      sql, args, _ := ToSQL(Eq{"a": Expr("select id from table where c = ?", 1)})
    72      // a=(select id from table where c = ?) [1]
    73  
    74  6. In and NotIn
    75  
    76      import . "xorm.io/builder"
    77  
    78      sql, args, _ := ToSQL(In("a", 1, 2, 3))
    79      // a IN (?,?,?) [1,2,3]
    80      sql, args, _ := ToSQL(In("a", []int{1, 2, 3}))
    81      // a IN (?,?,?) [1,2,3]
    82      sql, args, _ := ToSQL(In("a", Expr("select id from b where c = ?", 1))))
    83      // a IN (select id from b where c = ?) [1]
    84  
    85  7. IsNull and NotNull
    86  
    87      import . "xorm.io/builder"
    88  
    89      sql, args, _ := ToSQL(IsNull{"a"})
    90      // a IS NULL []
    91      sql, args, _ := ToSQL(NotNull{"b"})
    92       // b IS NOT NULL []
    93  
    94  8. And(conds ...Cond), And can connect one or more condtions via AND
    95  
    96      import . "xorm.io/builder"
    97  
    98      sql, args, _ := ToSQL(And(Eq{"a":1}, Like{"b", "c"}, Neq{"d", 2}))
    99      // a=? AND b LIKE ? AND d<>? [1, %c%, 2]
   100  
   101  9. Or(conds ...Cond), Or can connect one or more conditions via Or
   102  
   103      import . "xorm.io/builder"
   104  
   105      sql, args, _ := ToSQL(Or(Eq{"a":1}, Like{"b", "c"}, Neq{"d", 2}))
   106      // a=? OR b LIKE ? OR d<>? [1, %c%, 2]
   107      sql, args, _ := ToSQL(Or(Eq{"a":1}, And(Like{"b", "c"}, Neq{"d", 2})))
   108      // a=? OR (b LIKE ? AND d<>?) [1, %c%, 2]
   109  
   110  10. Between
   111  
   112      import . "xorm.io/builder"
   113  
   114      sql, args, _ := ToSQL(Between("a", 1, 2))
   115      // a BETWEEN 1 AND 2
   116  
   117  11. define yourself conditions
   118  Since Cond is a interface, you can define yourself conditions and compare with them
   119  */
   120  package builder