class Sequel::SQL::CaseExpression

Represents an SQL CASE expression, used for conditional branching in SQL.

Attributes

conditions[R]

An array of all two pairs with the first element specifying the condition and the second element specifying the result if the condition matches.

default[R]

The default value if no conditions match.

expression[R]

An optional expression to test the conditions against

Public Class Methods

new(conditions, default, expression=(no_expression=true; nil)) click to toggle source

Create an object with the given conditions and default value, and optional expression. An expression can be provided to test each condition against, instead of having all conditions represent their own boolean expression.

     # File lib/sequel/sql.rb
1204 def initialize(conditions, default, expression=(no_expression=true; nil))
1205   raise(Sequel::Error, 'CaseExpression conditions must be a hash or array of all two pairs') unless Sequel.condition_specifier?(conditions)
1206   @conditions = conditions.to_a.dup.freeze
1207   @default = default
1208   @expression = expression
1209   @no_expression = no_expression
1210   freeze
1211 end

Public Instance Methods

expression?() click to toggle source

Whether to use an expression for this CASE expression.

     # File lib/sequel/sql.rb
1214 def expression?
1215   !@no_expression
1216 end
with_merged_expression() click to toggle source

Merge the CASE expression into the conditions, useful for databases that don't support CASE expressions.

     # File lib/sequel/sql.rb
1220 def with_merged_expression
1221   if expression?
1222     e = expression
1223     CaseExpression.new(conditions.map{|c, r| [::Sequel::SQL::BooleanExpression.new(:'=', e, c), r]}, default)
1224   else
1225     self
1226   end
1227 end

Private Instance Methods

inspect_args() click to toggle source

CaseExpression's initializer checks whether an argument was provided, to differentiate CASE WHEN from CASE NULL WHEN, so check if an expression was provided, and only include the expression in the inspect output if so.

    # File lib/sequel/extensions/eval_inspect.rb
123 def inspect_args
124   if expression?
125     [:conditions, :default, :expression]
126   else
127     [:conditions, :default]
128   end
129 end