Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d69f8cf

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

File tree

25 files changed

+543
-8
lines changed

25 files changed

+543
-8
lines changed

‎docs/reference/config.md‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ sql:
1414
queries: "postgresql/query.sql"
1515
engine: "postgresql"
1616
gen:
17-
go:
17+
go:
1818
package: "authors"
1919
out: "postgresql"
2020
database:
@@ -122,7 +122,7 @@ The `analyzer` mapping supports the following keys:
122122

123123
- `database`:
124124
- If false, do not use the configured database for query analysis. Defaults to `true`.
125-
125+
126126
### gen
127127

128128
The `gen` mapping supports the following keys:
@@ -159,6 +159,8 @@ The `gen` mapping supports the following keys:
159159
- If true, generated methods will accept a DBTX argument instead of storing a DBTX on the `*Queries` struct. Defaults to `false`.
160160
- `emit_pointers_for_null_types`:
161161
- If true, generated types for nullable columns are emitted as pointers (ie. `*string`) instead of `database/sql` null types (ie. `NullString`). Currently only supported for PostgreSQL if `sql_package` is `pgx/v4` or `pgx/v5`, and for SQLite. Defaults to `false`.
162+
- `emit_nullable_for_null_arrays`:
163+
- If true, generated types for nullable columns with array types are emitted as list of nullable instead of a list of non-nullable. For example, `bool[]` SQL type is emitted as `[]sql.NullBool` instead of `[]bool` when the flag is set. Defaults to `false`.
162164
- `emit_enum_valid_method`:
163165
- If true, generate a Valid method on enum types,
164166
indicating whether a string is a valid enum value.
@@ -255,18 +257,18 @@ Each mapping in the `plugins` collection has the following keys:
255257
- The URL to fetch the WASM file. Supports the `https://` or `file://` schemes.
256258
- `sha256`
257259
- The SHA256 checksum for the downloaded file.
258-
260+
259261
```yaml
260262
version: "2"
261263
plugins:
262264
- name: "py"
263-
wasm:
265+
wasm:
264266
url: "https://github.com/sqlc-dev/sqlc-gen-python/releases/download/v0.16.0-alpha/sqlc-gen-python.wasm"
265267
sha256: "428476c7408fd4c032da4ec74e8a7344f4fa75e0f98a5a3302f238283b9b95f2"
266268
- name: "js"
267269
env:
268270
- PATH
269-
process:
271+
process:
270272
cmd: "sqlc-gen-json"
271273
```
272274

@@ -283,7 +285,7 @@ Each mapping in the `rules` collection has the following keys:
283285

284286
See the [vet](../howto/vet.md) documentation for a list of built-in rules and
285287
help writing custom rules.
286-
288+
287289
```yaml
288290
version: "2"
289291
sql:
@@ -317,7 +319,7 @@ rules:
317319
rule: |
318320
query.cmd == "exec"
319321
```
320-
322+
321323
### Global overrides
322324

323325
Sometimes, the same configuration must be done across various specifications of

‎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);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CREATE TABLE vampires (
2+
id uuid PRIMARY KEY DEFAULT gen_random_uuid ()
3+
);
4+
5+
CREATE TABLE memories (
6+
id uuid PRIMARY KEY DEFAULT gen_random_uuid (),
7+
vampire_id uuid REFERENCES vampires (id),
8+
created_at timestamp NOT NULL DEFAULT NOW(),
9+
updated_at timestamp
10+
);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /