-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathquery.sql.go
176 lines (145 loc) · 5.5 KB
/
query.sql.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Code generated by pggen. DO NOT EDIT.
package void
import (
"context"
"fmt"
"github.com/jackc/pgconn"
"github.com/jackc/pgtype"
"github.com/jackc/pgx/v4"
)
// Querier is a typesafe Go interface backed by SQL queries.
type Querier interface {
VoidOnly(ctx context.Context) (pgconn.CommandTag, error)
VoidOnlyTwoParams(ctx context.Context, id int32) (pgconn.CommandTag, error)
VoidTwo(ctx context.Context) (string, error)
VoidThree(ctx context.Context) (VoidThreeRow, error)
VoidThree2(ctx context.Context) ([]string, error)
}
var _ Querier = &DBQuerier{}
type DBQuerier struct {
conn genericConn // underlying Postgres transport to use
types *typeResolver // resolve types by name
}
// genericConn is a connection like *pgx.Conn, pgx.Tx, or *pgxpool.Pool.
type genericConn interface {
Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
Exec(ctx context.Context, sql string, arguments ...any) (pgconn.CommandTag, error)
}
// NewQuerier creates a DBQuerier that implements Querier.
func NewQuerier(conn genericConn) *DBQuerier {
return &DBQuerier{conn: conn, types: newTypeResolver()}
}
// typeResolver looks up the pgtype.ValueTranscoder by Postgres type name.
type typeResolver struct {
connInfo *pgtype.ConnInfo // types by Postgres type name
}
func newTypeResolver() *typeResolver {
ci := pgtype.NewConnInfo()
return &typeResolver{connInfo: ci}
}
// findValue find the OID, and pgtype.ValueTranscoder for a Postgres type name.
func (tr *typeResolver) findValue(name string) (uint32, pgtype.ValueTranscoder, bool) {
typ, ok := tr.connInfo.DataTypeForName(name)
if !ok {
return 0, nil, false
}
v := pgtype.NewValue(typ.Value)
return typ.OID, v.(pgtype.ValueTranscoder), true
}
// setValue sets the value of a ValueTranscoder to a value that should always
// work and panics if it fails.
func (tr *typeResolver) setValue(vt pgtype.ValueTranscoder, val interface{}) pgtype.ValueTranscoder {
if err := vt.Set(val); err != nil {
panic(fmt.Sprintf("set ValueTranscoder %T to %+v: %s", vt, val, err))
}
return vt
}
const voidOnlySQL = `SELECT void_fn();`
// VoidOnly implements Querier.VoidOnly.
func (q *DBQuerier) VoidOnly(ctx context.Context) (pgconn.CommandTag, error) {
ctx = context.WithValue(ctx, "pggen_query_name", "VoidOnly")
cmdTag, err := q.conn.Exec(ctx, voidOnlySQL)
if err != nil {
return cmdTag, fmt.Errorf("exec query VoidOnly: %w", err)
}
return cmdTag, err
}
const voidOnlyTwoParamsSQL = `SELECT void_fn_two_params($1, 'text');`
// VoidOnlyTwoParams implements Querier.VoidOnlyTwoParams.
func (q *DBQuerier) VoidOnlyTwoParams(ctx context.Context, id int32) (pgconn.CommandTag, error) {
ctx = context.WithValue(ctx, "pggen_query_name", "VoidOnlyTwoParams")
cmdTag, err := q.conn.Exec(ctx, voidOnlyTwoParamsSQL, id)
if err != nil {
return cmdTag, fmt.Errorf("exec query VoidOnlyTwoParams: %w", err)
}
return cmdTag, err
}
const voidTwoSQL = `SELECT void_fn(), 'foo' as name;`
// VoidTwo implements Querier.VoidTwo.
func (q *DBQuerier) VoidTwo(ctx context.Context) (string, error) {
ctx = context.WithValue(ctx, "pggen_query_name", "VoidTwo")
row := q.conn.QueryRow(ctx, voidTwoSQL)
var item string
if err := row.Scan(nil, &item); err != nil {
return item, fmt.Errorf("query VoidTwo: %w", err)
}
return item, nil
}
const voidThreeSQL = `SELECT void_fn(), 'foo' as foo, 'bar' as bar;`
type VoidThreeRow struct {
Foo string `json:"foo"`
Bar string `json:"bar"`
}
// VoidThree implements Querier.VoidThree.
func (q *DBQuerier) VoidThree(ctx context.Context) (VoidThreeRow, error) {
ctx = context.WithValue(ctx, "pggen_query_name", "VoidThree")
row := q.conn.QueryRow(ctx, voidThreeSQL)
var item VoidThreeRow
if err := row.Scan(nil, &item.Foo, &item.Bar); err != nil {
return item, fmt.Errorf("query VoidThree: %w", err)
}
return item, nil
}
const voidThree2SQL = `SELECT 'foo' as foo, void_fn(), void_fn();`
// VoidThree2 implements Querier.VoidThree2.
func (q *DBQuerier) VoidThree2(ctx context.Context) ([]string, error) {
ctx = context.WithValue(ctx, "pggen_query_name", "VoidThree2")
rows, err := q.conn.Query(ctx, voidThree2SQL)
if err != nil {
return nil, fmt.Errorf("query VoidThree2: %w", err)
}
defer rows.Close()
items := []string{}
for rows.Next() {
var item string
if err := rows.Scan(&item, nil, nil); err != nil {
return nil, fmt.Errorf("scan VoidThree2 row: %w", err)
}
items = append(items, item)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("close VoidThree2 rows: %w", err)
}
return items, err
}
// textPreferrer wraps a pgtype.ValueTranscoder and sets the preferred encoding
// format to text instead binary (the default). pggen uses the text format
// when the OID is unknownOID because the binary format requires the OID.
// Typically occurs for unregistered types.
type textPreferrer struct {
pgtype.ValueTranscoder
typeName string
}
// PreferredParamFormat implements pgtype.ParamFormatPreferrer.
func (t textPreferrer) PreferredParamFormat() int16 { return pgtype.TextFormatCode }
func (t textPreferrer) NewTypeValue() pgtype.Value {
return textPreferrer{ValueTranscoder: pgtype.NewValue(t.ValueTranscoder).(pgtype.ValueTranscoder), typeName: t.typeName}
}
func (t textPreferrer) TypeName() string {
return t.typeName
}
// unknownOID means we don't know the OID for a type. This is okay for decoding
// because pgx call DecodeText or DecodeBinary without requiring the OID. For
// encoding parameters, pggen uses textPreferrer if the OID is unknown.
const unknownOID = 0