-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgx.go
49 lines (42 loc) · 949 Bytes
/
pgx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package pgxstore
import (
"strconv"
"strings"
"github.com/halliday/go-ident"
"github.com/jackc/pgx/v4"
)
var pgxResultFormatsBinary = pgx.QueryResultFormats{pgx.BinaryFormatCode}
type pgxBuilder struct {
strings.Builder
numArgs int
args []any
}
func (b *pgxBuilder) resultFormatsBinary() {
if len(b.args) != 0 {
panic("resultFormatsBinary must be called before any WriteValue")
}
b.args = append(b.args, pgxResultFormatsBinary)
}
func (b *pgxBuilder) WriteValue(v any) string {
b.args = append(b.args, v)
b.numArgs++
s := "$" + strconv.Itoa(b.numArgs)
b.WriteString(s)
return s
}
func (b *pgxBuilder) WriteSelection(sel ident.Selection) (hasWhere bool) {
if sel.All {
if len(sel.Ids) != 0 {
b.WriteString(` WHERE id != ANY(`)
b.WriteValue(sel.Ids)
b.WriteString(")")
return true
}
} else {
b.WriteString(` WHERE id = ANY(`)
b.WriteValue(sel.Ids)
b.WriteString(")")
return true
}
return false
}