Skip to content

Commit c05375b

Browse files
authored
feat: Add MySQL support for BETWEEN arguments (#1265)
1 parent 90075ff commit c05375b

File tree

11 files changed

+184
-1
lines changed

11 files changed

+184
-1
lines changed

internal/compiler/find_params.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ func (p paramSearch) Visit(node ast.Node) astutils.Visitor {
6363
case *ast.A_Expr:
6464
p.parent = node
6565

66+
case *ast.BetweenExpr:
67+
p.parent = node
68+
6669
case *ast.FuncCall:
6770
p.parent = node
6871

internal/compiler/resolve.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,44 @@ func resolveCatalogRefs(c *catalog.Catalog, qc *QueryCatalog, rvs []*ast.RangeVa
196196
}
197197
}
198198

199+
case *ast.BetweenExpr:
200+
if n == nil || n.Expr == nil || n.Left == nil || n.Right == nil {
201+
fmt.Println("ast.BetweenExpr is nil")
202+
continue
203+
}
204+
205+
var key string
206+
if ref, ok := n.Expr.(*ast.ColumnRef); ok {
207+
if str, ok := ref.Fields.Items[0].(*ast.String); ok {
208+
key = str.Str
209+
}
210+
}
211+
212+
number := 0
213+
if pr, ok := n.Left.(*ast.ParamRef); ok {
214+
number = pr.Number
215+
}
216+
217+
for _, table := range tables {
218+
schema := table.Schema
219+
if schema == "" {
220+
schema = c.DefaultSchema
221+
}
222+
223+
if c, ok := typeMap[schema][table.Name][key]; ok {
224+
a = append(a, Parameter{
225+
Number: number,
226+
Column: &Column{
227+
Name: parameterName(ref.ref.Number, key),
228+
DataType: dataType(&c.Type),
229+
NotNull: c.IsNotNull,
230+
IsArray: c.IsArray,
231+
Table: table,
232+
},
233+
})
234+
}
235+
}
236+
199237
case *ast.FuncCall:
200238
fun, err := c.ResolveFuncCall(n)
201239
if err != nil {

internal/endtoend/testdata/between_args/mysql/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/between_args/mysql/go/models.go

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

internal/endtoend/testdata/between_args/mysql/go/query.sql.go

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CREATE TABLE products (
2+
id BIGINT NOT NULL AUTO_INCREMENT,
3+
name TEXT NOT NULL,
4+
price INT NOT NULL,
5+
PRIMARY KEY (id)
6+
);
7+
8+
-- name: GetBetweenPrices :many
9+
SELECT *
10+
FROM products
11+
WHERE price BETWEEN ? AND ?;
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": "mysql",
7+
"name": "querytest",
8+
"schema": "query.sql",
9+
"queries": "query.sql"
10+
}
11+
]
12+
}

internal/engine/dolphin/convert.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,13 @@ func (c *cc) convertBeginStmt(n *pcast.BeginStmt) ast.Node {
638638
}
639639

640640
func (c *cc) convertBetweenExpr(n *pcast.BetweenExpr) ast.Node {
641-
return todo(n)
641+
return &ast.BetweenExpr{
642+
Expr: c.convert(n.Expr),
643+
Left: c.convert(n.Left),
644+
Right: c.convert(n.Right),
645+
Location: n.OriginTextPosition(),
646+
Not: n.Not,
647+
}
642648
}
643649

644650
func (c *cc) convertBinlogStmt(n *pcast.BinlogStmt) ast.Node {

internal/sql/ast/between_expr.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package ast
2+
3+
type BetweenExpr struct {
4+
// Expr is the value expression to be compared.
5+
Expr Node
6+
// Left is the left expression in the between statement.
7+
Left Node
8+
// Right is the right expression in the between statement.
9+
Right Node
10+
// Not is true, the expression is "not between".
11+
Not bool
12+
Location int
13+
}
14+
15+
func (n *BetweenExpr) Pos() int {
16+
return n.Location
17+
}

internal/sql/astutils/rewrite.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,9 @@ func (a *application) apply(parent ast.Node, name string, iter *iterator, n ast.
394394
a.apply(n, "Refexpr", nil, n.Refexpr)
395395
a.apply(n, "Refassgnexpr", nil, n.Refassgnexpr)
396396

397+
case *ast.BetweenExpr:
398+
// pass
399+
397400
case *ast.BitString:
398401
// pass
399402

internal/sql/astutils/walk.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,17 @@ func Walk(f Visitor, node ast.Node) {
478478
Walk(f, n.Refassgnexpr)
479479
}
480480

481+
case *ast.BetweenExpr:
482+
if n.Expr != nil {
483+
Walk(f, n.Expr)
484+
}
485+
if n.Left != nil {
486+
Walk(f, n.Left)
487+
}
488+
if n.Right != nil {
489+
Walk(f, n.Right)
490+
}
491+
481492
case *ast.BitString:
482493
// pass
483494

0 commit comments

Comments
 (0)