class Sequel::SQL::CaseExpression

  1. lib/sequel/sql.rb
Superclass: GenericExpression

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

Methods

Public Class

  1. new

Public Instance

  1. conditions
  2. default
  3. expression
  4. expression?
  5. with_merged_expression

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))

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.

[show source]
     # File lib/sequel/sql.rb
1213 def initialize(conditions, default, expression=(no_expression=true; nil))
1214   raise(Sequel::Error, 'CaseExpression conditions must be a hash or array of all two pairs') unless Sequel.condition_specifier?(conditions)
1215   @conditions = conditions.to_a.dup.freeze
1216   @default = default
1217   @expression = expression
1218   @no_expression = no_expression
1219   freeze
1220 end

Public Instance methods

expression?()

Whether to use an expression for this CASE expression.

[show source]
     # File lib/sequel/sql.rb
1223 def expression?
1224   !@no_expression
1225 end
with_merged_expression()

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

[show source]
     # File lib/sequel/sql.rb
1229 def with_merged_expression
1230   if expression?
1231     e = expression
1232     CaseExpression.new(conditions.map{|c, r| [::Sequel::SQL::BooleanExpression.new(:'=', e, c), r]}, default)
1233   else
1234     self
1235   end
1236 end