forked from sqlc-dev/sqlc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbCode.tmpl
More file actions
117 lines (108 loc) · 2.93 KB
/
dbCode.tmpl
File metadata and controls
117 lines (108 loc) · 2.93 KB
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
{{define "dbCodeTemplateStd"}}
type DBTX interface {
{{- if .EmitBegin }}
BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error)
{{end}}
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
PrepareContext(context.Context, string) (*sql.Stmt, error)
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
}
{{ if .EmitMethodsWithDBArgument}}
func New() *Queries {
return &Queries{}
{{- else -}}
func New(db DBTX) *Queries {
return &Queries{db: db}
{{- end}}
}
{{if .EmitPreparedQueries}}
func Prepare(ctx context.Context, db DBTX) (*Queries, error) {
q := Queries{db: db}
var err error
{{- if eq (len .GoQueries) 0 }}
_ = err
{{- end }}
{{- range .GoQueries }}
if q.{{.FieldName}}, err = db.PrepareContext(ctx, {{.ConstantName}}); err != nil {
return nil, fmt.Errorf("error preparing query {{.MethodName}}: %w", err)
}
{{- end}}
return &q, nil
}
func (q *Queries) Close() error {
var err error
{{- range .GoQueries }}
if q.{{.FieldName}} != nil {
if cerr := q.{{.FieldName}}.Close(); cerr != nil {
err = fmt.Errorf("error closing {{.FieldName}}: %w", cerr)
}
}
{{- end}}
return err
}
func (q *Queries) exec(ctx context.Context, stmt *sql.Stmt, query string, args ...interface{}) (sql.Result, error) {
switch {
case stmt != nil && q.tx != nil:
return q.tx.StmtContext(ctx, stmt).ExecContext(ctx, args...)
case stmt != nil:
return stmt.ExecContext(ctx, args...)
default:
return q.db.ExecContext(ctx, query, args...)
}
}
func (q *Queries) query(ctx context.Context, stmt *sql.Stmt, query string, args ...interface{}) (*sql.Rows, error) {
switch {
case stmt != nil && q.tx != nil:
return q.tx.StmtContext(ctx, stmt).QueryContext(ctx, args...)
case stmt != nil:
return stmt.QueryContext(ctx, args...)
default:
return q.db.QueryContext(ctx, query, args...)
}
}
func (q *Queries) queryRow(ctx context.Context, stmt *sql.Stmt, query string, args ...interface{}) (*sql.Row) {
switch {
case stmt != nil && q.tx != nil:
return q.tx.StmtContext(ctx, stmt).QueryRowContext(ctx, args...)
case stmt != nil:
return stmt.QueryRowContext(ctx, args...)
default:
return q.db.QueryRowContext(ctx, query, args...)
}
}
{{end}}
type Queries struct {
{{- if not .EmitMethodsWithDBArgument}}
db DBTX
{{- end}}
{{- if .EmitPreparedQueries}}
tx *sql.Tx
{{- range .GoQueries}}
{{.FieldName}} *sql.Stmt
{{- end}}
{{- end}}
}
{{if not .EmitMethodsWithDBArgument}}
func (q *Queries) WithTx(tx *sql.Tx) *Queries {
return &Queries{
db: tx,
{{- if .EmitPreparedQueries}}
tx: tx,
{{- range .GoQueries}}
{{.FieldName}}: q.{{.FieldName}},
{{- end}}
{{- end}}
}
}
{{- if .EmitBegin }}
func (q *Queries) BeginTx(ctx context.Context, opts driver.TxOptions) (*Queries, error) {
tx, err := q.db.BeginTx(ctx, opts)
if (err != nil {
return nil, err
}
return q.WithTx(tx), nil
}
{{end}}
{{end}}
{{end}}