Skip to content

Commit

Permalink
update readme and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
wazsmwazsm committed Dec 2, 2019
1 parent 11894f8 commit 11137bd
Show file tree
Hide file tree
Showing 17 changed files with 127 additions and 124 deletions.
121 changes: 63 additions & 58 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ func main() {
sb := builder.NewSQLBuilder()

// build sql
sql, err := sb.Table("test").
Select("name", "age", "school").
Where("name", "=", "jack").
Where("age", ">=", 18).
OrWhere("name", "like", "%admin%").
sql, err := sb.Table("`test`").
Select("`name`", "`age`", "`school`").
Where("`name`", "=", "jack").
Where("`age`", ">=", 18).
OrWhere("`name`", "like", "%admin%").
GetQuerySQL()
if err != nil {
log.Fatal(err)
}
// get bind params
params := sb.GetQueryParams()

log.Println(sql) // SELECT `name`,`age`,`school` FROM test WHERE `name` = ? AND `age` >= ? OR `name` like ?
log.Println(sql) // SELECT `name`,`age`,`school` FROM `test` WHERE `name` = ? AND `age` >= ? OR `name` like ?
log.Println(params) // [jack 18 %admin%]

// now you can use the sql and params to database/sql Query()\Exec() function
Expand All @@ -72,18 +72,18 @@ import (
func main() {
sb := builder.NewSQLBuilder()

sql, err := sb.Table("test").
Select("name", "age", "school").
WhereIn("id", 1, 2, 3).
OrWhereNotIn("uid", 2, 4).
sql, err := sb.Table("`test`").
Select("`name`", "`age`", "`school`").
WhereIn("`id`", 1, 2, 3).
OrWhereNotIn("`uid`", 2, 4).
GetQuerySQL()
if err != nil {
log.Fatal(err)
}

params := sb.GetQueryParams()

log.Println(sql) // SELECT `name`,`age`,`school` FROM test WHERE `id` IN (?,?,?) OR `uid` NOT IN (?,?)
log.Println(sql) // SELECT `name`,`age`,`school` FROM `test` WHERE `id` IN (?,?,?) OR `uid` NOT IN (?,?)
log.Println(params) // [1 2 3 2 4]
}
```
Expand All @@ -103,27 +103,27 @@ import (
func main() {
sb := builder.NewSQLBuilder()

sql, err := sb.Table("test").
Select("name", "age", "school").
sql, err := sb.Table("`test`").
Select("`name`", "`age`", "`school`").
WhereRaw("`title` = ?", "hello").
Where("name", "=", "jack").
OrWhereRaw("`age` = ? OR `age` = ?", 22, 25).
Where("`name`", "=", "jack").
OrWhereRaw("(`age` = ? OR `age` = ?) AND `class` = ?", 22, 25, "2-3").
GetQuerySQL()
if err != nil {
log.Fatal(err)
}

params := sb.GetQueryParams()

log.Println(sql) // SELECT `name`,`age`,`school` FROM test WHERE `title` = ? AND `name` = ? OR `age` = ? OR `age` = ?
log.Println(sql) // SELECT `name`,`age`,`school` FROM `test` WHERE `title` = ? AND `name` = ? OR (`age` = ? OR `age` = ?) AND `class` = ?
log.Println(params) // [hello jack 22 25]
}

```

### aggregate func

also, you can use aggregate func with raw sql (in SelectRaw() method)
also, you can use aggregate func with raw sql

```go
package main
Expand All @@ -136,16 +136,16 @@ import (
func main() {
sb := builder.NewSQLBuilder()

sql, err := sb.Table("test").
SelectRaw("count(`age`), username").
sql, err := sb.Table("`test`").
Select("count(`age`)", "`username`").
GetQuerySQL()
if err != nil {
log.Fatal(err)
}

params := sb.GetQueryParams()

log.Println(sql) // SELECT count(`age`), username FROM test
log.Println(sql) // SELECT count(`age`), `username` FROM `test`
log.Println(params) // []
}

Expand All @@ -164,23 +164,24 @@ import (
func main() {
sb := builder.NewSQLBuilder()

sql, err := sb.Table("test").
SelectRaw("`school`, `class`, COUNT(*) as `ct`").
GroupBy("school", "class").
Having("ct", ">", "2").
sql, err := sb.Table("`test`").
Select("`school`", "`class`", "COUNT(*) as `ct`").
GroupBy("`school`", "`class`").
Having("`ct`", ">", "2").
// Having("COUNT(*)", ">", "2"). // same as above
GetQuerySQL()
if err != nil {
log.Fatal(err)
}

params := sb.GetQueryParams()

log.Println(sql) // SELECT `school`, `class`, COUNT(*) as `ct` FROM test GROUP BY `school`,`class` HAVING `ct` > ?
log.Println(sql) // SELECT `school`,`class`,COUNT(*) as `ct` FROM `test` GROUP BY `school`,`class` HAVING `ct` > ?
log.Println(params) // [2]
}
```

such as where, having can also use raw sql
such as where, having can also use raw sql with HavingRaw() method
```go
package main

Expand All @@ -203,7 +204,7 @@ func main() {

params := sb.GetQueryParams()

log.Println(sql) // SELECT `school`, `class`, COUNT(*) FROM test GROUP BY `school`,`class` HAVING COUNT(*) > 2
log.Println(sql) // SELECT `school`,`class`,COUNT(*) FROM `test` GROUP BY `school`,`class` HAVING COUNT(*) > 2
log.Println(params) // []
}

Expand All @@ -222,11 +223,11 @@ import (
func main() {
sb := builder.NewSQLBuilder()

sql, err := sb.Table("test").
Select("name", "age", "school").
Where("name", "=", "jack").
Where("age", ">=", 18).
OrderBy("DESC", "age", "class").
sql, err := sb.Table("`test`").
Select("`name`", "`age`", "`school`").
Where("`name`", "=", "jack").
Where("`age`", ">=", 18).
OrderBy("DESC", "`age`", "`class`").
Limit(1, 10).
GetQuerySQL()
if err != nil {
Expand All @@ -235,14 +236,14 @@ func main() {

params := sb.GetQueryParams()

log.Println(sql) // SELECT `name`,`age`,`school` FROM test WHERE `name` = ? AND `age` >= ? ORDER BY `age`,`class` DESC LIMIT ? OFFSET ?
log.Println(sql) // SELECT `name`,`age`,`school` FROM `test` WHERE `name` = ? AND `age` >= ? ORDER BY `age`,`class` DESC LIMIT ? OFFSET ?
log.Println(params) // [jack 18 10 1]
}
```

### join

join only provide raw sql mode
join only provide raw sql mode now

simple join
```go
Expand All @@ -256,18 +257,18 @@ import (
func main() {
sb := builder.NewSQLBuilder()

sql, err := sb.Table("test").
SelectRaw("`test`.`name`, `test`.`age`, `test2`.`teacher`").
sql, err := sb.Table("`test`").
Select("`test`.`name`", "`test`.`age`", "`test2`.`teacher`").
JoinRaw("LEFT JOIN `test2` ON `test`.`class` = `test2`.`class`").
WhereRaw("`test`.`age` >= ?", 18).
Where("`test`.`age`", ">=", 18).
GetQuerySQL()
if err != nil {
log.Fatal(err)
}

params := sb.GetQueryParams()

log.Println(sql) // SELECT `test`.`name`, `test`.`age`, `test2`.`teacher` FROM `test` LEFT JOIN `test2` ON `test`.`class` = `test2`.`class` WHERE `test`.`age` >= ?
log.Println(sql) // SELECT `test`.`name`,`test`.`age`,`test2`.`teacher` FROM `test` LEFT JOIN `test2` ON `test`.`class` = `test2`.`class` WHERE `test`.`age` >= ?
log.Println(params) // [18]
}

Expand All @@ -285,18 +286,18 @@ import (
func main() {
sb := builder.NewSQLBuilder()

sql, err := sb.Table("test").
SelectRaw("`test`.`name`, `test`.`age`, `test2`.`teacher`").
sql, err := sb.Table("`test`").
Select("`test`.`name`", "`test`.`age`", "`test2`.`teacher`").
JoinRaw("LEFT JOIN `test2` ON `test`.`class` = `test2`.`class` AND `test`.`num` = ?", 2333).
WhereRaw("`test`.`age` >= ?", 18).
Where("`test`.`age`", ">=", 18).
GetQuerySQL()
if err != nil {
log.Fatal(err)
}

params := sb.GetQueryParams()

log.Println(sql) // SELECT `test`.`name`, `test`.`age`, `test2`.`teacher` FROM `test` LEFT JOIN `test2` ON `test`.`class` = `test2`.`class` AND `test`.`num` = ? WHERE `test`.`age` >= ?
log.Println(sql) // SELECT `test`.`name`,`test`.`age`,`test2`.`teacher` FROM `test` LEFT JOIN `test2` ON `test`.`class` = `test2`.`class` AND `test`.`num` = ? WHERE `test`.`age` >= ?
log.Println(params) // [2333 18]
}

Expand All @@ -314,20 +315,24 @@ import (
func main() {
sb := builder.NewSQLBuilder()

sql, err := sb.TableRaw("`test` as t1").
SelectRaw("`t1`.`name`, `t1`.`age`, `t2`.`teacher`, `t3`.`address`").
sql, err := sb.Table("`test` as t1").
Select("`t1`.`name`", "`t1`.`age`", "`t2`.`teacher`", "`t3`.`address`").
JoinRaw("LEFT JOIN `test2` as `t2` ON `t1`.`class` = `t2`.`class`").
JoinRaw("INNER JOIN `test3` as t3 ON `t1`.`school` = `t3`.`school`").
WhereRaw("`t1`.`age` >= ?", 18).
Where("`t1`.`age`", ">=", 18).
GroupBy("`t1`.`age`").
Having("COUNT(`t1`.`age`)", ">", 2).
OrderBy("DESC", "`t1`.`age`").
Limit(1, 10).
GetQuerySQL()
if err != nil {
log.Fatal(err)
}

params := sb.GetQueryParams()

log.Println(sql) // SELECT `t1`.`name`, `t1`.`age`, `t2`.`teacher`, `t3`.`address` FROM `test` as t1 LEFT JOIN `test2` as `t2` ON `t1`.`class` = `t2`.`class` INNER JOIN `test3` as t3 ON `t1`.`school` = `t3`.`school` WHERE `t1`.`age` >= ?
log.Println(params) // [18]
log.Println(sql) // SELECT `t1`.`name`,`t1`.`age`,`t2`.`teacher`,`t3`.`address` FROM `test` as t1 LEFT JOIN `test2` as `t2` ON `t1`.`class` = `t2`.`class` INNER JOIN `test3` as t3 ON `t1`.`school` = `t3`.`school` WHERE `t1`.`age` >= ? GROUP BY `t1`.`age` HAVING COUNT(`t1`.`age`) > ? ORDER BY `t1`.`age` DESC LIMIT ? OFFSET ?
log.Println(params) // [18 2 10 1]
}

```
Expand All @@ -345,16 +350,16 @@ import (
func main() {
sb := builder.NewSQLBuilder()

sql, err := sb.Table("test").
Insert([]string{"name", "age"}, "jack", 18).
sql, err := sb.Table("`test`").
Insert([]string{"`name`", "`age`"}, "jack", 18).
GetInsertSQL()
if err != nil {
log.Fatal(err)
}

params := sb.GetInsertParams()

log.Println(sql) // INSERT INTO test (`name`,`age`) VALUES (?,?)
log.Println(sql) // INSERT INTO `test` (`name`,`age`) VALUES (?,?)
log.Println(params) // [jack 18]
}

Expand All @@ -372,17 +377,17 @@ import (
func main() {
sb := builder.NewSQLBuilder()

sql, err := sb.Table("test").
Update([]string{"name", "age"}, "jack", 18).
Where("id", "=", 11).
sql, err := sb.Table("`test`").
Update([]string{"`name`", "`age`"}, "jack", 18).
Where("`id`", "=", 11).
GetUpdateSQL()
if err != nil {
log.Fatal(err)
}

params := sb.GetUpdateParams()

log.Println(sql) // UPDATE test SET `name` = ?,`age` = ? WHERE `id` = ?
log.Println(sql) // UPDATE `test` SET `name` = ?,`age` = ? WHERE `id` = ?
log.Println(params) // [jack 18 11]
}

Expand All @@ -400,21 +405,21 @@ import (
func main() {
sb := builder.NewSQLBuilder()

sql, err := sb.Table("test").
Where("id", "=", 11).
sql, err := sb.Table("`test`").
Where("`id`", "=", 11).
GetDeleteSQL()
if err != nil {
log.Fatal(err)
}

params := sb.GetDeleteParams()

log.Println(sql) // DELETE FROM test WHERE `id` = ?
log.Println(sql) // DELETE FROM `test` WHERE `id` = ?
log.Println(params) // [11]
}

```

# License

The QueryBuilder is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT).
The QueryBuilder is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT).
4 changes: 2 additions & 2 deletions builder/sql_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@ func TestSQLBuilderWhereRaw(t *testing.T) {
Select("`name`", "`age`", "`school`").
WhereRaw("`title` = ?", "hello").
Where("`name`", "=", "jack").
OrWhereRaw("`age` = ? OR `age` = ?", 22, 25).
OrWhereRaw("(`age` = ? OR `age` = ?)", 22, 25).
GetQuerySQL()
if err != nil {
t.Error(err)
}
expectSQL := "SELECT `name`,`age`,`school` FROM `test` WHERE `title` = ? AND `name` = ? OR `age` = ? OR `age` = ?"
expectSQL := "SELECT `name`,`age`,`school` FROM `test` WHERE `title` = ? AND `name` = ? OR (`age` = ? OR `age` = ?)"
if sql != expectSQL {
t.Error("sql gen err")
}
Expand Down
6 changes: 3 additions & 3 deletions example/aggregate_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import (
func main() {
sb := builder.NewSQLBuilder()

sql, err := sb.Table("test").
Select("count(`age`)", "username").
sql, err := sb.Table("`test`").
Select("count(`age`)", "`username`").
GetQuerySQL()
if err != nil {
log.Fatal(err)
}

params := sb.GetQueryParams()

log.Println(sql) // SELECT count(`age`), username FROM test
log.Println(sql) // SELECT count(`age`), `username` FROM `test`
log.Println(params) // []
}
6 changes: 3 additions & 3 deletions example/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import (
func main() {
sb := builder.NewSQLBuilder()

sql, err := sb.Table("test").
Where("id", "=", 11).
sql, err := sb.Table("`test`").
Where("`id`", "=", 11).
GetDeleteSQL()
if err != nil {
log.Fatal(err)
}

params := sb.GetDeleteParams()

log.Println(sql) // DELETE FROM test WHERE `id` = ?
log.Println(sql) // DELETE FROM `test` WHERE `id` = ?
log.Println(params) // [11]
}
4 changes: 0 additions & 4 deletions example/go.mod

This file was deleted.

2 changes: 0 additions & 2 deletions example/go.sum

This file was deleted.

Loading

0 comments on commit 11137bd

Please sign in to comment.