activesalesforce 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/rforce.rb CHANGED
@@ -155,13 +155,17 @@ module RForce
155
155
  @batch_size = DEFAULT_BATCH_SIZE
156
156
  end
157
157
 
158
+ def show_debug
159
+ $DEBUG or ENV['SHOWSOAP']
160
+ end
161
+
158
162
  def init_server(url)
159
163
  @url = URI.parse(url)
160
164
  @server = Net::HTTP.new(@url.host, @url.port)
161
165
  @server.use_ssl = @url.scheme == 'https'
162
166
 
163
167
  # run ruby with -d to see SOAP wiredumps.
164
- @server.set_debug_output $stderr if $DEBUG
168
+ @server.set_debug_output $stderr if show_debug
165
169
  end
166
170
 
167
171
  #Log in to the server and remember the session ID
@@ -208,7 +212,7 @@ module RForce
208
212
  'User-Agent' => 'ActiveSalesforce RForce'
209
213
  }
210
214
 
211
- unless $DEBUG
215
+ unless show_debug
212
216
  headers['Accept-Encoding'] = 'gzip'
213
217
  headers['Content-Encoding'] = 'gzip'
214
218
  end
@@ -258,7 +262,7 @@ module RForce
258
262
 
259
263
  # encode gzip
260
264
  def encode(request)
261
- return request if $DEBUG
265
+ return request if show_debug
262
266
 
263
267
  begin
264
268
  ostream = StringIO.new
@@ -29,6 +29,13 @@ require 'thread'
29
29
  require File.dirname(__FILE__) + '/salesforce_login'
30
30
  require File.dirname(__FILE__) + '/column_definition'
31
31
 
32
+ class ResultArray < Array
33
+ attr_reader :actual_size
34
+
35
+ def initialize(actual_size)
36
+ @actual_size = actual_size
37
+ end
38
+ end
32
39
 
33
40
  module ActiveRecord
34
41
  class Base
@@ -140,11 +147,14 @@ module ActiveRecord
140
147
  entity_name = entity_name_from_table(table_name)
141
148
  column_names = api_column_names(table_name)
142
149
 
143
- soql = sql.sub(/SELECT \* FROM/, "SELECT #{column_names.join(', ')} FROM")
150
+ soql = sql.sub(/SELECT \* FROM \w+ /, "SELECT #{column_names.join(', ')} FROM #{table_name} ")
144
151
 
145
152
  # Look for a LIMIT clause
146
153
  soql.sub!(/LIMIT 1/, "")
147
154
 
155
+ # Look for an OFFSET clause
156
+ soql.sub!(/\d+ OFFSET \d+/, "")
157
+
148
158
  # Fixup column references to use api names
149
159
  columns = columns_map(table_name)
150
160
  while soql =~ /@@(\w+)/
@@ -157,9 +167,10 @@ module ActiveRecord
157
167
  @connection.batch_size = @batch_size if @batch_size
158
168
  @batch_size = nil
159
169
 
160
- records = get_result(@connection.query(:queryString => soql), :query).records
170
+ queryResult = get_result(@connection.query(:queryString => soql), :query)
171
+ records = queryResult.records
161
172
 
162
- result = []
173
+ result = ResultArray.new(queryResult[:size].to_i)
163
174
  return result unless records
164
175
 
165
176
  records = [ records ] unless records.is_a?(Array)
@@ -186,16 +197,28 @@ module ActiveRecord
186
197
  end
187
198
 
188
199
  def select_one(sql, name = nil) #:nodoc:
189
- @connection.batch_size = 1
200
+ self.batch_size = 1
201
+
202
+ # Check for SELECT COUNT(*) FROM query
203
+ matchCount = sql.match(/SELECT COUNT\(\*\) FROM (\w+)/)
204
+ if matchCount
205
+ sql = "SELECT id FROM #{matchCount[1].singularize} #{matchCount.post_match}"
206
+ end
207
+
190
208
  result = select_all(sql, name)
191
- result.nil? ? nil : result.first
209
+
210
+ if matchCount
211
+ { :count => result.actual_size }
212
+ else
213
+ result.nil? ? nil : result.first
214
+ end
192
215
  end
193
216
 
194
217
  def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
195
218
  tokens = sql.scan(/[0-9A-Za-z._]+/)
196
219
 
197
220
  # Convert sql to sobject
198
- table_name = tokens[2]
221
+ table_name = tokens[2].singularize
199
222
  entity_name = entity_name_from_table(table_name)
