Class: ROM::SQL::Attribute
- Inherits:
-
ROM::Schema::Attribute
- Object
- ROM::Schema::Attribute
- ROM::SQL::Attribute
- Extended by:
- Dry::Core::Cache
- Defined in:
- lib/rom/sql/attribute.rb
Overview
Extended schema attributes tailored for SQL databases
Defined Under Namespace
Modules: TypeExtensions
Constant Summary collapse
- OPERATORS =
%i[>= <= > <].freeze
- NONSTANDARD_EQUALITY_VALUES =
[true, false, nil].freeze
- QualifyError =
Error raised when an attribute cannot be qualified
Class.new(StandardError)
Class Method Summary collapse
- .[](*args) ⇒ Object private
Instance Method Summary collapse
-
#! ⇒ Attribute
Negate the attribute’s sql expression.
- #=~(other) ⇒ Object
-
#aliased(name) ⇒ SQL::Attribute
(also: #as)
Return a new attribute with an alias.
-
#canonical ⇒ Object
Return a new attribute in its canonical form.
-
#concat(other, sep = ' ') ⇒ SQL::Function
Create a CONCAT function from the attribute.
-
#foreign_key ⇒ SQL::Attribute
Return a new attribute marked as a FK.
-
#func(&block) ⇒ SQL::Function
Create a function DSL from the attribute.
-
#in(*args) ⇒ Object
Return a boolean expression with an inclusion test.
-
#is(other) ⇒ Object
Return a boolean expression with an equality operator.
-
#joined ⇒ SQL::Attribute
Return a new attribute marked as joined.
-
#joined? ⇒ Boolean
Return if an attribute was used in a join.
-
#not(other) ⇒ Object
Return a boolean expression with a negated equality operator.
-
#qualified ⇒ SQL::Attribute
Return a new attribute marked as qualified.
-
#qualified? ⇒ Boolean
Return if an attribute type is qualified.
-
#sql_literal(ds) ⇒ Object
private
Sequel calls this method to coerce an attribute into SQL string.
-
#to_sym ⇒ Symbol
Return symbol representation of an attribute.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ 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.
Delegate to sql expression if it responds to a given method
324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/rom/sql/attribute.rb', line 324 def method_missing(meth, *args, &block) if OPERATORS.include?(meth) __cmp__(meth, args[0]) elsif extensions.key?(meth) extensions[meth].(type, sql_expr, *args, &block) elsif sql_expr.respond_to?(meth) (sql_expr: sql_expr.__send__(meth, *args, &block)) else super end end |
Class Method Details
.[](*args) ⇒ 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.
63 64 65 |
# File 'lib/rom/sql/attribute.rb', line 63 def self.[](*args) fetch_or_store(args) { new(*args) } end |
Instance Method Details
#! ⇒ Attribute
Negate the attribute’s sql expression
236 237 238 |
# File 'lib/rom/sql/attribute.rb', line 236 def ! ~self end |
#=~(other) ⇒ Object
210 211 212 |
# File 'lib/rom/sql/attribute.rb', line 210 def =~(other) (sql_expr: sql_expr =~ binary_operation_arg(other)) end |
#aliased(name) ⇒ SQL::Attribute Also known as: as
Return a new attribute with an alias
77 78 79 |
# File 'lib/rom/sql/attribute.rb', line 77 def aliased(name) super.(name: .fetch(:name, name), sql_expr: sql_expr.as(name)) end |
#canonical ⇒ Object
Return a new attribute in its canonical form
85 86 87 88 89 90 91 |
# File 'lib/rom/sql/attribute.rb', line 85 def canonical if aliased? (alias: nil, sql_expr: nil) else self end end |
#concat(other, sep = ' ') ⇒ SQL::Function
Create a CONCAT function from the attribute
295 296 297 |
# File 'lib/rom/sql/attribute.rb', line 295 def concat(other, sep = ' ') Function.new(type).concat(self, sep, other) end |
#foreign_key ⇒ SQL::Attribute
Return a new attribute marked as a FK
160 161 162 |
# File 'lib/rom/sql/attribute.rb', line 160 def foreign_key (foreign_key: true) end |
#func(&block) ⇒ SQL::Function
Create a function DSL from the attribute
278 279 280 |
# File 'lib/rom/sql/attribute.rb', line 278 def func(&block) ProjectionDSL.new(name => self).call(&block).first end |
#in(*args) ⇒ Object
Return a boolean expression with an inclusion test
If the single argument passed to the method is a Range object then the resulting expression will restrict the attribute value with range’s bounds. Upper bound condition will be inclusive/non-inclusive depending on the range type.
If more than one argument is passed to the method or the first argument is not Range then the result will be a simple IN check.
258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/rom/sql/attribute.rb', line 258 def in(*args) if args.first.is_a?(Range) range = args.first lower_cond = __cmp__(:>=, range.begin) upper_cond = __cmp__(range.exclude_end? ? :< : :<=, range.end) Sequel::SQL::BooleanExpression.new(:AND, lower_cond, upper_cond) else __cmp__(:IN, args) end end |
#is(other) ⇒ Object
Return a boolean expression with an equality operator
205 206 207 |
# File 'lib/rom/sql/attribute.rb', line 205 def is(other) self =~ other end |
#joined ⇒ SQL::Attribute
Return a new attribute marked as joined
Whenever you join two schemas, the right schema’s attribute will be marked as joined using this method
121 122 123 |
# File 'lib/rom/sql/attribute.rb', line 121 def joined (joined: true) end |
#joined? ⇒ Boolean
Return if an attribute was used in a join
136 137 138 |
# File 'lib/rom/sql/attribute.rb', line 136 def joined? [:joined].equal?(true) end |
#not(other) ⇒ Object
Return a boolean expression with a negated equality operator
224 225 226 |
# File 'lib/rom/sql/attribute.rb', line 224 def not(other) !is(other) end |
#qualified ⇒ SQL::Attribute
Return a new attribute marked as qualified
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/rom/sql/attribute.rb', line 101 def qualified return self if qualified? case sql_expr when Sequel::SQL::AliasedExpression, Sequel::SQL::Identifier type = (qualified: true) type.(qualified: true, sql_expr: Sequel[type.to_sym]) else raise QualifyError, "can't qualify #{name.inspect} (#{sql_expr.inspect})" end end |
#qualified? ⇒ Boolean
Return if an attribute type is qualified
151 152 153 |
# File 'lib/rom/sql/attribute.rb', line 151 def qualified? [:qualified].equal?(true) 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.
Sequel calls this method to coerce an attribute into SQL string
304 305 306 307 308 309 310 |
# File 'lib/rom/sql/attribute.rb', line 304 def sql_literal(ds) if sql_expr sql_expr.sql_literal(ds) else Sequel[to_sym].sql_literal(ds) end end |
#to_sym ⇒ Symbol
Return symbol representation of an attribute
This uses convention from sequel where double underscore in the name is used for qualifying, and triple underscore means aliasing
182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/rom/sql/attribute.rb', line 182 def to_sym @_to_sym ||= if qualified? && aliased? :"#{source.dataset}__#{name}___#{[:alias]}" elsif qualified? :"#{source.dataset}__#{name}" elsif aliased? :"#{name}___#{[:alias]}" else name end end |