Skip to content

Commit 73cbcf3

Browse files
authored
Inflection Singular table names exclusion configuration (#1531)
1 parent 1e40dc8 commit 73cbcf3

File tree

20 files changed

+668
-178
lines changed

20 files changed

+668
-178
lines changed

internal/cmd/shim.go

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -79,38 +79,40 @@ func pluginCodegen(s config.Codegen) *plugin.Codegen {
7979

8080
func pluginPythonCode(s config.SQLPython) *plugin.PythonCode {
8181
return &plugin.PythonCode{
82-
Out: s.Out,
83-
Package: s.Package,
84-
EmitExactTableNames: s.EmitExactTableNames,
85-
EmitSyncQuerier: s.EmitSyncQuerier,
86-
EmitAsyncQuerier: s.EmitAsyncQuerier,
87-
EmitPydanticModels: s.EmitPydanticModels,
88-
QueryParameterLimit: s.QueryParameterLimit,
82+
Out: s.Out,
83+
Package: s.Package,
84+
EmitExactTableNames: s.EmitExactTableNames,
85+
EmitSyncQuerier: s.EmitSyncQuerier,
86+
EmitAsyncQuerier: s.EmitAsyncQuerier,
87+
EmitPydanticModels: s.EmitPydanticModels,
88+
QueryParameterLimit: s.QueryParameterLimit,
89+
InflectionExcludeTableNames: s.InflectionExcludeTableNames,
8990
}
9091
}
9192

9293
func pluginGoCode(s config.SQLGo) *plugin.GoCode {
9394
return &plugin.GoCode{
94-
EmitInterface: s.EmitInterface,
95-
EmitJsonTags: s.EmitJSONTags,
96-
EmitDbTags: s.EmitDBTags,
97-
EmitPreparedQueries: s.EmitPreparedQueries,
98-
EmitExactTableNames: s.EmitExactTableNames,
99-
EmitEmptySlices: s.EmitEmptySlices,
100-
EmitExportedQueries: s.EmitExportedQueries,
101-
EmitResultStructPointers: s.EmitResultStructPointers,
102-
EmitParamsStructPointers: s.EmitParamsStructPointers,
103-
EmitMethodsWithDbArgument: s.EmitMethodsWithDBArgument,
104-
EmitEnumValidMethod: s.EmitEnumValidMethod,
105-
EmitAllEnumValues: s.EmitAllEnumValues,
106-
JsonTagsCaseStyle: s.JSONTagsCaseStyle,
107-
Package: s.Package,
108-
Out: s.Out,
109-
SqlPackage: s.SQLPackage,
110-
OutputDbFileName: s.OutputDBFileName,
111-
OutputModelsFileName: s.OutputModelsFileName,
112-
OutputQuerierFileName: s.OutputQuerierFileName,
113-
OutputFilesSuffix: s.OutputFilesSuffix,
95+
EmitInterface: s.EmitInterface,
96+
EmitJsonTags: s.EmitJSONTags,
97+
EmitDbTags: s.EmitDBTags,
98+
EmitPreparedQueries: s.EmitPreparedQueries,
99+
EmitExactTableNames: s.EmitExactTableNames,
100+
EmitEmptySlices: s.EmitEmptySlices,
101+
EmitExportedQueries: s.EmitExportedQueries,
102+
EmitResultStructPointers: s.EmitResultStructPointers,
103+
EmitParamsStructPointers: s.EmitParamsStructPointers,
104+
EmitMethodsWithDbArgument: s.EmitMethodsWithDBArgument,
105+
EmitEnumValidMethod: s.EmitEnumValidMethod,
106+
EmitAllEnumValues: s.EmitAllEnumValues,
107+
JsonTagsCaseStyle: s.JSONTagsCaseStyle,
108+
Package: s.Package,
109+
Out: s.Out,
110+
SqlPackage: s.SQLPackage,
111+
OutputDbFileName: s.OutputDBFileName,
112+
OutputModelsFileName: s.OutputModelsFileName,
113+
OutputQuerierFileName: s.OutputQuerierFileName,
114+
OutputFilesSuffix: s.OutputFilesSuffix,
115+
InflectionExcludeTableNames: s.InflectionExcludeTableNames,
114116
}
115117
}
116118

@@ -137,9 +139,10 @@ func pluginPythonType(pt config.PythonType) *plugin.PythonType {
137139

138140
func pluginKotlinCode(s config.SQLKotlin) *plugin.KotlinCode {
139141
return &plugin.KotlinCode{
140-
Out: s.Out,
141-
Package: s.Package,
142-
EmitExactTableNames: s.EmitExactTableNames,
142+
Out: s.Out,
143+
Package: s.Package,
144+
EmitExactTableNames: s.EmitExactTableNames,
145+
InflectionExcludeTableNames: s.InflectionExcludeTableNames,
143146
}
144147
}
145148

internal/codegen/golang/result.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ func buildStructs(req *plugin.CodeGenRequest) []Struct {
6464
}
6565
structName := tableName
6666
if !req.Settings.Go.EmitExactTableNames {
67-
structName = inflection.Singular(structName)
67+
structName = inflection.Singular(inflection.SingularParams{
68+
Name: structName,
69+
Exclusions: req.Settings.Go.InflectionExcludeTableNames,
70+
})
6871
}
6972
s := Struct{
7073
Table: plugin.Identifier{Schema: schema.Name, Name: table.Rel.Name},

internal/codegen/kotlin/gen.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,10 @@ func buildDataClasses(req *plugin.CodeGenRequest) []Struct {
277277
}
278278
structName := dataClassName(tableName, req.Settings)
279279
if !req.Settings.Kotlin.EmitExactTableNames {
280-
structName = inflection.Singular(structName)
280+
structName = inflection.Singular(inflection.SingularParams{
281+
Name: structName,
282+
Exclusions: req.Settings.Kotlin.InflectionExcludeTableNames,
283+
})
281284
}
282285
s := Struct{
283286
Table: plugin.Identifier{Schema: schema.Name, Name: table.Rel.Name},

internal/codegen/python/gen.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,10 @@ func buildModels(req *plugin.CodeGenRequest) []Struct {
289289
}
290290
structName := tableName
291291
if !req.Settings.Python.EmitExactTableNames {
292-
structName = inflection.Singular(structName)
292+
structName = inflection.Singular(inflection.SingularParams{
293+
Name: structName,
294+
Exclusions: req.Settings.Python.InflectionExcludeTableNames,
295+
})
293296
}
294297
s := Struct{
295298
Table: plugin.Identifier{Schema: schema.Name, Name: table.Rel.Name},

internal/config/config.go

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -131,45 +131,48 @@ type SQLGen struct {
131131
}
132132

133133
type SQLGo struct {
134-
EmitInterface bool `json:"emit_interface" yaml:"emit_interface"`
135-
EmitJSONTags bool `json:"emit_json_tags" yaml:"emit_json_tags"`
136-
EmitDBTags bool `json:"emit_db_tags" yaml:"emit_db_tags"`
137-
EmitPreparedQueries bool `json:"emit_prepared_queries" yaml:"emit_prepared_queries"`
138-
EmitExactTableNames bool `json:"emit_exact_table_names,omitempty" yaml:"emit_exact_table_names"`
139-
EmitEmptySlices bool `json:"emit_empty_slices,omitempty" yaml:"emit_empty_slices"`
140-
EmitExportedQueries bool `json:"emit_exported_queries" yaml:"emit_exported_queries"`
141-
EmitResultStructPointers bool `json:"emit_result_struct_pointers" yaml:"emit_result_struct_pointers"`
142-
EmitParamsStructPointers bool `json:"emit_params_struct_pointers" yaml:"emit_params_struct_pointers"`
143-
EmitMethodsWithDBArgument bool `json:"emit_methods_with_db_argument,omitempty" yaml:"emit_methods_with_db_argument"`
144-
EmitEnumValidMethod bool `json:"emit_enum_valid_method,omitempty" yaml:"emit_enum_valid_method"`
145-
EmitAllEnumValues bool `json:"emit_all_enum_values,omitempty" yaml:"emit_all_enum_values"`
146-
JSONTagsCaseStyle string `json:"json_tags_case_style,omitempty" yaml:"json_tags_case_style"`
147-
Package string `json:"package" yaml:"package"`
148-
Out string `json:"out" yaml:"out"`
149-
Overrides []Override `json:"overrides,omitempty" yaml:"overrides"`
150-
Rename map[string]string `json:"rename,omitempty" yaml:"rename"`
151-
SQLPackage string `json:"sql_package" yaml:"sql_package"`
152-
OutputDBFileName string `json:"output_db_file_name,omitempty" yaml:"output_db_file_name"`
153-
OutputModelsFileName string `json:"output_models_file_name,omitempty" yaml:"output_models_file_name"`
154-
OutputQuerierFileName string `json:"output_querier_file_name,omitempty" yaml:"output_querier_file_name"`
155-
OutputFilesSuffix string `json:"output_files_suffix,omitempty" yaml:"output_files_suffix"`
134+
EmitInterface bool `json:"emit_interface" yaml:"emit_interface"`
135+
EmitJSONTags bool `json:"emit_json_tags" yaml:"emit_json_tags"`
136+
EmitDBTags bool `json:"emit_db_tags" yaml:"emit_db_tags"`
137+
EmitPreparedQueries bool `json:"emit_prepared_queries" yaml:"emit_prepared_queries"`
138+
EmitExactTableNames bool `json:"emit_exact_table_names,omitempty" yaml:"emit_exact_table_names"`
139+
EmitEmptySlices bool `json:"emit_empty_slices,omitempty" yaml:"emit_empty_slices"`
140+
EmitExportedQueries bool `json:"emit_exported_queries" yaml:"emit_exported_queries"`
141+
EmitResultStructPointers bool `json:"emit_result_struct_pointers" yaml:"emit_result_struct_pointers"`
142+
EmitParamsStructPointers bool `json:"emit_params_struct_pointers" yaml:"emit_params_struct_pointers"`
143+
EmitMethodsWithDBArgument bool `json:"emit_methods_with_db_argument,omitempty" yaml:"emit_methods_with_db_argument"`
144+
EmitEnumValidMethod bool `json:"emit_enum_valid_method,omitempty" yaml:"emit_enum_valid_method"`
145+
EmitAllEnumValues bool `json:"emit_all_enum_values,omitempty" yaml:"emit_all_enum_values"`
146+
JSONTagsCaseStyle string `json:"json_tags_case_style,omitempty" yaml:"json_tags_case_style"`
147+
Package string `json:"package" yaml:"package"`
148+
Out string `json:"out" yaml:"out"`
149+
Overrides []Override `json:"overrides,omitempty" yaml:"overrides"`
150+
Rename map[string]string `json:"rename,omitempty" yaml:"rename"`
151+
SQLPackage string `json:"sql_package" yaml:"sql_package"`
152+
OutputDBFileName string `json:"output_db_file_name,omitempty" yaml:"output_db_file_name"`
153+
OutputModelsFileName string `json:"output_models_file_name,omitempty" yaml:"output_models_file_name"`
154+
OutputQuerierFileName string `json:"output_querier_file_name,omitempty" yaml:"output_querier_file_name"`
155+
OutputFilesSuffix string `json:"output_files_suffix,omitempty" yaml:"output_files_suffix"`
156+
InflectionExcludeTableNames []string `json:"inflection_exclude_table_names,omitempty" yaml:"inflection_exclude_table_names"`
156157
}
157158

158159
type SQLKotlin struct {
159-
EmitExactTableNames bool `json:"emit_exact_table_names,omitempty" yaml:"emit_exact_table_names"`
160-
Package string `json:"package" yaml:"package"`
161-
Out string `json:"out" yaml:"out"`
160+
EmitExactTableNames bool `json:"emit_exact_table_names,omitempty" yaml:"emit_exact_table_names"`
161+
Package string `json:"package" yaml:"package"`
162+
Out string `json:"out" yaml:"out"`
163+
InflectionExcludeTableNames []string `json:"inflection_exclude_table_names,omitempty" yaml:"inflection_exclude_table_names"`
162164
}
163165

164166
type SQLPython struct {
165-
EmitExactTableNames bool `json:"emit_exact_table_names" yaml:"emit_exact_table_names"`
166-
EmitSyncQuerier bool `json:"emit_sync_querier" yaml:"emit_sync_querier"`
167-
EmitAsyncQuerier bool `json:"emit_async_querier" yaml:"emit_async_querier"`
168-
Package string `json:"package" yaml:"package"`
169-
Out string `json:"out" yaml:"out"`
170-
Overrides []Override `json:"overrides,omitempty" yaml:"overrides"`
171-
EmitPydanticModels bool `json:"emit_pydantic_models,omitempty" yaml:"emit_pydantic_models"`
172-
QueryParameterLimit *int32 `json:"query_parameter_limit,omitempty" yaml:"query_parameter_limit"`
167+
EmitExactTableNames bool `json:"emit_exact_table_names" yaml:"emit_exact_table_names"`
168+
EmitSyncQuerier bool `json:"emit_sync_querier" yaml:"emit_sync_querier"`
169+
EmitAsyncQuerier bool `json:"emit_async_querier" yaml:"emit_async_querier"`
170+
Package string `json:"package" yaml:"package"`
171+
Out string `json:"out" yaml:"out"`
172+
Overrides []Override `json:"overrides,omitempty" yaml:"overrides"`
173+
EmitPydanticModels bool `json:"emit_pydantic_models,omitempty" yaml:"emit_pydantic_models"`
174+
QueryParameterLimit *int32 `json:"query_parameter_limit,omitempty" yaml:"query_parameter_limit"`
175+
InflectionExcludeTableNames []string `json:"inflection_exclude_table_names,omitempty" yaml:"inflection_exclude_table_names"`
173176
}
174177

175178
type SQLJSON struct {

internal/endtoend/testdata/codegen_json/gen/codegen.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121
"emit_async_querier": false,
2222
"package": "",
2323
"out": "",
24-
"emit_pydantic_models": false
24+
"emit_pydantic_models": false,
25+
"inflection_exclude_table_names": []
2526
},
2627
"kotlin": {
2728
"emit_exact_table_names": false,
2829
"package": "",
29-
"out": ""
30+
"out": "",
31+
"inflection_exclude_table_names": []
3032
},
3133
"go": {
3234
"emit_interface": false,
@@ -48,7 +50,8 @@
4850
"output_querier_file_name": "",
4951
"output_files_suffix": "",
5052
"emit_enum_valid_method": false,
51-
"emit_all_enum_values": false
53+
"emit_all_enum_values": false,
54+
"inflection_exclude_table_names": []
5255
},
5356
"json": {
5457
"out": "gen",

internal/endtoend/testdata/inflection_exclude_table_names/postgresql/pgx/go/db.go

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

internal/endtoend/testdata/inflection_exclude_table_names/postgresql/pgx/go/models.go

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

internal/endtoend/testdata/inflection_exclude_table_names/postgresql/pgx/go/query.sql.go

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CREATE TABLE bars (id serial not null, name text not null, primary key (id));
2+
CREATE TABLE my_data (id serial not null, name text not null, primary key (id));
3+
CREATE TABLE exclusions (id serial not null, name text not null, primary key (id));
4+
5+
-- name: DeleteBarByID :one
6+
DELETE FROM bars WHERE id = $1 RETURNING id, name;
7+
8+
-- name: DeleteMyDataByID :one
9+
DELETE FROM my_data WHERE id = $1 RETURNING id, name;
10+
11+
-- name: DeleteExclusionByID :one
12+
DELETE FROM exclusions WHERE id = $1 RETURNING id, name;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"version": "2",
3+
"sql": [
4+
{
5+
"engine": "postgresql",
6+
"schema": "query.sql",
7+
"queries": "query.sql",
8+
"gen": {
9+
"go": {
10+
"package": "querytest",
11+
"sql_package": "pgx/v4",
12+
"out": "go",
13+
"inflection_exclude_table_names": [
14+
"my_data",
15+
"exclusions"
16+
]
17+
}
18+
}
19+
}
20+
]
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Code generated by sqlc. DO NOT EDIT.
2+
# versions:
3+
# sqlc v1.15.0
4+
import dataclasses
5+
6+
7+
@dataclasses.dataclass()
8+
class Bar:
9+
id: int
10+
name: str
11+
12+
13+
@dataclasses.dataclass()
14+
class Exclusions:
15+
id: int
16+
name: str
17+
18+
19+
@dataclasses.dataclass()
20+
class MyData:
21+
id: int
22+
name: str

0 commit comments

Comments
 (0)