/
xo_db.xo.go
431 lines (361 loc) · 9.34 KB
/
xo_db.xo.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
// Package model contains the types for schema ...
package model
// Code generated by xo. DO NOT EDIT.
import (
"context"
"database/sql"
"database/sql/driver"
"encoding/base64"
"encoding/csv"
"fmt"
"regexp"
"strconv"
"strings"
"time"
"github.com/graph-gophers/graphql-go"
"github.com/lib/pq"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
// XODB is the common interface for database operations that can be used with
// types from schema 'public'.
//
// This should work with database/sql.DB and database/sql.Tx.
type XODB interface {
Exec(string, ...interface{}) (sql.Result, error)
Query(string, ...interface{}) (*sql.Rows, error)
QueryRow(string, ...interface{}) *sql.Row
}
// XOLog provides the log func used by generated queries.
var XOLog = func(string, ...interface{}) {}
type Verifier interface {
VerifyAC(ctx context.Context, resourceID, action string, args interface{}) error
VerifyRefAC(ctx context.Context, resourceID, action string, args interface{}) error
}
type loggerContext struct{}
// DBCtx is the key for setting DB Context.WithValue
var LogCtx = loggerContext{}
func Log(ctx context.Context) logrus.FieldLogger {
logger, ok := ctx.Value(LogCtx).(logrus.FieldLogger)
if !ok {
return logrus.New()
}
return logger
}
// ScannerValuer is the common interface for types that implement both the
// database/sql.Scanner and sql/driver.Valuer interfaces.
type ScannerValuer interface {
sql.Scanner
driver.Valuer
}
// StringSlice is a slice of strings.
type StringSlice []string
// Cursor specifies an index to sort by, the direction of the sort, an offset, and a limit.
type Cursor struct {
Offset *int32
Limit *int32
Index *string
Desc *bool
Dead *bool
After *graphql.ID
First *int32
Before *graphql.ID
Last *int32
}
var (
defaultOffset int32 = 0
defaultLimit int32 = 50
defaultIndex string = "id"
defaultDesc bool = false
defaultDead bool = false
)
// DefaultCursor will get the 50 first non-deleted IDs from a table.
var DefaultCursor = Cursor{
Offset: &defaultOffset,
Limit: &defaultLimit,
Index: &defaultIndex,
Desc: &defaultDesc,
Dead: &defaultDead,
}
// sqlConjunctionMap supported conjunction, related to graphql enum: FilterConjunction
var sqlConjunctionMap = map[string]struct{}{
"AND": {},
"OR": {},
}
// filterPair item of filter
type filterPair struct {
fieldName string
option string
value interface{}
}
// filterArguments filter arguments
type filterArguments struct {
filterPairs []*filterPair
conjunction string
conjCnt int
}
// updateArguments additional parameters when updating method
type updateArguments struct {
NullFields []string
}
// isNullFields return a bool value whether the field is set to null
func isNullFields(nullFields []string, field string) bool {
for _, nullField := range nullFields {
if field == nullField {
return true
}
}
return false
}
// quoteEscapeRegex is the regex to match escaped characters in a string.
var quoteEscapeRegex = regexp.MustCompile(`([^\\]([\\]{2})*)\\"`)
// Scan satisfies the sql.Scanner interface for StringSlice.
func (ss *StringSlice) Scan(src interface{}) error {
buf, ok := src.([]byte)
if !ok {
return errors.New("invalid StringSlice")
}
// change quote escapes for csv parser
str := quoteEscapeRegex.ReplaceAllString(string(buf), `$1""`)
str = strings.Replace(str, `\\`, `\`, -1)
// remove braces
str = str[1 : len(str)-1]
// bail if only one
if len(str) == 0 {
*ss = StringSlice([]string{})
return nil
}
// parse with csv reader
cr := csv.NewReader(strings.NewReader(str))
slice, err := cr.Read()
if err != nil {
fmt.Printf("exiting!: %v\n", err)
return err
}
*ss = StringSlice(slice)
return nil
}
// Value satisfies the driver.Valuer interface for StringSlice.
func (ss StringSlice) Value() (driver.Value, error) {
v := make([]string, len(ss))
for i, s := range ss {
v[i] = `"` + strings.Replace(strings.Replace(s, `\`, `\\\`, -1), `"`, `\"`, -1) + `"`
}
return "{" + strings.Join(v, ",") + "}", nil
}
// Slice is a slice of ScannerValuers.
type Slice []ScannerValuer
// GraphQL related types
const GraphQLCommonTypes = `
type PageInfo {
hasNextPage: Boolean!
hasPreviousPage: Boolean!
startCursor: ID
endCursor: ID
}
scalar Time
enum FilterConjunction{
AND
OR
}
`
// PageInfoResolver defines the GraphQL PageInfo type
type PageInfoResolver struct {
startCursor graphql.ID
endCursor graphql.ID
hasNextPage bool
hasPreviousPage bool
}
// StartCursor returns the start cursor (global id)
func (r PageInfoResolver) StartCursor() *graphql.ID {
return &r.startCursor
}
// EndCursor returns the end cursor (global id)
func (r PageInfoResolver) EndCursor() *graphql.ID {
return &r.endCursor
}
// HasNextPage returns if next page is available
func (r PageInfoResolver) HasNextPage() bool {
return r.hasNextPage
}
// HasPreviousPage returns if previous page is available
func (r PageInfoResolver) HasPreviousPage() bool {
return r.hasNextPage
}
func encodeCursor(typeName string, id int) graphql.ID {
return graphql.ID(base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%d", typeName, id))))
}
type dbContext struct{}
// DBCtx is the key for setting DB Context.WithValue
var DBCtx = dbContext{}
type verifierContext struct{}
var VerifierCtx = verifierContext{}
// Bool returns a nullable bool.
func Bool(b bool) sql.NullBool {
return sql.NullBool{Bool: b, Valid: true}
}
// BoolPointer converts bool pointer to sql.NullBool
func BoolPointer(b *bool) sql.NullBool {
if b == nil {
return sql.NullBool{}
}
return sql.NullBool{Bool: *b, Valid: true}
}
// PointerBool converts bool to pointer to bool
func PointerBool(b sql.NullBool) *bool {
if !b.Valid {
return nil
}
return &b.Bool
}
// Int64 returns a nullable int64
func Int64(i int64) sql.NullInt64 {
return sql.NullInt64{Int64: i, Valid: true}
}
// Int64Pointer converts a int64 pointer to sql.NullInt64
func Int64Pointer(i *int64) sql.NullInt64 {
if i == nil {
return sql.NullInt64{}
}
return sql.NullInt64{Int64: *i, Valid: true}
}
// PointerInt64 converts sql.NullInt64 to pointer to int64
func PointerInt64(i sql.NullInt64) *int64 {
if !i.Valid {
return nil
}
return &i.Int64
}
// Float64 returns a nullable float64
func Float64(i float64) sql.NullFloat64 {
return sql.NullFloat64{Float64: i, Valid: true}
}
// Float64Pointer converts a float64 pointer to sql.NullFloat64
func Float64Pointer(i *float64) sql.NullFloat64 {
if i == nil {
return sql.NullFloat64{}
}
return sql.NullFloat64{Float64: *i, Valid: true}
}
// PointerFloat64 converts sql.NullFloat64 to pointer to float64
func PointerFloat64(i sql.NullFloat64) *float64 {
if !i.Valid {
return nil
}
return &i.Float64
}
// String returns a nullable string
func String(s string) sql.NullString {
return sql.NullString{String: s, Valid: true}
}
// StringPointer converts string pointer to sql.NullString
func StringPointer(s *string) sql.NullString {
if s == nil {
return sql.NullString{}
}
return sql.NullString{String: *s, Valid: true}
}
// PointerString converts sql.NullString to pointer to string
func PointerString(s sql.NullString) *string {
if !s.Valid {
return nil
}
return &s.String
}
// Time returns a nullable Time
func Time(t time.Time) pq.NullTime {
return pq.NullTime{Time: t, Valid: true}
}
// TimePointer converts time.Time pointer to pq.NullTime
func TimePointer(t *time.Time) pq.NullTime {
if t == nil {
return pq.NullTime{}
}
return pq.NullTime{Time: *t, Valid: true}
}
// TimeGqlPointer converts graphql.Time pointer to pq.NullTime
func TimeGqlPointer(t *graphql.Time) pq.NullTime {
if t == nil {
return pq.NullTime{}
}
return pq.NullTime{Time: t.Time, Valid: true}
}
// PointerTime converts pq.NullTIme to pointer to time.Time
func PointerTime(t pq.NullTime) *time.Time {
if !t.Valid {
return nil
}
return &t.Time
}
// PointerGqlTime converts pq.NullType to pointer to graphql.Time
func PointerGqlTime(t pq.NullTime) *graphql.Time {
if !t.Valid {
return nil
}
return &graphql.Time{t.Time}
}
// PointerStringInt64 converts Int64 pointer to string pointer
func PointerStringInt64(i *int64) *string {
if i == nil {
return nil
}
s := strconv.Itoa(int(*i))
return &s
}
// PointerStringSqlInt64 converts sql.NullInt64 pointer to graphql.ID pointer
func PointerStringSqlInt64(i sql.NullInt64) *string {
if !i.Valid {
return nil
}
s := strconv.Itoa(int(i.Int64))
return &s
}
// PointerStringFloat64 converts Float64 pointer to string pointer
func PointerStringFloat64(i *float64) *string {
if i == nil {
return nil
}
s := fmt.Sprintf("%.6f", *i)
return &s
}
// PointerFloat64SqlFloat64 converts sql.NullFloat64 pointer to graphql.ID pointer
func PointerFloat64SqlFloat64(i sql.NullFloat64) *float64 {
if !i.Valid {
return nil
}
s := i.Float64
return &s
}
type RootResolver struct{}
func GetRootSchema(extraQueries, extraMutations, extraTypes string) string {
return `
schema {
query: Query
mutation: Mutation
}
type Query {
` +
GetCountryQueries() + extraQueries +
`}
type Mutation {
` +
GetCountryMutations() + extraMutations +
`}
` +
GraphQLCountryTypes +
GraphQLCommonTypes +
extraTypes
}
func GetResolverAPIs(includeAPIs map[string]string, excludeAPIs []string) map[string]string {
apis := make(map[string]string)
for k, v := range GraphQLCountryAPIs {
apis[k] = v
}
for k, v := range includeAPIs {
apis[k] = v
}
for _, k := range excludeAPIs {
delete(apis, k)
}
return apis
}