Skip to content
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

Add support for emitting pointer types for nullable columns #1571

Merged
merged 2 commits into from
Nov 10, 2022
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ mysqlsh:
# $ protoc --version
# libprotoc 3.19.1
# $ go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
# $ go install github.com/planetscale/vtprotobuf/cmd/protoc-gen-go-vtproto@latest
# $ go install github.com/planetscale/vtprotobuf/cmd/protoc-gen-go-vtproto
proto: internal/plugin/codegen.pb.go

internal/plugin/codegen.pb.go: protos/plugin/codegen.proto
Expand Down
3 changes: 3 additions & 0 deletions docs/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ packages:
emit_result_struct_pointers: false
emit_params_struct_pointers: false
emit_methods_with_db_argument: false
emit_pointers_for_null_types: false
emit_enum_valid_method: false
emit_all_enum_values: false
json_tags_case_style: "camel"
Expand Down Expand Up @@ -383,6 +384,8 @@ Each mapping in the `packages` collection has the following keys:
- If true, parameters are passed as pointers to structs. Defaults to `false`.
- `emit_methods_with_db_argument`:
- If true, generated methods will accept a DBTX argument instead of storing a DBTX on the `*Queries` struct. Defaults to `false`.
- `emit_pointers_for_null_types`:
- If true and `sql_package` is set to `pgx/v4`, generated types for nullable columns are emitted as pointers (ie. `*string`) instead of `database/sql` null types (ie. `NullString`). Defaults to `false`.
- `emit_enum_valid_method`:
- If true, generate a Valid method on enum types,
indicating whether a string is a valid enum value.
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func pluginGoCode(s config.SQLGo) *plugin.GoCode {
EmitResultStructPointers: s.EmitResultStructPointers,
EmitParamsStructPointers: s.EmitParamsStructPointers,
EmitMethodsWithDbArgument: s.EmitMethodsWithDBArgument,
EmitPointersForNullTypes: s.EmitPointersForNullTypes,
EmitEnumValidMethod: s.EmitEnumValidMethod,
EmitAllEnumValues: s.EmitAllEnumValues,
JsonTagsCaseStyle: s.JSONTagsCaseStyle,
Expand Down
55 changes: 55 additions & 0 deletions internal/codegen/golang/postgresql_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,54 +37,79 @@ func postgresType(req *plugin.CodeGenRequest, col *plugin.Column) string {
columnType := sdk.DataType(col.Type)
notNull := col.NotNull || col.IsArray
driver := parseDriver(req.Settings)
emitPointersForNull := driver == SQLDriverPGXV4 && req.Settings.Go.EmitPointersForNullTypes

switch columnType {
case "serial", "serial4", "pg_catalog.serial4":
if notNull {
return "int32"
}
if emitPointersForNull {
return "*int32"
}
return "sql.NullInt32"

case "bigserial", "serial8", "pg_catalog.serial8":
if notNull {
return "int64"
}
if emitPointersForNull {
return "*int64"
}
return "sql.NullInt64"

case "smallserial", "serial2", "pg_catalog.serial2":
if notNull {
return "int16"
}
if emitPointersForNull {
return "*int16"
}
return "sql.NullInt16"

case "integer", "int", "int4", "pg_catalog.int4":
if notNull {
return "int32"
}
if emitPointersForNull {
return "*int32"
}
return "sql.NullInt32"

case "bigint", "int8", "pg_catalog.int8":
if notNull {
return "int64"
}
if emitPointersForNull {
return "*int64"
}
return "sql.NullInt64"

case "smallint", "int2", "pg_catalog.int2":
if notNull {
return "int16"
}
if emitPointersForNull {
return "*int16"
}
return "sql.NullInt16"

case "float", "double precision", "float8", "pg_catalog.float8":
if notNull {
return "float64"
}
if emitPointersForNull {
return "*float64"
}
return "sql.NullFloat64"

case "real", "float4", "pg_catalog.float4":
if notNull {
return "float32"
}
if emitPointersForNull {
return "*float32"
}
return "sql.NullFloat64" // TODO: Change to sql.NullFloat32 after updating the go.mod file

case "numeric", "pg_catalog.numeric", "money":
Expand All @@ -98,12 +123,18 @@ func postgresType(req *plugin.CodeGenRequest, col *plugin.Column) string {
if notNull {
return "string"
}
if emitPointersForNull {
return "*string"
}
return "sql.NullString"

case "boolean", "bool", "pg_catalog.bool":
if notNull {
return "bool"
}
if emitPointersForNull {
return "*bool"
}
return "sql.NullBool"

case "json":
Expand Down Expand Up @@ -141,30 +172,45 @@ func postgresType(req *plugin.CodeGenRequest, col *plugin.Column) string {
if notNull {
return "time.Time"
}
if emitPointersForNull {
return "*time.Time"
}
return "sql.NullTime"

case "pg_catalog.time", "pg_catalog.timetz":
if notNull {
return "time.Time"
}
if emitPointersForNull {
return "*time.Time"
}
return "sql.NullTime"

case "pg_catalog.timestamp", "pg_catalog.timestamptz", "timestamptz":
if notNull {
return "time.Time"
}
if emitPointersForNull {
return "*time.Time"
}
return "sql.NullTime"

case "text", "pg_catalog.varchar", "pg_catalog.bpchar", "string":
if notNull {
return "string"
}
if emitPointersForNull {
return "*string"
}
return "sql.NullString"

case "uuid":
if notNull {
return "uuid.UUID"
}
if emitPointersForNull {
return "*uuid.UUID"
}
return "uuid.NullUUID"

case "inet":
Expand Down Expand Up @@ -206,12 +252,18 @@ func postgresType(req *plugin.CodeGenRequest, col *plugin.Column) string {
if notNull {
return "string"
}
if emitPointersForNull {
return "*string"
}
return "sql.NullString"

case "interval", "pg_catalog.interval":
if notNull {
return "int64"
}
if emitPointersForNull {
return "*int64"
}
return "sql.NullInt64"

case "daterange":
Expand Down Expand Up @@ -299,6 +351,9 @@ func postgresType(req *plugin.CodeGenRequest, col *plugin.Column) string {
if notNull {
return "string"
}
if emitPointersForNull {
return "*string"
}
return "sql.NullString"
}
}
Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ type SQLGo struct {
EmitResultStructPointers bool `json:"emit_result_struct_pointers" yaml:"emit_result_struct_pointers"`
EmitParamsStructPointers bool `json:"emit_params_struct_pointers" yaml:"emit_params_struct_pointers"`
EmitMethodsWithDBArgument bool `json:"emit_methods_with_db_argument,omitempty" yaml:"emit_methods_with_db_argument"`
EmitPointersForNullTypes bool `json:"emit_pointers_for_null_types" yaml:"emit_pointers_for_null_types"`
EmitEnumValidMethod bool `json:"emit_enum_valid_method,omitempty" yaml:"emit_enum_valid_method"`
EmitAllEnumValues bool `json:"emit_all_enum_values,omitempty" yaml:"emit_all_enum_values"`
JSONTagsCaseStyle string `json:"json_tags_case_style,omitempty" yaml:"json_tags_case_style"`
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 @@ -32,6 +32,7 @@ type v1PackageSettings struct {
EmitResultStructPointers bool `json:"emit_result_struct_pointers" yaml:"emit_result_struct_pointers"`
EmitParamsStructPointers bool `json:"emit_params_struct_pointers" yaml:"emit_params_struct_pointers"`
EmitMethodsWithDBArgument bool `json:"emit_methods_with_db_argument" yaml:"emit_methods_with_db_argument"`
EmitPointersForNullTypes bool `json:"emit_pointers_for_null_types" yaml:"emit_pointers_for_null_types"`
EmitEnumValidMethod bool `json:"emit_enum_valid_method,omitempty" yaml:"emit_enum_valid_method"`
EmitAllEnumValues bool `json:"emit_all_enum_values,omitempty" yaml:"emit_all_enum_values"`
JSONTagsCaseStyle string `json:"json_tags_case_style,omitempty" yaml:"json_tags_case_style"`
Expand Down Expand Up @@ -130,6 +131,7 @@ func (c *V1GenerateSettings) Translate() Config {
EmitResultStructPointers: pkg.EmitResultStructPointers,
EmitParamsStructPointers: pkg.EmitParamsStructPointers,
EmitMethodsWithDBArgument: pkg.EmitMethodsWithDBArgument,
EmitPointersForNullTypes: pkg.EmitPointersForNullTypes,
EmitEnumValidMethod: pkg.EmitEnumValidMethod,
EmitAllEnumValues: pkg.EmitAllEnumValues,
Package: pkg.Name,
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.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- Character Types
-- https://www.postgresql.org/docs/current/datatype-character.html
CREATE TABLE dt_character (
a text,
b character varying(32),
c varchar(32),
d character(32),
e char(32)
);

CREATE TABLE dt_character_not_null (
a text NOT NULL,
b character varying(32) NOT NULL,
c varchar(32) NOT NULL,
d character(32) NOT NULL,
e char(32) NOT NULL
);
Loading