From b04a4ab70426ad246b545ed54b776f2aaff7fde0 Mon Sep 17 00:00:00 2001 From: Jille Timmermans Date: Sat, 24 Jun 2023 23:42:10 +0200 Subject: [PATCH] Add support for BETWEEN sqlc.arg(min) AND sqlc.arg(max) issue #2277 --- .../between_args/mysql/go/query.sql.go | 34 +++++++++++++++++++ .../testdata/between_args/mysql/query.sql | 5 +++ internal/sql/astutils/rewrite.go | 4 ++- 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/internal/endtoend/testdata/between_args/mysql/go/query.sql.go b/internal/endtoend/testdata/between_args/mysql/go/query.sql.go index a4c2221847..99491ba4b8 100644 --- a/internal/endtoend/testdata/between_args/mysql/go/query.sql.go +++ b/internal/endtoend/testdata/between_args/mysql/go/query.sql.go @@ -43,6 +43,40 @@ func (q *Queries) GetBetweenPrices(ctx context.Context, arg GetBetweenPricesPara return items, nil } +const getBetweenPricesNamed = `-- name: GetBetweenPricesNamed :many +SELECT id, name, price +FROM products +WHERE price BETWEEN ? AND ? +` + +type GetBetweenPricesNamedParams struct { + MinPrice int32 + MaxPrice int32 +} + +func (q *Queries) GetBetweenPricesNamed(ctx context.Context, arg GetBetweenPricesNamedParams) ([]Product, error) { + rows, err := q.db.QueryContext(ctx, getBetweenPricesNamed, arg.MinPrice, arg.MaxPrice) + if err != nil { + return nil, err + } + defer rows.Close() + var items []Product + for rows.Next() { + var i Product + if err := rows.Scan(&i.ID, &i.Name, &i.Price); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const getBetweenPricesTable = `-- name: GetBetweenPricesTable :many SELECT id, name, price FROM products diff --git a/internal/endtoend/testdata/between_args/mysql/query.sql b/internal/endtoend/testdata/between_args/mysql/query.sql index 1e38765622..7458e0ecb5 100644 --- a/internal/endtoend/testdata/between_args/mysql/query.sql +++ b/internal/endtoend/testdata/between_args/mysql/query.sql @@ -19,3 +19,8 @@ WHERE products.price BETWEEN ? AND ?; SELECT * FROM products as p WHERE p.price BETWEEN ? AND ?; + +-- name: GetBetweenPricesNamed :many +SELECT * +FROM products +WHERE price BETWEEN sqlc.arg(min_price) AND sqlc.arg(max_price); diff --git a/internal/sql/astutils/rewrite.go b/internal/sql/astutils/rewrite.go index 221e62a804..6d30ed33c4 100644 --- a/internal/sql/astutils/rewrite.go +++ b/internal/sql/astutils/rewrite.go @@ -400,7 +400,9 @@ func (a *application) apply(parent ast.Node, name string, iter *iterator, n ast. a.apply(n, "Refassgnexpr", nil, n.Refassgnexpr) case *ast.BetweenExpr: - // pass + a.apply(n, "Expr", nil, n.Expr) + a.apply(n, "Left", nil, n.Left) + a.apply(n, "Right", nil, n.Right) case *ast.BitString: // pass