Module: ROM::SQL::Relation::Reading
- Included in:
- ROM::SQL::Relation
- Defined in:
- lib/rom/sql/relation/reading.rb
Overview
Query API for SQL::Relation
Instance Method Summary collapse
-
#avg(*args) ⇒ Object
Returns a result of SQL AVG clause.
-
#count ⇒ Relation
Return relation count.
-
#distinct(*args, &block) ⇒ Relation
Returns a copy of the relation with a SQL DISTINCT clause.
-
#exclude(*args, &block) ⇒ Relation
Restrict a relation to not match criteria.
-
#fetch(pk) ⇒ Relation
Fetch a tuple identified by the pk.
-
#first ⇒ Hash
Get first tuple from the relation.
-
#group(*args, &block) ⇒ Relation
Group by specific columns.
-
#group_and_count(*args, &block) ⇒ Relation
Group by specific columns and count by group.
-
#group_append(*args, &block) ⇒ Relation
Group by more columns.
-
#having(*args, &block) ⇒ Relation
Restrict a relation to match grouping criteria.
-
#invert ⇒ Relation
Inverts the current WHERE and HAVING clauses.
-
#join(*args, &block) ⇒ Relation
(also: #inner_join)
Join with another relation using INNER JOIN.
-
#last ⇒ Hash
Get last tuple from the relation.
-
#left_join(*args, &block) ⇒ Relation
Join with another relation using LEFT OUTER JOIN.
-
#limit(*args) ⇒ Relation
Limit a relation to a specific number of tuples.
-
#map(key = nil, &block) ⇒ Object
Map tuples from the relation.
-
#max(*args) ⇒ Object
Returns a result of SQL MAX clause.
-
#min(*args) ⇒ Object
Returns a result of SQL MIN clause.
-
#offset(num) ⇒ Relation
Set offset for the relation.
-
#order(*args, &block) ⇒ Relation
Set order for the relation.
-
#pluck(name) ⇒ Array
Pluck values from a specific column.
-
#prefix(name = Dry::Core::Inflector.singularize(schema.name.dataset)) ⇒ Relation
Prefix all columns in a relation.
-
#qualified ⇒ Relation
Qualifies all columns in a relation.
-
#qualified_columns ⇒ Array<Symbol>
Return a list of qualified column names.
-
#read(sql) ⇒ SQL::Relation
Return a new relation from a raw SQL string.
-
#rename(options) ⇒ Relation
Rename columns in a relation.
-
#reverse(*args, &block) ⇒ Relation
Reverse the order of the relation.
-
#right_join(*args, &block) ⇒ Relation
Join with another relation using RIGHT JOIN.
-
#select(*args, &block) ⇒ Relation
(also: #project)
Select specific columns for select clause.
-
#select_append(*args, &block) ⇒ Relation
Append specific columns to select clause.
-
#select_group(*args, &block) ⇒ Relation
Select and group by specific columns.
-
#sum(*args) ⇒ Integer
Returns a result of SQL SUM clause.
-
#union(relation, options = EMPTY_HASH, &block) ⇒ Relation
Adds a UNION clause for relation dataset using second relation dataset.
-
#unique?(criteria) ⇒ TrueClass, FalseClass
Return if a restricted relation has 0 tuples.
-
#where(*args, &block) ⇒ Relation
Restrict a relation to match criteria.
Instance Method Details
#avg(*args) ⇒ Object
Returns a result of SQL AVG clause.
307 308 309 |
# File 'lib/rom/sql/relation/reading.rb', line 307 def avg(*args) dataset.__send__(__method__, *args) end |
#count ⇒ Relation
Return relation count
34 35 36 |
# File 'lib/rom/sql/relation/reading.rb', line 34 def count dataset.count end |
#distinct(*columns) ⇒ Relation #distinct(&block) ⇒ Relation
Returns a copy of the relation with a SQL DISTINCT clause.
251 252 253 |
# File 'lib/rom/sql/relation/reading.rb', line 251 def distinct(*args, &block) new(dataset.__send__(__method__, *args, &block)) end |
#exclude(*args, &block) ⇒ Relation
Restrict a relation to not match criteria
357 358 359 |
# File 'lib/rom/sql/relation/reading.rb', line 357 def exclude(*args, &block) new(dataset.__send__(__method__, *args, &block)) end |
#fetch(pk) ⇒ Relation
Fetch a tuple identified by the pk
21 22 23 |
# File 'lib/rom/sql/relation/reading.rb', line 21 def fetch(pk) by_pk(pk).one! end |
#first ⇒ Hash
Get first tuple from the relation
47 48 49 |
# File 'lib/rom/sql/relation/reading.rb', line 47 def first dataset.first end |
#group(*columns) ⇒ Relation #group(*attributes) ⇒ Relation #group(*attributes, &block) ⇒ Relation
Group by specific columns
656 657 658 659 660 661 662 663 664 665 666 |
# File 'lib/rom/sql/relation/reading.rb', line 656 def group(*args, &block) if block if args.size > 0 group(*args).group_append(&block) else new(dataset.__send__(__method__, *schema.group(&block))) end else new(dataset.__send__(__method__, *schema.project(*args).canonical)) end end |
#group_and_count(*args, &block) ⇒ Relation
Group by specific columns and count by group
718 719 720 |
# File 'lib/rom/sql/relation/reading.rb', line 718 def group_and_count(*args, &block) new(dataset.__send__(__method__, *args, &block)) end |
#group_append(*columns) ⇒ Relation #group_append(*attributes) ⇒ Relation #group_append(*attributes, &block) ⇒ Relation
Group by more columns
695 696 697 698 699 700 701 702 703 704 705 |
# File 'lib/rom/sql/relation/reading.rb', line 695 def group_append(*args, &block) if block if args.size > 0 group_append(*args).group_append(&block) else new(dataset.group_append(*schema.group(&block))) end else new(dataset.group_append(*args)) end end |
#having(conditions) ⇒ Relation #having(&block) ⇒ Relation
Restrict a relation to match grouping criteria
394 395 396 397 398 399 400 |
# File 'lib/rom/sql/relation/reading.rb', line 394 def having(*args, &block) if block new(dataset.having(*args).having(self.class.schema.restriction(&block))) else new(dataset.__send__(__method__, *args, &block)) end end |
#invert ⇒ Relation
Inverts the current WHERE and HAVING clauses. If there is neither a WHERE or HAVING clause, adds a WHERE clause that is always false.
414 415 416 |
# File 'lib/rom/sql/relation/reading.rb', line 414 def invert new(dataset.invert) end |
#join(dataset, join_conditions) ⇒ Relation #join(dataset, join_conditions, options) ⇒ Relation #join(relation) ⇒ Relation Also known as: inner_join
Join with another relation using INNER JOIN
544 545 546 |
# File 'lib/rom/sql/relation/reading.rb', line 544 def join(*args, &block) __join__(__method__, *args, &block) end |
#last ⇒ Hash
Get last tuple from the relation
60 61 62 |
# File 'lib/rom/sql/relation/reading.rb', line 60 def last dataset.last end |
#left_join(dataset, left_join_conditions) ⇒ Relation #left_join(dataset, left_join_conditions, options) ⇒ Relation #left_join(relation) ⇒ Relation
Join with another relation using LEFT OUTER JOIN
584 585 586 |
# File 'lib/rom/sql/relation/reading.rb', line 584 def left_join(*args, &block) __join__(__method__, *args, &block) end |
#limit(num) ⇒ Relation #limit(num, offset) ⇒ Relation
Limit a relation to a specific number of tuples
491 492 493 |
# File 'lib/rom/sql/relation/reading.rb', line 491 def limit(*args) new(dataset.__send__(__method__, *args)) end |
#map(key = nil, &block) ⇒ Object
Map tuples from the relation
124 125 126 127 128 129 130 |
# File 'lib/rom/sql/relation/reading.rb', line 124 def map(key = nil, &block) if key dataset.map(key, &block) else dataset.map(&block) end end |
#max(*args) ⇒ Object
Returns a result of SQL MAX clause.
293 294 295 |
# File 'lib/rom/sql/relation/reading.rb', line 293 def max(*args) dataset.__send__(__method__, *args) end |
#min(*args) ⇒ Object
Returns a result of SQL MIN clause.
279 280 281 |
# File 'lib/rom/sql/relation/reading.rb', line 279 def min(*args) dataset.__send__(__method__, *args) end |
#offset(num) ⇒ Relation
Set offset for the relation
505 506 507 |
# File 'lib/rom/sql/relation/reading.rb', line 505 def offset(num) new(dataset.__send__(__method__, num)) end |
#order(*columns) ⇒ Relation #order(*attributes) ⇒ Relation #order(&block) ⇒ Relation
Set order for the relation
449 450 451 452 453 454 455 |
# File 'lib/rom/sql/relation/reading.rb', line 449 def order(*args, &block) if block new(dataset.order(*args, *self.class.schema.order(&block))) else new(dataset.__send__(__method__, *args, &block)) end end |
#pluck(name) ⇒ Array
Pluck values from a specific column
141 142 143 |
# File 'lib/rom/sql/relation/reading.rb', line 141 def pluck(name) map(name) end |
#prefix(name = Dry::Core::Inflector.singularize(schema.name.dataset)) ⇒ Relation
Prefix all columns in a relation
This method is intended to be used internally within a relation object
77 78 79 |
# File 'lib/rom/sql/relation/reading.rb', line 77 def prefix(name = Dry::Core::Inflector.singularize(schema.name.dataset)) schema.prefix(name).(self) end |
#qualified ⇒ Relation
Qualifies all columns in a relation
This method is intended to be used internally within a relation object
92 93 94 |
# File 'lib/rom/sql/relation/reading.rb', line 92 def qualified schema.qualified.(self) end |
#qualified_columns ⇒ Array<Symbol>
Return a list of qualified column names
This method is intended to be used internally within a relation object
107 108 109 |
# File 'lib/rom/sql/relation/reading.rb', line 107 def qualified_columns schema.qualified.map(&:to_sym) end |
#read(sql) ⇒ SQL::Relation
Return a new relation from a raw SQL string
786 787 788 |
# File 'lib/rom/sql/relation/reading.rb', line 786 def read(sql) new(dataset.db[sql]) end |
#rename(options) ⇒ Relation
Rename columns in a relation
This method is intended to be used internally within a relation object
158 159 160 |
# File 'lib/rom/sql/relation/reading.rb', line 158 def rename() schema.rename().(self) end |
#reverse(*args, &block) ⇒ Relation
Reverse the order of the relation
465 466 467 |
# File 'lib/rom/sql/relation/reading.rb', line 465 def reverse(*args, &block) new(dataset.__send__(__method__, *args, &block)) end |
#right_join(dataset, right_join_conditions) ⇒ Relation #right_join(dataset, right_join_conditions, options) ⇒ Relation #right_join(relation) ⇒ Relation
Join with another relation using RIGHT JOIN
623 624 625 |
# File 'lib/rom/sql/relation/reading.rb', line 623 def right_join(*args, &block) __join__(__method__, *args, &block) end |
#select(*columns) ⇒ Relation #select(*attributes) ⇒ Relation #select(&block) ⇒ Relation #select(*columns, &block) ⇒ Relation Also known as: project
Select specific columns for select clause
215 216 217 |
# File 'lib/rom/sql/relation/reading.rb', line 215 def select(*args, &block) schema.project(*args, &block).(self) end |
#select_append(*args, &block) ⇒ Relation
Append specific columns to select clause
227 228 229 |
# File 'lib/rom/sql/relation/reading.rb', line 227 def select_append(*args, &block) schema.merge(self.class.schema.project(*args, &block)).(self) end |
#select_group(*args, &block) ⇒ Relation
Select and group by specific columns
733 734 735 736 |
# File 'lib/rom/sql/relation/reading.rb', line 733 def select_group(*args, &block) new_schema = schema.project(*args, &block) new_schema.(self).group(*new_schema) end |
#sum(*args) ⇒ Integer
Returns a result of SQL SUM clause.
265 266 267 |
# File 'lib/rom/sql/relation/reading.rb', line 265 def sum(*args) dataset.__send__(__method__, *args) end |
#union(relation, options = EMPTY_HASH, &block) ⇒ Relation
Adds a UNION clause for relation dataset using second relation dataset
754 755 756 |
# File 'lib/rom/sql/relation/reading.rb', line 754 def union(relation, = EMPTY_HASH, &block) new(dataset.__send__(__method__, relation.dataset, , &block)) end |
#unique?(criteria) ⇒ TrueClass, FalseClass
Return if a restricted relation has 0 tuples
772 773 774 |
# File 'lib/rom/sql/relation/reading.rb', line 772 def unique?(criteria) where(criteria).count.zero? end |
#where(conditions) ⇒ Relation #where(conditions, &block) ⇒ Relation #where(&block) ⇒ Relation
Restrict a relation to match criteria
339 340 341 342 343 344 345 |
# File 'lib/rom/sql/relation/reading.rb', line 339 def where(*args, &block) if block new(dataset.where(*args).where(self.class.schema.restriction(&block))) else new(dataset.__send__(__method__, *args)) end end |