Class: ROM::SQL::Relation
- Inherits:
-
Relation
- Object
- Relation
- ROM::SQL::Relation
- 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
Class Method Summary collapse
- .associations ⇒ Object private
- .define_default_views! ⇒ Object private
-
.inherited(klass) ⇒ Object
private
Set default dataset for a relation sub-class.
- .primary_key_columns(db, table) ⇒ Object private
Instance Method Summary collapse
-
#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.
-
#columns ⇒ Array<Symbol>
private
Return raw column names.
-
#count ⇒ Relation
included
from Reading
Return relation count.
-
#delete(*args, &block) ⇒ Relation
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.
-
#exclude(*args, &block) ⇒ Relation
included
from Reading
Restrict a relation to not match criteria.
-
#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.
-
#insert(*args, &block) ⇒ Relation
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.
-
#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) ⇒ Relation
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(name) ⇒ Array
included
from Reading
Pluck values from a specific column.
-
#prefix(name = Dry::Core::Inflector.singularize(schema.name.dataset)) ⇒ Relation
included
from Reading
Prefix all columns in a relation.
-
#qualified ⇒ Relation
included
from Reading
Qualifies all columns in a relation.
-
#qualified_columns ⇒ Array<Symbol>
included
from Reading
Return a list of qualified column names.
-
#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.
-
#union(relation, options = EMPTY_HASH, &block) ⇒ Relation
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) ⇒ Relation
included
from Writing
Update tuples in the relation.
-
#upsert(*args, &block) ⇒ Object
included
from Writing
Add upsert option (only PostgreSQL >= 9.5) Uses internal Sequel implementation Default - ON CONFLICT DO NOTHING more options: 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.
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.
100 101 102 |
# File 'lib/rom/sql/relation.rb', line 100 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.
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 |
# File 'lib/rom/sql/relation.rb', line 69 def self.define_default_views! 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 <<-RUBY, __FILE__, __LINE__ + 1 def by_pk(#{schema.primary_key.map(&:name).join(', ')}) where(#{schema.primary_key.map { |attr| "schema[:#{attr.name}] => #{attr.name}" }.join(', ')}) 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 define_method(:by_pk) do |pk| where(schema[primary_key] => pk) end end end |
.inherited(klass) ⇒ 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.
Set default dataset for a relation sub-class
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/rom/sql/relation.rb', line 32 def self.inherited(klass) super klass.class_eval do schema_dsl SQL::Schema::DSL schema_inferrer -> (name, gateway) do inferrer_for_db = ROM::SQL::Schema::Inferrer.get(gateway.connection.database_type.to_sym) begin inferrer_for_db.new.call(name, gateway) rescue Sequel::Error => e inferrer_for_db.on_error(klass, e) ROM::Schema::DEFAULT_INFERRER.() end end dataset do # TODO: feels strange to do it here - we need a new hook for this during finalization klass.define_default_views! schema = klass.schema table = opts[:from].first if db.table_exists?(table) if schema select(*schema.map(&:to_sym)).order(*schema.project(*schema.primary_key_names).qualified.map(&:to_sym)) else select(*columns).order(*klass.primary_key_columns(db, table)) end else self end end 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.
105 106 107 108 |
# File 'lib/rom/sql/relation.rb', line 105 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
125 126 127 |
# File 'lib/rom/sql/relation.rb', line 125 def assoc(name) associations[name].(__registry__) end |
#avg(*args) ⇒ Object Originally defined in module Reading
Returns a result of SQL AVG clause.
#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
134 135 136 |
# File 'lib/rom/sql/relation.rb', line 134 def columns @columns ||= dataset.columns end |
#delete(*args, &block) ⇒ Relation 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.
#exclude(*args, &block) ⇒ Relation Originally defined in module Reading
Restrict a relation to not match criteria
#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
#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 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 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
#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) ⇒ Relation 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(name) ⇒ Array Originally defined in module Reading
Pluck values from a specific column
#prefix(name = Dry::Core::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 ⇒ 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
#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 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.
#union(relation, options = EMPTY_HASH, &block) ⇒ Relation Originally defined in module Reading
Adds a UNION clause for relation dataset using second relation dataset
#unique?(criteria) ⇒ TrueClass, FalseClass Originally defined in module Reading
Return if a restricted relation has 0 tuples
#update(*args, &block) ⇒ Relation Originally defined in module Writing
Update tuples in the relation
#upsert(*args, &block) ⇒ Object Originally defined in module Writing
Add upsert option (only PostgreSQL >= 9.5) Uses internal Sequel implementation Default - ON CONFLICT DO NOTHING more options: sequel.jeremyevans.net/rdoc-adapters/classes/Sequel/Postgres/DatasetMethods.html#method-i-insert_conflict