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.
- data/lib/dm-salesforce/adapter.rb +1 -1
- data/lib/dm-salesforce/sql.rb +67 -56
- data/lib/dm-salesforce/types/boolean.rb +6 -8
- data/lib/dm-salesforce/version.rb +1 -1
- metadata +1 -1
@@ -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|
|
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
|
data/lib/dm-salesforce/sql.rb
CHANGED
@@ -1,56 +1,61 @@
|
|
1
1
|
module DataMapper::Salesforce
|
2
2
|
module SQL
|
3
|
-
def
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
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
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
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
|
-
|
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}
|
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(
|
66
|
-
|
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
|
73
|
-
case
|
74
|
-
when Array then
|
75
|
-
|
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
|
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
|
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
|
10
|
-
else
|
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
|
-
|
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
|