public this repo is viewable by everyone
Fork of sam/dm-core
Description: DataMapper - Core
Homepage: http://datamapper.org
Clone URL: git://github.com/myabc/dm-core.git
Merge branch 'master' of git://github.com/sam/dm-core
myabc (author)
4 days ago
commit  c7f6fe5ad63bdcb157e686d0349ba0f4f325889d
tree    8637c0029ca194bcdbe5f9cb652b8a9abeef392c
parent  a7e8027aa54899bf10d831e5c01d5b95b073f3b1 parent  d18d2d5d4a8feba60d4e59a1c9683542831ae1bb
...
102
103
104
105
 
106
107
108
...
102
103
104
 
105
106
107
108
0
@@ -102,7 +102,7 @@ now? In DataMapper you control the mappings:
0
 
0
   class Fruit
0
     include DataMapper::Resource
0
- set_table_name 'frt'
0
+ storage_names[:repo] = 'frt'
0
     property :name, String, :column => 'col2Name'
0
   end
0
 
...
152
153
154
155
156
157
158
 
159
160
161
162
163
164
165
166
167
 
 
 
168
 
 
 
 
169
170
171
...
179
180
181
182
 
183
184
185
186
187
188
189
190
191
192
193
194
 
 
 
 
 
 
 
195
196
197
 
 
 
198
199
200
201
202
203
204
205
206
207
208
209
 
 
 
 
 
 
210
 
211
212
213
214
215
216
217
218
 
 
219
 
 
220
221
222
...
224
225
226
227
228
229
230
231
232
 
 
233
234
235
...
239
240
241
242
243
244
245
246
247
 
 
248
249
250
...
254
255
256
257
258
259
260
261
262
 
 
263
264
265
...
281
282
283
284
285
 
286
287
 
288
289
290
...
296
297
298
299
 
300
301
302
...
559
560
561
 
562
563
564
...
570
571
572
 
573
574
575
...
152
153
154
 
155
156
 
157
158
 
 
 
 
 
 
 
 
159
160
161
162
163
164
165
166
167
168
169
...
177
178
179
 
180
181
 
 
 
 
 
 
 
 
 
 
 
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
 
 
 
 
 
 
 
200
201
202
203
204
205
206
207
208
209
210
211
 
 
 
 
212
213
214
215
216
217
218
219
...
221
222
223
 
224
225
 
 
226
227
228
229
230
231
...
235
236
237
 
238
239
 
 
240
241
242
243
244
245
...
249
250
251
 
252
253
 
 
254
255
256
257
258
259
...
275
276
277
 
278
279
280
281
282
283
284
285
...
291
292
293
 
294
295
296
297
...
554
555
556
557
558
559
560
...
566
567
568
569
570
571
572
0
@@ -152,20 +152,18 @@ module DataMapper
0
 
0
         connection = create_connection
0
         command = connection.create_command(sql)
0
-
0
         result = command.execute_non_query(*values)
0
 
0
- close_connection(connection)
0
+ return false if result.to_i != 1
0
 
0
- if result.to_i == 1
0
- key = resource.class.key(name)
0
- if key.size == 1 && (identity_field = key.first).serial?
0
- resource.instance_variable_set(identity_field.instance_variable_name, result.insert_id)
0
- end
0
- true
0
- else
0
- false
0
+ key = resource.class.key(name)
0
+ if key.size == 1 && (identity_field = key.first).serial?
0
+ resource.instance_variable_set(identity_field.instance_variable_name, result.insert_id)
0
         end
0
+
0
+ true
0
+ ensure
0
+ close_connection(connection) if connection
0
       end
0
 
0
       def exists?(storage_name)
0
@@ -179,44 +177,43 @@ module DataMapper
0
         set = Collection.new(repository, resource, properties_with_indexes)
0
 
0
         sql = read_statement(resource, key)
0
- DataMapper.logger.debug("READ: #{sql}")
0
+ DataMapper.logger.debug("READ: #{sql} KEY: #{key.inspect}")
0
 
