-
Notifications
You must be signed in to change notification settings - Fork 27
/
query.sql.go
383 lines (336 loc) · 12.6 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// Code generated by pggen. DO NOT EDIT.
package composite
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 {
SearchScreenshots(ctx context.Context, params SearchScreenshotsParams) ([]SearchScreenshotsRow, error)
SearchScreenshotsOneCol(ctx context.Context, params SearchScreenshotsOneColParams) ([][]Blocks, error)
InsertScreenshotBlocks(ctx context.Context, screenshotID int, body string) (InsertScreenshotBlocksRow, error)
ArraysInput(ctx context.Context, arrays Arrays) (Arrays, error)
UserEmails(ctx context.Context) (UserEmail, 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()}
}
// Arrays represents the Postgres composite type "arrays".
type Arrays struct {
Texts []string `json:"texts"`
Int8s []*int `json:"int8s"`
Bools []bool `json:"bools"`
Floats []*float64 `json:"floats"`
}
// Blocks represents the Postgres composite type "blocks".
type Blocks struct {
ID int `json:"id"`
ScreenshotID int `json:"screenshot_id"`
Body string `json:"body"`
}
// UserEmail represents the Postgres composite type "user_email".
type UserEmail struct {
ID string `json:"id"`
Email pgtype.Text `json:"email"`
}
// 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
}
type compositeField struct {
name string // name of the field
typeName string // Postgres type name
defaultVal pgtype.ValueTranscoder // default value to use
}
func (tr *typeResolver) newCompositeValue(name string, fields ...compositeField) pgtype.ValueTranscoder {
if _, val, ok := tr.findValue(name); ok {
return val
}
fs := make([]pgtype.CompositeTypeField, len(fields))
vals := make([]pgtype.ValueTranscoder, len(fields))
isBinaryOk := true
for i, field := range fields {
oid, val, ok := tr.findValue(field.typeName)
if !ok {
oid = unknownOID
val = field.defaultVal
}
isBinaryOk = isBinaryOk && oid != unknownOID
fs[i] = pgtype.CompositeTypeField{Name: field.name, OID: oid}
vals[i] = val
}
// Okay to ignore error because it's only thrown when the number of field
// names does not equal the number of ValueTranscoders.
typ, _ := pgtype.NewCompositeTypeValues(name, fs, vals)
if !isBinaryOk {
return textPreferrer{ValueTranscoder: typ, typeName: name}
}
return typ
}
func (tr *typeResolver) newArrayValue(name, elemName string, defaultVal func() pgtype.ValueTranscoder) pgtype.ValueTranscoder {
if _, val, ok := tr.findValue(name); ok {
return val
}
elemOID, elemVal, ok := tr.findValue(elemName)
elemValFunc := func() pgtype.ValueTranscoder {
return pgtype.NewValue(elemVal).(pgtype.ValueTranscoder)
}
if !ok {
elemOID = unknownOID
elemValFunc = defaultVal
}
typ := pgtype.NewArrayType(name, elemOID, elemValFunc)
if elemOID == unknownOID {
return textPreferrer{ValueTranscoder: typ, typeName: name}
}
return typ
}
// newArrays creates a new pgtype.ValueTranscoder for the Postgres
// composite type 'arrays'.
func (tr *typeResolver) newArrays() pgtype.ValueTranscoder {
return tr.newCompositeValue(
"arrays",
compositeField{name: "texts", typeName: "_text", defaultVal: &pgtype.TextArray{}},
compositeField{name: "int8s", typeName: "_int8", defaultVal: &pgtype.Int8Array{}},
compositeField{name: "bools", typeName: "_bool", defaultVal: &pgtype.BoolArray{}},
compositeField{name: "floats", typeName: "_float8", defaultVal: &pgtype.Float8Array{}},
)
}
// newArraysInit creates an initialized pgtype.ValueTranscoder for the
// Postgres composite type 'arrays' to encode query parameters.
func (tr *typeResolver) newArraysInit(v Arrays) pgtype.ValueTranscoder {
return tr.setValue(tr.newArrays(), tr.newArraysRaw(v))
}
// newArraysRaw returns all composite fields for the Postgres composite
// type 'arrays' as a slice of interface{} to encode query parameters.
func (tr *typeResolver) newArraysRaw(v Arrays) []interface{} {
return []interface{}{
v.Texts,
v.Int8s,
v.Bools,
v.Floats,
}
}
// newBlocks creates a new pgtype.ValueTranscoder for the Postgres
// composite type 'blocks'.
func (tr *typeResolver) newBlocks() pgtype.ValueTranscoder {
return tr.newCompositeValue(
"blocks",
compositeField{name: "id", typeName: "int4", defaultVal: &pgtype.Int4{}},
compositeField{name: "screenshot_id", typeName: "int8", defaultVal: &pgtype.Int8{}},
compositeField{name: "body", typeName: "text", defaultVal: &pgtype.Text{}},
)
}
// newUserEmail creates a new pgtype.ValueTranscoder for the Postgres
// composite type 'user_email'.
func (tr *typeResolver) newUserEmail() pgtype.ValueTranscoder {
return tr.newCompositeValue(
"user_email",
compositeField{name: "id", typeName: "text", defaultVal: &pgtype.Text{}},
compositeField{name: "email", typeName: "citext", defaultVal: &pgtype.Text{}},
)
}
// newBlocksArray creates a new pgtype.ValueTranscoder for the Postgres
// '_blocks' array type.
func (tr *typeResolver) newBlocksArray() pgtype.ValueTranscoder {
return tr.newArrayValue("_blocks", "blocks", tr.newBlocks)
}
// newboolArrayRaw returns all elements for the Postgres array type '_bool'
// as a slice of interface{} for use with the pgtype.Value Set method.
func (tr *typeResolver) newboolArrayRaw(vs []bool) []interface{} {
elems := make([]interface{}, len(vs))
for i, v := range vs {
elems[i] = v
}
return elems
}
const searchScreenshotsSQL = `SELECT
ss.id,
array_agg(bl) AS blocks
FROM screenshots ss
JOIN blocks bl ON bl.screenshot_id = ss.id
WHERE bl.body LIKE $1 || '%'
GROUP BY ss.id
ORDER BY ss.id
LIMIT $2 OFFSET $3;`
type SearchScreenshotsParams struct {
Body string `json:"Body"`
Limit int `json:"Limit"`
Offset int `json:"Offset"`
}
type SearchScreenshotsRow struct {
ID int `json:"id"`
Blocks []Blocks `json:"blocks"`
}
// SearchScreenshots implements Querier.SearchScreenshots.
func (q *DBQuerier) SearchScreenshots(ctx context.Context, params SearchScreenshotsParams) ([]SearchScreenshotsRow, error) {
ctx = context.WithValue(ctx, "pggen_query_name", "SearchScreenshots")
rows, err := q.conn.Query(ctx, searchScreenshotsSQL, params.Body, params.Limit, params.Offset)
if err != nil {
return nil, fmt.Errorf("query SearchScreenshots: %w", err)
}
defer rows.Close()
items := []SearchScreenshotsRow{}
blocksArray := q.types.newBlocksArray()
for rows.Next() {
var item SearchScreenshotsRow
if err := rows.Scan(&item.ID, blocksArray); err != nil {
return nil, fmt.Errorf("scan SearchScreenshots row: %w", err)
}
if err := blocksArray.AssignTo(&item.Blocks); err != nil {
return nil, fmt.Errorf("assign SearchScreenshots row: %w", err)
}
items = append(items, item)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("close SearchScreenshots rows: %w", err)
}
return items, err
}
const searchScreenshotsOneColSQL = `SELECT
array_agg(bl) AS blocks
FROM screenshots ss
JOIN blocks bl ON bl.screenshot_id = ss.id
WHERE bl.body LIKE $1 || '%'
GROUP BY ss.id
ORDER BY ss.id
LIMIT $2 OFFSET $3;`
type SearchScreenshotsOneColParams struct {
Body string `json:"Body"`
Limit int `json:"Limit"`
Offset int `json:"Offset"`
}
// SearchScreenshotsOneCol implements Querier.SearchScreenshotsOneCol.
func (q *DBQuerier) SearchScreenshotsOneCol(ctx context.Context, params SearchScreenshotsOneColParams) ([][]Blocks, error) {
ctx = context.WithValue(ctx, "pggen_query_name", "SearchScreenshotsOneCol")
rows, err := q.conn.Query(ctx, searchScreenshotsOneColSQL, params.Body, params.Limit, params.Offset)
if err != nil {
return nil, fmt.Errorf("query SearchScreenshotsOneCol: %w", err)
}
defer rows.Close()
items := [][]Blocks{}
blocksArray := q.types.newBlocksArray()
for rows.Next() {
var item []Blocks
if err := rows.Scan(blocksArray); err != nil {
return nil, fmt.Errorf("scan SearchScreenshotsOneCol row: %w", err)
}
if err := blocksArray.AssignTo(&item); err != nil {
return nil, fmt.Errorf("assign SearchScreenshotsOneCol row: %w", err)
}
items = append(items, item)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("close SearchScreenshotsOneCol rows: %w", err)
}
return items, err
}
const insertScreenshotBlocksSQL = `WITH screens AS (
INSERT INTO screenshots (id) VALUES ($1)
ON CONFLICT DO NOTHING
)
INSERT
INTO blocks (screenshot_id, body)
VALUES ($1, $2)
RETURNING id, screenshot_id, body;`
type InsertScreenshotBlocksRow struct {
ID int `json:"id"`
ScreenshotID int `json:"screenshot_id"`
Body string `json:"body"`
}
// InsertScreenshotBlocks implements Querier.InsertScreenshotBlocks.
func (q *DBQuerier) InsertScreenshotBlocks(ctx context.Context, screenshotID int, body string) (InsertScreenshotBlocksRow, error) {
ctx = context.WithValue(ctx, "pggen_query_name", "InsertScreenshotBlocks")
row := q.conn.QueryRow(ctx, insertScreenshotBlocksSQL, screenshotID, body)
var item InsertScreenshotBlocksRow
if err := row.Scan(&item.ID, &item.ScreenshotID, &item.Body); err != nil {
return item, fmt.Errorf("query InsertScreenshotBlocks: %w", err)
}
return item, nil
}
const arraysInputSQL = `SELECT $1::arrays;`
// ArraysInput implements Querier.ArraysInput.
func (q *DBQuerier) ArraysInput(ctx context.Context, arrays Arrays) (Arrays, error) {
ctx = context.WithValue(ctx, "pggen_query_name", "ArraysInput")
row := q.conn.QueryRow(ctx, arraysInputSQL, q.types.newArraysInit(arrays))
var item Arrays
arraysRow := q.types.newArrays()
if err := row.Scan(arraysRow); err != nil {
return item, fmt.Errorf("query ArraysInput: %w", err)
}
if err := arraysRow.AssignTo(&item); err != nil {
return item, fmt.Errorf("assign ArraysInput row: %w", err)
}
return item, nil
}
const userEmailsSQL = `SELECT ('foo', '[email protected]')::user_email;`
// UserEmails implements Querier.UserEmails.
func (q *DBQuerier) UserEmails(ctx context.Context) (UserEmail, error) {
ctx = context.WithValue(ctx, "pggen_query_name", "UserEmails")
row := q.conn.QueryRow(ctx, userEmailsSQL)
var item UserEmail
rowRow := q.types.newUserEmail()
if err := row.Scan(rowRow); err != nil {
return item, fmt.Errorf("query UserEmails: %w", err)
}
if err := rowRow.AssignTo(&item); err != nil {
return item, fmt.Errorf("assign UserEmails row: %w", err)
}
return item, nil
}
// 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