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
Instance Method Summary collapse
-
#as_hash(attribute = primary_key) ⇒ Hash
included
from Reading
Returns hash with all tuples being the key of each the provided attribute.
-
#assoc(name) ⇒ Relation
Return relation that will load associated tuples of this relation.
-
#avg(*args) ⇒ Object
included
from Reading
Returns a result of SQL AVG clause.
-
#count ⇒ Relation
included
from Reading
Return relation count.
-
#delete(*args, &block) ⇒ Integer
included
from Writing
Delete tuples from the relation.
-
#distinct(*args, &block) ⇒ Relation
included
from Reading
Returns a copy of the relation with a SQL DISTINCT clause.
-
#each_batch(size: 1000) {|| ... } ⇒ Object
included
from Reading
Process the dataset in batches.
-
#exclude(*args, &block) ⇒ Relation
included
from Reading
Restrict a relation to not match criteria.
-
#exist?(*args, &block) ⇒ TrueClass, FalseClass
included
from Reading
Checks whether a relation has at least one tuple.
-
#exists(other, condition = nil) ⇒ SQL::Relation
included
from Reading
Restrict with rows from another relation.
-
#fetch(pk) ⇒ Relation
included
from Reading
Fetch a tuple identified by the pk.
-
#first ⇒ Hash
included
from Reading
Get first tuple from the relation.
-
#group(*args, &block) ⇒ Relation
included
from Reading
Group by specific columns.
-
#group_and_count(*args, &block) ⇒ Relation
included
from Reading
Group by specific columns and count by group.
-
#group_append(*args, &block) ⇒ Relation
included
from Reading
Group by more columns.
-
#having(*args, &block) ⇒ Relation
included
from Reading
Restrict a relation to match grouping criteria.
-
#import(other, options = EMPTY_HASH) ⇒ Integer
included
from Writing
Insert tuples from other relation.
-
#insert(*args, &block) ⇒ Hash
included
from Writing
Insert tuple into relation.
-
#invert ⇒ Relation
included
from Reading
Inverts the current WHERE and HAVING clauses.
-
#join(*args, &block) ⇒ Relation
(also: #inner_join)
included
from Reading
Join with another relation using INNER JOIN.
-
#last ⇒ Hash
included
from Reading
Get last tuple from the relation.
-
#left_join(*args, &block) ⇒ Relation
included
from Reading
Join with another relation using LEFT OUTER JOIN.
-
#limit(*args) ⇒ Relation
included
from Reading
Limit a relation to a specific number of tuples.
-
#lock(**options, &block) ⇒ Object
included
from Reading
Lock rows with in the specified mode.
-
#map(key = nil, &block) ⇒ Object
included
from Reading
Map tuples from the relation.
-
#max(*args) ⇒ Object
included
from Reading
Returns a result of SQL MAX clause.
-
#min(*args) ⇒ Object
included
from Reading
Returns a result of SQL MIN clause.
-
#multi_insert(*args, &block) ⇒ Array<String>
included
from Writing
Multi insert tuples into relation.
-
#offset(num) ⇒ Relation
included
from Reading
Set offset for the relation.
-
#order(*args, &block) ⇒ Relation
included
from Reading
Set order for the relation.
-
#pluck(*names) ⇒ Array
included
from Reading
Pluck values from a specific column.
-
#prefix(name = Inflector.singularize(schema.name.dataset)) ⇒ Relation
included
from Reading
Prefix all columns in a relation.
-
#qualified(table_alias = nil) ⇒ Relation
included
from Reading
Qualifies all columns in a relation.
-
#qualified_columns ⇒ Array<Symbol>
included
from Reading
Return a list of qualified column names.
-
#query ⇒ SQL::Attribute
included
from Reading
Turn a relation into a subquery.
-
#read(sql) ⇒ SQL::Relation
included
from Reading
Return a new relation from a raw SQL string.
-
#rename(options) ⇒ Relation
included
from Reading
Rename columns in a relation.
-
#reverse(*args, &block) ⇒ Relation
included
from Reading
Reverse the order of the relation.
-
#right_join(*args, &block) ⇒ Relation
included
from Reading
Join with another relation using RIGHT JOIN.
-
#select(*args, &block) ⇒ Relation
(also: #project)
included
from Reading
Select specific columns for select clause.
-
#select_append(*args, &block) ⇒ Relation
included
from Reading
Append specific columns to select clause.
-
#select_group(*args, &block) ⇒ Relation
included
from Reading
Select and group by specific columns.
-
#sum(*args) ⇒ Integer
included
from Reading
Returns a result of SQL SUM clause.
-
#transaction(opts = EMPTY_HASH) {|t| ... } ⇒ Mixed
Open a database transaction.
-
#unfiltered ⇒ SQL::Relation
included
from Reading
Discard restrictions in
WHERE
andHAVING
clauses. -
#union(relation, options = EMPTY_HASH, &block) ⇒ Object
included
from Reading
Adds a UNION clause for relation dataset using second relation dataset.
-
#unique?(criteria) ⇒ TrueClass, FalseClass
included
from Reading
Return if a restricted relation has 0 tuples.
-
#update(*args, &block) ⇒ Integer
included
from Writing
Update tuples in the relation.
-
#upsert(*args, &block) ⇒ Integer
included
from Writing
Add upsert option (only PostgreSQL >= 9.5) Uses internal Sequel implementation Default - ON CONFLICT DO NOTHING more options: http://sequel.jeremyevans.net/rdoc-adapters/classes/Sequel/Postgres/DatasetMethods.html#method-i-insert_conflict.
-
#where(*args, &block) ⇒ Relation
included
from Reading
Restrict a relation to match criteria.
-
#wrap(*names) ⇒ Wrap
included
from Reading
Wrap other relations using association names.
Instance Method Details
#as_hash(attribute = primary_key) ⇒ Hash Originally defined in module Reading
Returns hash with all tuples being the key of each the provided attribute
#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
117 118 119 |
# File 'lib/rom/sql/relation.rb', line 117 def assoc(name) associations[name].() end |
#avg(*args) ⇒ Object Originally defined in module Reading
Returns a result of SQL AVG clause.
#delete(*args, &block) ⇒ Integer Originally defined in module Writing
Delete tuples from the relation
#distinct(*columns) ⇒ Relation #distinct(&block) ⇒ Relation Originally defined in module Reading
Returns a copy of the relation with a SQL DISTINCT clause.
#each_batch(size: 1000) {|| ... } ⇒ Object Originally defined in module Reading
Process the dataset in batches. The method yields a relation restricted by a primary key value. This means it discards any order internally and uses the PK sort. Currently, works only with a single-column primary key.
#exclude(*args, &block) ⇒ Relation Originally defined in module Reading
Restrict a relation to not match criteria
#exist?(*args, &block) ⇒ TrueClass, FalseClass Originally defined in module Reading
Checks whether a relation has at least one tuple
@example users.where(name: 'John').exist? # => true
users.exist?(name: 'Klaus') # => false
users.exist? { name.is('klaus') } # => false
@param [Array
#exists(other, condition = nil) ⇒ SQL::Relation Originally defined in module Reading
Restrict with rows from another relation. Accepts only SQL relations and uses the EXISTS clause under the hood
#first ⇒ Hash Originally defined in module Reading
Get first tuple from the relation
#group(*columns) ⇒ Relation #group(*attributes) ⇒ Relation #group(*attributes, &block) ⇒ Relation Originally defined in module Reading
Group by specific columns
#group_and_count(*args, &block) ⇒ Relation Originally defined in module Reading
Group by specific columns and count by group
#group_append(*columns) ⇒ Relation #group_append(*attributes) ⇒ Relation #group_append(*attributes, &block) ⇒ Relation Originally defined in module Reading
Group by more columns
#having(conditions) ⇒ Relation #having(&block) ⇒ Relation Originally defined in module Reading
Restrict a relation to match grouping criteria
#import(other_sql_relation, options) ⇒ Integer #import(other, options) ⇒ Integer Originally defined in module Writing
Insert tuples from other relation
NOTE: The method implicitly uses a transaction
#insert(*args, &block) ⇒ Hash Originally defined in module Writing
Insert tuple into relation
#invert ⇒ Relation Originally defined in module Reading
Inverts the current WHERE and HAVING clauses. If there is neither a WHERE or HAVING clause, adds a WHERE clause that is always false.
#join(dataset, join_conditions) ⇒ Relation #join(dataset, join_conditions, options) ⇒ Relation #join(relation) ⇒ Relation #join(relation, &block) ⇒ Relation Also known as: inner_join Originally defined in module Reading
Join with another relation using INNER JOIN
#last ⇒ Hash Originally defined in module Reading
Get last tuple from the relation
#left_join(dataset, left_join_conditions) ⇒ Relation #left_join(dataset, left_join_conditions, options) ⇒ Relation #left_join(relation) ⇒ Relation #join(relation, &block) ⇒ Relation Originally defined in module Reading
Join with another relation using LEFT OUTER JOIN
#limit(num) ⇒ Relation #limit(num, offset) ⇒ Relation Originally defined in module Reading
Limit a relation to a specific number of tuples
#lock(options) ⇒ SQL::Relation #lock(options) {|relation| ... } ⇒ Object Originally defined in module Reading
Lock rows with in the specified mode. Check out ROW_LOCK_MODES for the list of supported modes, keep in mind available lock modes heavily depend on the database type+version you're running on.
#map(key = nil, &block) ⇒ Object Originally defined in module Reading
Map tuples from the relation
#max(*args) ⇒ Object Originally defined in module Reading
Returns a result of SQL MAX clause.
#min(*args) ⇒ Object Originally defined in module Reading
Returns a result of SQL MIN clause.
#multi_insert(*args, &block) ⇒ Array<String> Originally defined in module Writing
Multi insert tuples into relation
#order(*columns) ⇒ Relation #order(*attributes) ⇒ Relation #order(&block) ⇒ Relation Originally defined in module Reading
Set order for the relation
#pluck(*names) ⇒ Array Originally defined in module Reading
Pluck values from a specific column
#prefix(name = Inflector.singularize(schema.name.dataset)) ⇒ Relation Originally defined in module Reading
Prefix all columns in a relation
This method is intended to be used internally within a relation object
#qualified(table_alias = nil) ⇒ Relation Originally defined in module Reading
Qualifies all columns in a relation
This method is intended to be used internally within a relation object
#qualified_columns ⇒ Array<Symbol> Originally defined in module Reading
Return a list of qualified column names
This method is intended to be used internally within a relation object
#query ⇒ SQL::Attribute Originally defined in module Reading
Turn a relation into a subquery. Can be used for selecting a column with a subquery or restricting the result set with a IN (SELECT ...) condtion.
#read(sql) ⇒ SQL::Relation Originally defined in module Reading
Return a new relation from a raw SQL string
#rename(options) ⇒ Relation Originally defined in module Reading
Rename columns in a relation
This method is intended to be used internally within a relation object
#reverse(*args, &block) ⇒ Relation Originally defined in module Reading
Reverse the order of the relation
#right_join(dataset, right_join_conditions) ⇒ Relation #right_join(dataset, right_join_conditions, options) ⇒ Relation #right_join(relation) ⇒ Relation #join(relation, &block) ⇒ Relation Originally defined in module Reading
Join with another relation using RIGHT JOIN
#select(*columns) ⇒ Relation #select(*attributes) ⇒ Relation #select(&block) ⇒ Relation #select(*columns, &block) ⇒ Relation Also known as: project Originally defined in module Reading
Select specific columns for select clause
#select_append(*args, &block) ⇒ Relation Originally defined in module Reading
Append specific columns to select clause
#select_group(*args, &block) ⇒ Relation Originally defined in module Reading
Select and group by specific columns
#sum(*args) ⇒ Integer Originally defined in module Reading
Returns a result of SQL SUM clause.
#transaction(opts = EMPTY_HASH) {|t| ... } ⇒ Mixed
Open a database transaction
142 143 144 |
# File 'lib/rom/sql/relation.rb', line 142 def transaction(opts = EMPTY_HASH, &block) Transaction.new(dataset.db).run(opts, &block) end |
#unfiltered ⇒ SQL::Relation Originally defined in module Reading
Discard restrictions in WHERE
and HAVING
clauses
#union(relation, options = EMPTY_HASH, &block) ⇒ Object Originally defined in module Reading
Adds a UNION clause for relation dataset using second relation dataset
@returRelation]
#unique?(criteria) ⇒ TrueClass, FalseClass Originally defined in module Reading
Return if a restricted relation has 0 tuples
#update(*args, &block) ⇒ Integer Originally defined in module Writing
Update tuples in the relation
#upsert(*args, &block) ⇒ Integer Originally defined in module Writing
Add upsert option (only PostgreSQL >= 9.5) Uses internal Sequel implementation Default - ON CONFLICT DO NOTHING more options: http://sequel.jeremyevans.net/rdoc-adapters/classes/Sequel/Postgres/DatasetMethods.html#method-i-insert_conflict