200
223
  columns = columns_map(table_name)
201
224
 
@@ -219,7 +242,7 @@ module ActiveRecord
219
242
 
220
243
  def update(sql, name = nil) #:nodoc:
221
244
  # Convert sql to sobject
222
- table_name = sql.match(/UPDATE (\w+) /)[1]
245
+ table_name = sql.match(/UPDATE (\w+) /)[1].singularize
223
246
  entity_name = entity_name_from_table(table_name)
224
247
  columns = columns_map(table_name)
225
248
 
@@ -243,10 +266,10 @@ module ActiveRecord
243
266
  def delete(sql, name = nil)
244
267
  # Extract the ids from the IN () clause
245
268
  match = sql.match(/IN \(([^\)]*)\)/)
246
-
269
+
247
270
  # If the IN clause was not found fall back to WHERE id = 'blah'
248
271
  ids = match ? match[1].scan(/\w+/) : [ sql.match(/WHERE id = '([^\']*)'/)[1] ]
249
-
272
+
250
273
  ids_element = []
251
274
  ids.each { |id| ids_element << :ids << id }
252
275
 
@@ -295,7 +318,7 @@ module ActiveRecord
295
318
 
296
319
  if metadata.childRelationships
297
320
  metadata.childRelationships.each do |relationship|
298
-
321
+
299
322
  if relationship[:childSObject].casecmp(entity_name) == 0
300
323
  r = SalesforceRelationship.new(relationship)
301
324
  cached_relationships << r
@@ -310,25 +333,34 @@ module ActiveRecord
310
333
 
311
334
  def configure_active_record(entity_name)
312
335
  klass = entity_name.constantize
313
-
314
336
  klass.table_name = entity_name
315
337
  klass.pluralize_table_names = false
316
338
  klass.set_inheritance_column nil
317
339
  klass.lock_optimistically = false
318
340
  klass.record_timestamps = false
319
- klass.default_timezone = :utc
341
+ klass.default_timezone = :utc
320
342
 
321
343
  # Create relationships for any reference field
322
344
  @relationships_map[entity_name].each do |relationship|
323
345
  referenceName = relationship.name
324
346
  unless self.respond_to? referenceName.to_sym or relationship.reference_to == "Profile"
347
+ reference_to = relationship.reference_to
348
+
349
+ begin
350
+ reference_to.constantize
351
+ rescue NameError => e
352
+ # Automatically create a least a stub for the referenced entity
353
+ referenced_klass = klass.class_eval("::#{reference_to} = Class.new(ActiveRecord::Base)")
354
+ puts "Created ActiveRecord stub for the referenced entity '#{reference_to}'"
355
+ end
356
+
325
357
  one_to_many = relationship.one_to_many
326
358
  foreign_key = relationship.foreign_key
327
359
 
328
360
  if one_to_many
329
- klass.has_many referenceName.to_sym, :class_name => relationship.reference_to, :foreign_key => foreign_key, :dependent => false
361
+ klass.has_many referenceName.to_sym, :class_name => reference_to, :foreign_key => foreign_key, :dependent => false
330
362
  else
331
- klass.belongs_to referenceName.to_sym, :class_name => relationship.reference_to, :foreign_key => foreign_key, :dependent => false
363
+ klass.belongs_to referenceName.to_sym, :class_name => reference_to, :foreign_key => foreign_key, :dependent => false
332
364
  end
333
365
 
334
366
  puts "Created one-to-#{one_to_many ? 'many' : 'one' } relationship '#{referenceName}' from #{entity_name} to #{relationship.reference_to} using #{foreign_key}"
@@ -384,7 +416,7 @@ module ActiveRecord
384
416
  end
385
417
 
386
418
  def table_name_from_sql(sql)
387
- sql.match(/FROM (\w+) /)[1]
419
+ sql.match(/FROM (\w+) /)[1].singularize
388
420
  end
389
421
 
390
422
  def column_names(table_name)
metadata CHANGED
@@ -3,7 +3,7 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: activesalesforce
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.2
6
+ version: 0.1.3
7
7
  date: 2006-02-02 00:00:00 -05:00
8
8
  summary: ActiveSalesforce is an extension to the Rails Framework that allows for the dynamic creation and management of ActiveRecord objects through the use of Salesforce meta-data and uses a Salesforce.com organization as the backing store.
9
9
  require_paths: