Class: Sequel::SQL::CaseExpression
- Inherits:
-
GenericExpression
- Object
- Expression
- GenericExpression
- Sequel::SQL::CaseExpression
- 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
-
#conditions ⇒ Object
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.
-
#default ⇒ Object
readonly
The default value if no conditions match.
-
#expression ⇒ Object
readonly
An optional expression to test the conditions against.
Instance Method Summary collapse
-
#expression? ⇒ Boolean
Whether to use an expression for this CASE expression.
-
#initialize(conditions, default, expression = (no_expression=true; nil)) ⇒ CaseExpression
constructor
Create an object with the given conditions and default value, and optional expression.
-
#with_merged_expression ⇒ Object
Merge the CASE expression into the conditions, useful for databases that don’t support CASE expressions.
Methods included from Postgres::HStoreOpMethods
Methods included from Postgres::RangeOpMethods
Methods included from Postgres::ArrayOpMethods
Methods included from Postgres::JSONOpMethods
Methods included from Postgres::InetOpMethods
Methods included from Postgres::PGRowOp::ExpressionMethods
Methods included from SubscriptMethods
Methods included from StringMethods
#escaped_ilike, #escaped_like, #ilike, #like
Methods included from PatternMatchMethods
Methods included from OrderMethods
Methods included from NumericMethods
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
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.
1190 1191 1192 1193 1194 1195 1196 1197 |
# File 'lib/sequel/sql.rb', line 1190 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
#conditions ⇒ Object (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.
1178 1179 1180 |
# File 'lib/sequel/sql.rb', line 1178 def conditions @conditions end |
#default ⇒ Object (readonly)
The default value if no conditions match.
1181 1182 1183 |
# File 'lib/sequel/sql.rb', line 1181 def default @default end |
#expression ⇒ Object (readonly)
An optional expression to test the conditions against
1184 1185 1186 |
# File 'lib/sequel/sql.rb', line 1184 def expression @expression end |
Instance Method Details
#expression? ⇒ Boolean
Whether to use an expression for this CASE expression.
1200 1201 1202 |
# File 'lib/sequel/sql.rb', line 1200 def expression? !@no_expression end |
#with_merged_expression ⇒ Object
Merge the CASE expression into the conditions, useful for databases that don’t support CASE expressions.
1206 1207 1208 1209 1210 1211 1212 1213 |
# File 'lib/sequel/sql.rb', line 1206 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 |