mirror of
https://github.com/hanzoai/sqlbuilder.git
synced 2026-10-01 00:37:42 +00:00
support nested sql
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
[](https://travis-ci.org/huandu/go-sqlbuilder)
|
||||
[](https://godoc.org/github.com/huandu/go-sqlbuilder)
|
||||
|
||||
Package `go-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` defined in package `database/sql`.
|
||||
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` defined in package `database/sql`.
|
||||
|
||||
## Install ##
|
||||
|
||||
|
||||
@@ -88,8 +88,16 @@ func (args *Args) Compile(sql string) (query string, values []interface{}) {
|
||||
sql = sql[i:]
|
||||
|
||||
if pointer, err := strconv.Atoi(digits); err == nil && pointer < len(args.args) {
|
||||
buf.WriteRune('?')
|
||||
values = append(values, args.args[pointer])
|
||||
arg := args.args[pointer]
|
||||
|
||||
if b, ok := arg.(Builder); ok {
|
||||
sql, nestedArgs := b.Build()
|
||||
buf.WriteString(sql)
|
||||
values = append(values, nestedArgs...)
|
||||
} else {
|
||||
buf.WriteRune('?')
|
||||
values = append(values, arg)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,13 +35,13 @@ func (db *DeleteBuilder) DeleteFrom(table string) *DeleteBuilder {
|
||||
return db
|
||||
}
|
||||
|
||||
// Where sets expressions of WHERE in UPDATE.
|
||||
// Where sets expressions of WHERE in DELETE.
|
||||
func (db *DeleteBuilder) Where(andExpr ...string) *DeleteBuilder {
|
||||
db.whereExprs = append(db.whereExprs, andExpr...)
|
||||
return db
|
||||
}
|
||||
|
||||
// String returns the compiled UPDATE string.
|
||||
// String returns the compiled DELETE string.
|
||||
func (db *DeleteBuilder) String() string {
|
||||
s, _ := db.Build()
|
||||
return s
|
||||
|
||||
@@ -50,7 +50,7 @@ func (ib *InsertBuilder) Values(value ...interface{}) *InsertBuilder {
|
||||
return ib
|
||||
}
|
||||
|
||||
// String returns the compiled UPDATE string.
|
||||
// String returns the compiled DELETE string.
|
||||
func (ib *InsertBuilder) String() string {
|
||||
s, _ := ib.Build()
|
||||
return s
|
||||
|
||||
@@ -107,7 +107,7 @@ func (sb *SelectBuilder) Offset(offset int) *SelectBuilder {
|
||||
return sb
|
||||
}
|
||||
|
||||
// As returns an AS expreesion.
|
||||
// As returns an AS expression.
|
||||
func (sb *SelectBuilder) As(col, alias string) string {
|
||||
return fmt.Sprintf("%v AS %v", Escape(col), Escape(alias))
|
||||
}
|
||||
|
||||
+5
-1
@@ -18,6 +18,10 @@ func ExampleSelectBuilder() {
|
||||
sb.IsNull("id_card"),
|
||||
sb.In("status", 1, 2, 5),
|
||||
),
|
||||
sb.NotIn(
|
||||
"id",
|
||||
NewSelectBuilder().Select("id").From("banned"),
|
||||
), // Nested SELECT.
|
||||
"modified_at > created_at + "+sb.Var(86400), // It's allowed to write arbitrary SQL.
|
||||
)
|
||||
sb.GroupBy("status").Having(sb.NotIn("status", 4, 5))
|
||||
@@ -29,6 +33,6 @@ func ExampleSelectBuilder() {
|
||||
fmt.Println(args)
|
||||
|
||||
// Output:
|
||||
// SELECT DISTINCT id, name, COUNT(*) AS t FROM demo.user WHERE id > ? AND name LIKE ? AND (id_card IS NULL OR status IN (?, ?, ?)) AND modified_at > created_at + ? GROUP BY status HAVING status NOT IN (?, ?) ORDER BY modified_at ASC LIMIT 10 OFFSET 5
|
||||
// SELECT DISTINCT id, name, COUNT(*) AS t FROM demo.user WHERE id > ? AND name LIKE ? AND (id_card IS NULL OR status IN (?, ?, ?)) AND id NOT IN (SELECT id FROM banned) AND modified_at > created_at + ? GROUP BY status HAVING status NOT IN (?, ?) ORDER BY modified_at ASC LIMIT 10 OFFSET 5
|
||||
// [1234 %Du 1 2 5 86400 4 5]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user