Class: ROM::SQL::Relation

Inherits:
Relation
  • Object
show all
Extended by:
ClassMethods
Includes:
Associations, Inspection
Defined in:
lib/rom/sql/relation.rb,
lib/rom/sql/relation/inspection.rb,
lib/rom/sql/relation/associations.rb,
lib/rom/sql/relation/class_methods.rb

Overview

Sequel-specific relation extensions

Defined Under Namespace

Modules: Associations, ClassMethods, Inspection

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

finalize, inherited, many_to_many, many_to_one, one_to_many

Methods included from Associations

#association_join, #association_left_join, #graph, #graph_join

Methods included from Inspection

#model, #primary_key

Constructor Details

#initialize(dataset, registry = {}) ⇒ Relation

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.

Returns a new instance of Relation.



20
21
22
23
# File 'lib/rom/sql/relation.rb', line 20

def initialize(dataset, registry = {})
  super
  @table = dataset.opts[:from].first
end

Instance Attribute Details

#headerHeader (readonly)

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 a header for this relation

Returns:



30
31
32
# File 'lib/rom/sql/relation.rb', line 30

def header
  @header
end

#tableObject (readonly)

Returns the value of attribute table.



17
18
19
# File 'lib/rom/sql/relation.rb', line 17

def table
  @table
end

Instance Method Details

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

  • (Array<Symbol>)


39
40
41
# File 'lib/rom/sql/relation.rb', line 39

def columns
  dataset.columns
end

#countRelation

Return relation count

Examples:

users.count # => 12

Returns:



100
101
102
# File 'lib/rom/sql/relation.rb', line 100

def count
  dataset.count
end

#delete(*args, &block) ⇒ Relation

Delete tuples from the relation

Examples:

users.delete # deletes all
users.where(name: 'Jane').delete # delete tuples
                                   from restricted relation

Returns:



321
322
323
# File 'lib/rom/sql/relation.rb', line 321

def delete(*args, &block)
  dataset.delete(*args, &block)
end

#distinct(*args, &block) ⇒ Relation

Returns a copy of the relation with a SQL DISTINCT clause.

Examples:

users.distinct(:country)

Returns:



136
137
138
# File 'lib/rom/sql/relation.rb', line 136

def distinct(*args, &block)
  __new__(dataset.__send__(__method__, *args, &block))
end

#exclude(*args, &block) ⇒ Relation

Restrict a relation to not match criteria

Examples:

users.exclude(name: 'Jane')

Returns:



160
161
162
# File 'lib/rom/sql/relation.rb', line 160

def exclude(*args, &block)
  __new__(dataset.__send__(__method__, *args, &block))
end

#firstRelation

Get first tuple from the relation

Examples:

users.first

Returns:



76
77
78
# File 'lib/rom/sql/relation.rb', line 76

def first
  dataset.first
end

#group(*args, &block) ⇒ Relation

Group by specific columns

Examples:

tasks.group(:user_id)

Returns:



254
255
256
# File 'lib/rom/sql/relation.rb', line 254

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

Examples:

tasks.group_and_count(:user_id)
# => [{ user_id: 1, count: 2 }, { user_id: 2, count: 3 }]

Returns:



267
268
269
# File 'lib/rom/sql/relation.rb', line 267

def group_and_count(*args, &block)
  __new__(dataset.__send__(__method__, *args, &block))
end

#inner_join(*args, &block) ⇒ Relation

Join other relation using inner join

Parameters:

  • relation (Symbol)

    name

  • join (Hash)

    keys

Returns:



230
231
232
# File 'lib/rom/sql/relation.rb', line 230

def inner_join(*args, &block)
  __new__(dataset.__send__(__method__, *args, &block))
end

#insert(*args, &block) ⇒ Relation

Insert tuple into relation

Examples:

users.insert(name: 'Jane')

Parameters:

  • tuple (Hash)

Returns:



294
295
296
# File 'lib/rom/sql/relation.rb', line 294

def insert(*args, &block)
  dataset.insert(*args, &block)
end

#lastRelation

Get last tuple from the relation

Examples:

users.first

Returns:



88
89
90
# File 'lib/rom/sql/relation.rb', line 88

def last
  dataset.last
end

#left_join(*args, &block) ⇒ Relation

Join other relation using left outer join

