Class: ROM::SQL::Function
- Inherits:
-
Attribute
- Object
- Attribute
- ROM::SQL::Function
- Includes:
- AttributeWrapping
- Defined in:
- lib/rom/sql/function.rb
Overview
Specialized attribute type for defining SQL functions
Constant Summary collapse
- WINDOW_FRAMES =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Hash.new do |cache, frame| type = frame.key?(:rows) ? 'ROWS' : 'RANGE' bounds = frame[:rows] || frame[:range] cache[frame] = "#{type} BETWEEN #{frame_limit(bounds[0])} AND #{frame_limit(bounds[1])}" end
Class Method Summary collapse
- .frame_limit(value) ⇒ Object private
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
- #name ⇒ Object private
- #new ⇒ Object private
- #not(other) ⇒ Object
-
#over(partition: nil, order: nil, frame: nil) ⇒ SQL::Function
Add an OVER clause making a window function call.
- #qualified(table_alias = nil) ⇒ Object private
- #qualified?(_table_alias = nil) ⇒ Boolean private
- #qualified_projection(table_alias = nil) ⇒ Object private
- #sql_literal(ds) ⇒ Object private
-
#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.
Methods included from AttributeWrapping
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.
281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/rom/sql/function.rb', line 281 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 |
Class Method Details
.frame_limit(value) ⇒ Object
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.
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/rom/sql/function.rb', line 16 def frame_limit(value) case value when :current then 'CURRENT ROW' when :start then 'UNBOUNDED PRECEDING' when :end then 'UNBOUNDED FOLLOWING' else if value.positive? "#{value} FOLLOWING" else "#{value.abs} PRECEDING" end end end |
Instance Method Details
#aliased(alias_name) ⇒ SQL::Function Also known as: as
Return a new attribute with an alias
54 55 56 |
# File 'lib/rom/sql/function.rb', line 54 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
200 201 202 203 204 205 206 207 |
# File 'lib/rom/sql/function.rb', line 200 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
185 186 187 |
# File 'lib/rom/sql/function.rb', line 185 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
224 225 226 227 228 229 230 231 232 233 |
# File 'lib/rom/sql/function.rb', line 224 def filter(condition = Undefined, &) if block_given? conditions = schema.restriction(&) conditions &= condition unless condition.equal?(Undefined) else conditions = condition end super(conditions) end |
#is(other) ⇒ Object
111 112 113 114 115 |
# File 'lib/rom/sql/function.rb', line 111 def is(other) ::ROM::SQL::Attribute[::ROM::SQL::Types::Bool].( sql_expr: ::Sequel::SQL::BooleanExpression.new(:'=', func, other) ) end |
#name ⇒ Object
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.
69 70 71 |
# File 'lib/rom/sql/function.rb', line 69 def name self.alias || super end |
#new ⇒ Object
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.
92 93 94 95 96 97 98 99 |
# File 'lib/rom/sql/function.rb', line 92 def new(&) case func when ::Sequel::SQL::Function (func: ::Sequel::SQL::Function.new!(func.name, func.args.map(&), func.opts)) else (func: func) end end |
#not(other) ⇒ Object
120 121 122 |
# File 'lib/rom/sql/function.rb', line 120 def not(other) !is(other) end |
#over(partition: nil, order: nil, frame: nil) ⇒ SQL::Function
Add an OVER clause making a window function call
168 169 170 |
# File 'lib/rom/sql/function.rb', line 168 def over(partition: nil, order: nil, frame: nil) super(partition: partition, order: order, frame: WINDOW_FRAMES[frame]) end |
#qualified(table_alias = nil) ⇒ Object
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.
76 77 78 79 80 |
# File 'lib/rom/sql/function.rb', line 76 def qualified(table_alias = nil) new { |arg| arg.respond_to?(:qualified) ? arg.qualified(table_alias) : arg } end |
#qualified?(_table_alias = nil) ⇒ Boolean
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.
104 105 106 |
# File 'lib/rom/sql/function.rb', line 104 def qualified?(_table_alias = nil) [:func].args.all?(&:qualified?) end |
#qualified_projection(table_alias = nil) ⇒ Object
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.
85 86 87 88 89 |
# File 'lib/rom/sql/function.rb', line 85 def qualified_projection(table_alias = nil) new { |arg| arg.respond_to?(:qualified_projection) ? arg.qualified_projection(table_alias) : arg } end |
#sql_literal(ds) ⇒ Object
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.
60 61 62 63 64 65 66 |
# File 'lib/rom/sql/function.rb', line 60 def sql_literal(ds) if name ds.literal(func.as(name)) else ds.literal(func) end 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
249 250 251 252 253 254 255 256 257 |
# File 'lib/rom/sql/function.rb', line 249 def within_group(*args, &) if block_given? group = args + ::ROM::SQL::OrderDSL.new(schema).(&) else group = args end super(*group) end |