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]
The 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. 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, line 1123 def initialize(conditions, default, expression=(no_expression=true; nil)) raise(Sequel::Error, 'CaseExpression conditions must be a hash or array of all two pairs') unless Sequel.condition_specifier?(conditions) @conditions, @default, @expression, @no_expression = conditions.to_a, default, expression, no_expression end
Public Instance Methods
expression?()
click to toggle source
Whether to use an expression for this CASE expression.
# File lib/sequel/sql.rb, line 1129 def expression? !@no_expression 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, line 1135 def with_merged_expression if expression? e = expression CaseExpression.new(conditions.map{|c, r| [::Sequel::SQL::BooleanExpression.new(:'=', e, c), r]}, default) else self end end