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.
-
#having(*args, &block) ⇒ Relation
Restrict a relation to match grouping criteria.
-
#inner_join(*args, &block) ⇒ Relation
Join with another relation using INNER JOIN.
-
#invert ⇒ Relation
Inverts the current WHERE and HAVING clauses.
-
#last ⇒ Hash
Get last tuple from the relation.
-
#left_join(*args, &block) ⇒ Relation
Join other relation using LEFT OUTER JOIN.
-
#limit(*args, &block) ⇒ 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(*args, &block) ⇒ 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 = Inflector.singularize(table)) ⇒ Relation
Prefix all columns in a relation.
-
#project(*names) ⇒ Relation
Project 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.
-
#select(*args, &block) ⇒ Relation
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.
269 270 271 |
# File 'lib/rom/sql/relation/reading.rb', line 269 def avg(*args) dataset.__send__(__method__, *args) end |
#count ⇒ Relation
Return relation count
32 33 34 |
# File 'lib/rom/sql/relation/reading.rb', line 32 def count dataset.count end |
#distinct(*args, &block) ⇒ Relation
Returns a copy of the relation with a SQL DISTINCT clause.
213 214 215 |
# File 'lib/rom/sql/relation/reading.rb', line 213 def distinct(*args, &block) __new__(dataset.__send__(__method__, *args, &block)) end |
#exclude(*args, &block) ⇒ Relation
Restrict a relation to not match criteria
304 305 306 |
# File 'lib/rom/sql/relation/reading.rb', line 304 def exclude(*args, &block) __new__(dataset.__send__(__method__, *args, &block)) end |
#fetch(pk) ⇒ Relation
Fetch a tuple identified by the pk
19 20 21 |
# File 'lib/rom/sql/relation/reading.rb', line 19 def fetch(pk) by_pk(pk).one! end |
#first ⇒ Hash
Get first tuple from the relation
45 46 47 |
# File 'lib/rom/sql/relation/reading.rb', line 45 def first dataset.first end |
#group(*args, &block) ⇒ Relation
Group by specific columns
439 440 441 |
# File 'lib/rom/sql/relation/reading.rb', line 439 def group(*args, &block) __new__(dataset.__send__(__method__, *args, &block)) end |
#group_and_count(*args, &block) ⇒ Relation
Group by specific columns and count by group
454 455 456 |
# File 'lib/rom/sql/relation/reading.rb', line 454 def group_and_count(*args, &block) __new__(dataset.__send__(__method__, *args, &block)) end |
#having(*args, &block) ⇒ Relation
Restrict a relation to match grouping criteria
322 323 324 |
# File 'lib/rom/sql/relation/reading.rb', line 322 def having(*args, &block) __new__(dataset.__send__(__method__, *args, &block)) end |
#inner_join(*args, &block) ⇒ Relation
Join with another relation using INNER JOIN
410 411 412 |
# File 'lib/rom/sql/relation/reading.rb', line 410 def inner_join(*args, &block) __new__(dataset.__send__(__method__, *args, &block)) 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.
338 339 340 |
# File 'lib/rom/sql/relation/reading.rb', line 338 def invert __new__(dataset.invert) end |
#last ⇒ Hash
Get last tuple from the relation
58 59 60 |
# File 'lib/rom/sql/relation/reading.rb', line 58 def last dataset.last end |
#left_join(*args, &block) ⇒ Relation
Join other relation using LEFT OUTER JOIN
425 426 427 |
# File 'lib/rom/sql/relation/reading.rb', line 425 def left_join(*args, &block) __new__(dataset.__send__(__method__, *args, &block)) end |
#limit(*args, &block) ⇒ Relation
Limit a relation to a specific number of tuples
381 382 383 |
# File 'lib/rom/sql/relation/reading.rb', line 381 def limit(*args, &block) __new__(dataset.__send__(__method__, *args, &block)) end |
#map(key = nil, &block) ⇒ Object
Map tuples from the relation
121 122 123 124 125 126 127 |
# File 'lib/rom/sql/relation/reading.rb', line 121 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.
255 256 257 |
# File 'lib/rom/sql/relation/reading.rb', line 255 def max(*args) dataset.__send__(__method__, *args) end |
#min(*args) ⇒ Object
Returns a result of SQL MIN clause.
241 242 243 |
# File 'lib/rom/sql/relation/reading.rb', line 241 def min(*args) dataset.__send__(__method__, *args) end |
#offset(*args, &block) ⇒ Relation
Set offset for the relation
395 396 397 |
# File 'lib/rom/sql/relation/reading.rb', line 395 def offset(*args, &block) __new__(dataset.__send__(__method__, *args, &block)) end |
#order(*args, &block) ⇒ Relation
Set order for the relation
352 353 354 |
# File 'lib/rom/sql/relation/reading.rb', line 352 def order(*args, &block) __new__(dataset.__send__(__method__, *args, &block)) end |
#pluck(name) ⇒ Array
Pluck values from a specific column
138 139 140 |
# File 'lib/rom/sql/relation/reading.rb', line 138 def pluck(name) map(name) end |
#prefix(name = Inflector.singularize(table)) ⇒ Relation
Prefix all columns in a relation
This method is intended to be used internally within a relation object
75 76 77 |
# File 'lib/rom/sql/relation/reading.rb', line 75 def prefix(name = Inflector.singularize(table)) rename(header.prefix(name).to_h) end |
#project(*names) ⇒ Relation
Project a relation
This method is intended to be used internally within a relation object
154 155 156 |
# File 'lib/rom/sql/relation/reading.rb', line 154 def project(*names) select(*header.project(*names)) end |
#qualified ⇒ Relation
Qualifies all columns in a relation
This method is intended to be used internally within a relation object
89 90 91 |
# File 'lib/rom/sql/relation/reading.rb', line 89 def qualified select(*qualified_columns) end |
#qualified_columns ⇒ Array<Symbol>
Return a list of qualified column names
This method is intended to be used internally within a relation object
104 105 106 |
# File 'lib/rom/sql/relation/reading.rb', line 104 def qualified_columns header.qualified.to_a end |
#read(sql) ⇒ SQL::Relation
Return a new relation from a raw SQL string
521 522 523 |
# File 'lib/rom/sql/relation/reading.rb', line 521 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
171 172 173 |
# File 'lib/rom/sql/relation/reading.rb', line 171 def rename() select(*header.rename()) end |
#reverse(*args, &block) ⇒ Relation
Reverse the order of the relation
364 365 366 |
# File 'lib/rom/sql/relation/reading.rb', line 364 def reverse(*args, &block) __new__(dataset.__send__(__method__, *args, &block)) end |
#select(*args, &block) ⇒ Relation
Select specific columns for select clause
184 185 186 |
# File 'lib/rom/sql/relation/reading.rb', line 184 def select(*args, &block) __new__(dataset.__send__(__method__, *args, &block)) end |
#select_append(*args, &block) ⇒ Relation
Append specific columns to select clause
199 200 201 |
# File 'lib/rom/sql/relation/reading.rb', line 199 def select_append(*args, &block) __new__(dataset.__send__(__method__, *args, &block)) end |
#select_group(*args, &block) ⇒ Relation
Select and group by specific columns
469 470 471 |
# File 'lib/rom/sql/relation/reading.rb', line 469 def select_group(*args, &block) __new__(dataset.__send__(__method__, *args, &block)) end |
#sum(*args) ⇒ Integer
Returns a result of SQL SUM clause.
227 228 229 |
# File 'lib/rom/sql/relation/reading.rb', line 227 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
489 490 491 |
# File 'lib/rom/sql/relation/reading.rb', line 489 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
507 508 509 |
# File 'lib/rom/sql/relation/reading.rb', line 507 def unique?(criteria) where(criteria).count.zero? end |
#where(*args, &block) ⇒ Relation
Restrict a relation to match criteria
If block is passed it’ll be executed in the context of a condition builder object.
290 291 292 |
# File 'lib/rom/sql/relation/reading.rb', line 290 def where(*args, &block) __new__(dataset.__send__(__method__, *args, &block)) end |