Skip to content

Commit

Permalink
feat(sqlite): Support emit_pointers_for_null_types (#3026)
Browse files Browse the repository at this point in the history
Pointer fields work with both the popular github.com/mattn/go-sqlite3
driver and the pure-Go modernc.org/sqlite driver.
  • Loading branch information
benhoyt committed Jan 2, 2024
1 parent d336e4a commit b3e4145
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,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 number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func goInnerType(req *plugin.GenerateRequest, options *opts.Options, col *plugin
case "postgresql":
return postgresType(req, options, col)
case "sqlite":
return sqliteType(req, col)
return sqliteType(req, options, col)
default:
return "interface{}"
}
Expand Down
22 changes: 21 additions & 1 deletion internal/codegen/golang/sqlite_type.go
Original file line number Diff line number Diff 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 number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

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

CREATE TABLE dt_types_not_null (
a int NOT NULL,
b real NOT NULL,
c bool NOT NULL,
d date NOT NULL,
e text NOT NULL,
f numeric NOT NULL
);
Original file line number Diff line number Diff 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
}
]
}

0 comments on commit b3e4145

Please sign in to comment.