Skip to content

Commit

Permalink
Merge pull request rails#289 from mekishizufu/fix_with_statements
Browse files Browse the repository at this point in the history
Fix WITH statements
  • Loading branch information
rafaelfranca committed Jun 13, 2014
2 parents 22136de + b4bec04 commit 282bb10
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/arel/visitors/to_sql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,9 @@ def visit_Arel_Nodes_DistinctOn o, collector
raise NotImplementedError, 'DISTINCT ON not implemented for this db'
end

def visit_Arel_Nodes_With o
"WITH #{o.children.map { |x| visit x }.join(', ')}"
def visit_Arel_Nodes_With o, collector
collector << "WITH "
inject_join o.children, collector, ', '
end

def visit_Arel_Nodes_WithRecursive o, collector
Expand Down
14 changes: 14 additions & 0 deletions test/test_select_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,20 @@ def test_manager_stores_bind_values
end

describe 'with' do
it 'should support basic WITH' do
users = Table.new(:users)
users_top = Table.new(:users_top)
comments = Table.new(:comments)

top = users.project(users[:id]).where(users[:karma].gt(100))
users_as = Arel::Nodes::As.new(users_top, top)
select_manager = comments.project(Arel.star).with(users_as)
.where(comments[:author_id].in(users_top.project(users_top[:id])))

select_manager.to_sql.must_be_like %{
WITH "users_top" AS (SELECT "users"."id" FROM "users" WHERE "users"."karma" > 100) SELECT * FROM "comments" WHERE "comments"."author_id" IN (SELECT "users_top"."id" FROM "users_top")
}
end

it "should support WITH RECURSIVE" do
comments = Table.new(:comments)
Expand Down

0 comments on commit 282bb10

Please sign in to comment.