Skip to content
Open
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
31 changes: 31 additions & 0 deletions internal/compiler/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,36 @@ func convertColumn(c *analyzer.Column) *Column {
}
}

func mergeColumnOrigin(dst, src *Column) {
if dst == nil || src == nil {
return
}

// Column overrides in the Go generator depend on the column's original
// table identity. For SQLite, the database analyzer is often the most
// accurate source for this information, especially for columns added by
// ALTER TABLE ... ADD COLUMN.
//
// Keep the catalog-inferred name/type/nullability unless the existing
// combine logic below decides to override the type. Only merge origin
// metadata here.
if src.OriginalName != "" {
dst.OriginalName = src.OriginalName
}
if src.Table != nil {
dst.Table = src.Table
}
if src.TableAlias != "" {
dst.TableAlias = src.TableAlias
}
if src.Scope != "" {
dst.Scope = src.Scope
}
if src.EmbedTable != nil {
dst.EmbedTable = src.EmbedTable
}
}

func combineAnalysis(prev *analysis, a *analyzer.Analysis) *analysis {
var cols []*Column
for _, c := range a.Columns {
Expand All @@ -79,6 +109,7 @@ func combineAnalysis(prev *analysis, a *analyzer.Analysis) *analysis {
}
if len(prev.Columns) == len(cols) {
for i := range prev.Columns {
mergeColumnOrigin(prev.Columns[i], cols[i])
// Only override column types if the analyzer provides a specific type
// (not "any"), since the catalog-based inference may have better info
if cols[i].DataType != "any" {
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.

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,2 @@
-- name: Test :one
SELECT 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE chat_messages (
id TEXT PRIMARY KEY NOT NULL,
body TEXT NOT NULL,
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
) STRICT;

ALTER TABLE chat_messages ADD COLUMN "updated_at" TEXT NOT NULL DEFAULT '';
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"version": "1",
"packages": [
{
"path": "go",
"name": "db",
"engine": "sqlite",
"schema": "schema.sql",
"queries": "query.sql",
"overrides": [
{
"column": "chat_messages.created_at",
"go_type": "time.Time"
},
{
"column": "chat_messages.updated_at",
"go_type": "time.Time"
}
]
}
]
}
14 changes: 9 additions & 5 deletions internal/engine/sqlite/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,18 @@ func (c *cc) convertAlter_table_stmtContext(n *parser.Alter_table_stmtContext) a
Table: parseTableName(n),
Cmds: &ast.List{},
}
name := def.Column_name().GetText()
name := identifier(def.Column_name().GetText())
typeName := "any"
if def.Type_name() != nil {
typeName = def.Type_name().GetText()
}
stmt.Cmds.Items = append(stmt.Cmds.Items, &ast.AlterTableCmd{
Name: &name,
Subtype: ast.AT_AddColumn,
Def: &ast.ColumnDef{
Colname: name,
TypeName: &ast.TypeName{
Name: def.Type_name().GetText(),
Name: typeName,
},
IsNotNull: hasNotNullConstraint(def.AllColumn_constraint()),
},
Expand All @@ -88,7 +92,7 @@ func (c *cc) convertAlter_table_stmtContext(n *parser.Alter_table_stmtContext) a
Table: parseTableName(n),
Cmds: &ast.List{},
}
name := n.Column_name(0).GetText()
name := identifier(n.Column_name(0).GetText())
stmt.Cmds.Items = append(stmt.Cmds.Items, &ast.AlterTableCmd{
Name: &name,
Subtype: ast.AT_DropColumn,
Expand Down Expand Up @@ -826,7 +830,7 @@ func (c *cc) convertUnaryExpr(n *parser.Expr_unaryContext) ast.Node {
if opCtx.MINUS() != nil {
// Negative number: -expr
return &ast.A_Expr{
Name: &ast.List{Items: []ast.Node{&ast.String{Str: "-"}}},
Name: &ast.List{Items: []ast.Node{&ast.String{Str: "-"}}},
Rexpr: expr,
}
}
Expand All @@ -837,7 +841,7 @@ func (c *cc) convertUnaryExpr(n *parser.Expr_unaryContext) ast.Node {
if opCtx.TILDE() != nil {
// Bitwise NOT: ~expr
return &ast.A_Expr{
Name: &ast.List{Items: []ast.Node{&ast.String{Str: "~"}}},
Name: &ast.List{Items: []ast.Node{&ast.String{Str: "~"}}},
Rexpr: expr,
}
}
Expand Down
Loading