Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

arel more nicely supports EXISTS queries

  • Loading branch information...
commit dbd0140974ed768705f3680d5d6e47a56305b965 1 parent 22a38c6
@tenderlove tenderlove authored
View
8 lib/arel/nodes/exists.rb
@@ -1,11 +1,7 @@
module Arel
module Nodes
- class Exists
- attr_reader :select_stmt
-
- def initialize select_stmt
- @select_stmt = select_stmt
- end
+ class Exists < Arel::Nodes::Function
+ alias :select_stmt :expressions
end
end
end
View
6 lib/arel/select_manager.rb
@@ -22,6 +22,12 @@ def skip amount
self
end
+ ###
+ # Produces an Arel::Nodes::Exists node
+ def exists
+ Arel::Nodes::Exists.new @ast
+ end
+
def where_clauses
#warn "where_clauses is deprecated" if $VERBOSE
to_sql = Visitors::ToSql.new @engine
View
2  lib/arel/tree_manager.rb
@@ -4,7 +4,7 @@ class TreeManager
include Arel::Relation
attr_accessor :visitor
- attr_reader :ast
+ attr_reader :ast, :engine
def initialize engine
@engine = engine
View
3  lib/arel/visitors/to_sql.rb
@@ -62,7 +62,8 @@ def visit_Arel_Nodes_InsertStatement o
end
def visit_Arel_Nodes_Exists o
- "EXISTS (#{visit o.select_stmt})"
+ "EXISTS (#{visit o.select_stmt})#{
+ o.alias ? " AS #{visit o.alias}" : ''}"
end
def visit_Arel_Nodes_Values o
View
28 test/test_select_manager.rb
@@ -56,9 +56,7 @@ def execute sql, name = nil, *args
manager.project SqlLiteral.new '*'
manager.from table
manager.order :foo
- manager.to_sql.must_be_like %{
- SELECT * FROM "users" ORDER BY foo
- }
+ manager.to_sql.must_be_like %{ SELECT * FROM "users" ORDER BY foo }
end
end
@@ -68,9 +66,7 @@ def execute sql, name = nil, *args
manager = Arel::SelectManager.new Table.engine
manager.from table
manager.group :foo
- manager.to_sql.must_be_like %{
- SELECT FROM "users" GROUP BY foo
- }
+ manager.to_sql.must_be_like %{ SELECT FROM "users" GROUP BY foo }
end
end
@@ -130,6 +126,26 @@ def execute sql, name = nil, *args
end
end
+ describe 'exists' do
+ it 'should create an exists clause' do
+ table = Table.new(:users)
+ manager = Arel::SelectManager.new Table.engine, table
+ manager.project SqlLiteral.new '*'
+ m2 = Arel::SelectManager.new(manager.engine)
+ m2.project manager.exists
+ m2.to_sql.must_be_like %{ SELECT EXISTS (#{manager.to_sql}) }
+ end
+
+ it 'can be aliased' do
+ table = Table.new(:users)
+ manager = Arel::SelectManager.new Table.engine, table
+ manager.project SqlLiteral.new '*'
+ m2 = Arel::SelectManager.new(manager.engine)
+ m2.project manager.exists.as('foo')
+ m2.to_sql.must_be_like %{ SELECT EXISTS (#{manager.to_sql}) AS foo }
+ end
+ end
+
describe 'ast' do
it 'should return the ast' do
table = Table.new :users

1 comment on commit dbd0140

@rikur

This breaks sqlite for me when used with rails edge:

.rvm/gems/ruby-1.9.2-p0@rails3/bundler/gems/arel-dbd0140974ed/lib/arel/visitors/visitor.rb:15:in visit': undefined methodvisit_ActiveSupport_StringInquirer' for #Arel::Visitors::SQLite:0x0000010439e108 (NoMethodError)

Please sign in to comment.
Something went wrong with that request. Please try again.