Skip to content

Commit 4f2f0f3

Browse files
authored
sqlite: Use convert functions instead of the listener (#519)
1 parent 5db4d1b commit 4f2f0f3

File tree

9 files changed

+302
-555
lines changed

9 files changed

+302
-555
lines changed

internal/cmd/generate.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,9 @@ func parse(e Env, name, dir string, sql config.SQL, combo config.CombinedSetting
226226
}
227227
return q, false
228228

229-
case config.EnginePostgreSQL:
229+
case config.EnginePostgreSQL, config.EngineXLemon:
230230
var eng postgreEngine
231-
if e.ExperimentalParser {
231+
if e.ExperimentalParser || sql.Engine == config.EngineXLemon {
232232
eng = compiler.NewEngine(sql, combo)
233233
} else {
234234
eng = &dinosqlEngine{}
@@ -257,7 +257,7 @@ func parse(e Env, name, dir string, sql config.SQL, combo config.CombinedSetting
257257
}
258258
return eng.Result(), false
259259

260-
case config.EngineXLemon, config.EngineXDolphin, config.EngineXElephant:
260+
case config.EngineXDolphin, config.EngineXElephant:
261261
r, err := compiler.Run(sql, combo)
262262
if err != nil {
263263
fmt.Fprintf(stderr, "# package %s\n", name)

internal/compiler/go_type.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func (r *Result) goInnerType(col *Column, settings config.CombinedSettings) stri
157157
return "interface{}"
158158
}
159159
if rel.Schema == "" {
160-
rel.Schema = "public"
160+
rel.Schema = r.Catalog.DefaultSchema
161161
}
162162

163163
for _, schema := range r.Catalog.Schemas {
@@ -168,7 +168,7 @@ func (r *Result) goInnerType(col *Column, settings config.CombinedSettings) stri
168168
switch t := typ.(type) {
169169
case *catalog.Enum:
170170
if rel.Name == t.Name && rel.Schema == schema.Name {
171-
if schema.Name == "public" {
171+
if schema.Name == r.Catalog.DefaultSchema {
172172
return golang.StructName(t.Name, settings)
173173
}
174174
return golang.StructName(schema.Name+"_"+t.Name, settings)

internal/compiler/result.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (r *Result) Structs(settings config.CombinedSettings) []golang.Struct {
4747
}
4848
for _, table := range schema.Tables {
4949
var tableName string
50-
if schema.Name == "public" {
50+
if schema.Name == r.Catalog.DefaultSchema {
5151
tableName = table.Rel.Name
5252
} else {
5353
tableName = schema.Name + "_" + table.Rel.Name
@@ -90,7 +90,7 @@ func (r *Result) Enums(settings config.CombinedSettings) []golang.Enum {
9090
continue
9191
}
9292
var enumName string
93-
if schema.Name == "public" {
93+
if schema.Name == r.Catalog.DefaultSchema {
9494
enumName = enum.Name
9595
} else {
9696
enumName = schema.Name + "_" + enum.Name

internal/endtoend/testdata/experimental_lemon/go/models.go

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

internal/endtoend/testdata/experimental_lemon/query.sql

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@ CREATE TABLE bar (
66
baz text NOT NULL
77
);
88

9-
SELECT bar FROM foo;
9+
CREATE TABLE baz (name text);
10+
ALTER TABLE baz ADD COLUMN email text;
1011

11-
DROP TABLE bar;
12-
DROP TABLE IF EXISTS bar;
13-
DROP TABLE IF EXISTS baz;
12+
-- name: ListFoo :many
13+
SELECT bar FROM foo;
1414

1515

16-
CREATE TABLE baz (name text);
17-
ALTER TABLE baz ADD COLUMN email text;

internal/sqlite/convert.go

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
package sqlite
2+
3+
import (
4+
"github.com/antlr/antlr4/runtime/Go/antlr"
5+
6+
"github.com/kyleconroy/sqlc/internal/sql/ast"
7+
"github.com/kyleconroy/sqlc/internal/sql/ast/pg"
8+
"github.com/kyleconroy/sqlc/internal/sqlite/parser"
9+
)
10+
11+
type node interface {
12+
GetParser() antlr.Parser
13+
}
14+
15+
func convertAlter_table_stmtContext(c *parser.Alter_table_stmtContext) ast.Node {
16+
if newTable, ok := c.New_table_name().(*parser.New_table_nameContext); ok {
17+
name := newTable.Any_name().GetText()
18+
return &ast.RenameTableStmt{
19+
Table: parseTableName(c),
20+
NewName: &name,
21+
}
22+
}
23+
24+
if newCol, ok := c.New_column_name().(*parser.New_column_nameContext); ok {
25+
name := newCol.Any_name().GetText()
26+
return &ast.RenameColumnStmt{
27+
Table: parseTableName(c),
28+
Col: &ast.ColumnRef{
29+
Name: c.Column_name().GetText(),
30+
},
31+
NewName: &name,
32+
}
33+
}
34+
35+
if def, ok := c.Column_def().(*parser.Column_defContext); ok {
36+
stmt := &ast.AlterTableStmt{
37+
Table: parseTableName(c),
38+
Cmds: &ast.List{},
39+
}
40+
name := def.Column_name().GetText()
41+
stmt.Cmds.Items = append(stmt.Cmds.Items, &ast.AlterTableCmd{
42+
Name: &name,
43+
Subtype: ast.AT_AddColumn,
44+
Def: &ast.ColumnDef{
45+
Colname: name,
46+
TypeName: &ast.TypeName{
47+
Name: def.Type_name().GetText(),
48+
},
49+
},
50+
})
51+
return stmt
52+
}
53+
54+
return &ast.TODO{}
55+
}
56+
57+
func convertAttach_stmtContext(c *parser.Attach_stmtContext) ast.Node {
58+
name := c.Database_name().GetText()
59+
return &ast.CreateSchemaStmt{
60+
Name: &name,
61+
}
62+
}
63+
64+
func convertCreate_table_stmtContext(c *parser.Create_table_stmtContext) ast.Node {
65+
stmt := &ast.CreateTableStmt{
66+
Name: parseTableName(c),
67+
IfNotExists: c.K_EXISTS() != nil,
68+
}
69+
for _, idef := range c.AllColumn_def() {
70+
if def, ok := idef.(*parser.Column_defContext); ok {
71+
stmt.Cols = append(stmt.Cols, &ast.ColumnDef{
72+
Colname: def.Column_name().GetText(),
73+
TypeName: &ast.TypeName{
74+
Name: def.Type_name().GetText(),
75+
},
76+
})
77+
}
78+
}
79+
return stmt
80+
}
81+
82+
func convertDrop_table_stmtContext(c *parser.Drop_table_stmtContext) ast.Node {
83+
return &ast.DropTableStmt{
84+
IfExists: c.K_EXISTS() != nil,
85+
Tables: []*ast.TableName{parseTableName(c)},
86+
}
87+
}
88+
89+
func convertFactored_select_stmtContext(c *parser.Factored_select_stmtContext) ast.Node {
90+
var tables []ast.Node
91+
var cols []ast.Node
92+
for _, icore := range c.AllSelect_core() {
93+
core, ok := icore.(*parser.Select_coreContext)
94+
if !ok {
95+
continue
96+
}
97+
for _, icol := range core.AllResult_column() {
98+
col, ok := icol.(*parser.Result_columnContext)
99+
if !ok {
100+
continue
101+
}
102+
iexpr := col.Expr()
103+
if iexpr == nil {
104+
continue
105+
}
106+
expr, ok := iexpr.(*parser.ExprContext)
107+
if !ok {
108+
continue
109+
}
110+
cols = append(cols, &ast.ResTarget{
111+
Val: &ast.ColumnRef{
112+
Name: expr.Column_name().GetText(),
113+
},
114+
})
115+
}
116+
for _, ifrom := range core.AllTable_or_subquery() {
117+
from, ok := ifrom.(*parser.Table_or_subqueryContext)
118+
if !ok {
119+
continue
120+
}
121+
name := ast.TableName{
122+
Name: from.Table_name().GetText(),
123+
}
124+
if from.Schema_name() != nil {
125+
name.Schema = from.Schema_name().GetText()
126+
}
127+
tables = append(tables, &name)
128+
}
129+
}
130+
return &pg.SelectStmt{
131+
FromClause: &ast.List{Items: tables},
132+
TargetList: &ast.List{Items: cols},
133+
}
134+
}
135+
136+
func convertSql_stmtContext(n *parser.Sql_stmtContext) ast.Node {
137+
if stmt := n.Alter_table_stmt(); stmt != nil {
138+
return convert(stmt)
139+
}
140+
if stmt := n.Analyze_stmt(); stmt != nil {
141+
return convert(stmt)
142+
}
143+
if stmt := n.Attach_stmt(); stmt != nil {
144+
return convert(stmt)
145+
}
146+
if stmt := n.Begin_stmt(); stmt != nil {
147+
return convert(stmt)
148+
}
149+
if stmt := n.Commit_stmt(); stmt != nil {
150+
return convert(stmt)
151+
}
152+
if stmt := n.Compound_select_stmt(); stmt != nil {
153+
return convert(stmt)
154+
}
155+
if stmt := n.Create_index_stmt(); stmt != nil {
156+
return convert(stmt)
157+
}
158+
if stmt := n.Create_table_stmt(); stmt != nil {
159+
return convert(stmt)
160+
}
161+
if stmt := n.Create_trigger_stmt(); stmt != nil {
162+
return convert(stmt)
163+
}
164+
if stmt := n.Create_view_stmt(); stmt != nil {
165+
return convert(stmt)
166+
}
167+
if stmt := n.Create_virtual_table_stmt(); stmt != nil {
168+
return convert(stmt)
169+
}
170+
if stmt := n.Delete_stmt(); stmt != nil {
171+
return convert(stmt)
172+
}
173+
if stmt := n.Delete_stmt_limited(); stmt != nil {
174+
return convert(stmt)
175+
}
176+
if stmt := n.Detach_stmt(); stmt != nil {
177+
return convert(stmt)
178+
}
179+
if stmt := n.Drop_index_stmt(); stmt != nil {
180+
return convert(stmt)
181+
}
182+
if stmt := n.Drop_table_stmt(); stmt != nil {
183+
return convert(stmt)
184+
}
185+
if stmt := n.Drop_trigger_stmt(); stmt != nil {
186+
return convert(stmt)
187+
}
188+
if stmt := n.Drop_view_stmt(); stmt != nil {
189+
return convert(stmt)
190+
}
191+
if stmt := n.Factored_select_stmt(); stmt != nil {
192+
return convert(stmt)
193+
}
194+
if stmt := n.Insert_stmt(); stmt != nil {
195+
return convert(stmt)
196+
}
197+
if stmt := n.Pragma_stmt(); stmt != nil {
198+
return convert(stmt)
199+
}
200+
if stmt := n.Reindex_stmt(); stmt != nil {
201+
return convert(stmt)
202+
}
203+
if stmt := n.Release_stmt(); stmt != nil {
204+
return convert(stmt)
205+
}
206+
if stmt := n.Rollback_stmt(); stmt != nil {
207+
return convert(stmt)
208+
}
209+
if stmt := n.Savepoint_stmt(); stmt != nil {
210+
return convert(stmt)
211+
}
212+
if stmt := n.Simple_select_stmt(); stmt != nil {
213+
return convert(stmt)
214+
}
215+
if stmt := n.Select_stmt(); stmt != nil {
216+
return convert(stmt)
217+
}
218+
if stmt := n.Update_stmt(); stmt != nil {
219+
return convert(stmt)
220+
}
221+
if stmt := n.Update_stmt_limited(); stmt != nil {
222+
return convert(stmt)
223+
}
224+
if stmt := n.Vacuum_stmt(); stmt != nil {
225+
return convert(stmt)
226+
}
227+
return nil
228+
}
229+
230+
func convert(node node) ast.Node {
231+
switch n := node.(type) {
232+
233+
case *parser.Alter_table_stmtContext:
234+
return convertAlter_table_stmtContext(n)
235+
236+
case *parser.Attach_stmtContext:
237+
return convertAttach_stmtContext(n)
238+
239+
case *parser.Create_table_stmtContext:
240+
return convertCreate_table_stmtContext(n)
241+
242+
case *parser.Drop_table_stmtContext:
243+
return convertDrop_table_stmtContext(n)
244+
245+
case *parser.Factored_select_stmtContext:
246+
return convertFactored_select_stmtContext(n)
247+
248+
case *parser.Sql_stmtContext:
249+
return convertSql_stmtContext(n)
250+
251+
default:
252+
return &ast.TODO{}
253+
}
254+
}

0 commit comments

Comments
 (0)