You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 7dc4b32
Browse filesBrowse the repository at this point in the historyBrowse files
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>
Copy file name to clipboardExpand all lines: docs/reference/config.md
+4Lines changed: 4 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -169,6 +169,8 @@ The `gen` mapping supports the following keys:
169
169
that returns all valid enum values.
170
170
- `emit_sql_as_comment`:
171
171
- 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`.
172
174
- `build_tags`:
173
175
- If set, add a `//go:build <build_tags>` directive at the beginning of each generated Go file.
174
176
- `initialisms`:
@@ -426,6 +428,8 @@ Each mapping in the `packages` collection has the following keys:
426
428
- If true, add DB tags to generated structs. Defaults to `false`.
427
429
- `emit_prepared_queries`:
428
430
- 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`.
429
433
- `emit_interface`:
430
434
- If true, output a `Querier` interface in the generated package. Defaults to `false`.
0 commit comments