Skip to content

Commit

Permalink
Expand expansions recursively
Browse files Browse the repository at this point in the history
  • Loading branch information
xiam committed Feb 15, 2017
1 parent 337a38c commit 03a584c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/sqlbuilder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ func TestSelect(t *testing.T) {
b.Select().Distinct(db.Raw(`ON(?) col1`, db.Raw(`SELECT foo FROM bar`))).Columns("col2", "col3").Distinct("col4", "col5").From("artist").String(),
)

assert.Equal(
`SELECT DISTINCT ON (SELECT foo FROM bar, SELECT baz from qux) col1, "col2", "col3", "col4", "col5" FROM "artist"`,
b.Select().Distinct(db.Raw(`ON ? col1`, []interface{}{db.Raw(`SELECT foo FROM bar`), db.Raw(`SELECT baz from qux`)})).Columns("col2", "col3").Distinct("col4", "col5").From("artist").String(),
)

assert.Equal(
`SELECT DISTINCT ON ((SELECT foo FROM bar, SELECT baz from qux)) col1, "col2", "col3", "col4", "col5" FROM "artist"`,
b.Select().Distinct(db.Raw(`ON (?) col1`, []db.RawValue{db.Raw(`SELECT foo FROM bar`), db.Raw(`SELECT baz from qux`)})).Columns("col2", "col3").Distinct("col4", "col5").From("artist").String(),
)

{
rawCase := db.Raw("CASE WHEN id IN ? THEN 0 ELSE 1 END", []int{1000, 2000})
sel := b.SelectFrom("artist").OrderBy(rawCase)
Expand Down
3 changes: 3 additions & 0 deletions lib/sqlbuilder/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,13 @@ func expandQuery(in string, args []interface{}, fn func(interface{}) (string, []
}
if len(args) > argn {
k, values := fn(args[argn])
k, values = expandQuery(k, values, fn)

if k != "" {
in = in[:i] + k + in[i+1:]
i += len(k) - 1
}

if len(values) > 0 {
argx = append(argx, values...)
}
Expand Down

0 comments on commit 03a584c

Please sign in to comment.