Class: ROM::SQL::Function
- Inherits:
-
Attribute
- Object
- Attribute
- ROM::SQL::Function
- Defined in:
- lib/rom/sql/function.rb
Overview
Specialized attribute type for defining SQL functions
Instance Method Summary collapse
-
#aliased(alias_name) ⇒ SQL::Function
(also: #as)
Return a new attribute with an alias.
-
#case(mapping) ⇒ ROM::SQL::Attribute
Add a CASE clause for handling if/then logic.
-
#cast(expr, db_type = TypeSerializer[:default].call(type)) ⇒ ROM::SQL::Attribute
Convert an expression result to another data type.
-
#filter(condition = Undefined) {|block| ... } ⇒ SQL::Function
Add a FILTER clause to aggregate function (supported by PostgreSQL 9.4+) Filter aggregate using the specified conditions.
- #is(other) ⇒ Object
- #not(other) ⇒ Object
-
#over(partition: nil, order: nil, frame: nil) ⇒ SQL::Function
Add an OVER clause making a window function call.
-
#within_group(*args) {|block| ... } ⇒ SQL::Function
Add a WITHIN GROUP clause to aggregate function (supported by PostgreSQL) Establishes an order for an ordered-set aggregate, see the docs for more details.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args) ⇒ Object (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/rom/sql/function.rb', line 243 def method_missing(meth, *args) if func if func.respond_to?(meth) (func: func.__send__(meth, *args)) else super end else (func: Sequel::SQL::Function.new(meth.to_s.upcase, *args)) end end |
Instance Method Details
#aliased(alias_name) ⇒ SQL::Function Also known as: as
Return a new attribute with an alias
53 54 55 |
# File 'lib/rom/sql/function.rb', line 53 def aliased(alias_name) super.with(name: name || alias_name) end |
#case(mapping) ⇒ ROM::SQL::Attribute
Add a CASE clause for handling if/then logic. This version of CASE search for the first
branch which evaluates to true
. See SQL::Attriubte#case if you're looking for the
version that matches an expression result
171 172 173 174 175 176 177 178 |
# File 'lib/rom/sql/function.rb', line 171 def case(mapping) mapping = mapping.dup otherwise = mapping.delete(:else) do raise ArgumentError, 'provide the default case using the :else keyword' end Attribute[type].(sql_expr: ::Sequel.case(mapping, otherwise)) end |
#cast(expr, db_type = TypeSerializer[:default].call(type)) ⇒ ROM::SQL::Attribute
Convert an expression result to another data type
156 157 158 |
# File 'lib/rom/sql/function.rb', line 156 def cast(expr, db_type = TypeSerializer[:default].call(type)) Attribute[type].(sql_expr: ::Sequel.cast(expr, db_type)) end |
#filter(condition = Undefined) {|block| ... } ⇒ SQL::Function
Add a FILTER clause to aggregate function (supported by PostgreSQL 9.4+) Filter aggregate using the specified conditions
195 196 197 198 199 200 201 202 203 204 |
# File 'lib/rom/sql/function.rb', line 195 def filter(condition = Undefined, &block) if block conditions = schema.restriction(&block) conditions = conditions & condition unless condition.equal?(Undefined) else conditions = condition end super(conditions) end |
#is(other) ⇒ Object
91 92 93 94 95 |
# File 'lib/rom/sql/function.rb', line 91 def is(other) ::ROM::SQL::Attribute[::ROM::SQL::Types::Bool].( sql_expr: ::Sequel::SQL::BooleanExpression.new(:'=', func, other) ) end |
#not(other) ⇒ Object
100 101 102 |
# File 'lib/rom/sql/function.rb', line 100 def not(other) !is(other) end |
#over(partition: nil, order: nil, frame: nil) ⇒ SQL::Function
Add an OVER clause making a window function call
140 141 142 |
# File 'lib/rom/sql/function.rb', line 140 def over(partition: nil, order: nil, frame: nil) super(partition: partition, order: order, frame: WINDOW_FRAMES[frame]) end |
#within_group(*args) {|block| ... } ⇒ SQL::Function
Add a WITHIN GROUP clause to aggregate function (supported by PostgreSQL) Establishes an order for an ordered-set aggregate, see the docs for more details
220 221 222 223 224 225 226 227 228 |
# File 'lib/rom/sql/function.rb', line 220 def within_group(*args, &block) if block group = args + ::ROM::SQL::OrderDSL.new(schema).(&block) else group = args end super(*group) end |