Class Sequel::SQL::CaseExpression
In: lib/sequel/sql.rb
lib/sequel/extensions/eval_inspect.rb
Parent: GenericExpression

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

Methods

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

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.

[Source]

      # File lib/sequel/sql.rb, line 1163
1163:       def initialize(conditions, default, expression=(no_expression=true; nil))
1164:         raise(Sequel::Error, 'CaseExpression conditions must be a hash or array of all two pairs') unless Sequel.condition_specifier?(conditions)
1165:         @conditions, @default, @expression, @no_expression = conditions.to_a, default, expression, no_expression
1166:       end

Public Instance methods

Whether to use an expression for this CASE expression.

[Source]

      # File lib/sequel/sql.rb, line 1169
1169:       def expression?
1170:         !@no_expression
1171:       end

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

[Source]

      # File lib/sequel/sql.rb, line 1175
1175:       def with_merged_expression
1176:         if expression?
1177:           e = expression
1178:           CaseExpression.new(conditions.map{|c, r| [::Sequel::SQL::BooleanExpression.new('=''=', e, c), r]}, default)
1179:         else
1180:           self
1181:         end
1182:       end

[Validate]