Skip to content

Commit 7dc4b32

Browse files
jarri-abidiclaude
andcommitted
feat(golang): add emit_read_only_prepared to generate read-only queries
Add an emit_read_only_prepared option for database/sql. When set, sqlc also generates read.go with a ReadQueries type, created with PrepareReadOnly, that prepares only the read-only queries: those that SELECT and do not INSERT, UPDATE, DELETE or SELECT ... FOR UPDATE. This lets read queries be prepared on a connection whose database user can only read, such as a read replica, without preparing write statements that user isn't allowed to prepare. Prepare is unchanged and keeps every query, and the models are shared, so nothing is duplicated. read.go gets its imports from the read-only queries' own argument and result types, including lib/pq when they bind or scan slices with pq.Array. Keywords are matched when followed by any whitespace, so queries written as "SELECT\n col1, ..." are recognised. pgx is not supported; no read.go is generated for it. Adds the option to the docs and both config schemas, and endtoend fixtures for MySQL, PostgreSQL, and PostgreSQL with slice arguments and results. Closes #3187. Supersedes #3291, which was closed for merge conflicts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 3ad8b20 commit 7dc4b32

30 files changed

Lines changed: 1320 additions & 6 deletions

File tree

‎docs/reference/config.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ The `gen` mapping supports the following keys:
169169
that returns all valid enum values.
170170
- `emit_sql_as_comment`:
171171
- If true, emits the SQL statement as a code-block comment above the generated function, appending to any existing comments. Defaults to `false`.
172+
- `emit_read_only_prepared`:
173+
- If true, also generate a `read.go` file with a `ReadQueries` type, created with `PrepareReadOnly`, that holds only the read-only queries: those that `SELECT` and do not `INSERT`, `UPDATE`, `DELETE` or `SELECT ... FOR UPDATE`. Prepare it on a connection whose database user can only read, such as a read replica, while `Prepare` keeps every query. Not supported for `pgx/v4` or `pgx/v5`, where no `read.go` is generated. Defaults to `false`.
172174
- `build_tags`:
173175
- If set, add a `//go:build <build_tags>` directive at the beginning of each generated Go file.
174176
- `initialisms`:
@@ -426,6 +428,8 @@ Each mapping in the `packages` collection has the following keys:
426428
- If true, add DB tags to generated structs. Defaults to `false`.
427429
- `emit_prepared_queries`:
428430
- If true, include support for prepared queries. Defaults to `false`.
431+
- `emit_read_only_prepared`:
432+
- If true, also generate a `read.go` file with a `ReadQueries` type, created with `PrepareReadOnly`, that holds only the read-only queries: those that `SELECT` and do not `INSERT`, `UPDATE`, `DELETE` or `SELECT ... FOR UPDATE`. Prepare it on a connection whose database user can only read, such as a read replica, while `Prepare` keeps every query. Not supported for `pgx/v4` or `pgx/v5`, where no `read.go` is generated. Defaults to `false`.
429433
- `emit_interface`:
430434
- If true, output a `Querier` interface in the generated package. Defaults to `false`.
431435
- `emit_exact_table_names`:

‎internal/codegen/golang/gen.go‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type tmplCtx struct {
2525
Enums []Enum
2626
Structs []Struct
2727
GoQueries []Query
28+
GoReadQueries []Query
2829
SqlcVersion string
2930

3031
// TODO: Race conditions
@@ -39,6 +40,7 @@ type tmplCtx struct {
3940
EmitMethodsWithDBArgument bool
4041
EmitEnumValidMethod bool
4142
EmitAllEnumValues bool
43+
EmitReadOnlyPrepared bool
4244
UsesCopyFrom bool
4345
UsesBatch bool
4446
OmitSqlcVersion bool
@@ -187,6 +189,7 @@ func generate(req *plugin.GenerateRequest, options *opts.Options, enums []Enum,
187189
EmitMethodsWithDBArgument: options.EmitMethodsWithDbArgument,
188190
EmitEnumValidMethod: options.EmitEnumValidMethod,
189191
EmitAllEnumValues: options.EmitAllEnumValues,
192+
EmitReadOnlyPrepared: options.EmitReadOnlyPrepared,
190193
UsesCopyFrom: usesCopyFrom(queries),
191194
UsesBatch: usesBatch(queries),
192195
SQLDriver: parseDriver(options.SqlPackage),
@@ -252,6 +255,7 @@ func generate(req *plugin.GenerateRequest, options *opts.Options, enums []Enum,
252255
w := bufio.NewWriter(&b)
253256
tctx.SourceName = name
254257
tctx.GoQueries = replacedQueries
258+
tctx.GoReadQueries = readOnly(replacedQueries)
255259
err := tmpl.ExecuteTemplate(w, templateName, &tctx)
256260
w.Flush()
257261
if err != nil {
@@ -290,6 +294,7 @@ func generate(req *plugin.GenerateRequest, options *opts.Options, enums []Enum,
290294
if options.OutputCopyfromFileName != "" {
291295
copyfromFileName = options.OutputCopyfromFileName
292296
}
297+
readQueriesFileName := "read.go"
293298

294299
batchFileName := "batch.go"
295300
if options.OutputBatchFileName != "" {
@@ -319,6 +324,11 @@ func generate(req *plugin.GenerateRequest, options *opts.Options, enums []Enum,
319324
return nil, err
320325
}
321326
}
327+
if tctx.EmitReadOnlyPrepared {
328+
if err := execute(readQueriesFileName, "readQueriesFile"); err != nil {
329+
return nil, err
330+
}
331+
}
322332

323333
files := map[string]struct{}{}
324334
for _, gq := range queries {
@@ -428,3 +438,50 @@ func filterUnusedStructs(enums []Enum, structs []Struct, queries []Query, qualif
428438

429439
return keepEnums, keepStructs
430440
}
441+
442+
// keywordFollowedBySpace reports whether kw appears in s followed by whitespace
443+
// (space, tab, or newline). A plain substring check on "KEYWORD " misses queries
444+
// formatted with a newline right after the keyword, e.g.:
445+
//
446+
// SELECT
447+
// col1, col2
448+
//
449+
// which is a common style for wide column lists and must not be misclassified
450+
// as lacking a SELECT (or falsely matching UPDATE/INSERT/DELETE only when
451+
// followed by a space).
452+
func keywordFollowedBySpace(s, kw string) bool {
453+
for {
454+
idx := strings.Index(s, kw)
455+
if idx == -1 {
456+
return false
457+
}
458+
after := idx + len(kw)
459+
if after < len(s) && (s[after] == ' ' || s[after] == '\t' || s[after] == '\n' || s[after] == '\r') {
460+
return true
461+
}
462+
s = s[after:]
463+
}
464+
}
465+
466+
func readOnly(queries []Query) []Query {
467+
var rq []Query
468+
for _, q := range queries {
469+
if !q.hasRetType() {
470+
continue
471+
}
472+
473+
qsql := strings.ToUpper(q.SQL)
474+
if !keywordFollowedBySpace(qsql, "SELECT") {
475+
continue
476+
}
477+
if keywordFollowedBySpace(qsql, "INSERT") ||
478+
keywordFollowedBySpace(qsql, "UPDATE") ||
479+
strings.Contains(qsql, "FOR UPDATE") ||
480+
keywordFollowedBySpace(qsql, "DELETE") {
481+
continue
482+
}
483+
484+
rq = append(rq, q)
485+
}
486+
return rq
487+
}

‎internal/codegen/golang/imports.go‎

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ func (i *importer) Imports(filename string) [][]ImportSpec {
102102
if i.Options.OutputBatchFileName != "" {
103103
batchFileName = i.Options.OutputBatchFileName
104104
}
105+
readQueriesFileName := "read.go"
105106

106107
switch filename {
107108
case dbFileName:
@@ -114,6 +115,8 @@ func (i *importer) Imports(filename string) [][]ImportSpec {
114115
return mergeImports(i.copyfromImports())
115116
case batchFileName:
116117
return mergeImports(i.batchImports())
118+
case readQueriesFileName:
119+
return mergeImports(i.readOnlyImports())
117120
default:
118121
return mergeImports(i.queryImports(filename))
119122
}
@@ -360,10 +363,27 @@ func (i *importer) queryImports(filename string) fileImports {
360363
}
361364
}
362365

363-
std, pkg := buildImports(i.Options, i.Engine, gq, func(name string) bool {
366+
return queryFileImports(i.Options, i.Engine, gq, anyNonCopyFrom, true)
367+
}
368+
369+
// readOnlyImports computes the imports needed by read.go. Its boilerplate
370+
// (PrepareReadOnly/Close/query/queryRow) always needs the same baseline as
371+
// db.go (context, database/sql, fmt), plus whatever the read-only subset of
372+
// queries (see readOnly) additionally needs for its own Ret/Arg types - e.g.
373+
// "strings" for a query using sqlc.slice(). Reusing dbImports alone silently
374+
// misses that second part.
375+
func (i *importer) readOnlyImports() fileImports {
376+
base := i.dbImports()
377+
extra := queryFileImports(i.Options, i.Engine, readOnly(i.Queries), false, false)
378+
merged := mergeImports(base, extra)
379+
return fileImports{Std: merged[0], Dep: merged[1]}
380+
}
381+
382+
func queryFileImports(options *opts.Options, engine string, gq []Query, anyNonCopyFrom bool, scanStructFields bool) fileImports {
383+
std, pkg := buildImports(options, engine, gq, func(name string) bool {
364384
for _, q := range gq {
365385
if q.hasRetType() {
366-
if q.Ret.EmitStruct() {
386+
if scanStructFields && q.Ret.EmitStruct() {
367387
for _, f := range q.Ret.Struct.Fields {
368388
if hasPrefixIgnoringSliceAndPointerPrefix(f.Type, name) {
369389
return true
@@ -375,7 +395,7 @@ func (i *importer) queryImports(filename string) fileImports {
375395
}
376396
}
377397
// Check the fields of the argument struct if it's emitted
378-
if q.Arg.EmitStruct() {
398+
if scanStructFields && q.Arg.EmitStruct() {
379399
for _, f := range q.Arg.Struct.Fields {
380400
if hasPrefixIgnoringSliceAndPointerPrefix(f.Type, name) {
381401
return true
@@ -392,6 +412,9 @@ func (i *importer) queryImports(filename string) fileImports {
392412
return false
393413
})
394414

415+
// lib/pq is needed whenever a query scans into or binds a slice with
416+
// pq.Array, which happens in the method bodies whether or not this file
417+
// declares the Arg/Ret structs, so it is not gated by scanStructFields.
395418
sliceScan := func() bool {
396419
for _, q := range gq {
397420
if q.hasRetType() {
@@ -450,15 +473,15 @@ func (i *importer) queryImports(filename string) fileImports {
450473
std["context"] = struct{}{}
451474
}
452475

453-
sqlpkg := parseDriver(i.Options.SqlPackage)
476+
sqlpkg := parseDriver(options.SqlPackage)
454477
if sqlcSliceScan() && !sqlpkg.IsPGX() {
455478
std["strings"] = struct{}{}
456479
}
457-
if sliceScan() && usesPqArrays(i.Engine, sqlpkg) {
480+
if sliceScan() && usesPqArrays(engine, sqlpkg) {
458481
pkg[ImportSpec{Path: "github.com/lib/pq"}] = struct{}{}
459482
}
460483

461-
if i.Options.WrapErrors {
484+
if options.WrapErrors {
462485
std["fmt"] = struct{}{}
463486
}
464487

‎internal/codegen/golang/opts/options.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type Options struct {
2727
EmitEnumValidMethod bool `json:"emit_enum_valid_method,omitempty" yaml:"emit_enum_valid_method"`
2828
EmitAllEnumValues bool `json:"emit_all_enum_values,omitempty" yaml:"emit_all_enum_values"`
2929
EmitSqlAsComment bool `json:"emit_sql_as_comment,omitempty" yaml:"emit_sql_as_comment"`
30+
EmitReadOnlyPrepared bool `json:"emit_read_only_prepared,omitempty" yaml:"emit_read_only_prepared"`
3031
JsonTagsCaseStyle string `json:"json_tags_case_style,omitempty" yaml:"json_tags_case_style"`
3132
Package string `json:"package" yaml:"package"`
3233
Out string `json:"out" yaml:"out"`
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
{{define "readQueriesFileStd"}}
2+
3+
{{if and .EmitPreparedQueries .EmitReadOnlyPrepared}}
4+
func PrepareReadOnly(ctx context.Context, db DBTX) (*ReadQueries, error) {
5+
q := ReadQueries{db: db}
6+
var err error
7+
{{- if eq (len .GoReadQueries) 0 }}
8+
_ = err
9+
{{- end }}
10+
{{- range .GoReadQueries }}
11+
if q.{{.FieldName}}, err = db.PrepareContext(ctx, {{.ConstantName}}); err != nil {
12+
return nil, fmt.Errorf("error preparing query {{.MethodName}}: %w", err)
13+
}
14+
{{- end}}
15+
return &q, nil
16+
}
17+
18+
type ReadQueries struct {
19+
{{- if not .EmitMethodsWithDBArgument}}
20+
db DBTX
21+
{{- end}}
22+
23+
{{- if .EmitPreparedQueries}}
24+
{{- range .GoReadQueries}}
25+
{{.FieldName}} *sql.Stmt
26+
{{- end}}
27+
{{- end}}
28+
}
29+
30+
func (q *ReadQueries) Close() error {
31+
var err error
32+
{{- range .GoReadQueries }}
33+
if q.{{.FieldName}} != nil {
34+
if cerr := q.{{.FieldName}}.Close(); cerr != nil {
35+
err = fmt.Errorf("error closing {{.FieldName}}: %w", cerr)
36+
}
37+
}
38+
{{- end}}
39+
return err
40+
}
41+
42+
func (q *ReadQueries) query(ctx context.Context, stmt *sql.Stmt, query string, args ...interface{}) (*sql.Rows, error) {
43+
switch {
44+
case stmt != nil:
45+
return stmt.QueryContext(ctx, args...)
46+
default:
47+
return q.db.QueryContext(ctx, query, args...)
48+
}
49+
}
50+
51+
func (q *ReadQueries) queryRow(ctx context.Context, stmt *sql.Stmt, query string, args ...interface{}) (*sql.Row) {
52+
switch {
53+
case stmt != nil:
54+
return stmt.QueryRowContext(ctx, args...)
55+
default:
56+
return q.db.QueryRowContext(ctx, query, args...)
57+
}
58+
}
59+
60+
{{range .GoReadQueries}}
61+
62+
{{if eq .Cmd ":one"}}
63+
{{range .Comments}}//{{.}}
64+
{{end -}}
65+
func (q *ReadQueries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}) ({{.Ret.DefineType}}, error) {
66+
{{- template "queryCodeStdExec" . }}
67+
{{- if or (ne .Arg.Pair .Ret.Pair) (ne .Arg.DefineType .Ret.DefineType) }}
68+
var {{.Ret.Name}} {{.Ret.Type}}
69+
{{- end}}
70+
err := row.Scan({{.Ret.Scan}})
71+
return {{.Ret.ReturnName}}, err
72+
}
73+
{{end}}
74+
75+
{{if eq .Cmd ":many"}}
76+
{{range .Comments}}//{{.}}
77+
{{end -}}
78+
func (q *ReadQueries) {{.MethodName}}(ctx context.Context, {{ dbarg }} {{.Arg.Pair}}) ([]{{.Ret.DefineType}}, error) {
79+
{{- template "queryCodeStdExec" . }}
80+
if err != nil {
81+
return nil, err
82+
}
83+
defer rows.Close()
84+
{{- if $.EmitEmptySlices}}
85+
items := []{{.Ret.DefineType}}{}
86+
{{else}}
87+
var items []{{.Ret.DefineType}}
88+
{{end -}}
89+
for rows.Next() {
90+
var {{.Ret.Name}} {{.Ret.Type}}
91+
if err := rows.Scan({{.Ret.Scan}}); err != nil {
92+
return nil, err
93+
}
94+
items = append(items, {{.Ret.ReturnName}})
95+
}
96+
if err := rows.Close(); err != nil {
97+
return nil, err
98+
}
99+
if err := rows.Err(); err != nil {
100+
return nil, err
101+
}
102+
return items, nil
103+
}
104+
{{end}}
105+
106+
{{end}}
107+
{{end}}
108+
{{end}}

‎internal/codegen/golang/templates/template.tmpl‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,4 +251,31 @@ import (
251251
{{if .SQLDriver.IsPGX }}
252252
{{- template "batchCodePgx" .}}
253253
{{end}}
254+
255+
{{template "readQueriesFile" . }}
256+
{{end}}
257+
258+
{{define "readQueriesFile"}}
259+
{{if not .SQLDriver.IsPGX }}
260+
{{if .BuildTags}}
261+
//go:build {{.BuildTags}}
262+
263+
{{end}}// Code generated by sqlc. DO NOT EDIT.
264+
{{if not .OmitSqlcVersion}}// versions:
265+
// sqlc {{.SqlcVersion}}
266+
267+
package {{.Package}}
268+
269+
{{ if hasImports .SourceName }}
270+
import (
271+
{{range imports .SourceName}}
272+
{{range .}}{{.}}
273+
{{end}}
274+
{{end}}
275+
)
276+
{{end}}
277+
278+
{{- template "readQueriesFileStd" .}}
279+
{{end}}
280+
{{end}}
254281
{{end}}

‎internal/config/v_one.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type v1PackageSettings struct {
4343
EmitEnumValidMethod bool `json:"emit_enum_valid_method,omitempty" yaml:"emit_enum_valid_method"`
4444
EmitAllEnumValues bool `json:"emit_all_enum_values,omitempty" yaml:"emit_all_enum_values"`
4545
EmitSqlAsComment bool `json:"emit_sql_as_comment,omitempty" yaml:"emit_sql_as_comment"`
46+
EmitReadOnlyPrepared bool `json:"emit_read_only_prepared,omitempty" yaml:"emit_read_only_prepared"`
4647
JSONTagsCaseStyle string `json:"json_tags_case_style,omitempty" yaml:"json_tags_case_style"`
4748
SQLPackage string `json:"sql_package" yaml:"sql_package"`
4849
SQLDriver string `json:"sql_driver" yaml:"sql_driver"`
@@ -158,6 +159,7 @@ func (c *V1GenerateSettings) Translate() Config {
158159
EmitEnumValidMethod: pkg.EmitEnumValidMethod,
159160
EmitAllEnumValues: pkg.EmitAllEnumValues,
160161
EmitSqlAsComment: pkg.EmitSqlAsComment,
162+
EmitReadOnlyPrepared: pkg.EmitReadOnlyPrepared,
161163
Package: pkg.Name,
162164
Out: pkg.Path,
163165
SqlPackage: pkg.SQLPackage,

‎internal/config/v_one.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@
137137
"emit_sql_as_comment": {
138138
"type": "boolean"
139139
},
140+
"emit_read_only_prepared": {
141+
"type": "boolean"
142+
},
140143
"build_tags": {
141144
"type": "string"
142145
},

‎internal/config/v_two.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@
146146
"emit_sql_as_comment": {
147147
"type": "boolean"
148148
},
149+
"emit_read_only_prepared": {
150+
"type": "boolean"
151+
},
149152
"build_tags": {
150153
"type": "string"
151154
},

0 commit comments

Comments
 (0)