Class: DataMapper::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/data_mapper/session.rb

Defined Under Namespace

Classes: MaterializationError

Constant Summary collapse

FIND_OPTIONS =
[
  :select, :limit, :class, :include, :reload, :conditions, :order
]

Instance Method Summary collapse

Constructor Details

#initialize(database) ⇒ Session

Returns a new instance of Session.



15
16
17
# File 'lib/data_mapper/session.rb', line 15

def initialize(database)
  @database = database
end

Instance Method Details

#all(options) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/data_mapper/session.rb', line 57

def all(options)
  set = LoadedSet.new(@database)
  reader = @database.query(options)
  instances = reader.map do |hash|
    load(options, hash, set)
  end
  reader.close
  return instances
rescue => error
  @database.log.error(error)
  raise error
end

#create_table(klass) ⇒ Object



171
172
173
174
175
# File 'lib/data_mapper/session.rb', line 171

def create_table(klass)
  @database.connection do |db|
    db.execute(@database.create_table_statement(klass))
  end unless table_exists?(klass)
end

#delete_all(klass) ⇒ Object



161
162
163
# File 'lib/data_mapper/session.rb', line 161

def delete_all(klass)
  @database.execute(@database.delete_statement(klass))
end

#destroy(instance) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/data_mapper/session.rb', line 147

def destroy(instance)
  instance.class.callbacks.execute(:before_destroy, instance)
  result = @database.execute(@database.delete_statement(instance))
  if result.success?
    instance.instance_variable_set(:@new_record, true)
    instance.original_hashes.clear
    instance.class.callbacks.execute(:after_destroy, instance)
  end
  return result.success?
rescue => error
  @database.log.error(error)
  raise error
end

#drop_table(klass) ⇒ Object



177
178
179
180
181
# File 'lib/data_mapper/session.rb', line 177

def drop_table(klass)
  @database.connection do |db|
    db.execute(@database.drop_table_statement(klass))
  end if table_exists?(klass)
end

#find(klass, type_or_id, options = {}, &b) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/data_mapper/session.rb', line 23

def find(klass, type_or_id, options = {}, &b)
  options.merge! b.to_hash if block_given?
  
  results = case type_or_id
    when :first then
      first(@database.select_statement(options.merge(:class => klass, :limit => 1)))
    when :all then
      all(@database.select_statement(options.merge(:class => klass)))
    else
      first(@database.select_statement(options.merge(:class => klass, :id => type_or_id)))
  end
  
  case results
    when Array then results.each { |instance| instance.session = self }
    when Base then results.session = self
  end
  return results
end

#first(options) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/data_mapper/session.rb', line 42

def first(options)
  if options.has_id? && !options.reload?
    instance = identity_map.get(options.klass, options.instance_id)
    return instance unless instance.nil?
  end
  
  reader = @database.query(options)
  instance = reader.eof? ? nil : load(options, reader.next)
  reader.close
  return instance
rescue DatabaseError => de
  de.options = options
  raise de
end

#identity_mapObject



19
20
21
# File 'lib/data_mapper/session.rb', line 19

def identity_map
  @identity_map || ( @identity_map = IdentityMap.new )
end

#insert(instance, inserted_id = nil) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/data_mapper/session.rb', line 118

def insert(instance, inserted_id = nil)
  instance.class.callbacks.execute(:before_create, instance)
  result = @database.execute(@database.insert_statement(instance))
  
  if result.success?
    instance.instance_variable_set(:@new_record, false)
    instance.instance_variable_set(:@id, inserted_id || result.last_inserted_id)
    calculate_original_hashes(instance)
    identity_map.set(instance)
    instance.class.callbacks.execute(:after_create, instance)
  end
  
  return result
rescue => error
  @database.log.error(error)
  raise error
end

#load(options, hash, set = LoadedSet.new(@database)) ⇒ Object



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
98
99
100
101
102
103
104
105
106
107
# File 'lib/data_mapper/session.rb', line 70

def load(options, hash, set = LoadedSet.new(@database))
        
  instance_class = unless hash['type'].nil?
    Kernel::const_get(hash['type'])
  else
    options.klass
  end
  
  mapping = @database[instance_class]
  
  instance_id = mapping.key.type_cast_value(hash['id'])      
  instance = identity_map.get(instance_class, instance_id)
  
  if instance.nil? || options.reload?
    instance ||= instance_class.new        
    instance.class.callbacks.execute(:before_materialize, instance)
    
    instance.instance_variable_set(:@new_record, false)
    hash.each_pair do |name_as_string,raw_value|
      name = name_as_string.to_sym
      if column = mapping.find_by_column_name(name)
        value = column.type_cast_value(raw_value)
        instance.instance_variable_set(column.instance_variable_name, value)
      else
        instance.instance_variable_set("@#{name}", value)
      end
      instance.original_hashes[name] = value.hash
    end

    instance.class.callbacks.execute(:after_materialize, instance)
    
    identity_map.set(instance)
  end
  
  instance.instance_variable_set(:@loaded_set, set)
  set.instances << instance
  return instance
end

#logObject



220
221
222
# File 'lib/data_mapper/session.rb', line 220

def log
  @database.log
end

#query(*args) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/data_mapper/session.rb', line 192

def query(*args)
  sql = args.shift
  
  unless args.empty?
    sql.gsub!(/\?/) do |x|
      @database.quote_value(args.shift)
    end
  end

  reader = @database.connection do |db|
    db.query(sql)
  end
  
  columns = reader.columns.keys
  klass = Struct.new(*columns.map { |c| c.to_sym })
  
  rows = reader.map do |row|
    klass.new(*columns.map { |c| row[c] })
  end
  
  reader.close
  return rows
end

#save(instance) ⇒ Object



109
110
111
112
113
114
115
116
# File 'lib/data_mapper/session.rb', line 109

def save(instance)
  return false unless instance.dirty?
  instance.class.callbacks.execute(:before_save, instance)
  result = instance.new_record? ? insert(instance) : update(instance)
  instance.session = self
  instance.class.callbacks.execute(:after_save, instance)
  result.success?
end

#schemaObject



216
217
218
# File 'lib/data_mapper/session.rb', line 216

def schema
  @database.schema
end

#table_exists?(klass) ⇒ Boolean

Returns:

  • (Boolean)


183
184
185
186
187
188
189
190
# File 'lib/data_mapper/session.rb', line 183

def table_exists?(klass)
  reader = @database.connection do |db|
    db.query(@database.table_exists_statement(klass))
  end
  result = !reader.eof?
  reader.close
  result
end

#truncate(klass) ⇒ Object



165
166
167
168
169
# File 'lib/data_mapper/session.rb', line 165

def truncate(klass)
  @database.connection do |db|
    db.execute(@database.truncate_table_statement(klass))
  end
end

#update(instance) ⇒ Object



136
137
138
139
140
141
142
143
144
145
# File 'lib/data_mapper/session.rb', line 136

def update(instance)
  instance.class.callbacks.execute(:before_update, instance)
  result = @database.execute(@database.update_statement(instance))
  calculate_original_hashes(instance)
  instance.class.callbacks.execute(:after_update, instance)
  return result
rescue => error
  @database.log.error(error)
  raise error
end