Permalink
Browse files

Allow specifying dataset alias via :alias option when using union/int…

…ersect/except
  • Loading branch information...
1 parent 13d2a4a commit ae8ed72eb03aa221565d2bd687f1213a8587b5bc @jeremyevans committed Nov 19, 2009
Showing with 14 additions and 3 deletions.
  1. +2 −0 CHANGELOG
  2. +3 −3 lib/sequel/dataset/query.rb
  3. +9 −0 spec/core/dataset_spec.rb
View
@@ -1,5 +1,7 @@
=== HEAD
+* Allow specifying the dataset alias via :alias option when using union/intersect/except (jeremyevans)
+
* Allow Model#destroy to take an options hash and respect a :transaction option (john_firebaugh)
* If a transaction is being used, raise_on_save_failure is false, and a before hook returns false, rollback the transaction (john_firebaugh, jeremyevans)
@@ -144,8 +144,8 @@ def from(*source)
#
# ds = DB[:items].order(:name).select(:id, :name)
# ds.sql #=> "SELECT id,name FROM items ORDER BY name"
- # ds.from_self.sql #=> "SELECT * FROM (SELECT id, name FROM items ORDER BY name) AS 't1'"
- # ds.from_self(:alias=>:foo).sql #=> "SELECT * FROM (SELECT id, name FROM items ORDER BY name) AS 'foo'"
+ # ds.from_self.sql #=> "SELECT * FROM (SELECT id, name FROM items ORDER BY name) AS t1"
+ # ds.from_self(:alias=>:foo).sql #=> "SELECT * FROM (SELECT id, name FROM items ORDER BY name) AS foo"
def from_self(opts={})
fs = {}
@opts.keys.each{|k| fs[k] = nil unless FROM_SELF_KEEP_OPTS.include?(k)}
@@ -370,7 +370,7 @@ def _filter(clause, *cond, &block)
# Add the dataset to the list of compounds
def compound_clone(type, dataset, opts)
ds = compound_from_self.clone(:compounds=>Array(@opts[:compounds]).map{|x| x.dup} + [[type, dataset.compound_from_self, opts[:all]]])
- opts[:from_self] == false ? ds : ds.from_self
+ opts[:from_self] == false ? ds : ds.from_self(opts)
end
# SQL fragment based on the expr type. See #filter.
@@ -2095,6 +2095,15 @@ def each(&block)
"SELECT * FROM (SELECT * FROM b WHERE (z = 2) EXCEPT ALL SELECT * FROM a WHERE (z = 1)) AS t1"
end
+ specify "should support :alias option for specifying identifier" do
+ @a.union(@b, :alias=>:xx).sql.should == \
+ "SELECT * FROM (SELECT * FROM a WHERE (z = 1) UNION SELECT * FROM b WHERE (z = 2)) AS xx"
+ @a.intersect(@b, :alias=>:xx).sql.should == \
+ "SELECT * FROM (SELECT * FROM a WHERE (z = 1) INTERSECT SELECT * FROM b WHERE (z = 2)) AS xx"
+ @a.except(@b, :alias=>:xx).sql.should == \
+ "SELECT * FROM (SELECT * FROM a WHERE (z = 1) EXCEPT SELECT * FROM b WHERE (z = 2)) AS xx"
+ end
+
specify "should support :from_self=>false option to not wrap the compound in a SELECT * FROM (...)" do
@b.union(@a, :from_self=>false).sql.should == \
"SELECT * FROM b WHERE (z = 2) UNION SELECT * FROM a WHERE (z = 1)"

0 comments on commit ae8ed72

Please sign in to comment.