Class: ROM::SQL::Relation

Inherits:
Relation
  • Object
show all
Extended by:
Notifications::Listener
Includes:
ROM::SQL, Reading, Writing
Defined in:
lib/rom/sql/relation.rb,
lib/rom/sql/relation/reading.rb,
lib/rom/sql/relation/writing.rb

Overview

Sequel-specific relation extensions

API:

  • public

Defined Under Namespace

Modules: Reading, Writing

Constant Summary

Constants included from Reading

Reading::ROW_LOCK_MODES

Constants included from ROM::SQL

CheckConstraintError, ConstraintError, DatabaseError, ERROR_MAP, ForeignKeyConstraintError, MigrationError, MissingConfigurationError, MissingPrimaryKeyError, NoAssociationError, NotNullConstraintError, UniqueConstraintError, UnknownDBTypeError, UnsupportedConversion, VERSION

Class Method Summary collapse

Instance Method Summary collapse

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?, #where, #wrap

Methods included from Writing

#delete, #import, #insert, #multi_insert, #update, #upsert

Methods included from ROM::SQL

migration, with_gateway

Class Method Details

.associationsObject

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.

API:

  • private



90
91
92
# File 'lib/rom/sql/relation.rb', line 90

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.

API:

  • private



50
51
52
53
54
55
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
# File 'lib/rom/sql/relation.rb', line 50

def self.define_default_views!
  undef_method :by_pk if method_defined?(:by_pk)

  if schema.primary_key.size > 1
    # @!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 "      def by_pk(\#{schema.primary_key.map(&:name).join(', ')})\n        where(\#{schema.primary_key.map { |attr| \"schema.canonical[:\#{attr.name}] => \#{attr.name}\" }.join(', ')})\n      end\n    RUBY\n  else\n    # @!method by_pk(pk)\n    #   Return a relation restricted by its primary key\n    #\n    #   @param [Object] pk The primary key value\n    #\n    #   @return [SQL::Relation]\n    #\n    #   @api public\n    class_eval <<-RUBY, __FILE__, __LINE__ + 1\n      def by_pk(pk)\n        if primary_key.nil?\n          raise MissingPrimaryKeyError.new(\n            \"Missing primary key for :\\\#{schema.name}\"\n          )\n        end\n        where(schema.canonical[schema.canonical.primary_key_name].qualified => pk)\n      end\n    RUBY\n  end\nend\n", __FILE__, __LINE__ + 1

.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.

API:

  • private



95
96
97
98
# File 'lib/rom/sql/relation.rb', line 95

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

Examples:

assoc(:tasks).where(tasks[:title] => "Task One")

Parameters:

  • The association name

Returns:

API:

  • public



115
116
117
# File 'lib/rom/sql/relation.rb', line 115

def assoc(name)
  associations[name].()
end

#columnsArray<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

Returns:

API:

  • private



149
150
151
# File 'lib/rom/sql/relation.rb', line 149

def columns
  @columns ||= dataset.columns
end

#transaction(opts = EMPTY_HASH) {|t| ... } ⇒ Mixed

Open a database transaction

Parameters:

  • (defaults to: EMPTY_HASH)

Options Hash (opts):

  • :auto_savepoint (Boolean)

    Automatically use a savepoint for Database#transaction calls inside this transaction block.

  • :isolation (Symbol)

    The transaction isolation level to use for this transaction, should be :uncommitted, :committed, :repeatable, or :serializable, used if given and the database/adapter supports customizable transaction isolation levels.

  • :num_retries (Integer)

    The number of times to retry if the :retry_on option is used. The default is 5 times. Can be set to nil to retry indefinitely, but that is not recommended.

  • :before_retry (Proc)

    Proc to execute before rertrying if the :retry_on option is used. Called with two arguments: the number of retry attempts (counting the current one) and the error the last attempt failed with.

  • :prepare (String)

    A string to use as the transaction identifier for a prepared transaction (two-phase commit), if the database/adapter supports prepared transactions.

  • :retry_on (Class)

    An exception class or array of exception classes for which to automatically retry the transaction. Can only be set if not inside an existing transaction. Note that this should not be used unless the entire transaction block is idempotent, as otherwise it can cause non-idempotent behavior to execute multiple times.

  • :rollback (Symbol)

    Can the set to :reraise to reraise any Sequel::Rollback exceptions raised, or :always to always rollback even if no exceptions occur (useful for testing).

  • :server (Symbol)

    The server to use for the transaction. Set to :default, :read_only, or whatever symbol you used in the connect string when naming your servers.

  • :savepoint (Boolean)

    Whether to create a new savepoint for this transaction, only respected if the database/adapter supports savepoints. By default Sequel will reuse an existing transaction, so if you want to use a savepoint you must use this option. If the surrounding transaction uses :auto_savepoint, you can set this to false to not use a savepoint. If the value given for this option is :only, it will only create a savepoint if it is inside a transacation.

  • :deferrable (Boolean)

    **PG 9.1+ only** If present, set to DEFERRABLE if true or NOT DEFERRABLE if false.

  • :read_only (Boolean)

    **PG only** If present, set to READ ONLY if true or READ WRITE if false.

  • :synchronous (Symbol)

    **PG only** if non-nil, set synchronous_commit appropriately. Valid values true, :on, false, :off, :local (9.1+), and :remote_write (9.2+).

Yields:

  • (t)

    Transaction

Returns:

API:

  • public



140
141
142
# File 'lib/rom/sql/relation.rb', line 140

def transaction(opts = EMPTY_HASH, &block)
  Transaction.new(dataset.db).run(opts, &block)
end