Class: Sequel::SQL::CaseExpression

Inherits:
GenericExpression show all
Defined in:
lib/sequel_core/sql.rb

Overview

Represents an SQL CASE expression, used for conditions.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ComplexExpressionMethods

#extract, #sql_boolean, #sql_number, #sql_string

Methods included from StringMethods

#ilike, #like

Methods included from BooleanMethods

#~

Methods included from OrderMethods

#asc, #desc

Methods included from CastMethods

#cast, #cast_numeric, #cast_string

Methods included from AliasMethods

#as

Methods inherited from Expression

#lit

Constructor Details

#initialize(conditions, default, expression = nil) ⇒ CaseExpression

Create an object with the given conditions and default value.

Raises:



489
490
491
492
# File 'lib/sequel_core/sql.rb', line 489

def initialize(conditions, default, expression = nil)
  raise(Sequel::Error, 'CaseExpression conditions must be an array with all_two_pairs') unless Array === conditions and conditions.all_two_pairs?
  @conditions, @default, @expression = conditions, default, expression
end

Instance Attribute Details

#conditionsObject (readonly)

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



479
480
481
# File 'lib/sequel_core/sql.rb', line 479

def conditions
  @conditions
end

#defaultObject (readonly)

The default value if no conditions are true



482
483
484
# File 'lib/sequel_core/sql.rb', line 482

def default
  @default
end

#expressionObject (readonly)

The expression to test the conditions against



485
486
487
# File 'lib/sequel_core/sql.rb', line 485

def expression
  @expression
end

Instance Method Details

#to_s(ds) ⇒ Object

Delegate the creation of the resulting SQL to the given dataset, since it may be database dependent.



496
497
498
# File 'lib/sequel_core/sql.rb', line 496

def to_s(ds)
  ds.case_expression_sql(self)
end