0
- begin
0
- connection = create_connection
0
- command = connection.create_command(sql)
0
- command.set_types(properties.map { |property| property.primitive })
0
- reader = command.execute_reader(*key)
0
- while(reader.next!)
0
- set.load(reader.values)
0
- end
0
- ensure
0
- reader.close if reader
0
- close_connection(connection)
0
+ connection = create_connection
0
+ command = connection.create_command(sql)
0
+ command.set_types(properties.map { |property| property.primitive })
0
+ reader = command.execute_reader(*key)
0
+
0
+ while(reader.next!)
0
+ set.load(reader.values)
0
         end
0
 
0
         set.first
0
+ ensure
0
+ reader.close if reader
0
+ close_connection(connection) if connection
0
       end
0
 
0
       def update(repository, resource)
0
         properties = resource.dirty_attributes
0
 
0
- if properties.empty?
0
- return false
0
- else
0
- sql = update_statement(resource.class, properties)
0
- values = properties.map { |property| resource.instance_variable_get(property.instance_variable_name) }
0
- parameters = (values + resource.key)
0
- DataMapper.logger.debug("UPDATE: #{sql} PARAMETERS: #{parameters.inspect}")
0
+ return false if properties.empty?
0
+
0
+ sql = update_statement(resource.class, properties)
0
+ values = properties.map { |property| resource.instance_variable_get(property.instance_variable_name) }
0
+ parameters = (values + resource.key)
0
+ DataMapper.logger.debug("UPDATE: #{sql} PARAMETERS: #{parameters.inspect}")
0
 
0
+ begin
0
           connection = create_connection
0
           command = connection.create_command(sql)
0
 
0
           affected_rows = command.execute_non_query(*parameters).to_i
0
-
0
- close_connection(connection)
0
-
0
- affected_rows == 1
0
+ ensure
0
+ close_connection(connection) if connection
0
         end
0
+
0
+ affected_rows == 1
0
       end
0
 
0
       def delete(repository, resource)
0
@@ -224,12 +221,11 @@ module DataMapper
0
 
0
         connection = create_connection
0
         command = connection.create_command(delete_statement(resource.class))
0
-
0
         affected_rows = command.execute_non_query(*key).to_i
0
 
0
- close_connection(connection)
0
-
0
         affected_rows == 1
0
+ ensure
0
+ close_connection(connection) if connection
0
       end
0
 
0
       def create_model_storage(repository, model)
0
@@ -239,12 +235,11 @@ module DataMapper
0
 
0
         connection = create_connection
0
         command = connection.create_command(sql)
0
-
0
         result = command.execute_non_query
0
 
0
- close_connection(connection)
0
-
0
         result.to_i == 1
0
+ ensure
0
+ close_connection(connection) if connection
0
       end
0
 
0
       def destroy_model_storage(repository, model)
0
@@ -254,12 +249,11 @@ module DataMapper
0
 
0
         connection = create_connection
0
         command = connection.create_command(sql)
0
-
0
         result = command.execute_non_query
0
 
0
- close_connection(connection)
0
-
0
         result.to_i == 1
0
+ ensure
0
+ close_connection(connection) if connection
0
       end
0
 
0
       #
0
@@ -281,10 +275,11 @@ module DataMapper
0
         Collection.new(repository, model, properties_with_indexes) do |set|
0
           DataMapper.logger.debug("READ_SET: #{sql} PARAMETERS: #{parameters.inspect}")
0
 
0
- connection = create_connection
0
           begin
0
+ connection = create_connection
0
             command = connection.create_command(sql)
0
             command.set_types(properties.map { |property| property.primitive })
0
+
0
             reader = command.execute_reader(*parameters)
0
 
0
             while(reader.next!)
0
@@ -296,7 +291,7 @@ module DataMapper
0
             raise se
0
           ensure
0
             reader.close if reader
0
- close_connection(connection)
0
+ close_connection(connection) if connection
0
           end
0
         end
0
       end
0
@@ -559,6 +554,7 @@ module DataMapper
0
         def equality_operator(query, model_name, operator, property, qualify, value)
0
           case value
0
             when Array then "#{property_to_column_name(model_name, property, qualify)} IN ?"
0
+ when Range then "#{property_to_column_name(model_name, property, qualify)} BETWEEN ?"
0
             when NilClass then "#{property_to_column_name(model_name, property, qualify)} IS NULL"
0
             when DataMapper::Query then
0
               query.merge_sub_select_conditions(operator, property, value)
0
@@ -570,6 +566,7 @@ module DataMapper
0
         def inequality_operator(query, model_name, operator, property, qualify, value)
0
           case value
0
             when Array then "#{property_to_column_name(model_name, property, qualify)} NOT IN ?"
0
+ when Range then "#{property_to_column_name(model_name, property, qualify)} NOT BETWEEN ?"
0
             when NilClass then "#{property_to_column_name(model_name, property, qualify)} IS NOT NULL"
0
             when DataMapper::Query then
0
               query.merge_sub_select_conditions(operator, property, value)
...
46
47
48
49
50
51
52
53
54
55
56
57
58
59
 
 
 
60
61
62
...
76
77
78
 
 
 
 
 
 
79
80
81
...
46
47
48
 
 
49
50
51
52
53
54
 
 
 
55
56
57
58
59
60
...
74
75
76
77
78
79
80
81
82
83
84
85
0
@@ -46,17 +46,15 @@ module DataMapper
0
           self
0
         end
0
 
0
- alias << push
0
-
0
         def unshift(*resources)
0
           append_resource(resources)
0
           @children.unshift(*resources)
0
           self
0
         end
0
 
0
- def clear
0
- each { |resource| remove_resource(resource) }
0
- @children.clear
0
+ def <<(resource)
0
+ append_resource([ resource ])
0
+ @children << resource
0
           self
0
         end
0
 
0
@@ -76,6 +74,12 @@ module DataMapper
0
           remove_resource(@children.delete_at(index))
0
         end
0
 
0
+ def clear
0
+ each { |resource| remove_resource(resource) }
0
+ @children.clear
0
+ self
0
+ end
0
+
0
         def save
0
           save_resources(@dirty_children)
0
           @dirty_children = []
...
7
8
9
10
 
11
12
13
14
15
 
 
 
 
 
16
17
18
19
 
20
21
 
22
23
24
25
 
26
27
28
29
 
30
31
32
33
 
34
35
36
37
38
39
40
 
...
7
8
9
 
10
11
12
 
 
 
13
14
15
16
17
18
19
20
 
21
22
 
23
24
25
26
 
27
28
29
30
 
31
32
33
34
 
35
36
37
38
39
40
 
41
42
0
@@ -7,33 +7,35 @@ module DataMapper
0
       model.extend ClassMethods
0
       DataMapper::AutoMigrator.models << model
0
     end
0
-
0
+
0
     module ClassMethods
0
       def auto_migrate!(repository_name = default_repository_name)
0
- self.relationships(repository_name).each_pair { |name, relationship| relationship.child_key }
0
- repository(repository_name).adapter.destroy_model_storage(repository(repository_name), self)
0
- repository(repository_name).adapter.create_model_storage(repository(repository_name), self)
0
+ repository(repository_name) do |r|
0
+ relationships(r.name).each_value { |relationship| relationship.child_key }
0
+ r.adapter.destroy_model_storage(r, self)
0
+ r.adapter.create_model_storage(r, self)
0
+ end
0
       end
0
     end
0
   end
0
-
0
+
0
   class AutoMigrator
0
-
0
+
0
     def self.models
0
       @@models ||= []
0
     end
0
-
0
+
0
     def self.auto_migrate(repository_name)
0
       # First ensure that association keys are forced to load.
0
       models.each do |model|
0
- model.relationships(repository_name).each_pair do |name, relationship|
0
+ model.relationships(repository_name).each_value do |relationship|
0
           relationship.child_key
0
         end
0
       end
0
-
0
+
0
       models.each do |model|
0
         model.auto_migrate!(repository_name)
0
       end
0
     end
0
   end
0
-end # module DataMapper
0
\ No newline at end of file
0
+end # module DataMapper
...
6
7
8
9
 
10
11
12
...
6
7
8
 
9
10
11
12
0
@@ -6,7 +6,7 @@ module DataMapper
0
   end
0
   
0
   class DestructiveMigrator < Migrator
0
- def self.migrate(repository)
0
+ def self.migrate(repository_name)
0
       models.each do |model|
0
         model.auto_migrate!
0
       end
...
18
19
20
21
 
22
23
 
24
25
26
...
18
19
20
 
21
22
 
23
24
25
26
0
@@ -18,9 +18,9 @@ module DataMapper
0
       end
0
     end
0
     
0
- def self.migrate(repository)
0
+ def self.migrate(repository_name)
0
       subclasses.collect do |migrator|
0
- migrator.migrate(repository)
0
+ migrator.migrate(repository_name)
0
       end.flatten
0
     end
0
   end
...
273
274
275
 
 
 
 
 
276
277
278
...
439
440
441
 
442
443
444
...
273
274
275
276
277
278
279
280
281
282
283
...
444
445
446
447
448
449
450
0
@@ -273,6 +273,11 @@ module DataMapper
0
     end
0
 
0
     def hash
0
+ if @custom && !@bound
0
+ @type.bind(self)
0
+ @bound = true
0
+ end
0
+
0
       return @model.hash + @name.hash
0
     end
0
 
0
@@ -439,6 +444,7 @@ module DataMapper
0
 
0
       @model.auto_generate_validations(self) if @model.respond_to?(:auto_generate_validations)
0
       @model.property_serialization_setup(self) if @model.respond_to?(:property_serialization_setup)
0
+
0
     end
0
 
0
     def determine_visibility # :nodoc:
...
14
15
16
 
17
18
19
...
14
15
16
17
18
19
20
0
@@ -14,6 +14,7 @@ module DataMapper
0
 
0
     def add(*properties)
0
       @entries.push(*properties)
0
+ properties.each { |property| property.hash }
0
       self
0
     end
0
 
...
102
103
104
105
 
106
107
108
109
 
110
111
112
...
102
103
104
 
105
106
107
108
 
109
110
111
112
0
@@ -102,11 +102,11 @@ module DataMapper
0
     end
0
     
0
     def migrate!
0
- Migrator.migrate(self)
0
+ Migrator.migrate(name)
0
     end
0
     
0
     def auto_migrate!
0
- AutoMigrator.auto_migrate(self)
0
+ AutoMigrator.auto_migrate(name)
0
     end
0
 
0
     #
...
5
6
7
8
 
9
10
11
 
12
13
14
...
5
6
7
 
8
9
10
 
11
12
13
14
0
@@ -5,10 +5,10 @@ module DataMapper
0
     attr_reader :transaction_primitives, :adapters, :state
0
 
0
     #
0
- # Create a new DataMapper::Adapters::Transaction
0
+ # Create a new DataMapper::Transaction
0
     #
0
     # ==== Parameters
0
- # See DataMapper::Adapters::Transaction#link
0
+ # See DataMapper::Transaction#link
0
     #
0
     # In fact, it just calls #link with the given arguments at the end of the constructor.
0
     #
...
149
150
151
 
 
 
 
 
 
152
153
154
...
149
150
151
152
153
154
155
156
157
158
159
160
0
@@ -149,6 +149,12 @@ module DataMapper
0
     def self.load(value, property)
0
       value
0
     end
0
+
0
+ def self.bind(property)
0
+ # This method should not modify the state of this type class, and
0
+ # should produce no side-effects on the type class. It's just a
0
+ # hook to allow your custom-type to modify the property it's bound to.
0
+ end
0
 
0
   end # class Type
0
 
...
2
3
4
 
 
...
2
3
4
5
6
0
@@ -2,3 +2,5 @@ dir = Pathname(__FILE__).dirname.expand_path / 'types'
0
 
0
 require dir / 'boolean'
0
 require dir / 'text'
0
+require dir / 'paranoid_datetime'
0
+require dir / 'paranoid_boolean'
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
0
@@ -0,0 +1,23 @@
0
+module DataMapper
0
+ module Types
0
+ class ParanoidBoolean < DataMapper::Type(Boolean)
0
+ primitive TrueClass
0
+ default false
0
+
0
+ def self.bind(property)
0
+ model = property.model
0
+ repository = property.repository
0
+
0
+ model.class_eval <<-EOS
0
+ def destroy
0
+ attribute_set(#{property.name.inspect}, true)
0
+ save
0
+ end
0
+ EOS
0
+
0
+ model.send(:scope_stack) << DataMapper::Query.new(repository, model, property.name => nil)
0
+
0
+ end
0
+ end
0
+ end
0
+end
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
0
@@ -0,0 +1,22 @@
0
+module DataMapper
0
+ module Types
0
+ class ParanoidDateTime < DataMapper::Type(DateTime)
0
+ primitive DateTime
0
+
0
+ def self.bind(property)
0
+ model = property.model
0
+ repository = property.repository
0
+
0
+ model.class_eval <<-EOS
0
+ def destroy
0
+ attribute_set(#{property.name.inspect}, DateTime::now)
0
+ save
0
+ end
0
+ EOS
0
+
0
+ model.send(:scope_stack) << DataMapper::Query.new(repository, model, property.name => nil)
0
+
0
+ end
0
+ end
0
+ end
0
+end
0
\ No newline at end of file
...
124
125
126
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
128
129
...
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
0
@@ -124,6 +124,21 @@ begin
0
         yard.should respond_to(:engine)
0
         yard.should respond_to(:engine=)
0
       end
0
+
0
+ it "#many_to_one with namespaced models" do
0
+ module FlightlessBirds
0
+ class Ostrich
0
+ include DataMapper::Resource
0
+ property :id, Fixnum, :serial => true
0
+ property :name, String
0
+ many_to_one :sky # there's something sad about this :'(
0
+ end
0
+ end
0
+
0
+ FlightlessBirds::Ostrich.properties.slice(:sky_id).should_not be_empty
0
+
0
+ end
0
+
0
 
0
       it "should load the associated instance" do
0
         y = repository(:sqlite3).all(Yard, :id => 1).first
...
258
259
260
261
262
263
264
265
266
267
268
269
...
275
276
277
278
 
279
280
281
282
283
 
284
285
286
287
288
289
 
 
290
291
292
293
294
295
296
 
 
297
298
299
...
258
259
260
 
 
 
 
 
 
261
262
263
...
269
270
271
 
272
273
274
275
276
 
277
278
279
280
281
 
 
282
283
284
285
286
287
288
 
 
289
290
291
292
293
0
@@ -258,12 +258,6 @@ begin
0
           property :id, Fixnum, :serial => true
0
           property :name, String
0
           property :port, String
0
-
0
- class << self
0
- def property_by_name(name)
0
- properties(repository.name)[name]
0
- end
0
- end
0
         end
0
 
0
         SailBoat.auto_migrate!(:postgres)
0
@@ -275,25 +269,25 @@ begin
0
 
0
       it "should order results" do
0
         result = repository(:postgres).all(SailBoat,{:order => [
0
- DataMapper::Query::Direction.new(SailBoat.property_by_name(:name), :asc)
0
+ DataMapper::Query::Direction.new(SailBoat.properties[:name], :asc)
0
         ]})
0
         result[0].id.should == 1
0
 
0
         result = repository(:postgres).all(SailBoat,{:order => [
0
- DataMapper::Query::Direction.new(SailBoat.property_by_name(:port), :asc)
0
+ DataMapper::Query::Direction.new(SailBoat.properties[:port], :asc)
0
         ]})
0
         result[0].id.should == 3
0
 
0
         result = repository(:postgres).all(SailBoat,{:order => [
0
- DataMapper::Query::Direction.new(SailBoat.property_by_name(:name), :asc),
0
- DataMapper::Query::Direction.new(SailBoat.property_by_name(:port), :asc)
0
+ DataMapper::Query::Direction.new(SailBoat.properties[:name], :asc),
0
+ DataMapper::Query::Direction.new(SailBoat.properties[:port], :asc)
0
         ]})
0
         result[0].id.should == 1
0
 
0
 
0
         result = repository(:postgres).all(SailBoat,{:order => [
0
- SailBoat.property_by_name(:name),
0
- DataMapper::Query::Direction.new(SailBoat.property_by_name(:port), :asc)
0
+ SailBoat.properties[:name],
0
+ DataMapper::Query::Direction.new(SailBoat.properties[:port], :asc)
0
         ]})
0
         result[0].id.should == 1
0
       end
...
87
88
89
90
91
92
93
94
95
96
97
98
...
87
88
89
 
 
 
 
 
 
90
91
92
0
@@ -87,12 +87,6 @@ begin
0
           property :notes, String, :lazy => [:notes]
0
           property :trip_report, String, :lazy => [:notes,:trip]
0
           property :miles, Fixnum, :lazy => [:trip]
0
-
0
- class << self
0
- def property_by_name(name)
0
- properties(repository.name)[name]
0
- end
0
- end
0
         end
0
         
0
         SailBoat.auto_migrate!(:sqlite3)
...
17
18
19
20
21
22
23
24
25
26
27
28
...
46
47
48
49
 
50
51
52
53
54
 
55
56
57
58
59
60
 
 
61
62
63
64
65
66
 
 
67
68
69
...
17
18
19
 
 
 
 
 
 
20
21
22
...
40
41
42
 
43
44
45
46
47
 
48
49
50
51
52
 
 
53
54
55
56
57
58
 
 
59
60
61
62
63
0
@@ -17,12 +17,6 @@ begin
0
           property :id, Fixnum, :serial => true
0
           property :name, String
0
           property :port, String
0
-
0
- class << self
0
- def property_by_name(name)
0
- properties(repository.name)[name]
0
- end
0
- end
0
         end
0
         
0
         SailBoat.auto_migrate!(:sqlite3)
0
@@ -46,24 +40,24 @@ begin
0
       
0
       it "should order results" do
0
         result = repository(:sqlite3).all(SailBoat,{:order => [
0
- DataMapper::Query::Direction.new(SailBoat.property_by_name(:name), :asc)
0
+ DataMapper::Query::Direction.new(SailBoat.properties[:name], :asc)
0
             ]})
0
         result[0].id.should == 1
0
 
0
         result = repository(:sqlite3).all(SailBoat,{:order => [
0
- DataMapper::Query::Direction.new(SailBoat.property_by_name(:port), :asc)
0
+ DataMapper::Query::Direction.new(SailBoat.properties[:port], :asc)
0
             ]})
0
         result[0].id.should == 3
0
 
0
         result = repository(:sqlite3).all(SailBoat,{:order => [
0
- DataMapper::Query::Direction.new(SailBoat.property_by_name(:name), :asc),
0
- DataMapper::Query::Direction.new(SailBoat.property_by_name(:port), :asc)
0
+ DataMapper::Query::Direction.new(SailBoat.properties[:name], :asc),
0
+ DataMapper::Query::Direction.new(SailBoat.properties[:port], :asc)
0
             ]})
0
         result[0].id.should == 1
0
 
0
         result = repository(:sqlite3).all(SailBoat,{:order => [
0
- SailBoat.property_by_name(:name),
0
- DataMapper::Query::Direction.new(SailBoat.property_by_name(:port), :asc)
0
+ SailBoat.properties[:name],
0
+ DataMapper::Query::Direction.new(SailBoat.properties[:port], :asc)
0
             ]})
0
         result[0].id.should == 1
0
 
...
85
86
87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
89
90
...
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
0
@@ -85,6 +85,57 @@ begin
0
       end
0
     end
0
 
0
+ it "should respect paranoia with a datetime" do
0
+
0
+ class Lime
0
+ include DataMapper::Resource
0
+ property :id, Fixnum, :serial => true
0
+ property :color, String
0
+ property :deleted_at, DataMapper::Types::ParanoidDateTime
0
+ end
0
+