class Sequel::SQL::CaseExpression
Represents an SQL CASE expression, used for conditional branching in SQL.
Attributes
An array of all two pairs with the first element specifying the condition and the second element specifying the result if the condition matches.
The default value if no conditions match.
An optional expression to test the conditions against
Public Class Methods
Source
# File lib/sequel/sql.rb 1217 def initialize(conditions, default, expression=(no_expression=true; nil)) 1218 raise(Sequel::Error, 'CaseExpression conditions must be a hash or array of all two pairs') unless Sequel.condition_specifier?(conditions) 1219 @conditions = conditions.to_a.dup.freeze 1220 @default = default 1221 @expression = expression 1222 @no_expression = no_expression 1223 freeze 1224 end
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.
Public Instance Methods
Source
# File lib/sequel/sql.rb 1227 def expression? 1228 !@no_expression 1229 end
Whether to use an expression for this CASE expression.
Source
# File lib/sequel/sql.rb 1233 def with_merged_expression 1234 if expression? 1235 e = expression 1236 CaseExpression.new(conditions.map{|c, r| [::Sequel::SQL::BooleanExpression.new(:'=', e, c), r]}, default) 1237 else 1238 self 1239 end 1240 end
Merge the CASE expression into the conditions, useful for databases that don’t support CASE expressions.
Private Instance Methods
Source
# File lib/sequel/extensions/eval_inspect.rb 127 def inspect_args 128 if expression? 129 [:conditions, :default, :expression] 130 else 131 [:conditions, :default] 132 end 133 end
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.