Skip to content

Commit 0cb0562

Browse files
authored
update readme to introduce utility methods
Add doc per request in #67
1 parent 153aee0 commit 0cb0562

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

README.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@
99
- [Usage](#usage)
1010
- [Basic usage](#basic-usage)
1111
- [Pre-defined SQL builders](#pre-defined-sql-builders)
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)
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)
1414
- [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)
1616
- [Argument modifiers](#argument-modifiers)
1717
- [Freestyle builder](#freestyle-builder)
1818
- [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)
2020
- [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)
2222
- [License](#license)
2323

2424
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)
6868

6969
### Pre-defined SQL builders
7070

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.
7272

7373
- [Struct](https://pkg.go.dev/github.com/huandu/go-sqlbuilder#Struct): Builder factory for a struct.
7474
- [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
8181
- [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).
8282
- [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.
8383

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.
8585

8686
```go
8787
// Build a SQL to create a HIVE table.
@@ -102,6 +102,12 @@ fmt.Println(sql)
102102
// CREATE TABLE users PARTITION BY (year) AS SELECT columns[0] id, columns[1] name, columns[2] year FROM `all-users.csv` LIMIT 100
103103
```
104104

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+
105111
To learn how to use builders, check out [examples on GoDoc](https://pkg.go.dev/github.com/huandu/go-sqlbuilder#pkg-examples).
106112

107113
### Build SQL for MySQL, PostgreSQL, SQLServer or SQLite

0 commit comments

Comments
 (0)