Skip to content

Commit 4808ce2

Browse files
committed
feat(codegen): expose DBTX inside Queries when expose_db_connection is enabled
Signed-off-by: Nathanael DEMACON <[email protected]>
1 parent 349137b commit 4808ce2

File tree

11 files changed

+188
-0
lines changed

11 files changed

+188
-0
lines changed

docs/reference/config.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ The `gen` mapping supports the following keys:
189189
- Customize the name of the copyfrom file. Defaults to `copyfrom.go`.
190190
- `output_files_suffix`:
191191
- If specified the suffix will be added to the name of the generated files.
192+
- `expose_db_connection`:
193+
- If true, expose the `DB` field on the `Queries` struct. Defaults to `false`.
192194
- `query_parameter_limit`:
193195
- The number of positional arguments that will be generated for Go functions. To always emit a parameter struct, set this to `0`. Defaults to `1`.
194196
- `rename`:

internal/codegen/golang/gen.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type tmplCtx struct {
4141
UsesBatch bool
4242
OmitSqlcVersion bool
4343
BuildTags string
44+
ExposeDbConnection bool
4445
}
4546

4647
func (t *tmplCtx) OutputQuery(sourceName string) bool {
@@ -187,6 +188,7 @@ func generate(req *plugin.GenerateRequest, options *opts.Options, enums []Enum,
187188
SqlcVersion: req.SqlcVersion,
188189
BuildTags: options.BuildTags,
189190
OmitSqlcVersion: options.OmitSqlcVersion,
191+
ExposeDbConnection: options.ExposeDbConnection,
190192
}
191193

192194
if tctx.UsesCopyFrom && !tctx.SQLDriver.IsPGX() && options.SqlDriver != opts.SQLDriverGoSQLDriverMySQL {

internal/codegen/golang/opts/options.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type Options struct {
3838
OutputQuerierFileName string `json:"output_querier_file_name,omitempty" yaml:"output_querier_file_name"`
3939
OutputCopyfromFileName string `json:"output_copyfrom_file_name,omitempty" yaml:"output_copyfrom_file_name"`
4040
OutputFilesSuffix string `json:"output_files_suffix,omitempty" yaml:"output_files_suffix"`
41+
ExposeDbConnection bool `json:"expose_db_connection,omitempty" yaml:"expose_db_connection"`
4142
InflectionExcludeTableNames []string `json:"inflection_exclude_table_names,omitempty" yaml:"inflection_exclude_table_names"`
4243
QueryParameterLimit *int32 `json:"query_parameter_limit,omitempty" yaml:"query_parameter_limit"`
4344
OmitSqlcVersion bool `json:"omit_sqlc_version,omitempty" yaml:"omit_sqlc_version"`
@@ -150,6 +151,9 @@ func ValidateOpts(opts *Options) error {
150151
if *opts.QueryParameterLimit < 0 {
151152
return fmt.Errorf("invalid options: query parameter limit must not be negative")
152153
}
154+
if opts.ExposeDbConnection && opts.EmitMethodsWithDbArgument {
155+
return fmt.Errorf("invalid options: expose_db_connection and emit_methods_with_db_argument options are mutually exclusive")
156+
}
153157

154158
return nil
155159
}

internal/codegen/golang/templates/pgx/dbCode.tmpl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,10 @@ func (q *Queries) WithTx(tx pgx.Tx) *Queries {
3434
}
3535
}
3636
{{end}}
37+
38+
{{if .ExposeDbConnection}}
39+
func (q *Queries) Conn() DBTX {
40+
return q.db
41+
}
42+
{{end}}
3743
{{end}}

internal/config/v_two.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@
143143
"emit_sql_as_comment": {
144144
"type": "boolean"
145145
},
146+
"expose_db_connection": {
147+
"type": "boolean"
148+
},
146149
"build_tags": {
147150
"type": "string"
148151
},

internal/endtoend/testdata/expose_db_connection/db/db.go

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/expose_db_connection/db/models.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/expose_db_connection/db/query.sql.go

Lines changed: 82 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-- name: GetAuthor :one
2+
SELECT * FROM authors
3+
WHERE id = $1 LIMIT 1;
4+
5+
-- name: ListAuthors :many
6+
SELECT * FROM authors
7+
ORDER BY name;
8+
9+
-- name: CreateAuthor :one
10+
INSERT INTO authors (
11+
name, bio
12+
) VALUES (
13+
$1, $2
14+
)
15+
RETURNING *;
16+
17+
-- name: DeleteAuthor :exec
18+
DELETE FROM authors
19+
WHERE id = $1;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE authors (
2+
id BIGSERIAL PRIMARY KEY,
3+
name text NOT NULL,
4+
bio text
5+
);

0 commit comments

Comments
 (0)