Skip to content

Commit 3abf5ae

Browse files
authored
pgstmt: impl select exists (#41)
1 parent 47d11e1 commit 3abf5ae

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

pgstmt/select.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type SelectStatement interface {
1212
Distinct() Distinct
1313
Columns(col ...any)
1414
ColumnSelect(f func(b SelectStatement), as string)
15+
ColumnExists(f func(b SelectStatement))
1516
From(table ...string)
1617
FromSelect(f func(b SelectStatement), as string)
1718
FromValues(f func(b Values), as string)
@@ -103,6 +104,15 @@ func (st *selectStmt) ColumnSelect(f func(b SelectStatement), as string) {
103104
st.columns.push(&b)
104105
}
105106

107+
func (st *selectStmt) ColumnExists(f func(b SelectStatement)) {
108+
var x selectStmt
109+
f(&x)
110+
111+
var b buffer
112+
b.push("exists", paren(x.make()))
113+
st.columns.push(&b)
114+
}
115+
106116
func (st *selectStmt) From(table ...string) {
107117
st.from.pushString(table...)
108118
}

pgstmt/select_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,26 @@ func TestSelect(t *testing.T) {
546546
`,
547547
[]any{1},
548548
},
549+
{
550+
"select exists",
551+
pgstmt.Select(func(b pgstmt.SelectStatement) {
552+
b.ColumnExists(func(b pgstmt.SelectStatement) {
553+
b.Columns("1")
554+
b.From("table1")
555+
b.Where(func(b pgstmt.Cond) {
556+
b.Eq("t1", 1)
557+
})
558+
})
559+
}),
560+
`
561+
select exists (
562+
select 1
563+
from table1
564+
where (t1 = $1)
565+
)
566+
`,
567+
[]any{1},
568+
},
549569
}
550570

551571
for _, tC := range cases {

0 commit comments

Comments
 (0)