Skip to content
Open
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
16 changes: 9 additions & 7 deletions docs/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ sql:
queries: "postgresql/query.sql"
engine: "postgresql"
gen:
go:
go:
package: "authors"
out: "postgresql"
database:
Expand Down Expand Up @@ -122,7 +122,7 @@ The `analyzer` mapping supports the following keys:

- `database`:
- If false, do not use the configured database for query analysis. Defaults to `true`.

### gen

The `gen` mapping supports the following keys:
Expand Down Expand Up @@ -159,6 +159,8 @@ The `gen` mapping supports the following keys:
- 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, 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`.
- `emit_nullable_for_null_arrays`:
- 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`.
- `emit_enum_valid_method`:
- If true, generate a Valid method on enum types,
indicating whether a string is a valid enum value.
Expand Down Expand Up @@ -255,18 +257,18 @@ Each mapping in the `plugins` collection has the following keys:
- The URL to fetch the WASM file. Supports the `https://` or `file://` schemes.
- `sha256`
- The SHA256 checksum for the downloaded file.

```yaml
version: "2"
plugins:
- name: "py"
wasm:
wasm:
url: "https://github.com/sqlc-dev/sqlc-gen-python/releases/download/v0.16.0-alpha/sqlc-gen-python.wasm"
sha256: "428476c7408fd4c032da4ec74e8a7344f4fa75e0f98a5a3302f238283b9b95f2"
- name: "js"
env:
- PATH
process:
process:
cmd: "sqlc-gen-json"
```

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

See the [vet](../howto/vet.md) documentation for a list of built-in rules and
help writing custom rules.

```yaml
version: "2"
sql:
Expand Down Expand Up @@ -317,7 +319,7 @@ rules:
rule: |
query.cmd == "exec"
```

### Global overrides

Sometimes, the same configuration must be done across various specifications of
Expand Down
2 changes: 1 addition & 1 deletion internal/codegen/golang/mysql_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func mysqlType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Column) string {
columnType := sdk.DataType(col.Type)
notNull := col.NotNull || col.IsArray
notNull := col.NotNull || (col.IsArray && !options.EmitNullableForNullArrays)
unsigned := col.Unsigned

switch columnType {
Expand Down
1 change: 1 addition & 0 deletions internal/codegen/golang/opts/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Options struct {
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"`
EmitNullableForNullArrays bool `json:"emit_nullable_for_null_arrays" yaml:"emit_nullable_for_null_arrays"`
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"`
EmitSqlAsComment bool `json:"emit_sql_as_comment,omitempty" yaml:"emit_sql_as_comment"`
Expand Down
2 changes: 1 addition & 1 deletion internal/codegen/golang/postgresql_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func parseIdentifierString(name string) (*plugin.Identifier, error) {

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

Expand Down
2 changes: 1 addition & 1 deletion internal/codegen/golang/sqlite_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func sqliteType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Column) string {
dt := strings.ToLower(sdk.DataType(col.Type))
notNull := col.NotNull || col.IsArray
notNull := col.NotNull || (col.IsArray && !options.EmitNullableForNullArrays)
emitPointersForNull := options.EmitPointersForNullTypes

switch dt {
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 @@ -39,6 +39,7 @@ type v1PackageSettings struct {
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"`
EmitNullableForNullArrays bool `json:"emit_nullable_for_null_arrays" yaml:"emit_nullable_for_null_arrays"`
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"`
EmitSqlAsComment bool `json:"emit_sql_as_comment,omitempty" yaml:"emit_sql_as_comment"`
Expand Down Expand Up @@ -149,6 +150,7 @@ func (c *V1GenerateSettings) Translate() Config {
EmitParamsStructPointers: pkg.EmitParamsStructPointers,
EmitMethodsWithDbArgument: pkg.EmitMethodsWithDBArgument,
EmitPointersForNullTypes: pkg.EmitPointersForNullTypes,
EmitNullableForNullArrays: pkg.EmitNullableForNullArrays,
EmitEnumValidMethod: pkg.EmitEnumValidMethod,
EmitAllEnumValues: pkg.EmitAllEnumValues,
EmitSqlAsComment: pkg.EmitSqlAsComment,
Expand Down
32 changes: 32 additions & 0 deletions internal/endtoend/testdata/unnest_null/postgresql/pgx/v4/go/db.go

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.

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,9 @@
-- name: CreateMemories :many
INSERT INTO memories (vampire_id)
SELECT
unnest(sqlc.narg(vamprie_id)::uuid[]) AS vampire_id
RETURNING
*;

-- name: GetVampireIDs :many
SELECT vampires.id::uuid FROM unnest(sqlc.narg(vampire_id)::uuid[]) AS vampires (id);
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE vampires (
id uuid PRIMARY KEY DEFAULT gen_random_uuid ()
);

CREATE TABLE memories (
id uuid PRIMARY KEY DEFAULT gen_random_uuid (),
vampire_id uuid REFERENCES vampires (id),
created_at timestamp NOT NULL DEFAULT NOW(),
updated_at timestamp
);
15 changes: 15 additions & 0 deletions internal/endtoend/testdata/unnest_null/postgresql/pgx/v4/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "postgresql",
"sql_package": "pgx/v4",
"name": "querytest",
"schema": "schema.sql",
"queries": "query.sql",
"emit_interface": true,
"emit_nullable_for_null_arrays": true
}
]
}
32 changes: 32 additions & 0 deletions internal/endtoend/testdata/unnest_null/postgresql/pgx/v5/go/db.go

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.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading