Permalink
Browse files

Make Dataset#exists return a LiteralString

  • Loading branch information...
1 parent ca11616 commit c075608be547ae60d0e70a3f76768ac9d7984164 @jeremyevans committed Dec 2, 2008
Showing with 24 additions and 2 deletions.
  1. +2 −0 CHANGELOG
  2. +2 −2 lib/sequel_core/dataset/sql.rb
  3. +20 −0 spec/sequel_core/dataset_spec.rb
View
@@ -1,5 +1,7 @@
=== HEAD
+* Make Dataset#exists return a LiteralString (jeremyevans)
+
* Support multiple SQL statements in one query in the MySQL adapter (jeremyevans)
* Add stored procedure support for the MySQL and JDBC adapters (jeremyevans, krsgoss) (#252)
@@ -123,12 +123,12 @@ def exclude(*cond, &block)
clone(clause => cond)
end
- # Returns an EXISTS clause for the dataset.
+ # Returns an EXISTS clause for the dataset as a LiteralString.
#
# DB.select(1).where(DB[:items].exists).sql
# #=> "SELECT 1 WHERE EXISTS (SELECT * FROM items)"
def exists(opts = nil)
- "EXISTS (#{select_sql(opts)})"
+ "EXISTS (#{select_sql(opts)})".lit
end
# Returns a copy of the dataset with the given conditions imposed upon it.
@@ -191,6 +191,26 @@ def v.values; {}; end
end
end
+context "Dataset#exists" do
+ setup do
+ @ds1 = Sequel::Dataset.new(nil).from(:test)
+ @ds2 = @ds1.filter(:price < 100)
+ @ds3 = @ds1.filter(:price > 50)
+ end
+
+ specify "should work in filters" do
+ @ds1.filter(@ds2.exists).sql.should ==
+ 'SELECT * FROM test WHERE (EXISTS (SELECT * FROM test WHERE (price < 100)))'
+ @ds1.filter(@ds2.exists & @ds3.exists).sql.should ==
+ 'SELECT * FROM test WHERE (EXISTS (SELECT * FROM test WHERE (price < 100)) AND EXISTS (SELECT * FROM test WHERE (price > 50)))'
+ end
+
+ specify "should work in select" do
+ @ds1.select(@ds2.exists.as(:a), @ds3.exists.as(:b)).sql.should ==
+ 'SELECT EXISTS (SELECT * FROM test WHERE (price < 100)) AS a, EXISTS (SELECT * FROM test WHERE (price > 50)) AS b FROM test'
+ end
+end
+
context "Dataset#where" do
setup do
@dataset = Sequel::Dataset.new(nil).from(:test)

0 comments on commit c075608

Please sign in to comment.