-
Notifications
You must be signed in to change notification settings - Fork 879
SQLite: Coerce jsonb columns to json before returning to Go code #3968
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ import ( | |
"strings" | ||
|
||
"github.com/sqlc-dev/sqlc/internal/config" | ||
"github.com/sqlc-dev/sqlc/internal/engine/sqlite" | ||
"github.com/sqlc-dev/sqlc/internal/source" | ||
"github.com/sqlc-dev/sqlc/internal/sql/ast" | ||
"github.com/sqlc-dev/sqlc/internal/sql/astutils" | ||
|
@@ -149,6 +150,17 @@ func (c *Compiler) expandStmt(qc *QueryCatalog, raw *ast.RawStmt, node ast.Node) | |
if counts[cname] > 1 { | ||
cname = tableName + "." + cname | ||
} | ||
// Under SQLite, neither json nor jsonb are real data types, and | ||
// rather just of type blob, so database drivers just return | ||
// whatever raw binary is stored as values. This is a problem | ||
// for jsonb, which is considered an internal format to SQLite | ||
// and no attempt should be made to parse it outside of the | ||
// database itself. For jsonb columns in SQLite, wrap returned | ||
// columns in `json(col)` to coerce the internal binary format | ||
// to JSON parsable by the user-space application. | ||
if _, ok := c.parser.(*sqlite.Parser); ok && column.DataType == "jsonb" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So I left the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I looked through the code to see if there was a better place to put this. The ideal solution is to create a Instead, let's create a new What do you think? |
||
cname = "json(" + cname + ")" | ||
} | ||
cols = append(cols, cname) | ||
} | ||
} | ||
|
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,15 @@ | ||
-- name: InsertFoo :exec | ||
INSERT INTO foo ( | ||
a, | ||
b, | ||
c, | ||
d | ||
) VALUES ( | ||
@a, | ||
@b, | ||
@c, | ||
@d | ||
) RETURNING *; | ||
|
||
-- name: SelectFoo :exec | ||
SELECT * FROM foo; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
CREATE TABLE foo ( | ||
a json not null, | ||
b jsonb not null, | ||
c json, | ||
d jsonb | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"version": "1", | ||
"packages": [ | ||
{ | ||
"path": "go", | ||
"engine": "postgresql", | ||
"sql_package": "pgx/v5", | ||
"name": "querytest", | ||
"schema": "schema.sql", | ||
"queries": "query.sql" | ||
} | ||
] | ||
} |
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,15 @@ | ||
-- name: InsertFoo :exec | ||
INSERT INTO foo ( | ||
a, | ||
b, | ||
c, | ||
d | ||
) VALUES ( | ||
@a, | ||
@b, | ||
@c, | ||
@d | ||
) RETURNING *; | ||
|
||
-- name: SelectFoo :exec | ||
SELECT * FROM foo; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
CREATE TABLE foo ( | ||
a json not null, | ||
b jsonb not null, | ||
c json, | ||
d jsonb | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"version": "1", | ||
"packages": [ | ||
{ | ||
"path": "go", | ||
"engine": "sqlite", | ||
"name": "querytest", | ||
"schema": "schema.sql", | ||
"queries": "query.sql" | ||
} | ||
] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
json.RawMessage
preferred over[]byte
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it's what we do for PostgreSQL.