Skip to content
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 docs/reference/config.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -154,7 +154,7 @@ The `gen` mapping supports the following keys:
- `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` or `pgx/v5`, generated types for nullable columns are emitted as pointers (ie. `*string`) instead of `database/sql` null types (ie. `NullString`). Defaults to `false`.
- 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_enum_valid_method`:
- If true, generate a Valid method on enum types,
indicating whether a string is a valid enum value.
Expand Down
2 changes: 1 addition & 1 deletion internal/codegen/golang/go_type.go
Original file line numberDiff line numberDiff line change
Expand Up@@ -85,7 +85,7 @@ func goInnerType(req *plugin.GenerateRequest, options *opts.Options, col *plugin
case"postgresql":
returnpostgresType(req, options, col)
case"sqlite":
returnsqliteType(req, col)
returnsqliteType(req, options, col)
default:
return"interface{}"
}
Expand Down
22 changes: 21 additions & 1 deletion internal/codegen/golang/sqlite_type.go
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,21 +4,26 @@ import (
"log"
"strings"

"github.com/sqlc-dev/sqlc/internal/codegen/golang/opts"
"github.com/sqlc-dev/sqlc/internal/codegen/sdk"
"github.com/sqlc-dev/sqlc/internal/debug"
"github.com/sqlc-dev/sqlc/internal/plugin"
)

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

switch dt{

case "int", "integer", "tinyint", "smallint", "mediumint", "bigint", "unsignedbigint", "int2", "int8":
if notNull{
return "int64"
}
if emitPointersForNull{
return "*int64"
}
return "sql.NullInt64"

case "blob":
Expand All@@ -28,18 +33,27 @@ func sqliteType(req *plugin.GenerateRequest, col *plugin.Column) string{
if notNull{
return "float64"
}
if emitPointersForNull{
return "*float64"
}
return "sql.NullFloat64"

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

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

case "any":
Expand All@@ -60,12 +74,18 @@ func sqliteType(req *plugin.GenerateRequest, col *plugin.Column) string{
if notNull{
return "string"
}
if emitPointersForNull{
return "*string"
}
return "sql.NullString"

case strings.HasPrefix(dt, "decimal"), dt == "numeric":
if notNull{
return "float64"
}
if emitPointersForNull{
return "*float64"
}
return "sql.NullFloat64"

default:
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 numberDiff line numberDiff line change
@@ -0,0 +1 @@
SELECT 1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@

CREATETABLEdt_types (
a int,
b real,
c bool,
d date,
e text,
f numeric
);

CREATETABLEdt_types_not_null (
a intNOT NULL,
b realNOT NULL,
c bool NOT NULL,
d dateNOT NULL,
e textNOT NULL,
f numericNOT NULL
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "sqlite",
"name": "datatype",
"schema": "sql/",
"queries": "sql/",
"emit_pointers_for_null_types": true
}
]
}