Skip to content

Commit

Permalink
feat(sqlite): Add support for delete statements (#1447)
Browse files Browse the repository at this point in the history
  • Loading branch information
PadraigK committed Feb 19, 2022
1 parent 557dff6 commit fc7bf6d
Show file tree
Hide file tree
Showing 20 changed files with 3,216 additions and 2,001 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

@@ -1,3 +1 @@
CREATE TABLE venues (name text);

CREATE TABLE IF NOT EXISTS arenas (name text PRIMARY KEY, location text, size int NOT NULL) WITHOUT ROWID;
CREATE TABLE venues (name text);

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.

@@ -0,0 +1,2 @@
-- name: Placeholder :exec
SELECT 1;
@@ -0,0 +1 @@
CREATE TABLE IF NOT EXISTS arenas (name text PRIMARY KEY, location text, size int NOT NULL) WITHOUT ROWID;
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "_lemon",
"name": "querytest",
"schema": "schema.sql",
"queries": "query.sql"
}
]
}
29 changes: 29 additions & 0 deletions internal/endtoend/testdata/delete_from/sqlite/go/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions internal/endtoend/testdata/delete_from/sqlite/go/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions internal/endtoend/testdata/delete_from/sqlite/go/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions internal/endtoend/testdata/delete_from/sqlite/query.sql
@@ -0,0 +1,2 @@
-- name: DeleteFrom :exec
DELETE FROM foo WHERE id = ?;
1 change: 1 addition & 0 deletions internal/endtoend/testdata/delete_from/sqlite/schema.sql
@@ -0,0 +1 @@
CREATE TABLE foo (id text not null);
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/delete_from/sqlite/sqlc.json
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"engine": "_lemon",
"path": "go",
"name": "querytest",
"schema": "schema.sql",
"queries": "query.sql"
}
]
}
149 changes: 144 additions & 5 deletions internal/engine/sqlite/convert.go
Expand Up @@ -2,6 +2,7 @@ package sqlite

import (
"github.com/antlr/antlr4/runtime/Go/antlr"
"strconv"
"strings"

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

func convertDelete_stmtContext(c *parser.Delete_stmtContext) ast.Node {
if qualifiedName, ok := c.Qualified_table_name().(*parser.Qualified_table_nameContext); ok {

tableName := qualifiedName.Table_name().GetText()
relation := &ast.RangeVar{
Relname: &tableName,
}

if qualifiedName.Schema_name() != nil {
schemaName := qualifiedName.Schema_name().GetText()
relation.Schemaname = &schemaName
}

if qualifiedName.Alias() != nil {
alias := qualifiedName.Alias().GetText()
relation.Alias = &ast.Alias{Aliasname: &alias}
}

delete := &ast.DeleteStmt{
Relation: relation,
ReturningList: &ast.List{},
WithClause: nil,
}

if c.WHERE_() != nil {
if c.Expr() != nil {
delete.WhereClause = convert(c.Expr())
}
}

return delete
}

return &ast.TODO{}
}

func convertDrop_stmtContext(c *parser.Drop_stmtContext) ast.Node {
if c.TABLE_() != nil {
name := ast.TableName{
Expand All @@ -123,7 +160,7 @@ func NewIdentifer(t string) *ast.String {
return &ast.String{Str: identifier(t)}
}

func convertExprContext(c *parser.ExprContext) ast.Node {
func convertFuncContext(c *parser.Expr_functionContext) ast.Node {
if name, ok := c.Function_name().(*parser.Function_nameContext); ok {
funcName := strings.ToLower(name.GetText())

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

if c.Column_name() != nil {
return convertColumnNameExpr(c)
}
return &ast.TODO{}
}

func convertExprContext(c *parser.ExprContext) ast.Node {
return &ast.TODO{}
}

func convertColumnNameExpr(c *parser.ExprContext) *ast.ColumnRef {
func convertColumnNameExpr(c *parser.Expr_qualified_column_nameContext) *ast.ColumnRef {
var items []ast.Node
if schema, ok := c.Schema_name().(*parser.Schema_nameContext); ok {
schemaText := schema.GetText()
Expand All @@ -174,6 +211,20 @@ func convertColumnNameExpr(c *parser.ExprContext) *ast.ColumnRef {
}
}

func convertComparison(c *parser.Expr_comparisonContext) ast.Node {
aExpr := &ast.A_Expr{
Name: &ast.List{
Items: []ast.Node{
&ast.String{Str: "="}, // TODO: add actual comparison
},
},
Lexpr: convert(c.Expr(0)),
Rexpr: convert(c.Expr(1)),
}

return aExpr
}

func convertSimpleSelect_stmtContext(c *parser.Simple_select_stmtContext) ast.Node {
if core, ok := c.Select_core().(*parser.Select_coreContext); ok {
cols := getCols(core)
Expand Down Expand Up @@ -335,6 +386,70 @@ func convertSql_stmtContext(n *parser.Sql_stmtContext) ast.Node {
return nil
}

func convertLiteral(c *parser.Expr_literalContext) ast.Node {
if literal, ok := c.Literal_value().(*parser.Literal_valueContext); ok {

if literal.NUMERIC_LITERAL() != nil {
i, _ := strconv.ParseInt(literal.GetText(), 10, 64)
return &ast.A_Const{
Val: &ast.Integer{Ival: i},
}
}

if literal.STRING_LITERAL() != nil {
return &ast.A_Const{
Val: &ast.String{Str: literal.GetText()},
}
}

if literal.TRUE_() != nil || literal.FALSE_() != nil {
var i int64
if literal.TRUE_() != nil {
i = 1
}

return &ast.A_Const{
Val: &ast.Integer{Ival: i},
}
}

}
return &ast.TODO{}
}

func convertMathOperationNode(c *parser.Expr_math_opContext) ast.Node {
return &ast.A_Expr{
Name: &ast.List{
Items: []ast.Node{
&ast.String{Str: "+"}, // todo: Convert operation types
},
},
Lexpr: convert(c.Expr(0)),
Rexpr: convert(c.Expr(1)),
}
}

func convertBinaryNode(c *parser.Expr_binaryContext) ast.Node {
return &ast.BoolExpr{
// TODO: Set op
Args: &ast.List{
Items: []ast.Node{
convert(c.Expr(0)),
convert(c.Expr(1)),
},
},
}
}

func convertParam(c *parser.Expr_bindContext) ast.Node {
if c.BIND_PARAMETER() != nil {
return &ast.ParamRef{ // TODO: Need to count these up instead of always using 0
Location: c.GetStart().GetStart(),
}
}
return &ast.TODO{}
}

func convert(node node) ast.Node {
switch n := node.(type) {

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

case *parser.Delete_stmtContext:
return convertDelete_stmtContext(n)

case *parser.ExprContext:
return convertExprContext(n)

case *parser.Expr_functionContext:
return convertFuncContext(n)

case *parser.Expr_qualified_column_nameContext:
return convertColumnNameExpr(n)

case *parser.Expr_comparisonContext:
return convertComparison(n)

case *parser.Expr_bindContext:
return convertParam(n)

case *parser.Expr_literalContext:
return convertLiteral(n)

case *parser.Expr_binaryContext:
return convertBinaryNode(n)

case *parser.Expr_math_opContext:
return convertMathOperationNode(n)

case *parser.Factored_select_stmtContext:
// TODO: need to handle this
return &ast.TODO{}
Expand Down

0 comments on commit fc7bf6d

Please sign in to comment.