Skip to content

Commit

Permalink
new: Added Flavor method to Builder interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Vasily Rodionov authored and Vasily Rodionov committed Oct 31, 2024
1 parent e7bb45c commit 4b59c4a
Show file tree
Hide file tree
Showing 19 changed files with 119 additions and 89 deletions.
12 changes: 12 additions & 0 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
type Builder interface {
Build() (sql string, args []interface{})
BuildWithFlavor(flavor Flavor, initialArg ...interface{}) (sql string, args []interface{})
Flavor() Flavor
}

type compiledBuilder struct {
Expand All @@ -30,6 +31,12 @@ func (cb *compiledBuilder) BuildWithFlavor(flavor Flavor, initialArg ...interfac
return cb.args.CompileWithFlavor(cb.format, flavor, initialArg...)
}

// Flavor returns flavor of builder
// Always returns DefaultFlavor
func (cb *compiledBuilder) Flavor() Flavor {
return cb.args.Flavor
}

type flavoredBuilder struct {
builder Builder
flavor Flavor
Expand All @@ -43,6 +50,11 @@ func (fb *flavoredBuilder) BuildWithFlavor(flavor Flavor, initialArg ...interfac
return fb.builder.BuildWithFlavor(flavor, initialArg...)
}

// Flavor returns flavor of builder
func (fb *flavoredBuilder) Flavor() Flavor {
return fb.flavor
}

// WithFlavor creates a new Builder based on builder with a default flavor.
func WithFlavor(builder Builder, flavor Flavor) Builder {
return &flavoredBuilder{
Expand Down
19 changes: 19 additions & 0 deletions builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,22 @@ func TestBuildWithCQL(t *testing.T) {
a.Equal(sql, "BEGIN BATCH USING TIMESTAMP ? INSERT INTO t1 (col1, col2) VALUES (?, ?); INSERT INTO t2 (col3, col4) VALUES (?, ?); APPLY BATCH;")
a.Equal(args, []interface{}{1481124356754405, 1, 2, 3, 4})
}

func TestBuilderGetFlavor(t *testing.T) {
a := assert.New(t)

defaultBuilder := Build("SELECT * FROM foo WHERE id = $0", 1234)
a.Equal(DefaultFlavor, defaultBuilder.Flavor())

buildfBuilder := Buildf("SELECT * FROM foo WHERE id = %v", 1234)
a.Equal(DefaultFlavor, buildfBuilder.Flavor())

namedBuilder := Buildf("SELECT * FROM ${table} WHERE id = 1234", map[string]interface{}{
"table": "foo",
})
a.Equal(DefaultFlavor, namedBuilder.Flavor())

flavoredBuilder := WithFlavor(Build("SELECT * FROM foo WHERE id = $0", 1234), PostgreSQL)
a.Equal(PostgreSQL, flavoredBuilder.Flavor())

}
3 changes: 2 additions & 1 deletion createtable.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ func (ctb *CreateTableBuilder) SetFlavor(flavor Flavor) (old Flavor) {
return
}

func (ctb *CreateTableBuilder) GetFlavor() Flavor {
// Flavor returns flavor of builder
func (ctb *CreateTableBuilder) Flavor() Flavor {
return ctb.args.Flavor
}

Expand Down
17 changes: 8 additions & 9 deletions createtable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,12 @@ func ExampleCreateTableBuilder_NumDefine() {
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)

ctb.SetFlavor(PostgreSQL)
flavor := ctb.Flavor()
a.Equal(PostgreSQL, flavor)

ctbClick := ClickHouse.NewCreateTableBuilder()
flavor = ctbClick.Flavor()
a.Equal(ClickHouse, flavor)
}
3 changes: 2 additions & 1 deletion cte.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ func (cteb *CTEBuilder) SetFlavor(flavor Flavor) (old Flavor) {
return
}

func (cteb *CTEBuilder) GetFlavor() Flavor {
// Flavor returns flavor of builder
func (cteb *CTEBuilder) Flavor() Flavor {
return cteb.args.Flavor
}

Expand Down
34 changes: 16 additions & 18 deletions cte_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,27 +134,25 @@ func TestRecursiveCTEBuilder(t *testing.T) {
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)

cteb.SetFlavor(PostgreSQL)
flavor := cteb.Flavor()
a.Equal(PostgreSQL, flavor)

ctebClick := ClickHouse.NewCTEBuilder()
flavor = ctebClick.Flavor()
a.Equal(ClickHouse, 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)

ctetb.SetFlavor(PostgreSQL)
flavor := ctetb.Flavor()
a.Equal(PostgreSQL, flavor)

ctetbClick := ClickHouse.NewCTEQueryBuilder()
flavor = ctetbClick.Flavor()
a.Equal(ClickHouse, flavor)
}
3 changes: 2 additions & 1 deletion ctequery.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ func (ctetb *CTEQueryBuilder) SetFlavor(flavor Flavor) (old Flavor) {
return
}

func (ctetb *CTEQueryBuilder) GetFlavor() Flavor {
// Flavor returns flavor of builder
func (ctetb *CTEQueryBuilder) Flavor() Flavor {
return ctetb.args.Flavor
}

Expand Down
3 changes: 2 additions & 1 deletion delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ func (db *DeleteBuilder) SetFlavor(flavor Flavor) (old Flavor) {
return
}

func (db *DeleteBuilder) GetFlavor() Flavor {
// Flavor returns flavor of builder
func (db *DeleteBuilder) Flavor() Flavor {
return db.args.Flavor
}

Expand Down
17 changes: 8 additions & 9 deletions delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,12 @@ func ExampleDeleteBuilder_With() {
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)

db.SetFlavor(PostgreSQL)
flavor := db.Flavor()
a.Equal(PostgreSQL, flavor)

dbClick := ClickHouse.NewDeleteBuilder()
flavor = dbClick.Flavor()
a.Equal(ClickHouse, flavor)
}
3 changes: 2 additions & 1 deletion insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ func (ib *InsertBuilder) SetFlavor(flavor Flavor) (old Flavor) {
return
}

func (ib *InsertBuilder) GetFlavor() Flavor {
// Flavor returns flavor of builder
func (ib *InsertBuilder) Flavor() Flavor {
return ib.args.Flavor
}

Expand Down
17 changes: 8 additions & 9 deletions insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,12 @@ func ExampleInsertBuilder_NumValue() {
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)

ib.SetFlavor(PostgreSQL)
flavor := ib.Flavor()
a.Equal(PostgreSQL, flavor)

ibClick := ClickHouse.NewInsertBuilder()
flavor = ibClick.Flavor()
a.Equal(ClickHouse, flavor)
}
3 changes: 2 additions & 1 deletion select.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,8 @@ func (sb *SelectBuilder) SetFlavor(flavor Flavor) (old Flavor) {
return
}

func (sb *SelectBuilder) GetFlavor() Flavor {
// Flavor returns flavor of builder
func (sb *SelectBuilder) Flavor() Flavor {
return sb.args.Flavor
}

Expand Down
17 changes: 8 additions & 9 deletions select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,13 +379,12 @@ func TestSelectBuilderSelectMore(t *testing.T) {
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)

sb.SetFlavor(PostgreSQL)
flavor := sb.Flavor()
a.Equal(PostgreSQL, flavor)

sbClick := ClickHouse.NewSelectBuilder()
flavor = sbClick.Flavor()
a.Equal(ClickHouse, flavor)
}
3 changes: 2 additions & 1 deletion union.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ func (ub *UnionBuilder) SetFlavor(flavor Flavor) (old Flavor) {
return
}

func (ub *UnionBuilder) GetFlavor() Flavor {
// Flavor returns flavor of builder
func (ub *UnionBuilder) Flavor() Flavor {
return ub.args.Flavor
}

Expand Down
17 changes: 8 additions & 9 deletions union_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,12 @@ func TestUnionForSQLite(t *testing.T) {
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)

ub.SetFlavor(PostgreSQL)
flavor := ub.Flavor()
a.Equal(PostgreSQL, flavor)

ubClick := ClickHouse.NewUnionBuilder()
flavor = ubClick.Flavor()
a.Equal(ClickHouse, flavor)
}
3 changes: 2 additions & 1 deletion update.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ func (ub *UpdateBuilder) SetFlavor(flavor Flavor) (old Flavor) {
return
}

func (ub *UpdateBuilder) GetFlavor() Flavor {
// Flavor returns flavor of builder
func (ub *UpdateBuilder) Flavor() Flavor {
return ub.args.Flavor
}

Expand Down
17 changes: 8 additions & 9 deletions update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,12 @@ func ExampleUpdateBuilder_With() {
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)

ub.SetFlavor(PostgreSQL)
flavor := ub.Flavor()
a.Equal(PostgreSQL, flavor)

ubClick := ClickHouse.NewUpdateBuilder()
flavor = ubClick.Flavor()
a.Equal(ClickHouse, flavor)
}
3 changes: 2 additions & 1 deletion whereclause.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ func (wc *WhereClause) SetFlavor(flavor Flavor) (old Flavor) {
return
}

func (wc *WhereClause) GetFlavor() Flavor {
// Flavor returns flavor of clause
func (wc *WhereClause) Flavor() Flavor {
return wc.flavor
}

Expand Down
14 changes: 6 additions & 8 deletions whereclause_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,20 +315,18 @@ func TestEmptyStringsWhereAddWhereExpr(t *testing.T) {
func TestWhereClauseGetFlavor(t *testing.T) {
a := assert.New(t)
wc := NewWhereClause()
postgresFlavor := PostgreSQL
wc.SetFlavor(postgresFlavor)
flavor := wc.GetFlavor()
a.Equal(postgresFlavor, flavor)
wc.SetFlavor(PostgreSQL)
flavor := wc.Flavor()
a.Equal(PostgreSQL, flavor)
}

func TestWhereClauseCopyGetFlavor(t *testing.T) {
a := assert.New(t)

wc := NewWhereClause()
postgresFlavor := PostgreSQL
wc.SetFlavor(postgresFlavor)
wc.SetFlavor(PostgreSQL)

wcCopy := CopyWhereClause(wc)
flavor := wcCopy.GetFlavor()
a.Equal(postgresFlavor, flavor)
flavor := wcCopy.Flavor()
a.Equal(PostgreSQL, flavor)
}

0 comments on commit 4b59c4a

Please sign in to comment.