Skip to content

Commit 2637260

Browse files
committed
feat: improve support for sqlite named parameters
1 parent 5555393 commit 2637260

File tree

5 files changed

+43
-8
lines changed

5 files changed

+43
-8
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# package querytest
2-
query.sql:9:1: column reference "invalid_reference" not found
2+
query.sql:11:10: column reference "invalid_reference" not found
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# package querytest
2-
query.sql:9:1: table alias "p" does not exist
2+
query.sql:11:9: table alias "p" does not exist

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

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/engine/sqlite/convert.go

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ func (c *cc) convertFuncContext(n *parser.Expr_functionContext) ast.Node {
223223
Args: args,
224224
AggOrder: &ast.List{},
225225
AggDistinct: n.DISTINCT_() != nil,
226+
Location: n.GetStart().GetStart(),
226227
}
227228
}
228229
}
@@ -253,6 +254,7 @@ func (c *cc) convertColumnNameExpr(n *parser.Expr_qualified_column_nameContext)
253254
Fields: &ast.List{
254255
Items: items,
255256
},
257+
Location: n.GetStart().GetStart(),
256258
}
257259
}
258260

@@ -554,11 +556,38 @@ func (c *cc) convertBinaryNode(n *parser.Expr_binaryContext) ast.Node {
554556

555557
func (c *cc) convertParam(n *parser.Expr_bindContext) ast.Node {
556558
if n.BIND_PARAMETER() != nil {
557-
// Parameter numbers start at one
558-
c.paramCount += 1
559-
return &ast.ParamRef{
560-
Number: c.paramCount,
561-
Location: n.GetStart().GetStart(),
559+
text := n.GetText()
560+
if strings.ContainsRune("@$:", rune(text[0])) {
561+
loc := n.GetStart().GetStart()
562+
return &ast.A_Expr{
563+
Kind: ast.A_Expr_Kind(1),
564+
Name: &ast.List{
565+
Items: []ast.Node{&ast.String{Str: text[:1]}},
566+
},
567+
Rexpr: &ast.ColumnRef{
568+
Name: text[1:],
569+
Fields: &ast.List{
570+
Items: []ast.Node{&ast.String{Str: text[1:]}},
571+
},
572+
Location: loc + 1,
573+
},
574+
Location: loc,
575+
}
576+
}
577+
if text == "?" {
578+
// Parameter numbers start at one
579+
c.paramCount += 1
580+
return &ast.ParamRef{
581+
Number: c.paramCount,
582+
Location: n.GetStart().GetStart(),
583+
}
584+
} else {
585+
argn, _ := strconv.Atoi(text[1:])
586+
return &ast.ParamRef{
587+
Number: argn,
588+
Location: n.GetStart().GetStart(),
589+
Dollar: true,
590+
}
562591
}
563592
}
564593
return todo(n)

internal/sql/rewrite/parameters.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ func NamedParameters(engine config.Engine, raw *ast.RawStmt, numbs map[int]bool,
102102
} else {
103103
replace = "?"
104104
}
105+
} else if engine == config.EngineSQLite {
106+
replace = fmt.Sprintf("?%d", argn)
105107
} else {
106108
replace = fmt.Sprintf("$%d", argn)
107109
}
@@ -130,6 +132,8 @@ func NamedParameters(engine config.Engine, raw *ast.RawStmt, numbs map[int]bool,
130132
var replace string
131133
if engine == config.EngineMySQL || !dollar {
132134
replace = "?"
135+
} else if engine == config.EngineSQLite {
136+
replace = fmt.Sprintf("?%d", argn)
133137
} else {
134138
replace = fmt.Sprintf("$%d", argn)
135139
}
@@ -156,6 +160,8 @@ func NamedParameters(engine config.Engine, raw *ast.RawStmt, numbs map[int]bool,
156160
var replace string
157161
if engine == config.EngineMySQL || !dollar {
158162
replace = "?"
163+
} else if engine == config.EngineSQLite {
164+
replace = fmt.Sprintf("?%d", argn)
159165
} else {
160166
replace = fmt.Sprintf("$%d", argn)
161167
}

0 commit comments

Comments
 (0)