Repository navigation
Fix placeholder bugs by refactoring to make them impossible #7
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
jgaskins
merged 10 commits into
jgaskins:master
from
concentric-health:fix-placeholder-bugs
Oct 7, 2026
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b4fe85b
Add missing spec coverage
mloughran efa9720
Refactor subquery embedding onto Subquery
mloughran 082a437
Fix renumbering of multi-digit placeholders
mloughran 49d3591
Fix binding of LIMIT & OFFSET args in iterator and compound query
mloughran c82f7c1
Fix to avoid binding ORDER BY values in any?/none? queries
mloughran 0f3d100
Refactor to key OrderBy Hash by QueryExpression
mloughran 3e448c8
Fix remaining placeholder bugs by refactoring to make them impossible
mloughran ffa14e2
Refactor to remove placeholder numbering from QueryValue internals
mloughran c04a011
Fix renumbering of $n inside string literals
mloughran 75930a3
Refactor to avoid need to wrap values for QueryExpression.parse
mloughran File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,15 +5,67 @@ require "../src/query_expression" | |
| module Interro | ||
| describe QueryExpression do | ||
| it "generates an AND conjunction of two expressions" do | ||
| lhs = QueryExpression.new("foo", ">", "$1", [Any.new(69)]) | ||
| rhs = QueryExpression.new("bar", "=", "$2", [Any.new(420)]) | ||
| lhs = QueryExpression.parse("foo > $1", [69]) | ||
| rhs = QueryExpression.parse("bar = $1", [420]) | ||
| (lhs & rhs).to_sql.should eq "(foo > $1) AND (bar = $2)" | ||
| end | ||
|
|
||
| it "generates an OR conjunction of two expressions" do | ||
| lhs = QueryExpression.new("foo", ">", "$1", [Any.new(69)]) | ||
| rhs = QueryExpression.new("bar", "=", "$2", [Any.new(420)]) | ||
| lhs = QueryExpression.parse("foo > $1", [69]) | ||
| rhs = QueryExpression.parse("bar = $1", [420]) | ||
| (lhs | rhs).to_sql.should eq "(foo > $1) OR (bar = $2)" | ||
| end | ||
|
|
||
| describe ".parse" do | ||
| it "binds a repeated placeholder once per reference" do | ||
| expression = QueryExpression.parse("a = $1 OR b = $1", [1]) | ||
|
|
||
| expression.to_sql.should eq "a = $1 OR b = $2" | ||
| expression.values.should eq [Any.new(1), Any.new(1)] | ||
| end | ||
|
|
||
| it "binds values in reference order when placeholders appear out of order" do | ||
| expression = QueryExpression.parse("b = $2 AND a = $1", [1, 2]) | ||
|
|
||
| expression.to_sql.should eq "b = $1 AND a = $2" | ||
| expression.values.should eq [Any.new(2), Any.new(1)] | ||
| end | ||
|
|
||
| it "raises when a placeholder references a missing value" do | ||
| expect_raises ArgumentError, "references $2" do | ||
| QueryExpression.parse("a = $2", [1]) | ||
| end | ||
| end | ||
|
|
||
| it "rejects $0" do | ||
| expect_raises ArgumentError, "references $0" do | ||
| QueryExpression.parse("a = $0", [1]) | ||
| end | ||
| end | ||
|
|
||
| it "leaves $n inside string literals alone, including '' escapes" do | ||
| expression = QueryExpression.parse("note = 'it''s $1' AND id = $1", [1]) | ||
|
|
||
| expression.to_sql.should eq "note = 'it''s $1' AND id = $1" | ||
| expression.values.should eq [Any.new(1)] | ||
| end | ||
| end | ||
|
|
||
| describe "#to_sql" do | ||
| it "continues numbering across fragments rendered into one args array" do | ||
| first = QueryExpression.parse("a = $1", [1]) | ||
| second = QueryExpression.parse("b = $1", [2]) | ||
| args = [] of Any | ||
|
|
||
| sql = String.build do |str| | ||
| first.to_sql str, args | ||
| str << " AND " | ||
| second.to_sql str, args | ||
| end | ||
|
|
||
| sql.should eq "a = $1 AND b = $2" | ||
| args.should eq [Any.new(1), Any.new(2)] | ||
| end | ||
|
Comment on lines
+55
to
+68
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feels like pure sorcery even though it's quite simple. Unbelievable that this works. |
||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.