Skip to content

Commit

Permalink
doc: fillout _or operator doc. (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
Colstuwjx authored Jun 10, 2020
1 parent 72838a9 commit 8d6f9ca
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 20 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,25 @@ where := map[string]interface{}{
"score": 5,
"age >": 35,
"address": builder.IsNotNull,
"_or": []map[string]interface{}{
{
"x1": 11,
"x2 >=": 45,
},
{
"x3": "234",
"x4 <>": "tx2",
},
},
"_orderby": "bonus desc",
"_groupby": "department",
}
table := "some_table"
selectFields := []string{"name", "age", "sex"}
cond, values, err := builder.BuildSelect(table, where, selectFields)

//cond = SELECT name, age, sex FROM some_table WHERE (score=? AND city IN (?, ?) AND age>? AND address IS NOT NULL) GROUP BY department ORDER BY bonus DESC
//values = []interface{}{"beijing", "shanghai", 5, 35}
//cond = SELECT name,age,sex FROM some_table WHERE (((x1=? AND x2>=?) OR (x3=? AND x4!=?)) AND score=? AND city IN (?,?) AND age>? AND address IS NOT NULL) GROUP BY department ORDER BY bonus DESC
//values = []interface{}{11, 45, "234", "tx2", 5, "beijing", "shanghai", 35}

rows, err := db.Query(cond, values...)
```
Expand Down
67 changes: 49 additions & 18 deletions builder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,19 @@ func main() {
panic(err)
}
mp := map[string]interface{}{
"country": "China",
"role": "driver",
"age >": 45,
"country": "China",
"role": "driver",
"age >": 45,
"_or": []map[string]interface{}{
{
"x1": 11,
"x2 >=": 45,
},
{
"x3": "234",
"x4 <>": "tx2",
},
},
"_groupby": "name",
"_having": map[string]interface{}{
"total >": 1000,
Expand All @@ -35,13 +45,13 @@ func main() {
}
cond,vals,err := qb.BuildSelect("tableName", where, []string{"name", "count(price) as total", "age"})

//cond: SELECT name,count(price) as total,age FROM tableName WHERE (age>? AND country=? AND role=?) GROUP BY name HAVING (total>? AND total<=?) ORDER BY age DESC
//vals: []interface{}{45, "China", "driver", 1000, 50000}
//cond: SELECT name,count(price) as total,age FROM tableName WHERE (((x1=? AND x2>=?) OR (x3=? AND x4!=?)) AND country=? AND role=? AND age>?) GROUP BY name HAVING (total>? AND total<=?) ORDER BY age DESC
//vals: []interface{}{11, 45, "234", "tx2", "China", "driver", 45, 1000, 50000}

if nil != err {
panic(err)
}

rows,err := db.Query(cond, vals...)
if nil != err {
panic(err)
Expand Down Expand Up @@ -85,27 +95,38 @@ operators supported:

``` go
where := map[string]interface{}{
"foo <>": "aha",
"bar <=": 45,
"sex in": []interface{}{"girl", "boy"},
"name like": "%James",
"foo <>": "aha",
"bar <=": 45,
"sex in": []interface{}{"girl", "boy"},
"name like": "%James",
}
```

others supported:

* _or
* _orderby
* _groupby
* _having
* _limit

``` go
where := map[string]interface{}{
"age >": 100,
"_orderby": "fieldName asc",
"_groupby": "fieldName",
"_having": map[string]interface{}{"foo":"bar",},
"_limit": []uint{offset, row_count},
"age >": 100,
"_or": []map[string]interface{}{
{
"x1": 11,
"x2 >=": 45,
},
{
"x3": "234",
"x4 <>": "tx2",
},
},
"_orderby": "fieldName asc",
"_groupby": "fieldName",
"_having": map[string]interface{}{"foo":"bar",},
"_limit": []uint{offset, row_count},
}
```
Note:
Expand Down Expand Up @@ -155,9 +176,19 @@ BuildUpdate is very likely to BuildSelect but it **doesn't support**:

``` go
where := map[string]interface{}{
"foo <>": "aha",
"bar <=": 45,
"sex in": []interface{}{"girl", "boy"},
"foo <>": "aha",
"bar <=": 45,
"sex in": []interface{}{"girl", "boy"},
"_or": []map[string]interface{}{
{
"x1": 11,
"x2 >=": 45,
},
{
"x3": "234",
"x4 <>": "tx2",
},
},
}
update := map[string]interface{}{
"role": "primaryschoolstudent",
Expand Down

0 comments on commit 8d6f9ca

Please sign in to comment.