From 94954606618f471d9dd84494ad4fd8a526ac51b5 Mon Sep 17 00:00:00 2001 From: Andrew Benton Date: Tue, 16 May 2023 14:44:50 -0700 Subject: [PATCH] Handle untyped sqlite columns without crashing. Fixes https://github.com/kyleconroy/sqlc/issues/2279 --- .../untyped_columns/sqlite/stdlib/db/db.go | 31 +++++++++++++++++++ .../sqlite/stdlib/db/models.go | 13 ++++++++ .../sqlite/stdlib/db/query.sql.go | 21 +++++++++++++ .../untyped_columns/sqlite/stdlib/query.sql | 5 +++ .../untyped_columns/sqlite/stdlib/sqlc.json | 16 ++++++++++ internal/engine/sqlite/convert.go | 6 +++- 6 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 internal/endtoend/testdata/untyped_columns/sqlite/stdlib/db/db.go create mode 100644 internal/endtoend/testdata/untyped_columns/sqlite/stdlib/db/models.go create mode 100644 internal/endtoend/testdata/untyped_columns/sqlite/stdlib/db/query.sql.go create mode 100644 internal/endtoend/testdata/untyped_columns/sqlite/stdlib/query.sql create mode 100644 internal/endtoend/testdata/untyped_columns/sqlite/stdlib/sqlc.json diff --git a/internal/endtoend/testdata/untyped_columns/sqlite/stdlib/db/db.go b/internal/endtoend/testdata/untyped_columns/sqlite/stdlib/db/db.go new file mode 100644 index 0000000000..e0b5347fe3 --- /dev/null +++ b/internal/endtoend/testdata/untyped_columns/sqlite/stdlib/db/db.go @@ -0,0 +1,31 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.18.0 + +package db + +import ( + "context" + "database/sql" +) + +type DBTX interface { + ExecContext(context.Context, string, ...interface{}) (sql.Result, error) + PrepareContext(context.Context, string) (*sql.Stmt, error) + QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error) + QueryRowContext(context.Context, string, ...interface{}) *sql.Row +} + +func New(db DBTX) *Queries { + return &Queries{db: db} +} + +type Queries struct { + db DBTX +} + +func (q *Queries) WithTx(tx *sql.Tx) *Queries { + return &Queries{ + db: tx, + } +} diff --git a/internal/endtoend/testdata/untyped_columns/sqlite/stdlib/db/models.go b/internal/endtoend/testdata/untyped_columns/sqlite/stdlib/db/models.go new file mode 100644 index 0000000000..433864b05f --- /dev/null +++ b/internal/endtoend/testdata/untyped_columns/sqlite/stdlib/db/models.go @@ -0,0 +1,13 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.18.0 + +package db + +import () + +type Repro struct { + ID interface{} + Name interface{} + Seq interface{} +} diff --git a/internal/endtoend/testdata/untyped_columns/sqlite/stdlib/db/query.sql.go b/internal/endtoend/testdata/untyped_columns/sqlite/stdlib/db/query.sql.go new file mode 100644 index 0000000000..36196ad571 --- /dev/null +++ b/internal/endtoend/testdata/untyped_columns/sqlite/stdlib/db/query.sql.go @@ -0,0 +1,21 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.18.0 +// source: query.sql + +package db + +import ( + "context" +) + +const getRepro = `-- name: GetRepro :one +select id, name, seq from repro where id = ? limit 1 +` + +func (q *Queries) GetRepro(ctx context.Context, id interface{}) (Repro, error) { + row := q.db.QueryRowContext(ctx, getRepro, id) + var i Repro + err := row.Scan(&i.ID, &i.Name, &i.Seq) + return i, err +} diff --git a/internal/endtoend/testdata/untyped_columns/sqlite/stdlib/query.sql b/internal/endtoend/testdata/untyped_columns/sqlite/stdlib/query.sql new file mode 100644 index 0000000000..2808a6aae2 --- /dev/null +++ b/internal/endtoend/testdata/untyped_columns/sqlite/stdlib/query.sql @@ -0,0 +1,5 @@ +-- original table name in sqlite schema was sqlite_sequence, rest of def is identical +create table repro(id, name, seq); + +-- name: GetRepro :one +select * from repro where id = ? limit 1; \ No newline at end of file diff --git a/internal/endtoend/testdata/untyped_columns/sqlite/stdlib/sqlc.json b/internal/endtoend/testdata/untyped_columns/sqlite/stdlib/sqlc.json new file mode 100644 index 0000000000..4af3ff1ae3 --- /dev/null +++ b/internal/endtoend/testdata/untyped_columns/sqlite/stdlib/sqlc.json @@ -0,0 +1,16 @@ +{ + "version": "2", + "sql": [ + { + "engine": "sqlite", + "schema": "query.sql", + "queries": "query.sql", + "gen": { + "go": { + "package": "db", + "out": "db" + } + } + } + ] +} \ No newline at end of file diff --git a/internal/engine/sqlite/convert.go b/internal/engine/sqlite/convert.go index eac9eb3236..facc76ac4b 100644 --- a/internal/engine/sqlite/convert.go +++ b/internal/engine/sqlite/convert.go @@ -109,10 +109,14 @@ func (c *cc) convertCreate_table_stmtContext(n *parser.Create_table_stmtContext) } for _, idef := range n.AllColumn_def() { if def, ok := idef.(*parser.Column_defContext); ok { + typeName := "any" + if def.Type_name() != nil { + typeName = def.Type_name().GetText() + } stmt.Cols = append(stmt.Cols, &ast.ColumnDef{ Colname: identifier(def.Column_name().GetText()), IsNotNull: hasNotNullConstraint(def.AllColumn_constraint()), - TypeName: &ast.TypeName{Name: def.Type_name().GetText()}, + TypeName: &ast.TypeName{Name: typeName}, }) } }