Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pgstmt: impl select exists
Browse files Browse the repository at this point in the history
acoshift committed Jan 29, 2024

Verified

This commit was signed with the committer’s verified signature. The key has expired.
ejgallego Emilio Jesús Gallego Arias
1 parent 47d11e1 commit 5fafbf7
Showing 2 changed files with 30 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pgstmt/select.go
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ type SelectStatement interface {
Distinct() Distinct
Columns(col ...any)
ColumnSelect(f func(b SelectStatement), as string)
ColumnExists(f func(b SelectStatement))
From(table ...string)
FromSelect(f func(b SelectStatement), as string)
FromValues(f func(b Values), as string)
@@ -103,6 +104,15 @@ func (st *selectStmt) ColumnSelect(f func(b SelectStatement), as string) {
st.columns.push(&b)
}

func (st *selectStmt) ColumnExists(f func(b SelectStatement)) {
var x selectStmt
f(&x)

var b buffer
b.push("exists", paren(x.make()))
st.columns.push(&b)
}

func (st *selectStmt) From(table ...string) {
st.from.pushString(table...)
}
20 changes: 20 additions & 0 deletions pgstmt/select_test.go
Original file line number Diff line number Diff line change
@@ -546,6 +546,26 @@ func TestSelect(t *testing.T) {
`,
[]any{1},
},
{
"select exists",
pgstmt.Select(func(b pgstmt.SelectStatement) {
b.ColumnExists(func(b pgstmt.SelectStatement) {
b.Columns("1")
b.From("table1")
b.Where(func(b pgstmt.Cond) {
b.Eq("t1", 1)
})
})
}),
`
select exists (
select 1
from table1
where (t1 = $1)
)
`,
[]any{1},
},
}

for _, tC := range cases {

0 comments on commit 5fafbf7

Please sign in to comment.