Module: DataMapper::Adapters::SalesforceAdapter::SQL

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

Instance Method Summary collapse

Instance Method Details

#comparison_operator(comparison) ⇒ Object



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

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



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

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



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

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



72
73
74
# File 'lib/dm-salesforce-adapter/sql.rb', line 72

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

#include_operator(property, operand) ⇒ Object



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

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

#like_operator(operand) ⇒ Object



83
84
85
# File 'lib/dm-salesforce-adapter/sql.rb', line 83

def like_operator(operand)
  "LIKE"
end

#negate_operation(operand, repository) ⇒ Object



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

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

#order(direction) ⇒ Object



68
69
70
# File 'lib/dm-salesforce-adapter/sql.rb', line 68

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

#property_to_column_name(prop, repository) ⇒ Object



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

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



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

def quote_value(value, property)
  if property.is_a? 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(/\\/, %{\\\\})}'"
  else "#{value}"
  end
end

#storage_name(rel, repository) ⇒ Object



64
65
66
# File 'lib/dm-salesforce-adapter/sql.rb', line 64

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