public
Description: DataMapper Adapters
Homepage: http://www.yehudakatz.com
Clone URL: git://github.com/wycats/dm-adapters.git
Search Repo:
Refactor read_* to avoid repeating logic.
wycats (author)
Sun Jun 15 01:59:35 -0700 2008
commit  f81e43f0e68f07f8d0161f0a234913b7df24b9f1
tree    b72042e29051d5cc355e399ecc78e4fcfd4d2576
parent  91df8fd2f8b0f4ca57becb6cc4361bdda1917a3f
...
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
142
143
 
 
 
 
 
144
145
146
147
 
148
149
150
...
159
160
161
 
 
162
163
164
165
166
 
 
 
 
 
167
168
169
 
 
 
 
 
170
171
172
173
174
175
 
176
177
178
...
104
105
106
 
107
108
 
 
 
 
109
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
111
112
113
114
115
116
117
118
119
120
121
122
 
123
124
125
126
...
135
136
137
138
139
140
 
 
 
 
141
142
143
144
145
146
 
 
147
148
149
150
151
152
153
 
 
154
155
156
157
158
159
0
@@ -104,47 +104,23 @@ module DataMapper
0
         require "salesforce_api"
0
         @connection = SalesforceAPI::Connection.new(URI.unescape(@uri.user), @uri.password, "#{ENV["HOME"]}/.salesforce/#{basename}").driver
0
       end
0
-
0
+
0
       def read_many(query)
0
- repository = query.repository
0
- properties = query.fields
0
- properties_with_indexes = Hash[*properties.zip((0...properties.size).to_a).flatten]
0
-
0
         Collection.new(query) do |set|
0
-
0
- conditions = query.conditions.map {|c| SQL.from_condition(c, repository)}.compact.join(") AND (")
0
-
0
- query_string = "SELECT #{query.fields.map {|f| f.field}.join(", ")} from #{query.model.storage_name(repository.name)}"
0
- query_string << " WHERE (#{conditions})" unless conditions.empty?
0
- query_string << " ORDER BY #{SQL.order(query.order[0])}" unless query.order.empty?
0
- query_string << " LIMIT #{query.limit}" if query.limit
0
-
0
- DataMapper.logger.debug query_string
0
-
0
- begin
0
- results = @connection.query(:queryString => query_string).result
0
- rescue SOAP::FaultError => e
0
- raise SalesforceAPI::ReadError, e.message
0
- end
0
-
0
- results = results.size > 0 ? results.records : []
0
-
0
- results.each do |result|
0
- props = properties_with_indexes.inject([]) do |accum, (prop, idx)|
0
- accum[idx] = result.send(soap_attr(prop, repository))
0
- accum
0
- end
0
- set.load(props)
0
- end
0
-
0
+ read(query, set, true)
0
         end
0
       end
0
       
0
       def read_one(query)
0
+ read(query, query.model, false)
0
+ end
0
+
0
+ private
0
+ def read(query, set, arr = true)
0
         repository = query.repository
0
         properties = query.fields
0
         properties_with_indexes = Hash[*properties.zip((0...properties.size).to_a).flatten]
0
-
0
+
0
         conditions = query.conditions.map {|c| SQL.from_condition(c, repository)}.compact.join(") AND (")
0
       
0
         query_string = "SELECT #{query.fields.map {|f| f.field}.join(", ")} from #{query.model.storage_name(repository.name)}"
0
@@ -159,20 +135,25 @@ module DataMapper
0
         rescue SOAP::FaultError => e
0
           raise SalesforceAPI::ReadError, e.message
0
         end
0
+
0
+ return nil unless results.records
0
         
0
- results = results.size > 0 ? results.records : []
0
-
0
- result = results.first
0
- return nil unless result
0
+ # This is the core logic that handles the difference between all/first
0
+ (results.records || []).each do |result|
0
+ props = props_from_result(properties_with_indexes, result, repository)
0
+ arr ? set.load(props) : (break set.load(props, query))
0
+ end
0
         
0
- props = properties_with_indexes.inject([]) do |accum, (prop, idx)|
0
- accum[idx] = result.send(soap_attr(prop, repository))
0
+ end
0
+
0
+ def props_from_result(properties_with_indexes, result, repo)
0
+ properties_with_indexes.inject([]) do |accum, (prop, idx)|
0
+ accum[idx] = result.send(soap_attr(prop, repo))
0
           accum
0
         end
0
- return query.model.load(props, query)
0
-
0
       end
0
       
0
+ public
0
       def update(attributes, query)
0
         arr = if key_condition = query.conditions.find {|op,prop,val| prop.key?}
0
           [ make_sforce_obj(query, attributes, key_condition.last) ]

Comments

  • randy Sun Jun 15 06:29:37 -0700 2008

    YAY! ::claps hands::