Skip to content

Customizable batch output file name (add OutputBatchFileName field) #2178

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

Merged
merged 3 commits into from
Apr 7, 2023
Merged
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
5 changes: 5 additions & 0 deletions docs/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ The `gen` mapping supports the following keys:
that returns all valid enum values.
- `json_tags_case_style`:
- `camel` for camelCase, `pascal` for PascalCase, `snake` for snake_case or `none` to use the column name in the DB. Defaults to `none`.
- `output_batch_file_name`:
- Customize the name of the batch file. Defaults to `batch.go`.
- `output_db_file_name`:
- Customize the name of the db file. Defaults to `db.go`.
- `output_models_file_name`:
Expand Down Expand Up @@ -346,6 +348,7 @@ packages:
emit_enum_valid_method: false
emit_all_enum_values: false
json_tags_case_style: "camel"
output_batch_file_name: "batch.go"
output_db_file_name: "db.go"
output_models_file_name: "models.go"
output_querier_file_name: "querier.go"
Expand Down Expand Up @@ -397,6 +400,8 @@ Each mapping in the `packages` collection has the following keys:
that returns all valid enum values.
- `json_tags_case_style`:
- `camel` for camelCase, `pascal` for PascalCase, `snake` for snake_case or `none` to use the column name in the DB. Defaults to `none`.
- `output_batch_file_name`:
- Customize the name of the batch file. Defaults to `batch.go`.
- `output_db_file_name`:
- Customize the name of the db file. Defaults to `db.go`.
- `output_models_file_name`:
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func pluginGoCode(s config.SQLGo) *plugin.GoCode {
Out: s.Out,
SqlPackage: s.SQLPackage,
OutputDbFileName: s.OutputDBFileName,
OutputBatchFileName: s.OutputBatchFileName,
OutputModelsFileName: s.OutputModelsFileName,
OutputQuerierFileName: s.OutputQuerierFileName,
OutputFilesSuffix: s.OutputFilesSuffix,
Expand Down
3 changes: 3 additions & 0 deletions internal/codegen/golang/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ func generate(req *plugin.CodeGenRequest, enums []Enum, structs []Struct, querie
// TODO(Jille): Make this configurable.

batchFileName := "batch.go"
if golang.OutputBatchFileName != "" {
batchFileName = golang.OutputBatchFileName
}

if err := execute(dbFileName, "dbFile"); err != nil {
return nil, err
Expand Down
3 changes: 3 additions & 0 deletions internal/codegen/golang/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ func (i *importer) Imports(filename string) [][]ImportSpec {
}
copyfromFileName := "copyfrom.go"
batchFileName := "batch.go"
if i.Settings.Go.OutputBatchFileName != "" {
batchFileName = i.Settings.Go.OutputBatchFileName
}

switch filename {
case dbFileName:
Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ type SQLGo struct {
Overrides []Override `json:"overrides,omitempty" yaml:"overrides"`
Rename map[string]string `json:"rename,omitempty" yaml:"rename"`
SQLPackage string `json:"sql_package" yaml:"sql_package"`
OutputBatchFileName string `json:"output_batch_file_name,omitempty" yaml:"output_batch_file_name"`
OutputDBFileName string `json:"output_db_file_name,omitempty" yaml:"output_db_file_name"`
OutputModelsFileName string `json:"output_models_file_name,omitempty" yaml:"output_models_file_name"`
OutputQuerierFileName string `json:"output_querier_file_name,omitempty" yaml:"output_querier_file_name"`
Expand Down
2 changes: 2 additions & 0 deletions internal/config/v_one.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type v1PackageSettings struct {
JSONTagsCaseStyle string `json:"json_tags_case_style,omitempty" yaml:"json_tags_case_style"`
SQLPackage string `json:"sql_package" yaml:"sql_package"`
Overrides []Override `json:"overrides" yaml:"overrides"`
OutputBatchFileName string `json:"output_batch_file_name,omitempty" yaml:"output_batch_file_name"`
OutputDBFileName string `json:"output_db_file_name,omitempty" yaml:"output_db_file_name"`
OutputModelsFileName string `json:"output_models_file_name,omitempty" yaml:"output_models_file_name"`
OutputQuerierFileName string `json:"output_querier_file_name,omitempty" yaml:"output_querier_file_name"`
Expand Down Expand Up @@ -150,6 +151,7 @@ func (c *V1GenerateSettings) Translate() Config {
SQLPackage: pkg.SQLPackage,
Overrides: pkg.Overrides,
JSONTagsCaseStyle: pkg.JSONTagsCaseStyle,
OutputBatchFileName: pkg.OutputBatchFileName,
OutputDBFileName: pkg.OutputDBFileName,
OutputModelsFileName: pkg.OutputModelsFileName,
OutputQuerierFileName: pkg.OutputQuerierFileName,
Expand Down
3 changes: 2 additions & 1 deletion internal/endtoend/testdata/codegen_json/gen/codegen.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"emit_all_enum_values": false,
"inflection_exclude_table_names": [],
"emit_pointers_for_null_types": false,
"query_parameter_limit": 1
"query_parameter_limit": 1,
"output_batch_file_name": ""
},
"json": {
"out": "gen",
Expand Down

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

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

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

4 changes: 4 additions & 0 deletions internal/endtoend/testdata/output_file_names/pgx/v4/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ CREATE TABLE "user" (id bigserial not null);

-- name: User :many
SELECT "user".* FROM "user";

-- name: UsersB :batchmany
SELECT * FROM "user"
WHERE id = $1;
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"schema": "query.sql",
"queries": "query.sql",
"emit_interface": true,
"output_batch_file_name": "batch_gen.go",
"output_db_file_name": "db_gen.go",
"output_models_file_name": "models_gen.go",
"output_querier_file_name": "querier_gen.go"
Expand Down

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

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

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

4 changes: 4 additions & 0 deletions internal/endtoend/testdata/output_file_names/pgx/v5/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ CREATE TABLE "user" (id bigserial not null);

-- name: User :many
SELECT "user".* FROM "user";

-- name: UsersB :batchmany
SELECT * FROM "user"
WHERE id = $1;
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"schema": "query.sql",
"queries": "query.sql",
"emit_interface": true,
"output_batch_file_name": "batch_gen.go",
"output_db_file_name": "db_gen.go",
"output_models_file_name": "models_gen.go",
"output_querier_file_name": "querier_gen.go"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"emit_all_enum_values": false,
"inflection_exclude_table_names": [],
"emit_pointers_for_null_types": false,
"query_parameter_limit": 1
"query_parameter_limit": 1,
"output_batch_file_name": ""
},
"json": {
"out": "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"emit_all_enum_values": false,
"inflection_exclude_table_names": [],
"emit_pointers_for_null_types": false,
"query_parameter_limit": 1
"query_parameter_limit": 1,
"output_batch_file_name": ""
},
"json": {
"out": "",
Expand Down
Loading