Skip to content

fix(engine/sqlite): support -> and ->> operators #2927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 31, 2023
Merged
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
6 changes: 3 additions & 3 deletions internal/compiler/output_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,11 @@ func (c *Compiler) outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, er
if res.Name != nil {
name = *res.Name
}
switch {
case lang.IsComparisonOperator(astutils.Join(n.Name, "")):
switch op := astutils.Join(n.Name, ""); {
case lang.IsComparisonOperator(op):
// TODO: Generate a name for these operations
cols = append(cols, &Column{Name: name, DataType: "bool", NotNull: true})
case lang.IsMathematicalOperator(astutils.Join(n.Name, "")):
case lang.IsMathematicalOperator(op):
cols = append(cols, &Column{Name: name, DataType: "int", NotNull: true})
default:
cols = append(cols, &Column{Name: name, DataType: "any", NotNull: false})
Expand Down
2 changes: 1 addition & 1 deletion internal/compiler/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
// TODO: Move this to database-specific engine package
dataType := "any"
if astutils.Join(n.Name, ".") == "||" {
dataType = "string"
dataType = "text"
}

defaultP := named.NewParam("")
Expand Down
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/json_param_type/sqlite/go/db.go

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

14 changes: 14 additions & 0 deletions internal/endtoend/testdata/json_param_type/sqlite/go/models.go

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

22 changes: 22 additions & 0 deletions internal/endtoend/testdata/json_param_type/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/json_param_type/sqlite/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- name: FindByAddress :one
SELECT * FROM "user" WHERE "metadata"->>'address1' = ?1 LIMIT 1;
4 changes: 4 additions & 0 deletions internal/endtoend/testdata/json_param_type/sqlite/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE "user" (
"id" INT,
"metadata" TEXT
);
9 changes: 9 additions & 0 deletions internal/endtoend/testdata/json_param_type/sqlite/sqlc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: "2"
sql:
- engine: "sqlite"
schema: "schema.sql"
queries: "query.sql"
gen:
go:
package: "querytest"
out: "go"
14 changes: 7 additions & 7 deletions internal/engine/sqlite/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -764,19 +764,19 @@ func (c *cc) convertLiteral(n *parser.Expr_literalContext) ast.Node {
return todo("convertLiteral", n)
}

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

func (c *cc) convertBinaryNode(n *parser.Expr_binaryContext) ast.Node {
func (c *cc) convertBoolNode(n *parser.Expr_boolContext) ast.Node {
return &ast.BoolExpr{
// TODO: Set op
Args: &ast.List{
Expand Down Expand Up @@ -1163,14 +1163,14 @@ func (c *cc) convert(node node) ast.Node {
case *parser.Expr_literalContext:
return c.convertLiteral(n)

case *parser.Expr_binaryContext:
return c.convertBinaryNode(n)
case *parser.Expr_boolContext:
return c.convertBoolNode(n)

case *parser.Expr_listContext:
return c.convertExprListContext(n)

case *parser.Expr_math_opContext:
return c.convertMathOperationNode(n)
case *parser.Expr_binaryContext:
return c.convertBinaryNode(n)

case *parser.Expr_in_selectContext:
return c.convertInSelectNode(n)
Expand Down
2 changes: 2 additions & 0 deletions internal/engine/sqlite/parser/SQLiteLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ COMMA: ',';
ASSIGN: '=';
STAR: '*';
PLUS: '+';
PTR2: '->>';
PTR: '->';
MINUS: '-';
TILDE: '~';
PIPE2: '||';
Expand Down
8 changes: 7 additions & 1 deletion internal/engine/sqlite/parser/SQLiteLexer.interp

Large diffs are not rendered by default.

Loading