Skip to content

Commit fc7bf6d

Browse files
authored
feat(sqlite): Add support for delete statements (#1447)
1 parent 557dff6 commit fc7bf6d

File tree

20 files changed

+3216
-2001
lines changed

20 files changed

+3216
-2001
lines changed

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

Lines changed: 0 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
CREATE TABLE venues (name text);
2-
3-
CREATE TABLE IF NOT EXISTS arenas (name text PRIMARY KEY, location text, size int NOT NULL) WITHOUT ROWID;
1+
CREATE TABLE venues (name text);

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

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

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

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

internal/endtoend/testdata/ddl_create_table_without_rowid/sqlite/go/query.sql.go

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- name: Placeholder :exec
2+
SELECT 1;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE TABLE IF NOT EXISTS arenas (name text PRIMARY KEY, location text, size int NOT NULL) WITHOUT ROWID;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "go",
6+
"engine": "_lemon",
7+
"name": "querytest",
8+
"schema": "schema.sql",
9+
"queries": "query.sql"
10+
}
11+
]
12+
}

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

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

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

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

internal/endtoend/testdata/delete_from/sqlite/go/query.sql.go

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- name: DeleteFrom :exec
2+
DELETE FROM foo WHERE id = ?;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE TABLE foo (id text not null);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"engine": "_lemon",
6+
"path": "go",
7+
"name": "querytest",
8+
"schema": "schema.sql",
9+
"queries": "query.sql"
10+
}
11+
]
12+
}

internal/engine/sqlite/convert.go

Lines changed: 144 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package sqlite
22

33
import (
44
"github.com/antlr/antlr4/runtime/Go/antlr"
5+
"strconv"
56
"strings"
67

78
"github.com/kyleconroy/sqlc/internal/engine/sqlite/parser"
@@ -97,6 +98,42 @@ func convertCreate_table_stmtContext(c *parser.Create_table_stmtContext) ast.Nod
9798
return stmt
9899
}
99100

101+
func convertDelete_stmtContext(c *parser.Delete_stmtContext) ast.Node {
102+
if qualifiedName, ok := c.Qualified_table_name().(*parser.Qualified_table_nameContext); ok {
103+
104+
tableName := qualifiedName.Table_name().GetText()
105+
relation := &ast.RangeVar{
106+
Relname: &tableName,
107+
}
108+
109+
if qualifiedName.Schema_name() != nil {
110+
schemaName := qualifiedName.Schema_name().GetText()
111+
relation.Schemaname = &schemaName
112+
}
113+
114+
if qualifiedName.Alias() != nil {
115+
alias := qualifiedName.Alias().GetText()
116+
relation.Alias = &ast.Alias{Aliasname: &alias}
117+
}
118+
119+
delete := &ast.DeleteStmt{
120+
Relation: relation,
121+
ReturningList: &ast.List{},
122+
WithClause: nil,
123+
}
124+
125+
if c.WHERE_() != nil {
126+
if c.Expr() != nil {
127+
delete.WhereClause = convert(c.Expr())
128+
}
129+
}
130+
131+
return delete
132+
}
133+
134+
return &ast.TODO{}
135+
}
136+
100137
func convertDrop_stmtContext(c *parser.Drop_stmtContext) ast.Node {
101138
if c.TABLE_() != nil {
102139
name := ast.TableName{
@@ -123,7 +160,7 @@ func NewIdentifer(t string) *ast.String {
123160
return &ast.String{Str: identifier(t)}
124161
}
125162

126-
func convertExprContext(c *parser.ExprContext) ast.Node {
163+
func convertFuncContext(c *parser.Expr_functionContext) ast.Node {
127164
if name, ok := c.Function_name().(*parser.Function_nameContext); ok {
128165
funcName := strings.ToLower(name.GetText())
129166

@@ -145,14 +182,14 @@ func convertExprContext(c *parser.ExprContext) ast.Node {
145182
return fn
146183
}
147184

148-
if c.Column_name() != nil {
149-
return convertColumnNameExpr(c)
150-
}
185+
return &ast.TODO{}
186+
}
151187

188+
func convertExprContext(c *parser.ExprContext) ast.Node {
152189
return &ast.TODO{}
153190
}
154191

155-
func convertColumnNameExpr(c *parser.ExprContext) *ast.ColumnRef {
192+
func convertColumnNameExpr(c *parser.Expr_qualified_column_nameContext) *ast.ColumnRef {
156193
var items []ast.Node
157194
if schema, ok := c.Schema_name().(*parser.Schema_nameContext); ok {
158195
schemaText := schema.GetText()
@@ -174,6 +211,20 @@ func convertColumnNameExpr(c *parser.ExprContext) *ast.ColumnRef {
174211
}
175212
}
176213

214+
func convertComparison(c *parser.Expr_comparisonContext) ast.Node {
215+
aExpr := &ast.A_Expr{
216+
Name: &ast.List{
217+
Items: []ast.Node{
218+
&ast.String{Str: "="}, // TODO: add actual comparison
219+
},
220+
},
221+
Lexpr: convert(c.Expr(0)),
222+
Rexpr: convert(c.Expr(1)),
223+
}
224+
225+
return aExpr
226+
}
227+
177228
func convertSimpleSelect_stmtContext(c *parser.Simple_select_stmtContext) ast.Node {
178229
if core, ok := c.Select_core().(*parser.Select_coreContext); ok {
179230
cols := getCols(core)
@@ -335,6 +386,70 @@ func convertSql_stmtContext(n *parser.Sql_stmtContext) ast.Node {
335386
return nil
336387
}
337388

389+
func convertLiteral(c *parser.Expr_literalContext) ast.Node {
390+
if literal, ok := c.Literal_value().(*parser.Literal_valueContext); ok {
391+
392+
if literal.NUMERIC_LITERAL() != nil {
393+
i, _ := strconv.ParseInt(literal.GetText(), 10, 64)
394+
return &ast.A_Const{
395+
Val: &ast.Integer{Ival: i},
396+
}
397+
}
398+
399+
if literal.STRING_LITERAL() != nil {
400+
return &ast.A_Const{
401+
Val: &ast.String{Str: literal.GetText()},
402+
}
403+
}
404+
405+
if literal.TRUE_() != nil || literal.FALSE_() != nil {
406+
var i int64
407+
if literal.TRUE_() != nil {
408+
i = 1
409+
}
410+
411+
return &ast.A_Const{
412+
Val: &ast.Integer{Ival: i},
413+
}
414+
}
415+
416+
}
417+
return &ast.TODO{}
418+
}
419+
420+
func convertMathOperationNode(c *parser.Expr_math_opContext) ast.Node {
421+
return &ast.A_Expr{
422+
Name: &ast.List{
423+
Items: []ast.Node{
424+
&ast.String{Str: "+"}, // todo: Convert operation types
425+
},
426+
},
427+
Lexpr: convert(c.Expr(0)),
428+
Rexpr: convert(c.Expr(1)),
429+
}
430+
}
431+
432+
func convertBinaryNode(c *parser.Expr_binaryContext) ast.Node {
433+
return &ast.BoolExpr{
434+
// TODO: Set op
435+
Args: &ast.List{
436+
Items: []ast.Node{
437+
convert(c.Expr(0)),
438+
convert(c.Expr(1)),
439+
},
440+
},
441+
}
442+
}
443+
444+
func convertParam(c *parser.Expr_bindContext) ast.Node {
445+
if c.BIND_PARAMETER() != nil {
446+
return &ast.ParamRef{ // TODO: Need to count these up instead of always using 0
447+
Location: c.GetStart().GetStart(),
448+
}
449+
}
450+
return &ast.TODO{}
451+
}
452+
338453
func convert(node node) ast.Node {
339454
switch n := node.(type) {
340455

@@ -350,9 +465,33 @@ func convert(node node) ast.Node {
350465
case *parser.Drop_stmtContext:
351466
return convertDrop_stmtContext(n)
352467

468+
case *parser.Delete_stmtContext:
469+
return convertDelete_stmtContext(n)
470+
353471
case *parser.ExprContext:
354472
return convertExprContext(n)
355473

474+
case *parser.Expr_functionContext:
475+
return convertFuncContext(n)
476+
477+
case *parser.Expr_qualified_column_nameContext:
478+
return convertColumnNameExpr(n)
479+
480+
case *parser.Expr_comparisonContext:
481+
return convertComparison(n)
482+
483+
case *parser.Expr_bindContext:
484+
return convertParam(n)
485+
486+
case *parser.Expr_literalContext:
487+
return convertLiteral(n)
488+
489+
case *parser.Expr_binaryContext:
490+
return convertBinaryNode(n)
491+
492+
case *parser.Expr_math_opContext:
493+
return convertMathOperationNode(n)
494+
356495
case *parser.Factored_select_stmtContext:
357496
// TODO: need to handle this
358497
return &ast.TODO{}

0 commit comments

Comments
 (0)