Class: DB::Model::Relation

Inherits:
Object
  • Object
show all
Includes:
Countable, Deletable
Defined in:
lib/db/model/relation.rb

Direct Known Subclasses

Scope, Table

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Countable

#count, #empty?

Methods included from Deletable

#delete

Constructor Details

#initialize(context, model, cache = nil) ⇒ Relation

Returns a new instance of Relation.



29
30
31
32
33
34
35
# File 'lib/db/model/relation.rb', line 29

def initialize(context, model, cache = nil)
  @context = context
  @model = model
  @cache = cache
  
  @select = nil
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



39
40
41
# File 'lib/db/model/relation.rb', line 39

def cache
  @cache
end

#contextObject (readonly)

Returns the value of attribute context.



37
38
39
# File 'lib/db/model/relation.rb', line 37

def context
  @context
end

#modelObject (readonly)

Returns the value of attribute model.



38
39
40
# File 'lib/db/model/relation.rb', line 38

def model
  @model
end

Instance Method Details

#cache_keyObject



139
140
141
# File 'lib/db/model/relation.rb', line 139

def cache_key
  [@model, self.predicate]
end

#create(**attributes) ⇒ Object



43
44
45
# File 'lib/db/model/relation.rb', line 43

def create(**attributes)
  self.new(**attributes).save
end

#each(cache: @cache, &block) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/db/model/relation.rb', line 111

def each(cache: @cache, &block)
  if @cache
    @cache.fetch(self.cache_key) do
      self.select.to_a(@context, @cache)
    end.each(&block)
  else
    self.select.each(@context, &block)
  end
end

#find(*key) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/db/model/relation.rb', line 55

def find(*key)
  if predicate = self.predicate
    predicate = predicate & @model.find_predicate(*key)
  else
    predicate = @model.find_predicate(*key)
  end
  
  return Statement::Select.new(@model,
    where: predicate,
    limit: Statement::Limit::ONE
  ).to_a(@context).first
end

#first(count = nil) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/db/model/relation.rb', line 121

def first(count = nil)
  if count
    self.select.first(@context, count, @cache)
  else
    self.select.first(@context, 1, @cache).first
  end
end

#insert(keys, rows, **attributes) ⇒ Object



51
52
53
# File 'lib/db/model/relation.rb', line 51

def insert(keys, rows, **attributes)
  @model.insert(@context, keys, rows, **attributes)
end

#inspectObject



155
156
157
# File 'lib/db/model/relation.rb', line 155

def inspect
  to_s
end

#new(**attributes) ⇒ Object



47
48
49
# File 'lib/db/model/relation.rb', line 47

def new(**attributes)
  @model.new(@context, {}, @cache).assign(attributes)
end

#predicateObject



78
79
80
# File 'lib/db/model/relation.rb', line 78

def predicate
  nil
end

#preload(name) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/db/model/relation.rb', line 82

def preload(name)
  @cache ||= Cache.new
  
  scopes = []
  self.each do |record|
    scopes << record.send(name)
  end
  
  # Build a buffer of queries:
  query = @context.query
  first = true
  
  scopes.each do |scope|
    query.clause(";") unless first
    first = false
    
    scope.select.append_to(query)
  end
  
  query.call do |connection|
    scopes.each do |scope|
      result = connection.next_result
      scope.update_cache(result)
    end
  end
  
  return self
end

#selectObject



147
148
149
# File 'lib/db/model/relation.rb', line 147

def select
  @select ||= Statement::Select.new(@model, where: self.predicate)
end

#to_aObject



129
130
131
132
133
134
135
136
137
# File 'lib/db/model/relation.rb', line 129

def to_a
  records = []
  
  self.each do |record|
    records << record
  end
  
  return records
end

#to_sObject



151
152
153
# File 'lib/db/model/relation.rb', line 151

def to_s
  "\#<#{self.class} #{@model}>"
end

#update_cache(result) ⇒ Object



143
144
145
# File 'lib/db/model/relation.rb', line 143

def update_cache(result)
  @cache.update(self.cache_key, self.select.apply(@context, result))
end

#where(*arguments, **options, &block) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/db/model/relation.rb', line 68

def where(*arguments, **options, &block)
  where = @model.where(@context, *arguments, **options, &block)
  
  if predicate = self.predicate
    where.predicate &= predicate
  end
  
  return where
end