mirror of
https://github.com/hanzoai/sqlbuilder.git
synced 2026-10-01 00:37:42 +00:00
new: flavor getters
This commit is contained in:
@@ -158,6 +158,10 @@ func (ctb *CreateTableBuilder) SetFlavor(flavor Flavor) (old Flavor) {
|
||||
return
|
||||
}
|
||||
|
||||
func (ctb *CreateTableBuilder) GetFlavor() Flavor {
|
||||
return ctb.args.Flavor
|
||||
}
|
||||
|
||||
// Var returns a placeholder for value.
|
||||
func (ctb *CreateTableBuilder) Var(arg interface{}) string {
|
||||
return ctb.args.Add(arg)
|
||||
|
||||
@@ -5,6 +5,9 @@ package sqlbuilder
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/huandu/go-assert"
|
||||
)
|
||||
|
||||
func ExampleCreateTable() {
|
||||
@@ -86,3 +89,17 @@ func ExampleCreateTableBuilder_NumDefine() {
|
||||
// Output:
|
||||
// 5
|
||||
}
|
||||
|
||||
func TestCreateTableGetFlavor(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
ctb := newCreateTableBuilder()
|
||||
postgresFlavor := PostgreSQL
|
||||
clickhouseFlavor := ClickHouse
|
||||
ctb.SetFlavor(postgresFlavor)
|
||||
flavor := ctb.GetFlavor()
|
||||
a.Equal(postgresFlavor, flavor)
|
||||
|
||||
ctbClick := clickhouseFlavor.NewCreateTableBuilder()
|
||||
flavor = ctbClick.GetFlavor()
|
||||
a.Equal(clickhouseFlavor, flavor)
|
||||
}
|
||||
|
||||
@@ -112,6 +112,10 @@ func (cteb *CTEBuilder) SetFlavor(flavor Flavor) (old Flavor) {
|
||||
return
|
||||
}
|
||||
|
||||
func (cteb *CTEBuilder) GetFlavor() Flavor {
|
||||
return cteb.args.Flavor
|
||||
}
|
||||
|
||||
// SQL adds an arbitrary sql to current position.
|
||||
func (cteb *CTEBuilder) SQL(sql string) *CTEBuilder {
|
||||
cteb.injection.SQL(cteb.marker, sql)
|
||||
|
||||
+28
@@ -130,3 +130,31 @@ func TestRecursiveCTEBuilder(t *testing.T) {
|
||||
sql = ctetb.String()
|
||||
a.Equal(sql, "/* table init */ t (a, b) /* after table */ AS (SELECT a, b FROM t) /* after table as */")
|
||||
}
|
||||
|
||||
func TestCTEGetFlavor(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
cteb := newCTEBuilder()
|
||||
postgresFlavor := PostgreSQL
|
||||
clickhouseFlavor := ClickHouse
|
||||
cteb.SetFlavor(postgresFlavor)
|
||||
flavor := cteb.GetFlavor()
|
||||
a.Equal(postgresFlavor, flavor)
|
||||
|
||||
ctebClick := clickhouseFlavor.NewCTEBuilder()
|
||||
flavor = ctebClick.GetFlavor()
|
||||
a.Equal(clickhouseFlavor, flavor)
|
||||
}
|
||||
|
||||
func TestCTEQueryBuilderGetFlavor(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
ctetb := newCTEQueryBuilder()
|
||||
postgresFlavor := PostgreSQL
|
||||
clickhouseFlavor := ClickHouse
|
||||
ctetb.SetFlavor(postgresFlavor)
|
||||
flavor := ctetb.GetFlavor()
|
||||
a.Equal(postgresFlavor, flavor)
|
||||
|
||||
ctetbClick := clickhouseFlavor.NewCTEQueryBuilder()
|
||||
flavor = ctetbClick.GetFlavor()
|
||||
a.Equal(clickhouseFlavor, flavor)
|
||||
}
|
||||
|
||||
@@ -124,6 +124,10 @@ func (ctetb *CTEQueryBuilder) SetFlavor(flavor Flavor) (old Flavor) {
|
||||
return
|
||||
}
|
||||
|
||||
func (ctetb *CTEQueryBuilder) GetFlavor() Flavor {
|
||||
return ctetb.args.Flavor
|
||||
}
|
||||
|
||||
// SQL adds an arbitrary sql to current position.
|
||||
func (ctetb *CTEQueryBuilder) SQL(sql string) *CTEQueryBuilder {
|
||||
ctetb.injection.SQL(ctetb.marker, sql)
|
||||
|
||||
@@ -200,6 +200,10 @@ func (db *DeleteBuilder) SetFlavor(flavor Flavor) (old Flavor) {
|
||||
return
|
||||
}
|
||||
|
||||
func (db *DeleteBuilder) GetFlavor() Flavor {
|
||||
return db.args.Flavor
|
||||
}
|
||||
|
||||
// SQL adds an arbitrary sql to current position.
|
||||
func (db *DeleteBuilder) SQL(sql string) *DeleteBuilder {
|
||||
db.injection.SQL(db.marker, sql)
|
||||
|
||||
@@ -5,6 +5,9 @@ package sqlbuilder
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/huandu/go-assert"
|
||||
)
|
||||
|
||||
func ExampleDeleteFrom() {
|
||||
@@ -80,3 +83,17 @@ func ExampleDeleteBuilder_With() {
|
||||
// Output:
|
||||
// WITH users AS (SELECT id, name FROM users WHERE name IS NULL) DELETE FROM orders WHERE users.id = orders.user_id
|
||||
}
|
||||
|
||||
func TestDeleteBuilderGetFlavor(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
db := newDeleteBuilder()
|
||||
postgresFlavor := PostgreSQL
|
||||
clickhouseFlavor := ClickHouse
|
||||
db.SetFlavor(postgresFlavor)
|
||||
flavor := db.GetFlavor()
|
||||
a.Equal(postgresFlavor, flavor)
|
||||
|
||||
dbClick := clickhouseFlavor.NewDeleteBuilder()
|
||||
flavor = dbClick.GetFlavor()
|
||||
a.Equal(clickhouseFlavor, flavor)
|
||||
}
|
||||
|
||||
@@ -213,6 +213,10 @@ func (ib *InsertBuilder) SetFlavor(flavor Flavor) (old Flavor) {
|
||||
return
|
||||
}
|
||||
|
||||
func (ib *InsertBuilder) GetFlavor() Flavor {
|
||||
return ib.args.Flavor
|
||||
}
|
||||
|
||||
// Var returns a placeholder for value.
|
||||
func (ib *InsertBuilder) Var(arg interface{}) string {
|
||||
return ib.args.Add(arg)
|
||||
|
||||
@@ -5,6 +5,9 @@ package sqlbuilder
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/huandu/go-assert"
|
||||
)
|
||||
|
||||
func ExampleInsertInto() {
|
||||
@@ -241,3 +244,17 @@ func ExampleInsertBuilder_NumValue() {
|
||||
// Output:
|
||||
// 2
|
||||
}
|
||||
|
||||
func TestInsertBuilderGetFlavor(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
ib := newInsertBuilder()
|
||||
postgresFlavor := PostgreSQL
|
||||
clickhouseFlavor := ClickHouse
|
||||
ib.SetFlavor(postgresFlavor)
|
||||
flavor := ib.GetFlavor()
|
||||
a.Equal(postgresFlavor, flavor)
|
||||
|
||||
ibClick := clickhouseFlavor.NewInsertBuilder()
|
||||
flavor = ibClick.GetFlavor()
|
||||
a.Equal(clickhouseFlavor, flavor)
|
||||
}
|
||||
|
||||
@@ -537,6 +537,10 @@ func (sb *SelectBuilder) SetFlavor(flavor Flavor) (old Flavor) {
|
||||
return
|
||||
}
|
||||
|
||||
func (sb *SelectBuilder) GetFlavor() Flavor {
|
||||
return sb.args.Flavor
|
||||
}
|
||||
|
||||
// SQL adds an arbitrary sql to current position.
|
||||
func (sb *SelectBuilder) SQL(sql string) *SelectBuilder {
|
||||
sb.injection.SQL(sb.marker, sql)
|
||||
|
||||
@@ -375,3 +375,17 @@ func TestSelectBuilderSelectMore(t *testing.T) {
|
||||
).SQL("/* second */").SelectMore("name").SQL("/* third */")
|
||||
a.Equal(sb.String(), "SELECT id, name /* first */ /* third */ WHERE name IS NOT NULL /* second */")
|
||||
}
|
||||
|
||||
func TestSelectBuilderGetFlavor(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
sb := newSelectBuilder()
|
||||
postgresFlavor := PostgreSQL
|
||||
clickhouseFlavor := ClickHouse
|
||||
sb.SetFlavor(postgresFlavor)
|
||||
flavor := sb.GetFlavor()
|
||||
a.Equal(postgresFlavor, flavor)
|
||||
|
||||
sbClick := clickhouseFlavor.NewSelectBuilder()
|
||||
flavor = sbClick.GetFlavor()
|
||||
a.Equal(clickhouseFlavor, flavor)
|
||||
}
|
||||
|
||||
@@ -204,6 +204,10 @@ func (ub *UnionBuilder) SetFlavor(flavor Flavor) (old Flavor) {
|
||||
return
|
||||
}
|
||||
|
||||
func (ub *UnionBuilder) GetFlavor() Flavor {
|
||||
return ub.args.Flavor
|
||||
}
|
||||
|
||||
// Var returns a placeholder for value.
|
||||
func (ub *UnionBuilder) Var(arg interface{}) string {
|
||||
return ub.args.Add(arg)
|
||||
|
||||
@@ -91,3 +91,17 @@ func TestUnionForSQLite(t *testing.T) {
|
||||
|
||||
a.Equal(sql, "SELECT id, name FROM users WHERE created_at > DATE('now', '-15 days') UNION ALL SELECT id, nick_name FROM user_extras WHERE status IN (1, 2, 3) ORDER BY id")
|
||||
}
|
||||
|
||||
func TestUnionBuilderGetFlavor(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
ub := newUnionBuilder()
|
||||
postgresFlavor := PostgreSQL
|
||||
clickhouseFlavor := ClickHouse
|
||||
ub.SetFlavor(postgresFlavor)
|
||||
flavor := ub.GetFlavor()
|
||||
a.Equal(postgresFlavor, flavor)
|
||||
|
||||
ubClick := clickhouseFlavor.NewUnionBuilder()
|
||||
flavor = ubClick.GetFlavor()
|
||||
a.Equal(clickhouseFlavor, flavor)
|
||||
}
|
||||
|
||||
@@ -269,6 +269,10 @@ func (ub *UpdateBuilder) SetFlavor(flavor Flavor) (old Flavor) {
|
||||
return
|
||||
}
|
||||
|
||||
func (ub *UpdateBuilder) GetFlavor() Flavor {
|
||||
return ub.args.Flavor
|
||||
}
|
||||
|
||||
// SQL adds an arbitrary sql to current position.
|
||||
func (ub *UpdateBuilder) SQL(sql string) *UpdateBuilder {
|
||||
ub.injection.SQL(ub.marker, sql)
|
||||
|
||||
@@ -149,3 +149,17 @@ func ExampleUpdateBuilder_With() {
|
||||
// Output:
|
||||
// WITH users AS (SELECT id, name FROM users WHERE prime IS NOT NULL) UPDATE orders SET orders.transport_fee = 0 WHERE users.id = orders.user_id
|
||||
}
|
||||
|
||||
func TestUpdateBuilderGetFlavor(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
ub := newUpdateBuilder()
|
||||
postgresFlavor := PostgreSQL
|
||||
clickhouseFlavor := ClickHouse
|
||||
ub.SetFlavor(postgresFlavor)
|
||||
flavor := ub.GetFlavor()
|
||||
a.Equal(postgresFlavor, flavor)
|
||||
|
||||
ubClick := clickhouseFlavor.NewUpdateBuilder()
|
||||
flavor = ubClick.GetFlavor()
|
||||
a.Equal(clickhouseFlavor, flavor)
|
||||
}
|
||||
|
||||
@@ -86,6 +86,10 @@ func (wc *WhereClause) SetFlavor(flavor Flavor) (old Flavor) {
|
||||
return
|
||||
}
|
||||
|
||||
func (wc *WhereClause) GetFlavor() Flavor {
|
||||
return wc.flavor
|
||||
}
|
||||
|
||||
// AddWhereExpr adds an AND expression to WHERE clause with the specified arguments.
|
||||
func (wc *WhereClause) AddWhereExpr(args *Args, andExpr ...string) *WhereClause {
|
||||
if len(andExpr) == 0 {
|
||||
|
||||
@@ -311,3 +311,24 @@ func TestEmptyStringsWhereAddWhereExpr(t *testing.T) {
|
||||
a.Equal(ub.String(), "UPDATE t SET foo = 1 ")
|
||||
a.Equal(db.String(), "DELETE FROM t ")
|
||||
}
|
||||
|
||||
func TestWhereClauseGetFlavor(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
wc := NewWhereClause()
|
||||
postgresFlavor := PostgreSQL
|
||||
wc.SetFlavor(postgresFlavor)
|
||||
flavor := wc.GetFlavor()
|
||||
a.Equal(postgresFlavor, flavor)
|
||||
}
|
||||
|
||||
func TestWhereClauseCopyGetFlavor(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
|
||||
wc := NewWhereClause()
|
||||
postgresFlavor := PostgreSQL
|
||||
wc.SetFlavor(postgresFlavor)
|
||||
|
||||
wcCopy := CopyWhereClause(wc)
|
||||
flavor := wcCopy.GetFlavor()
|
||||
a.Equal(postgresFlavor, flavor)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user