-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* pgstmt: add union * pgstmt: add join union to select statement * add offset * add nested union * update test
- Loading branch information
Showing
5 changed files
with
274 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package pgstmt | ||
|
||
func Union(f func(b UnionStatement)) *Result { | ||
var st unionStmt | ||
f(&st) | ||
return newResult(build(st.make())) | ||
} | ||
|
||
type UnionStatement interface { | ||
Select(f func(b SelectStatement)) | ||
AllSelect(f func(b SelectStatement)) | ||
Union(f func(b UnionStatement)) | ||
AllUnion(f func(b UnionStatement)) | ||
OrderBy(col string) OrderBy | ||
Limit(n int64) | ||
Offset(n int64) | ||
} | ||
|
||
type unionStmt struct { | ||
b buffer | ||
orderBy group | ||
limit *int64 | ||
offset *int64 | ||
} | ||
|
||
func (st *unionStmt) Select(f func(b SelectStatement)) { | ||
var x selectStmt | ||
f(&x) | ||
|
||
if st.b.empty() { | ||
st.b.push(paren(x.make())) | ||
} else { | ||
st.b.push("union", paren(x.make())) | ||
} | ||
} | ||
|
||
func (st *unionStmt) AllSelect(f func(b SelectStatement)) { | ||
var x selectStmt | ||
f(&x) | ||
|
||
if st.b.empty() { | ||
st.b.push(paren(x.make())) | ||
} else { | ||
st.b.push("union all", paren(x.make())) | ||
} | ||
} | ||
|
||
func (st *unionStmt) Union(f func(b UnionStatement)) { | ||
var x unionStmt | ||
f(&x) | ||
|
||
if st.b.empty() { | ||
st.b.push(paren(x.make())) | ||
} else { | ||
st.b.push("union", paren(x.make())) | ||
} | ||
} | ||
|
||
func (st *unionStmt) AllUnion(f func(b UnionStatement)) { | ||
var x unionStmt | ||
f(&x) | ||
|
||
if st.b.empty() { | ||
st.b.push(paren(x.make())) | ||
} else { | ||
st.b.push("union all", paren(x.make())) | ||
} | ||
} | ||
|
||
func (st *unionStmt) OrderBy(col string) OrderBy { | ||
p := orderBy{ | ||
col: col, | ||
} | ||
st.orderBy.push(&p) | ||
return &p | ||
} | ||
|
||
func (st *unionStmt) Limit(n int64) { | ||
st.limit = &n | ||
} | ||
|
||
func (st *unionStmt) Offset(n int64) { | ||
st.offset = &n | ||
} | ||
|
||
func (st *unionStmt) make() *buffer { | ||
var b buffer | ||
b.push(&st.b) | ||
if !st.orderBy.empty() { | ||
b.push("order by", &st.orderBy) | ||
} | ||
if st.limit != nil { | ||
b.push("limit", *st.limit) | ||
} | ||
if st.offset != nil { | ||
b.push("offset", *st.offset) | ||
} | ||
return &b | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package pgstmt_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/acoshift/pgsql/pgstmt" | ||
) | ||
|
||
func TestUnion(t *testing.T) { | ||
t.Parallel() | ||
|
||
cases := []struct { | ||
name string | ||
result *pgstmt.Result | ||
query string | ||
args []interface{} | ||
}{ | ||
{ | ||
"union select", | ||
pgstmt.Union(func(b pgstmt.UnionStatement) { | ||
b.Select(func(b pgstmt.SelectStatement) { | ||
b.Columns("id") | ||
b.From("table1") | ||
}) | ||
b.AllSelect(func(b pgstmt.SelectStatement) { | ||
b.Columns("id") | ||
b.From("table2") | ||
}) | ||
b.OrderBy("id") | ||
b.Limit(10) | ||
b.Offset(2) | ||
}), | ||
` | ||
(select id from table1) | ||
union all (select id from table2) | ||
order by id | ||
limit 10 offset 2 | ||
`, | ||
nil, | ||
}, | ||
{ | ||
"union nested", | ||
pgstmt.Union(func(b pgstmt.UnionStatement) { | ||
b.Union(func(b pgstmt.UnionStatement) { | ||
b.Select(func(b pgstmt.SelectStatement) { | ||
b.Columns("id") | ||
b.From("table1") | ||
}) | ||
b.Select(func(b pgstmt.SelectStatement) { | ||
b.Columns("id") | ||
b.From("table2") | ||
}) | ||
}) | ||
b.Select(func(b pgstmt.SelectStatement) { | ||
b.Columns("id") | ||
b.From("table3") | ||
}) | ||
b.AllUnion(func(b pgstmt.UnionStatement) { | ||
b.Select(func(b pgstmt.SelectStatement) { | ||
b.Columns("id") | ||
b.From("table4") | ||
}) | ||
b.Select(func(b pgstmt.SelectStatement) { | ||
b.Columns("id") | ||
b.From("table5") | ||
}) | ||
}) | ||
}), | ||
` | ||
( | ||
(select id from table1) | ||
union (select id from table2) | ||
) | ||
union (select id from table3) | ||
union all ( | ||
(select id from table4) | ||
union | ||
(select id from table5) | ||
) | ||
`, | ||
nil, | ||
}, | ||
} | ||
|
||
for _, tC := range cases { | ||
t.Run(tC.name, func(t *testing.T) { | ||
q, args := tC.result.SQL() | ||
assert.Equal(t, stripSpace(tC.query), q) | ||
assert.EqualValues(t, tC.args, args) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters