Skip to content

Commit

Permalink
Fix query building issue (#13)
Browse files Browse the repository at this point in the history
* Fix query building issue

* Remove duplicate specs

* Update version

* Add Gemfile.lock
  • Loading branch information
vishalvijay committed Jul 24, 2017
1 parent 208dd70 commit 0f90cd4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
salesforce-orm (1.1.1)
salesforce-orm (1.2.0)
activerecord (~> 3)
activerecord-nulldb-adapter (~> 0)
restforce (~> 2.5)
Expand Down
20 changes: 7 additions & 13 deletions lib/salesforce-orm/sql_to_soql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,13 @@ def aggregate_function?(keyword)
keyword =~ /^(AVG|COUNT|COUNT|COUNT_DISTINCT|MIN|MAX|SUM)\(/i
end

def convert_aliased_fields(sql_str, split_by = Regexp.new('\s+'), join_by = ' ')
spcial_char_regex = /[=<>!,]+/
sql_str.split(split_by, -1).map do |keyword|
if aggregate_function?(keyword)
aggregate_data = keyword.match(/^(.*)\((.*)(\).*)/i).captures
raise Error::CommonError, 'Invalid aggregate function' unless aggregate_data[1]
"#{aggregate_data[0]}(#{convert_aliased_fields(aggregate_data[1])}#{aggregate_data[2]}"
elsif keyword =~ spcial_char_regex
convert_aliased_fields(keyword, spcial_char_regex, keyword.gsub(spcial_char_regex).first)
else
klass.field_map[keyword.to_sym] || keyword
end
end.join(join_by)
def convert_aliased_fields(sql_str)
result = sql_str.clone
klass.field_map.keys.each do |k|
regex = Regexp.new("([\(\)=<>!,%'\s\t]+)#{k.to_s}([\(\)=<>!,%'\s\t]+|$)")
result.gsub!(regex, "\\1#{klass.field_map[k].to_s}\\2")
end
result.gsub!(/\s+/, ' ')
end

def boolean_data_type_conversion(sql)
Expand Down
2 changes: 1 addition & 1 deletion lib/salesforce-orm/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module SalesforceOrm
VERSION = '1.1.1'.freeze
VERSION = '1.2.0'.freeze
end
12 changes: 11 additions & 1 deletion spec/sql_to_soql_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def remap_sample_object(field_map: nil, data_type_map: nil, object_name: nil)

it 'should convert IS NOT to !=' do
soql = SampleObject.where('id IS NOT NULL').to_soql
expect(soql).to eq('SELECT Id, CreatedDate, LastModifiedDate FROM SampleObject WHERE (id != NULL)')
expect(soql).to eq('SELECT Id, CreatedDate, LastModifiedDate FROM SampleObject WHERE (Id != NULL)')
end

it 'should convert IS to =' do
Expand Down Expand Up @@ -79,4 +79,14 @@ def remap_sample_object(field_map: nil, data_type_map: nil, object_name: nil)
expect(soql).to eq('SELECT Id, CreatedDate, LastModifiedDate, FieldOne, FieldTwo__c FROM SampleObject WHERE FieldOne = \'Hi\' AND FieldTwo__c = 333 AND yo IN (1, 2, 3)')
end
end

it 'should handle ()' do
remap_sample_object(field_map: {
field_one: :FieldOne,
field_two: :FieldTwo__c
}) do
soql = SampleObject.where(id: 2).where(field_one: [1, 2, 3]).where('(id IS NULL AND idor = 3 AND (id=121 OR Id!= 12) OR ID = false').to_soql
expect(soql).to eq('SELECT Id, CreatedDate, LastModifiedDate, FieldOne, FieldTwo__c FROM SampleObject WHERE Id = 2 AND FieldOne IN (1, 2, 3) AND ((Id = NULL AND idor = 3 AND (Id=121 OR Id!= 12) OR ID = false)')
end
end
end

0 comments on commit 0f90cd4

Please sign in to comment.