From 11137bdb28a7d6f849846e97f1549d4f31c29a8c Mon Sep 17 00:00:00 2001 From: wazsmwazsm <942443360@qq.com> Date: Mon, 2 Dec 2019 10:30:03 +0800 Subject: [PATCH] update readme and examples --- README.md | 121 +++++++++++++++++++----------------- builder/sql_builder_test.go | 4 +- example/aggregate_func.go | 6 +- example/delete.go | 6 +- example/go.mod | 4 -- example/go.sum | 2 - example/group_by.go | 10 +-- example/having_raw.go | 8 +-- example/insert.go | 6 +- example/join.go | 8 +-- example/join2.go | 14 +++-- example/join_with_params.go | 8 +-- example/query.go | 12 ++-- example/update.go | 8 +-- example/where.go | 12 ++-- example/where_in.go | 10 +-- example/where_raw.go | 12 ++-- 17 files changed, 127 insertions(+), 124 deletions(-) delete mode 100644 example/go.mod delete mode 100644 example/go.sum diff --git a/README.md b/README.md index ca7e187..0b9d207 100644 --- a/README.md +++ b/README.md @@ -37,11 +37,11 @@ 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) @@ -49,7 +49,7 @@ func main() { // 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 @@ -72,10 +72,10 @@ 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) @@ -83,7 +83,7 @@ func main() { 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] } ``` @@ -103,11 +103,11 @@ 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) @@ -115,7 +115,7 @@ func main() { 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] } @@ -123,7 +123,7 @@ func main() { ### 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 @@ -136,8 +136,8 @@ 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) @@ -145,7 +145,7 @@ func main() { params := sb.GetQueryParams() - log.Println(sql) // SELECT count(`age`), username FROM test + log.Println(sql) // SELECT count(`age`), `username` FROM `test` log.Println(params) // [] } @@ -164,10 +164,11 @@ 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) @@ -175,12 +176,12 @@ func main() { 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 @@ -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) // [] } @@ -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 { @@ -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 @@ -256,10 +257,10 @@ 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) @@ -267,7 +268,7 @@ func main() { 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] } @@ -285,10 +286,10 @@ 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) @@ -296,7 +297,7 @@ func main() { 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] } @@ -314,11 +315,15 @@ 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) @@ -326,8 +331,8 @@ func main() { 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] } ``` @@ -345,8 +350,8 @@ 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) @@ -354,7 +359,7 @@ func main() { 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] } @@ -372,9 +377,9 @@ 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) @@ -382,7 +387,7 @@ func main() { 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] } @@ -400,8 +405,8 @@ 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) @@ -409,7 +414,7 @@ func main() { params := sb.GetDeleteParams() - log.Println(sql) // DELETE FROM test WHERE `id` = ? + log.Println(sql) // DELETE FROM `test` WHERE `id` = ? log.Println(params) // [11] } @@ -417,4 +422,4 @@ func main() { # License -The QueryBuilder is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT). \ No newline at end of file +The QueryBuilder is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT). diff --git a/builder/sql_builder_test.go b/builder/sql_builder_test.go index e97da4e..3f38926 100644 --- a/builder/sql_builder_test.go +++ b/builder/sql_builder_test.go @@ -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") } diff --git a/example/aggregate_func.go b/example/aggregate_func.go index c4fae57..2c3a2e9 100644 --- a/example/aggregate_func.go +++ b/example/aggregate_func.go @@ -8,8 +8,8 @@ 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) @@ -17,6 +17,6 @@ func main() { params := sb.GetQueryParams() - log.Println(sql) // SELECT count(`age`), username FROM test + log.Println(sql) // SELECT count(`age`), `username` FROM `test` log.Println(params) // [] } diff --git a/example/delete.go b/example/delete.go index 23dcfa3..b79b559 100644 --- a/example/delete.go +++ b/example/delete.go @@ -8,8 +8,8 @@ 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) @@ -17,6 +17,6 @@ func main() { params := sb.GetDeleteParams() - log.Println(sql) // DELETE FROM test WHERE `id` = ? + log.Println(sql) // DELETE FROM `test` WHERE `id` = ? log.Println(params) // [11] } diff --git a/example/go.mod b/example/go.mod deleted file mode 100644 index 4d7aabe..0000000 --- a/example/go.mod +++ /dev/null @@ -1,4 +0,0 @@ -module github.com/wazsmwazsm/QueryBuilder/example - -go 1.12 - diff --git a/example/go.sum b/example/go.sum deleted file mode 100644 index ebfbc79..0000000 --- a/example/go.sum +++ /dev/null @@ -1,2 +0,0 @@ -github.com/wazsmwazsm/QueryBuilder v0.0.0-20191201160002-f24fa899dda5 h1:ZeoVA12TSBNSKo+bdhAbUh4AuWkYZc5F+CgYa0Ei5SQ= -github.com/wazsmwazsm/QueryBuilder v0.0.0-20191201160002-f24fa899dda5/go.mod h1:FbnoVfAppXflGWMiB8xGbkPZi4NssvpSaVPsfklAXaU= diff --git a/example/group_by.go b/example/group_by.go index 22c789c..5561107 100644 --- a/example/group_by.go +++ b/example/group_by.go @@ -8,10 +8,10 @@ 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"). GetQuerySQL() if err != nil { log.Fatal(err) @@ -19,6 +19,6 @@ func main() { 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] } diff --git a/example/having_raw.go b/example/having_raw.go index f92750c..efd34aa 100644 --- a/example/having_raw.go +++ b/example/having_raw.go @@ -8,9 +8,9 @@ import ( func main() { sb := builder.NewSQLBuilder() - sql, err := sb.Table("test"). - SelectRaw("`school`, `class`, COUNT(*)"). - GroupBy("school", "class"). + sql, err := sb.Table("`test`"). + Select("`school`", "`class`", "COUNT(*)"). + GroupBy("`school`", "`class`"). HavingRaw("COUNT(*) > 2"). GetQuerySQL() if err != nil { @@ -19,6 +19,6 @@ 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) // [] } diff --git a/example/insert.go b/example/insert.go index 9b18973..1bc7e7c 100644 --- a/example/insert.go +++ b/example/insert.go @@ -8,8 +8,8 @@ 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) @@ -17,6 +17,6 @@ func main() { 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] } diff --git a/example/join.go b/example/join.go index 44954b6..ee5ea82 100644 --- a/example/join.go +++ b/example/join.go @@ -8,10 +8,10 @@ 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) @@ -19,6 +19,6 @@ func main() { 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] } diff --git a/example/join2.go b/example/join2.go index 31b577b..bf18181 100644 --- a/example/join2.go +++ b/example/join2.go @@ -8,11 +8,15 @@ 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) @@ -20,6 +24,6 @@ func main() { 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] } diff --git a/example/join_with_params.go b/example/join_with_params.go index 1efe24f..45c750e 100644 --- a/example/join_with_params.go +++ b/example/join_with_params.go @@ -8,10 +8,10 @@ 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) @@ -19,6 +19,6 @@ func main() { 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] } diff --git a/example/query.go b/example/query.go index 3c3c34a..129b67e 100644 --- a/example/query.go +++ b/example/query.go @@ -8,11 +8,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 { @@ -21,6 +21,6 @@ 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] } diff --git a/example/update.go b/example/update.go index 7b829d9..8c9eb10 100644 --- a/example/update.go +++ b/example/update.go @@ -8,9 +8,9 @@ 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) @@ -18,6 +18,6 @@ func main() { 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] } diff --git a/example/where.go b/example/where.go index f73df76..7db3b62 100644 --- a/example/where.go +++ b/example/where.go @@ -8,11 +8,11 @@ import ( func main() { sb := builder.NewSQLBuilder() - 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) @@ -20,6 +20,6 @@ func main() { 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%] } diff --git a/example/where_in.go b/example/where_in.go index 6c33232..4e5fc78 100644 --- a/example/where_in.go +++ b/example/where_in.go @@ -8,10 +8,10 @@ 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) @@ -19,6 +19,6 @@ func main() { 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] } diff --git a/example/where_raw.go b/example/where_raw.go index 0ecde59..4fb686d 100644 --- a/example/where_raw.go +++ b/example/where_raw.go @@ -8,11 +8,11 @@ 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) @@ -20,6 +20,6 @@ func main() { params := sb.GetQueryParams() - log.Println(sql) // SELECT `name`,`age`,`school` FROM test WHERE `title` = ? AND `name` = ? OR `age` = ? OR `age` = ? - log.Println(params) // [hello jack 22 25] + 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 2-3] }