Class: ROM::SQL::Relation
- Inherits:
-
Relation
- Object
- Relation
- ROM::SQL::Relation
- Extended by:
- Notifications::Listener
- Defined in:
- lib/rom/sql/relation.rb,
lib/rom/sql/relation/reading.rb,
lib/rom/sql/relation/writing.rb
Overview
Sequel-specific relation extensions
Defined Under Namespace
Constant Summary
Constants included from Reading
Constants included from ROM::SQL
CheckConstraintError, ConstraintError, DatabaseError, ERROR_MAP, ForeignKeyConstraintError, MigrationError, MissingConfigurationError, MissingPrimaryKeyError, NoAssociationError, NotNullConstraintError, UniqueConstraintError, UnknownDBTypeError, UnsupportedConversion, VERSION
Class Method Summary collapse
- .associations ⇒ Object private
-
.define_default_views! ⇒ Object
private
rubocop:disable Metrics/MethodLength.
- .primary_key_columns(db, table) ⇒ Object private
Instance Method Summary collapse
-
#assoc(name) ⇒ Relation
Return relation that will load associated tuples of this relation.
-
#columns ⇒ Array<Symbol>
private
Return raw column names.
-
#transaction(opts = EMPTY_HASH) {|t| ... } ⇒ Mixed
Open a database transaction.
Methods included from Reading
#as_hash, #avg, #count, #distinct, #each_batch, #exclude, #exist?, #exists, #fetch, #first, #group, #group_and_count, #group_append, #having, #invert, #join, #last, #left_join, #limit, #lock, #map, #max, #min, #offset, #order, #pluck, #prefix, #qualified, #qualified_columns, #query, #read, #rename, #reverse, #right_join, #select, #select_append, #select_group, #sum, #unfiltered, #union, #unique?, #unordered, #where, #wrap
Methods included from Writing
#delete, #import, #insert, #multi_insert, #update, #upsert
Methods included from ROM::SQL
Class Method Details
.associations ⇒ 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.
102 103 104 |
# File 'lib/rom/sql/relation.rb', line 102 def self.associations schema.associations end |
.define_default_views! ⇒ 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.
rubocop:disable Metrics/MethodLength
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/rom/sql/relation.rb', line 56 def self.define_default_views! undef_method :by_pk if method_defined?(:by_pk) if schema.primary_key.size > 1 pks = schema.primary_key # @!method by_pk(val1, val2) # Return a relation restricted by its composite primary key # # @param [Array] args A list with composite pk values # # @return [SQL::Relation] # # @api public class_eval(<<-RUBY, __FILE__, __LINE__ + 1) def by_pk(#{pks.map(&:name).join(", ")}) # def by_pk(val1, val2) s = schema.canonical # s = schema.canonical where( # where( #{pks.map { "s[:#{_1.name}] => #{_1.name}" }.join(", ")} # s[:val1] => val1, s[:val2] => val2 ) # ) end # end RUBY else # @!method by_pk(pk) # Return a relation restricted by its primary key # # @param [Object] pk The primary key value # # @return [SQL::Relation] # # @api public class_eval(<<-RUBY, __FILE__, __LINE__ + 1) def by_pk(pk) # def by_pk(pk) s = schema.canonical # s = schema.canonical if primary_key.nil? # if primary_key.nil? raise MissingPrimaryKeyError.new( # raise MissingPrimaryKeyError.new( "Missing primary key for :\#{schema.name}" # "Missing primary key for :#{schema.name}" ) # ) end # end where(s[s.primary_key_name].qualified => pk) # where(s[s.primary_key_name].qualified => pk) end # end RUBY end end |
.primary_key_columns(db, table) ⇒ 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.
107 108 109 110 |
# File 'lib/rom/sql/relation.rb', line 107 def self.primary_key_columns(db, table) names = db.respond_to?(:primary_key) ? Array(db.primary_key(table)) : [:id] names.map { |col| :"#{table}__#{col}" } end |
Instance Method Details
#assoc(name) ⇒ Relation
Return relation that will load associated tuples of this relation
This method is useful for defining custom relation views for relation composition when you want to enhance default association query
127 128 129 |
# File 'lib/rom/sql/relation.rb', line 127 def assoc(name) associations[name].() end |
#columns ⇒ Array<Symbol>
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.
Return raw column names
197 198 199 |
# File 'lib/rom/sql/relation.rb', line 197 def columns @columns ||= dataset.columns end |
#transaction(opts = EMPTY_HASH) {|t| ... } ⇒ Mixed
Open a database transaction
188 189 190 |
# File 'lib/rom/sql/relation.rb', line 188 def transaction(opts = EMPTY_HASH, &) Transaction.new(dataset.db).run(opts, &) end |