Parameters:

  • relation (Symbol)

    name

  • join (Hash)

    keys

Returns:



242
243
244
# File 'lib/rom/sql/relation.rb', line 242

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

Examples:

users.limit(1)

Returns:



196
197
198
# File 'lib/rom/sql/relation.rb', line 196

def limit(*args, &block)
  __new__(dataset.__send__(__method__, *args, &block))
end

#map(&block) ⇒ Object

Map tuples from the relation

Examples:

users.map { |user| ... }


218
219
220
# File 'lib/rom/sql/relation.rb', line 218

def map(&block)
  to_enum.map(&block)
end

#offset(*args, &block) ⇒ Relation

Set offset for the relation

Examples:

users.limit(10).offset(2)

Returns:



208
209
210
# File 'lib/rom/sql/relation.rb', line 208

def offset(*args, &block)
  __new__(dataset.__send__(__method__, *args, &block))
end

#order(*args, &block) ⇒ Relation

Set order for the relation

Examples:

users.order(:name)

Returns:



172
173
174
# File 'lib/rom/sql/relation.rb', line 172

def order(*args, &block)
  __new__(dataset.__send__(__method__, *args, &block))
end

#prefix(name = Inflector.singularize(table)) ⇒ Object



54
55
56
# File 'lib/rom/sql/relation.rb', line 54

def prefix(name = Inflector.singularize(table))
  rename(header.prefix(name).to_h)
end

#project(*names) ⇒ Object



44
45
46
# File 'lib/rom/sql/relation.rb', line 44

def project(*names)
  select(*header.project(*names))
end

#qualifiedObject



59
60
61
# File 'lib/rom/sql/relation.rb', line 59

def qualified
  select(*qualified_columns)
end

#qualified_columnsObject



64
65
66
# File 'lib/rom/sql/relation.rb', line 64

def qualified_columns
  header.qualified.to_a
end

#rename(options) ⇒ Object



49
50
51
# File 'lib/rom/sql/relation.rb', line 49

def rename(options)
  select(*header.rename(options))
end

#reverse(*args, &block) ⇒ Relation

Reverse the order of the relation

Examples:

users.order(:name).reverse

Returns:



184
185
186
# File 'lib/rom/sql/relation.rb', line 184

def reverse(*args, &block)
  __new__(dataset.__send__(__method__, *args, &block))
end

#select(*args, &block) ⇒ Relation

Select specific columns for select clause

Examples:

users.select(:id, :name)

Returns:



112
113
114
# File 'lib/rom/sql/relation.rb', line 112

def select(*args, &block)
  __new__(dataset.__send__(__method__, *args, &block))
end

#select_append(*args, &block) ⇒ Relation

Append specific columns to select clause

Examples:

users.select(:id, :name).select_append(:email)

Returns:



124
125
126
# File 'lib/rom/sql/relation.rb', line 124

def select_append(*args, &block)
  __new__(dataset.__send__(__method__, *args, &block))
end

#select_group(*args, &block) ⇒ Relation

Select and group by specific columns

Examples:

tasks.select_group(:user_id)
# => [{ user_id: 1 }, { user_id: 2 }]

Returns:



280
281
282
# File 'lib/rom/sql/relation.rb', line 280

def select_group(*args, &block)
  __new__(dataset.__send__(__method__, *args, &block))
end

#unique?(criteria) ⇒ Relation

Return if a restricted relation has 0 tuples

Examples:

users.unique?(email: '[email protected]') # true

users.insert(email: '[email protected]')

users.unique?(email: '[email protected]') # false

Parameters:

  • criteria (Hash)

    hash for the where clause

Returns:



339
340
341
# File 'lib/rom/sql/relation.rb', line 339

def unique?(criteria)
  where(criteria).count.zero?
end

#update(*args, &block) ⇒ Relation

Update tuples in the relation

Examples:

users.update(name: 'Jane')
users.where(name: 'Jane').update(name: 'Jane Doe')

Returns:



307
308
309
# File 'lib/rom/sql/relation.rb', line 307

def update(*args, &block)
  dataset.update(*args, &block)
end

#where(*args, &block) ⇒ Relation

Restrict a relation to match criteria

Examples:

users.where(name: 'Jane')

Returns:



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

def where(*args, &block)
  __new__(dataset.__send__(__method__, *args, &block))
end