Skip to content

Commit 8e9ea79

Browse files
committed
fix: let sqlc output nullable array
Close #1851 This allow postgres to output nullable array.
1 parent 6389cdc commit 8e9ea79

File tree

26 files changed

+552
-18
lines changed

26 files changed

+552
-18
lines changed

examples/ondeck/postgresql/models.go

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

examples/ondeck/postgresql/venue.sql.go

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

internal/codegen/golang/opts/options.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type Options struct {
2222
EmitParamsStructPointers bool `json:"emit_params_struct_pointers" yaml:"emit_params_struct_pointers"`
2323
EmitMethodsWithDbArgument bool `json:"emit_methods_with_db_argument,omitempty" yaml:"emit_methods_with_db_argument"`
2424
EmitPointersForNullTypes bool `json:"emit_pointers_for_null_types" yaml:"emit_pointers_for_null_types"`
25+
EmitNullableForNullArrays bool `json:"emit_nullable_for_null_arrays" yaml:"emit_nullable_for_null_arrays"`
2526
EmitEnumValidMethod bool `json:"emit_enum_valid_method,omitempty" yaml:"emit_enum_valid_method"`
2627
EmitAllEnumValues bool `json:"emit_all_enum_values,omitempty" yaml:"emit_all_enum_values"`
2728
EmitSqlAsComment bool `json:"emit_sql_as_comment,omitempty" yaml:"emit_sql_as_comment"`

internal/codegen/golang/postgresql_type.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func parseIdentifierString(name string) (*plugin.Identifier, error) {
3636

3737
func postgresType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Column) string {
3838
columnType := sdk.DataType(col.Type)
39-
notNull := col.NotNull || col.IsArray
39+
notNull := col.NotNull || (col.IsArray && !options.EmitNullableForNullArrays)
4040
driver := parseDriver(options.SqlPackage)
4141
emitPointersForNull := driver.IsPGX() && options.EmitPointersForNullTypes
4242

internal/config/v_one.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type v1PackageSettings struct {
3939
EmitParamsStructPointers bool `json:"emit_params_struct_pointers" yaml:"emit_params_struct_pointers"`
4040
EmitMethodsWithDBArgument bool `json:"emit_methods_with_db_argument" yaml:"emit_methods_with_db_argument"`
4141
EmitPointersForNullTypes bool `json:"emit_pointers_for_null_types" yaml:"emit_pointers_for_null_types"`
42+
EmitNullableForNullArrays bool `json:"emit_nullable_for_null_arrays" yaml:"emit_nullable_for_null_arrays"`
4243
EmitEnumValidMethod bool `json:"emit_enum_valid_method,omitempty" yaml:"emit_enum_valid_method"`
4344
EmitAllEnumValues bool `json:"emit_all_enum_values,omitempty" yaml:"emit_all_enum_values"`
4445
EmitSqlAsComment bool `json:"emit_sql_as_comment,omitempty" yaml:"emit_sql_as_comment"`
@@ -149,6 +150,7 @@ func (c *V1GenerateSettings) Translate() Config {
149150
EmitParamsStructPointers: pkg.EmitParamsStructPointers,
150151
EmitMethodsWithDbArgument: pkg.EmitMethodsWithDBArgument,
151152
EmitPointersForNullTypes: pkg.EmitPointersForNullTypes,
153+
EmitNullableForNullArrays: pkg.EmitNullableForNullArrays,
152154
EmitEnumValidMethod: pkg.EmitEnumValidMethod,
153155
EmitAllEnumValues: pkg.EmitAllEnumValues,
154156
EmitSqlAsComment: pkg.EmitSqlAsComment,

internal/endtoend/testdata/unnest_null/postgresql/pgx/v4/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/unnest_null/postgresql/pgx/v4/go/models.go

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

internal/endtoend/testdata/unnest_null/postgresql/pgx/v4/go/querier.go

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

internal/endtoend/testdata/unnest_null/postgresql/pgx/v4/go/query.sql.go

Lines changed: 69 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- name: CreateMemories :many
2+
INSERT INTO memories (vampire_id)
3+
SELECT
4+
unnest(sqlc.narg(vamprie_id)::uuid[]) AS vampire_id
5+
RETURNING
6+
*;
7+
8+
-- name: GetVampireIDs :many
9+
SELECT vampires.id::uuid FROM unnest(sqlc.narg(vampire_id)::uuid[]) AS vampires (id);

0 commit comments

Comments
 (0)