Skip to content

Commit 4bbbb5c

Browse files
committed
Add Where#merge
1 parent 62f93bc commit 4bbbb5c

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

lib/sql/composer/nodes/where.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ def append(hash = {}, &block)
3636
with(operations: operations + new_operations)
3737
end
3838

39+
def merge(other)
40+
with(operations: operations + other.operations)
41+
end
42+
3943
def to_ast
4044
[:where, operations.map(&:to_ast)]
4145
end

lib/sql/composer/statement.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ def each(&block)
2020
def append(type, *args, &block)
2121
new_dsl =
2222
if block
23-
dsl.new(&block)
23+
dsl.new(ast: [], &block)
2424
else
25-
dsl.new.__send__(type, *args)
25+
dsl.new(ast: []).__send__(type, *args)
2626
end
2727

2828
left = self

spec/integration/sql/compose_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,22 @@
442442
end
443443
end
444444

445+
context "with existing :where" do
446+
let(:query) do
447+
source_query.append(:where, name: "Jane").append(:where, id: 1)
448+
end
449+
450+
specify do
451+
expect(result).to eql(
452+
<<~SQL.strip.gsub("\n", " ")
453+
SELECT id, name
454+
FROM users
455+
WHERE name = 'Jane' AND id = 1
456+
SQL
457+
)
458+
end
459+
end
460+
445461
context "with a block" do
446462
let(:query) do
447463
source_query.append(:order) { WHERE(`name` == "Jane") }

0 commit comments

Comments
 (0)