Skip to content

Commit b3e4145

Browse files
authored
feat(sqlite): Support emit_pointers_for_null_types (#3026)
Pointer fields work with both the popular github.com/mattn/go-sqlite3 driver and the pure-Go modernc.org/sqlite driver.
1 parent d336e4a commit b3e4145

File tree

8 files changed

+113
-3
lines changed

8 files changed

+113
-3
lines changed

docs/reference/config.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ The `gen` mapping supports the following keys:
156156
- `emit_methods_with_db_argument`:
157157
- If true, generated methods will accept a DBTX argument instead of storing a DBTX on the `*Queries` struct. Defaults to `false`.
158158
- `emit_pointers_for_null_types`:
159-
- 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`.
159+
- 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`.
160160
- `emit_enum_valid_method`:
161161
- If true, generate a Valid method on enum types,
162162
indicating whether a string is a valid enum value.

internal/codegen/golang/go_type.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func goInnerType(req *plugin.GenerateRequest, options *opts.Options, col *plugin
8585
case "postgresql":
8686
return postgresType(req, options, col)
8787
case "sqlite":
88-
return sqliteType(req, col)
88+
return sqliteType(req, options, col)
8989
default:
9090
return "interface{}"
9191
}

internal/codegen/golang/sqlite_type.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,26 @@ import (
44
"log"
55
"strings"
66

7+
"github.com/sqlc-dev/sqlc/internal/codegen/golang/opts"
78
"github.com/sqlc-dev/sqlc/internal/codegen/sdk"
89
"github.com/sqlc-dev/sqlc/internal/debug"
910
"github.com/sqlc-dev/sqlc/internal/plugin"
1011
)
1112

12-
func sqliteType(req *plugin.GenerateRequest, col *plugin.Column) string {
13+
func sqliteType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Column) string {
1314
dt := strings.ToLower(sdk.DataType(col.Type))
1415
notNull := col.NotNull || col.IsArray
16+
emitPointersForNull := options.EmitPointersForNullTypes
1517

1618
switch dt {
1719

1820
case "int", "integer", "tinyint", "smallint", "mediumint", "bigint", "unsignedbigint", "int2", "int8":
1921
if notNull {
2022
return "int64"
2123
}
24+
if emitPointersForNull {
25+
return "*int64"
26+
}
2227
return "sql.NullInt64"
2328

2429
case "blob":
@@ -28,18 +33,27 @@ func sqliteType(req *plugin.GenerateRequest, col *plugin.Column) string {
2833
if notNull {
2934
return "float64"
3035
}
36+
if emitPointersForNull {
37+
return "*float64"
38+
}
3139
return "sql.NullFloat64"
3240

3341
case "boolean", "bool":
3442
if notNull {
3543
return "bool"
3644
}
45+
if emitPointersForNull {
46+
return "*bool"
47+
}
3748
return "sql.NullBool"
3849

3950
case "date", "datetime", "timestamp":
4051
if notNull {
4152
return "time.Time"
4253
}
54+
if emitPointersForNull {
55+
return "*time.Time"
56+
}
4357
return "sql.NullTime"
4458

4559
case "any":
@@ -60,12 +74,18 @@ func sqliteType(req *plugin.GenerateRequest, col *plugin.Column) string {
6074
if notNull {
6175
return "string"
6276
}
77+
if emitPointersForNull {
78+
return "*string"
79+
}
6380
return "sql.NullString"
6481

6582
case strings.HasPrefix(dt, "decimal"), dt == "numeric":
6683
if notNull {
6784
return "float64"
6885
}
86+
if emitPointersForNull {
87+
return "*float64"
88+
}
6989
return "sql.NullFloat64"
7090

7191
default:

internal/endtoend/testdata/emit_pointers_for_null_types/sqlite/go/db.go

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

internal/endtoend/testdata/emit_pointers_for_null_types/sqlite/go/models.go

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SELECT 1;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
CREATE TABLE dt_types (
3+
a int,
4+
b real,
5+
c bool,
6+
d date,
7+
e text,
8+
f numeric
9+
);
10+
11+
CREATE TABLE dt_types_not_null (
12+
a int NOT NULL,
13+
b real NOT NULL,
14+
c bool NOT NULL,
15+
d date NOT NULL,
16+
e text NOT NULL,
17+
f numeric NOT NULL
18+
);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "go",
6+
"engine": "sqlite",
7+
"name": "datatype",
8+
"schema": "sql/",
9+
"queries": "sql/",
10+
"emit_pointers_for_null_types": true
11+
}
12+
]
13+
}

0 commit comments

Comments
 (0)