Permalink
Browse files

enhanced case expressions to allow specifying the expression to test …

…against.
  • Loading branch information...
1 parent 076b8c0 commit 3e5c15e950ca6e6edc6d9b5cede1998a03bde394 @jarredholman jarredholman committed with Oct 1, 2008
Showing with 18 additions and 7 deletions.
  1. +4 −4 lib/sequel_core/core_sql.rb
  2. +6 −1 lib/sequel_core/dataset/sql.rb
  3. +5 −2 lib/sequel_core/sql.rb
  4. +3 −0 spec/sequel_core/core_sql_spec.rb
@@ -7,8 +7,8 @@ def ~
# Return a Sequel::SQL::CaseExpression with this array as the conditions and the given
# default value.
- def case(default)
- ::Sequel::SQL::CaseExpression.new(self, default)
+ def case(default, expression = nil)
+ ::Sequel::SQL::CaseExpression.new(self, default, expression)
end
# Return a Sequel::SQL::BooleanExpression created from this array, matching all of the
@@ -87,8 +87,8 @@ def ~
# Return a Sequel::SQL::CaseExpression with this hash as the conditions and the given
# default value. Note that the order of the conditions will be arbitrary, so all
# conditions should be orthogonal.
- def case(default)
- ::Sequel::SQL::CaseExpression.new(to_a, default)
+ def case(default, expression = nil)
+ ::Sequel::SQL::CaseExpression.new(to_a, default, expression)
end
# Return a Sequel::SQL::BooleanExpression created from this hash, matching all of the
@@ -31,7 +31,12 @@ def aliased_expression_sql(ae)
# SQL fragment for specifying given CaseExpression.
def case_expression_sql(ce)
- "(CASE #{ce.conditions.collect{|c,r| "WHEN #{literal(c)} THEN #{literal(r)} "}.join}ELSE #{literal(ce.default)} END)"
+ sql = '(CASE '
+ sql << "#{literal(ce.expression)} " if ce.expression
+ ce.conditions.collect{ |c,r|
+ sql << "WHEN #{literal(c)} THEN #{literal(r)} "
+ }
+ sql << "ELSE #{literal(ce.default)} END)"
end
# SQL fragment for specifying all columns in a given table.
@@ -481,11 +481,14 @@ class CaseExpression < GenericExpression
# The default value if no conditions are true
attr_reader :default
+ # The expression to test the conditions against
+ attr_reader :expression
+
# Create an object with the given conditions and
# default value.
- def initialize(conditions, default)
+ def initialize(conditions, default, expression = nil)
raise(Sequel::Error, 'CaseExpression conditions must be an array with all_two_pairs') unless Array === conditions and conditions.all_two_pairs?
- @conditions, @default = conditions, default
+ @conditions, @default, @expression = conditions, default, expression
end
# Delegate the creation of the resulting SQL to the given dataset,
@@ -30,10 +30,13 @@
specify "should return SQL CASE expression" do
@d.literal({:x=>:y}.case(:z)).should == '(CASE WHEN x THEN y ELSE z END)'
+ @d.literal({:x=>:y}.case(:z, :exp)).should == '(CASE exp WHEN x THEN y ELSE z END)'
['(CASE WHEN x THEN y WHEN a THEN b ELSE z END)',
'(CASE WHEN a THEN b WHEN x THEN y ELSE z END)'].should(include(@d.literal({:x=>:y, :a=>:b}.case(:z))))
@d.literal([[:x, :y]].case(:z)).should == '(CASE WHEN x THEN y ELSE z END)'
@d.literal([[:x, :y], [:a, :b]].case(:z)).should == '(CASE WHEN x THEN y WHEN a THEN b ELSE z END)'
+ @d.literal([[:x, :y], [:a, :b]].case(:z, :exp)).should == '(CASE exp WHEN x THEN y WHEN a THEN b ELSE z END)'
+ @d.literal([[:x, :y], [:a, :b]].case(:z, :exp__w)).should == '(CASE exp.w WHEN x THEN y WHEN a THEN b ELSE z END)'
end
specify "should raise an error if an array that isn't all two pairs is used" do

0 comments on commit 3e5c15e

Please sign in to comment.