Class: Sequel::SQL::CaseExpression

Inherits:
GenericExpression show all
Defined in:
lib/sequel/sql.rb,
lib/sequel/extensions/eval_inspect.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from IsDistinctFrom::Methods

#is_distinct_from

Methods included from Sequel::SQLite::JSONOpMethods

#sqlite_json_op, #sqlite_jsonb_op

Methods included from Postgres::HStoreOpMethods

#hstore

Methods included from Postgres::RangeOpMethods

#pg_range

Methods included from Postgres::ArrayOpMethods

#pg_array

Methods included from Postgres::JSONOpMethods

#pg_json, #pg_jsonb

Methods included from Postgres::InetOpMethods

#pg_inet

Methods included from Postgres::PGRowOp::ExpressionMethods

#pg_row

Methods included from SubscriptMethods

#sql_subscript

Methods included from StringMethods

#escaped_ilike, #escaped_like, #ilike, #like

Methods included from PatternMatchMethods

#!~, #=~

Methods included from OrderMethods

#asc, #desc

Methods included from NumericMethods

#+, #coerce

Methods included from ComplexExpressionMethods

#extract, #sql_boolean, #sql_number, #sql_string

Methods included from CastMethods

#cast, #cast_numeric, #cast_string

Methods included from BooleanMethods

#~

Methods included from AliasMethods

#as

Methods inherited from Expression

#==, attr_reader, #clone, #eql?, #hash, inherited, #inspect

Constructor Details

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

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.

Raises:



1207
1208
1209
1210
1211
1212
1213
1214
# File 'lib/sequel/sql.rb', line 1207

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 = conditions.to_a.dup.freeze
  @default = default
  @expression = expression
  @no_expression = no_expression
  freeze
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 if the condition matches.



1195
1196
1197
# File 'lib/sequel/sql.rb', line 1195

def conditions
  @conditions
end

#defaultObject (readonly)

The default value if no conditions match.



1198
1199
1200
# File 'lib/sequel/sql.rb', line 1198

def default
  @default
end

#expressionObject (readonly)

An optional expression to test the conditions against



1201
1202
1203
# File 'lib/sequel/sql.rb', line 1201

def expression
  @expression
end

Instance Method Details

#expression?Boolean

Whether to use an expression for this CASE expression.

Returns:

  • (Boolean)


1217
1218
1219
# File 'lib/sequel/sql.rb', line 1217

def expression?
  !@no_expression
end

#with_merged_expressionObject

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



1223
1224
1225
1226
1227
1228
1229
1230
# File 'lib/sequel/sql.rb', line 1223

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