You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-[Build SQL for MySQL, PostgreSQL, SQLServer or SQLite](#build-sql-for-mysql--postgresql-sqlserver-or-sqlite)
13
-
-[Using `Struct` as a light weight ORM](#using--struct--as-a-light-weight-orm)
12
+
-[Build SQL for MySQL, PostgreSQL, SQLServer or SQLite](#build-sql-for-mysql-postgresql-sqlserver-or-sqlite)
13
+
-[Using `Struct` as a light weight ORM](#using-struct-as-a-light-weight-orm)
14
14
-[Nested SQL](#nested-sql)
15
-
-[Use `sql.Named` in a builder](#use--sqlnamed--in-a-builder)
15
+
-[Use `sql.Named` in a builder](#use-sqlnamed-in-a-builder)
16
16
-[Argument modifiers](#argument-modifiers)
17
17
-[Freestyle builder](#freestyle-builder)
18
18
-[Using special syntax to build SQL](#using-special-syntax-to-build-sql)
19
-
-[Interpolate `args` in the `sql`](#interpolate--args--in-the--sql-)
19
+
-[Interpolate `args` in the `sql`](#interpolate-args-in-the-sql)
20
20
-[FAQ](#faq)
21
-
-[What's the difference between this package and `squirrel`](#what-s-the-difference-between-this-package-and--squirrel-)
21
+
-[What's the difference between this package and `squirrel`](#whats-the-difference-between-this-package-and-squirrel)
22
22
-[License](#license)
23
23
24
24
Package `sqlbuilder` provides a set of flexible and powerful SQL string builders. The only goal of this package is to build SQL string with arguments which can be used in `DB#Query` or `DB#Exec` defined in package `database/sql`.
@@ -68,7 +68,7 @@ fmt.Println(args)
68
68
69
69
### Pre-defined SQL builders
70
70
71
-
Following builders are implemented right now. API document and examples are provided in the `godoc` document.
71
+
This package includes following pre-defined builders so far. API document and examples can be found in the `godoc` online document.
72
72
73
73
-[Struct](https://pkg.go.dev/github.com/huandu/go-sqlbuilder#Struct): Builder factory for a struct.
74
74
-[CreateTableBuilder](https://pkg.go.dev/github.com/huandu/go-sqlbuilder#CreateTableBuilder): Builder for CREATE TABLE.
@@ -81,7 +81,7 @@ Following builders are implemented right now. API document and examples are prov
81
81
-[Build](https://pkg.go.dev/github.com/huandu/go-sqlbuilder#Build): Advanced freestyle builder using special syntax defined in [Args#Compile](https://pkg.go.dev/github.com/huandu/go-sqlbuilder#Args.Compile).
82
82
-[BuildNamed](https://pkg.go.dev/github.com/huandu/go-sqlbuilder#BuildNamed): Advanced freestyle builder using `${key}` to refer the value of a map by key.
83
83
84
-
There is a method `SQL(sql string)` implemented by all statement builders like `SelectBuilder`. We can use this method to insert any arbitrary SQL fragment when building a SQL. It's quite useful to build SQL containing non-standard syntax supported by a OLTP or OLAP system.
84
+
There is a special method `SQL(sql string)` implemented by all statement builders. We can use this method to insert any arbitrary SQL fragment into a builder when building a SQL. It's quite useful to build SQL containing non-standard syntax supported by a OLTP or OLAP system.
85
85
86
86
```go
87
87
// Build a SQL to create a HIVE table.
@@ -102,6 +102,12 @@ fmt.Println(sql)
102
102
// CREATE TABLE users PARTITION BY (year) AS SELECT columns[0] id, columns[1] name, columns[2] year FROM `all-users.csv` LIMIT 100
103
103
```
104
104
105
+
Following are some utility methods to deal with special cases.
106
+
107
+
-[Flatten](https://pkg.go.dev/github.com/huandu/go-sqlbuilder#Flatten) can convert an array-like variable to a flat slice of `[]interface{}` recursively. For instance, calling `Flatten([]interface{"foo", []int{2, 3}})` returns `[]interface{}{"foo", 2, 3}`. This method can work with builder methods like `In`/`NotIn`/`Values`/etc to convert a typed array to `[]interface{}` or merge inputs.
108
+
-[List](https://pkg.go.dev/github.com/huandu/go-sqlbuilder#List) works similar to `Flatten` except that its return value is dedecated for builder args. For instance, calling `Buildf("my_func(%v)", List([]int{1, 2, 3})).Build()` returns SQL `my_func(?, ?, ?)` and args `[]interface{}{1, 2, 3}`.
109
+
-[Raw](https://pkg.go.dev/github.com/huandu/go-sqlbuilder#Raw) marks a string as "raw string" in args. For instance, calling `Buildf("SELECT %v", Raw("NOW()")).Build()` returns SQL `SELECT NOW()`.
110
+
105
111
To learn how to use builders, check out [examples on GoDoc](https://pkg.go.dev/github.com/huandu/go-sqlbuilder#pkg-examples).
106
112
107
113
### Build SQL for MySQL, PostgreSQL, SQLServer or SQLite
0 commit comments