Module: SalesforceAdapter::SQL

Included in:
SalesforceAdapter
Defined in:
lib/dm-salesforce-adapter/sql.rb

Instance Method Summary collapse

Instance Method Details

#comparison_operator(comparison) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/dm-salesforce-adapter/sql.rb', line 28

def comparison_operator(comparison)
  subject = comparison.subject
  value   = comparison.value

  case comparison.slug
  when :eql    then equality_operator(subject, value)
  when :in     then include_operator(subject, value)
  when :not    then inequality_operator(subject, value)
  when :regexp then regexp_operator(value)
  when :like   then like_operator(value)
  when :gt     then '>'
  when :lt     then '<'
  when :gte    then '>='
  when :lte    then '<='
  end
end

#comparison_statement(comparison, repository) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/dm-salesforce-adapter/sql.rb', line 12

def comparison_statement(comparison, repository)
  subject = comparison.subject
  value = comparison.value

  if comparison.relationship?
    return conditions_statement(comparison.foreign_key_mapping, repository)
  elsif comparison.slug == :in && value.empty?
    return []  # match everything
  end

  operator    = comparison_operator(comparison)
  column_name = property_to_column_name(subject, repository)

  "#{column_name} #{operator} #{quote_value(value,subject)}"
end

#conditions_statement(conditions, repository) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/dm-salesforce-adapter/sql.rb', line 3

def conditions_statement(conditions, repository)
  case conditions
  when DataMapper::Query::Conditions::NotOperation       then negate_operation(conditions.operand, repository)
  when DataMapper::Query::Conditions::AbstractOperation  then conditions.operands.first # ignores AND/OR grouping for now.
  when DataMapper::Query::Conditions::AbstractComparison then comparison_statement(conditions, repository)
  else raise("Unkown condition type #{conditions.class}: #{conditions.inspect}")
  end
end

#equality_operator(property, operand) ⇒ Object



70
71
72
# File 'lib/dm-salesforce-adapter/sql.rb', line 70

def equality_operator(property, operand)
  operand.nil? ? 'IS' : '='
end

#include_operator(property, operand) ⇒ Object



74
75
76
77
78
79
# File 'lib/dm-salesforce-adapter/sql.rb', line 74

def include_operator(property, operand)
  case operand
  when Array then 'IN'
  when Range then 'BETWEEN'
  end
end

#like_operator(operand) ⇒ Object



81
82
83
# File 'lib/dm-salesforce-adapter/sql.rb', line 81

def like_operator(operand)
  "LIKE"
end

#negate_operation(operand, repository) ⇒ Object



45
46
47
48
49
# File 'lib/dm-salesforce-adapter/sql.rb', line 45

def negate_operation(operand, repository)
  statement = conditions_statement(operand, repository)
  statement = "NOT(#{statement})" unless statement.nil?
  statement
end

#order(direction) ⇒ Object



66
67
68
# File 'lib/dm-salesforce-adapter/sql.rb', line 66

def order(direction)
  "#{direction.target.field} #{direction.operator.to_s.upcase}"
end

#property_to_column_name(prop, repository) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/dm-salesforce-adapter/sql.rb', line 51

def property_to_column_name(prop, repository)
  case prop
  when DataMapper::Property
    prop.field
  when DataMapper::Query::Path
    rels = prop.relationships
    names = rels.map {|r| storage_name(r, repository) }.join(".")
    "#{names}.#{prop.field}"
  end
end

#quote_value(value, property) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/dm-salesforce-adapter/sql.rb', line 85

def quote_value(value, property)
  if property.kind_of? Property::Boolean
    # True on salesforce needs to be TRUE/FALSE for WHERE clauses but not for inserts.
    return value == Property::Boolean::TRUE ? 'TRUE' : 'FALSE'
  end

  case value
  when Array then "(#{value.map {|v| quote_value(v, property)}.join(", ")})"
  when NilClass then "NULL"
  when String then "'#{value.gsub(/'/, "\\'").gsub(/\\/, %{\\\\})}'"
  when Date then value.iso8601
  when DateTime, Time then value.iso8601(2)
  else "#{value}"
  end
end

#storage_name(rel, repository) ⇒ Object



62
63
64
# File 'lib/dm-salesforce-adapter/sql.rb', line 62

def storage_name(rel, repository)
  rel.parent_model.storage_name(repository.name)
end