Skip to content

Commit e90cd37

Browse files
committed
add config option to control if have schema prefix for types or not
1 parent 4fded23 commit e90cd37

4 files changed

Lines changed: 41 additions & 15 deletions

File tree

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,29 @@ sql:
2020
emit_async_querier: true
2121
```
2222
23+
### Configuration Options
24+
25+
These are the supported `options` for the `py` plugin. Add them under the `codegen[].options` section of your `sqlc.yaml`.
26+
27+
- package: Module path used for imports in generated query files (e.g., `from <package> import models`).
28+
- emit_sync_querier: Emit a synchronous `Querier` class using `sqlalchemy.engine.Connection`.
29+
- emit_async_querier: Emit an asynchronous `AsyncQuerier` class using `sqlalchemy.ext.asyncio.AsyncConnection`.
30+
- emit_pydantic_models: Emit Pydantic models instead of `dataclasses` for models.py. See the section below.
31+
- emit_str_enum: Emit enums as `enum.StrEnum` (Python >=3.11). When false, emit `(str, enum.Enum)`. See the section below.
32+
- emit_schema_name_prefix: When true, prefix non-default schema to generated types to avoid name collisions. Examples:
33+
- false (default): `Book`, `BookStatus`
34+
- true: `MySchemaBook`, `MySchemaBookStatus` when the objects live in schema `my_schema`.
35+
- emit_exact_table_names: When true, do not singularize table names for model class names.
36+
- query_parameter_limit: Integer controlling when query params are grouped into a single struct argument.
37+
- If the number of parameters exceeds this value, a single `Params` struct is emitted.
38+
- Set to 0 to always emit a struct; omit or set to a large value to keep separate parameters.
39+
- inflection_exclude_table_names: A list of table names to exclude from singularization when `emit_exact_table_names` is false.
40+
- overrides: Column type overrides; see the section below.
41+
42+
Notes
43+
- out: Controlled by `codegen[].out` at the sqlc level. The plugin’s `out` option is not used; prefer the top-level `out` value.
44+
45+
2346
### Emit Pydantic Models instead of `dataclasses`
2447

2548
Option: `emit_pydantic_models`

internal/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ type Config struct {
88
Out string `json:"out"`
99
EmitPydanticModels bool `json:"emit_pydantic_models"`
1010
EmitStrEnum bool `json:"emit_str_enum"`
11+
EmitSchemaNamePrefix bool `json:"emit_schema_name_prefix"`
1112
QueryParameterLimit *int32 `json:"query_parameter_limit"`
1213
InflectionExcludeTableNames []string `json:"inflection_exclude_table_names"`
1314
}

internal/gen.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -181,18 +181,19 @@ func (q Query) ArgDictNode() *pyast.Node {
181181
}
182182

183183
func makePyType(req *plugin.GenerateRequest, col *plugin.Column) pyType {
184-
typ := pyInnerType(req, col)
184+
185+
typ := pyInnerType(conf, req, col)
185186
return pyType{
186187
InnerType: typ,
187188
IsArray: col.IsArray,
188189
IsNull: !col.NotNull,
189190
}
190191
}
191192

192-
func pyInnerType(req *plugin.GenerateRequest, col *plugin.Column) string {
193+
func pyInnerType(conf Config, req *plugin.GenerateRequest, col *plugin.Column) string {
193194
switch req.Settings.Engine {
194195
case "postgresql":
195-
return postgresType(req, col)
196+
return postgresType(conf, req, col)
196197
default:
197198
log.Println("unsupported engine type")
198199
return "Any"
@@ -226,18 +227,18 @@ func pyEnumValueName(value string) string {
226227
return strings.ToUpper(id)
227228
}
228229

229-
func buildEnums(req *plugin.GenerateRequest) []Enum {
230+
func buildEnums(conf Config, req *plugin.GenerateRequest) []Enum {
230231
var enums []Enum
231232
for _, schema := range req.Catalog.Schemas {
232233
if schema.Name == "pg_catalog" || schema.Name == "information_schema" {
233234
continue
234235
}
235236
for _, enum := range schema.Enums {
236237
var enumName string
237-
if schema.Name == req.Catalog.DefaultSchema {
238-
enumName = enum.Name
239-
} else {
238+
if conf.EmitSchemaNamePrefix && schema.Name != req.Catalog.DefaultSchema {
240239
enumName = schema.Name + "_" + enum.Name
240+
} else {
241+
enumName = enum.Name
241242
}
242243
e := Enum{
243244
Name: modelName(enumName, req.Settings),
@@ -267,10 +268,10 @@ func buildModels(conf Config, req *plugin.GenerateRequest) []Struct {
267268
}
268269
for _, table := range schema.Tables {
269270
var tableName string
270-
if schema.Name == req.Catalog.DefaultSchema {
271-
tableName = table.Rel.Name
272-
} else {
271+
if conf.EmitSchemaNamePrefix && schema.Name != req.Catalog.DefaultSchema {
273272
tableName = schema.Name + "_" + table.Rel.Name
273+
} else {
274+
tableName = table.Rel.Name
274275
}
275276
structName := tableName
276277
if !conf.EmitExactTableNames {
@@ -1185,7 +1186,7 @@ func Generate(_ context.Context, req *plugin.GenerateRequest) (*plugin.GenerateR
11851186
}
11861187
}
11871188

1188-
enums := buildEnums(req)
1189+
enums := buildEnums(conf, req)
11891190
models := buildModels(conf, req)
11901191
queries, err := buildQueries(conf, req, models)
11911192
if err != nil {

internal/postgresql_type.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/sqlc-dev/plugin-sdk-go/sdk"
88
)
99

10-
func postgresType(req *plugin.GenerateRequest, col *plugin.Column) string {
10+
func postgresType(conf Config, req *plugin.GenerateRequest, col *plugin.Column) string {
1111
columnType := sdk.DataType(col.Type)
1212

1313
switch columnType {
@@ -50,10 +50,11 @@ func postgresType(req *plugin.GenerateRequest, col *plugin.Column) string {
5050
for _, enum := range schema.Enums {
5151
// Match both unqualified and schema-qualified enum type names
5252
if columnType == enum.Name || columnType == schema.Name+"."+enum.Name {
53-
if schema.Name == req.Catalog.DefaultSchema {
54-
return "models." + modelName(enum.Name, req.Settings)
53+
name := enum.Name
54+
if conf.EmitSchemaNamePrefix && schema.Name != req.Catalog.DefaultSchema {
55+
name = schema.Name + "_" + enum.Name
5556
}
56-
return "models." + modelName(schema.Name+"_"+enum.Name, req.Settings)
57+
return "models." + modelName(name, req.Settings)
5758
}
5859
}
5960
}

0 commit comments

Comments
 (0)