dm-salesforce 0.10.1 → 0.10.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -110,7 +110,7 @@ module DataMapper::Salesforce
110
110
  private
111
111
  def execute_query(query)
112
112
  repository = query.repository
113
- conditions = query.conditions.map {|c| from_condition(c, repository)}.compact.join(") AND (")
113
+ conditions = query.conditions.map {|c| conditions_statement(c, repository)}.compact.join(") AND (")
114
114
 
115
115
  fields = query.fields.map do |f|
116
116
  case f
@@ -1,56 +1,61 @@
1
1
  module DataMapper::Salesforce
2
2
  module SQL
3
- def from_condition(condition, repository)
4
- slug = condition.class.slug
5
- condition = case condition
6
- when DataMapper::Query::Conditions::AbstractOperation then condition.operands.first
7
- when DataMapper::Query::Conditions::AbstractComparison
8
- if condition.subject.kind_of?(DataMapper::Associations::Relationship)
9
- foreign_key_conditions(condition)
10
- else
11
- condition
12
- end
13
- else raise("Unkown condition type #{condition.class}: #{condition.inspect}")
14
- end
3
+ def conditions_statement(conditions, repository)
4
+ case conditions
5
+ when DataMapper::Query::Conditions::NotOperation then negate_operation(conditions.operand, repository)
6
+ when DataMapper::Query::Conditions::AbstractOperation then conditions.operands.first # ignores AND/OR grouping for now.
7
+ when DataMapper::Query::Conditions::AbstractComparison then comparison_statement(conditions, repository)
8
+ else raise("Unkown condition type #{conditions.class}: #{conditions.inspect}")
9
+ end
10
+ end
15
11
 
16
- value = condition.value
17
- prop = condition.subject
18
- operator = case slug
19
- when String then operator
20
- when :eql, :in then equality_operator(value)
21
- when :not then inequality_operator(value)
22
- when :like then "LIKE #{quote_value(value)}"
23
- when :gt then "> #{quote_value(value)}"
24
- when :gte then ">= #{quote_value(value)}"
25
- when :lt then "< #{quote_value(value)}"
26
- when :lte then "<= #{quote_value(value)}"
27
- else raise "CAN HAS CRASH?"
28
- end
12
+ def comparison_statement(comparison, repository)
13
+ subject = comparison.subject
14
+ value = comparison.value
15
+
16
+ if comparison.relationship?
17
+ return conditions_statement(comparison.foreign_key_mapping, repository)
18
+ elsif comparison.slug == :in && value.empty?
19
+ return [] # match everything
20
+ end
21
+
22
+ operator = comparison_operator(comparison)
23
+ column_name = property_to_column_name(subject, repository)
24
+
25
+ "#{column_name} #{operator} #{quote_value(value,subject)}"
26
+ end
27
+
28
+ def comparison_operator(comparison)
29
+ subject = comparison.subject
30
+ value = comparison.value
31
+
32
+ case comparison.slug
33
+ when :eql then equality_operator(subject, value)
34
+ when :in then include_operator(subject, value)
35
+ when :not then inequality_operator(subject, value)
36
+ when :regexp then regexp_operator(value)
37
+ when :like then like_operator(value)
38
+ when :gt then '>'
39
+ when :lt then '<'
40
+ when :gte then '>='
41
+ when :lte then '<='
42
+ end
43
+ end
44
+
45
+ def negate_operation(operand, repository)
46
+ statement = conditions_statement(operand, repository)
47
+ statement = "NOT(#{statement})" unless statement.nil?
48
+ statement
49
+ end
50
+
51
+ def property_to_column_name(prop, repository)
29
52
  case prop
30
53
  when DataMapper::Property
31
- "#{prop.field} #{operator}"
54
+ prop.field
32
55
  when DataMapper::Query::Path
33
56
  rels = prop.relationships
34
57
  names = rels.map {|r| storage_name(r, repository) }.join(".")
35
- "#{names}.#{prop.field} #{operator}"
36
- end
37
- end
38
-
39
- def foreign_key_conditions(condition)
40
- subject = case condition.subject
41
- when DataMapper::Associations::ManyToOne::Relationship
42
- condition.subject.child_key.first
43
- else
44
- condition.subject.parent_key.first
45
- end
46
-
47
- case condition.value
48
- when Array, DataMapper::Collection
49
- value = condition.send(:expected).flatten
50
- DataMapper::Query::Conditions::InclusionComparison.new(subject, value)
51
- else
52
- value = condition.send(:expected)
53
- DataMapper::Query::Conditions::EqualToComparison.new(subject, value)
58
+ "#{names}.#{prop.field}"
54
59
  end
55
60
  end
56
61
 
@@ -62,23 +67,29 @@ module DataMapper::Salesforce
62
67
  "#{direction.target.field} #{direction.operator.to_s.upcase}"
63
68
  end
64
69
 
65
- def equality_operator(value)
66
- case value
67
- when Array then "IN #{quote_value(value)}"
68
- else "= #{quote_value(value)}"
69
- end
70
+ def equality_operator(property, operand)
71
+ operand.nil? ? 'IS' : '='
70
72
  end
71
73
 
72
- def inequality_operator(value)
73
- case value
74
- when Array then "NOT IN #{quote_value(value)}"
75
- else "!= #{quote_value(value)}"
74
+ def include_operator(property, operand)
75
+ case operand
76
+ when Array then 'IN'
77
+ when Range then 'BETWEEN'
76
78
  end
77
79
  end
78
80
 
79
- def quote_value(value)
81
+ def like_operator(operand)
82
+ "LIKE"
83
+ end
84
+
85
+ def quote_value(value, property)
86
+ if property.type == DataMapper::Salesforce::Types::Boolean
87
+ # True on salesforce needs to be TRUE/FALSE for WHERE clauses but not for inserts.
88
+ return value == DataMapper::Salesforce::Types::Boolean::TRUE ? 'TRUE' : 'FALSE'
89
+ end
90
+
80
91
  case value
81
- when Array then "(#{value.map {|v| quote_value(v)}.join(", ")})"
92
+ when Array then "(#{value.map {|v| quote_value(v, property)}.join(", ")})"
82
93
  when NilClass then "NULL"
83
94
  when String then "'#{value.gsub(/'/, "\\'").gsub(/\\/, %{\\\\})}'"
84
95
  else "#{value}"
@@ -1,22 +1,20 @@
1
1
  module DataMapper::Salesforce
2
2
  module Types
3
3
  class Boolean < Type
4
- primitive String
4
+ primitive Integer
5
+ FALSE = 0
6
+ TRUE = 1
5
7
  default false
6
8
 
7
9
  def self.dump(value, property)
8
10
  case value
9
- when nil, false then '0'
10
- else value
11
+ when nil, false then FALSE
12
+ else TRUE
11
13
  end
12
14
  end
13
15
 
14
16
  def self.load(value, property)
15
- case value
16
- when TrueClass then value
17
- when '1', 'true' then true
18
- else false
19
- end
17
+ [true, 1, '1', 'true', 'TRUE', TRUE].include?(value)
20
18
  end
21
19
  end
22
20
  end
@@ -1,3 +1,3 @@
1
1
  module DataMapper::Salesforce
2
- VERSION = "0.10.1"
2
+ VERSION = "0.10.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dm-salesforce
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.1
4
+ version: 0.10.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yehuda Katz