Skip to content

feat(codegen): expose DBTX inside Queries when expose_db_connection is enabled #3803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ The `gen` mapping supports the following keys:
- Customize the name of the copyfrom file. Defaults to `copyfrom.go`.
- `output_files_suffix`:
- If specified the suffix will be added to the name of the generated files.
- `expose_db_connection`:
- If true, expose the `DB` field on the `Queries` struct. Defaults to `false`.
- `query_parameter_limit`:
- 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`.
- `rename`:
Expand Down
2 changes: 2 additions & 0 deletions internal/codegen/golang/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type tmplCtx struct {
UsesBatch bool
OmitSqlcVersion bool
BuildTags string
ExposeDbConnection bool
}

func (t *tmplCtx) OutputQuery(sourceName string) bool {
Expand Down Expand Up @@ -187,6 +188,7 @@ func generate(req *plugin.GenerateRequest, options *opts.Options, enums []Enum,
SqlcVersion: req.SqlcVersion,
BuildTags: options.BuildTags,
OmitSqlcVersion: options.OmitSqlcVersion,
ExposeDbConnection: options.ExposeDbConnection,
}

if tctx.UsesCopyFrom && !tctx.SQLDriver.IsPGX() && options.SqlDriver != opts.SQLDriverGoSQLDriverMySQL {
Expand Down
4 changes: 4 additions & 0 deletions internal/codegen/golang/opts/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Options struct {
OutputQuerierFileName string `json:"output_querier_file_name,omitempty" yaml:"output_querier_file_name"`
OutputCopyfromFileName string `json:"output_copyfrom_file_name,omitempty" yaml:"output_copyfrom_file_name"`
OutputFilesSuffix string `json:"output_files_suffix,omitempty" yaml:"output_files_suffix"`
ExposeDbConnection bool `json:"expose_db_connection,omitempty" yaml:"expose_db_connection"`
InflectionExcludeTableNames []string `json:"inflection_exclude_table_names,omitempty" yaml:"inflection_exclude_table_names"`
QueryParameterLimit *int32 `json:"query_parameter_limit,omitempty" yaml:"query_parameter_limit"`
OmitSqlcVersion bool `json:"omit_sqlc_version,omitempty" yaml:"omit_sqlc_version"`
Expand Down Expand Up @@ -150,6 +151,9 @@ func ValidateOpts(opts *Options) error {
if *opts.QueryParameterLimit < 0 {
return fmt.Errorf("invalid options: query parameter limit must not be negative")
}
if opts.ExposeDbConnection && opts.EmitMethodsWithDbArgument {
return fmt.Errorf("invalid options: expose_db_connection and emit_methods_with_db_argument options are mutually exclusive")
}

return nil
}
6 changes: 6 additions & 0 deletions internal/codegen/golang/templates/pgx/dbCode.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@ func (q *Queries) WithTx(tx pgx.Tx) *Queries {
}
}
{{end}}

{{if .ExposeDbConnection}}
func (q *Queries) Conn() DBTX {
return q.db
}
{{end}}
{{end}}
3 changes: 3 additions & 0 deletions internal/config/v_two.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@
"emit_sql_as_comment": {
"type": "boolean"
},
"expose_db_connection": {
"type": "boolean"
},
"build_tags": {
"type": "string"
},
Expand Down
35 changes: 35 additions & 0 deletions internal/endtoend/testdata/expose_db_connection/db/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions internal/endtoend/testdata/expose_db_connection/db/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 82 additions & 0 deletions internal/endtoend/testdata/expose_db_connection/db/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions internal/endtoend/testdata/expose_db_connection/exec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"contexts": ["managed-db"]
}
19 changes: 19 additions & 0 deletions internal/endtoend/testdata/expose_db_connection/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- name: GetAuthor :one
SELECT * FROM authors
WHERE id = $1 LIMIT 1;

-- name: ListAuthors :many
SELECT * FROM authors
ORDER BY name;

-- name: CreateAuthor :one
INSERT INTO authors (
name, bio
) VALUES (
$1, $2
)
RETURNING *;

-- name: DeleteAuthor :exec
DELETE FROM authors
WHERE id = $1;
5 changes: 5 additions & 0 deletions internal/endtoend/testdata/expose_db_connection/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE authors (
id BIGSERIAL PRIMARY KEY,
name text NOT NULL,
bio text
);
9 changes: 9 additions & 0 deletions internal/endtoend/testdata/expose_db_connection/sqlc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: "2"
sql:
- engine: "postgresql"
schema: "schema.sql"
queries: "query.sql"
gen:
go:
out: "db"
expose_db_connection: true
Loading