Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Properly handle reading properties that are relationships

  • Loading branch information...
commit 1c38b8653a6fbd59f5a5a23a37e5f06f85c0ef69 1 parent e72d737
@larrytheliquid larrytheliquid authored
View
1  lib/dm-salesforce.rb
@@ -2,7 +2,6 @@
require 'dm-core'
require 'dm-validations'
-require 'dm-salesforce/sql'
require 'dm-salesforce/extensions'
require 'dm-salesforce/adapter'
require 'dm-salesforce/connection'
View
73 lib/dm-salesforce/adapter.rb
@@ -65,7 +65,7 @@ def update(attributes, collection)
[ make_salesforce_obj(query, attributes, key_condition) ]
else
read_many(query).map do |obj|
- obj = make_salesforce_obj(query, attributes, x.key)
+ obj = make_salesforce_obj(query, attributes, obj.key)
end
end
connection.update(arr).size
@@ -136,11 +136,11 @@ def read(query)
repository = query.repository
properties = query.fields
properties_with_indexes = Hash[*properties.zip((0...properties.size).to_a).flatten]
- conditions = query.conditions.map {|c| SQL.from_condition(c, repository)}.compact.join(") AND (")
+ conditions = query.conditions.map {|c| from_condition(c, repository)}.compact.join(") AND (")
sql = "SELECT #{query.fields.map {|f| f.field}.join(", ")} from #{query.model.storage_name(repository.name)}"
sql << " WHERE (#{conditions})" unless conditions.empty?
- sql << " ORDER BY #{SQL.order(query.order[0])}" unless query.order.empty?
+ sql << " ORDER BY #{order(query.order[0])}" unless query.order.empty?
sql << " LIMIT #{query.limit}" if query.limit
DataMapper.logger.debug sql
@@ -191,5 +191,72 @@ def normalize_id_value(klass, property, value)
end
value
end
+
+ def from_condition(condition, repository)
+ slug = condition.class.slug
+ condition = case condition
+ when DataMapper::Query::Conditions::AbstractOperation then condition.operands.first
+ when DataMapper::Query::Conditions::AbstractComparison
+ if condition.subject.kind_of?(DataMapper::Associations::Relationship)
+ foreign_key_conditions(condition)
+ else
+ condition
+ end
+ else raise("Unkown condition type #{condition.class}: #{condition.inspect}")
+ end
+
+ value = condition.value
+ prop = condition.subject
+ operator = case slug
+ when String then operator
+ when :eql, :in then equality_operator(value)
+ when :not then inequality_operator(value)
+ when :like then "LIKE #{quote_value(value)}"
+ when :gt then "> #{quote_value(value)}"
+ when :gte then ">= #{quote_value(value)}"
+ when :lt then "< #{quote_value(value)}"
+ when :lte then "<= #{quote_value(value)}"
+ else raise "CAN HAS CRASH?"
+ end
+ case prop
+ when DataMapper::Property
+ "#{prop.field} #{operator}"
+ when DataMapper::Query::Path
+ rels = prop.relationships
+ names = rels.map {|r| storage_name(r, repository) }.join(".")
+ "#{names}.#{prop.field} #{operator}"
+ end
+ end
+
+ def storage_name(rel, repository)
+ rel.parent_model.storage_name(repository.name)
+ end
+
+ def order(direction)
+ "#{direction.target.field} #{direction.operator.to_s.upcase}"
+ end
+
+ def equality_operator(value)
+ case value
+ when Array then "IN #{quote_value(value)}"
+ else "= #{quote_value(value)}"
+ end
+ end
+
+ def inequality_operator(value)
+ case value
+ when Array then "NOT IN #{quote_value(value)}"
+ else "!= #{quote_value(value)}"
+ end
+ end
+
+ def quote_value(value)
+ case value
+ when Array then "(#{value.map {|v| quote_value(v)}.join(", ")})"
+ when NilClass then "NULL"
+ when String then "'#{value.gsub(/'/, "\\'").gsub(/\\/, %{\\\\})}'"
+ else "#{value}"
+ end
+ end
end
end
View
68 lib/dm-salesforce/sql.rb
@@ -1,68 +0,0 @@
-module DataMapperSalesforce
- module SQL
- class << self
- def from_condition(condition, repository)
- slug = condition.class.slug
- condition = case condition
- when DataMapper::Query::Conditions::AbstractOperation then condition.operands.first
- when DataMapper::Query::Conditions::AbstractComparison then condition
- else raise("Unkown condition type #{condition.class}: #{condition.inspect}")
- end
-
- value = condition.value
- prop = condition.subject
- operator = case slug
- when String then operator
- when :eql, :in then equality_operator(value)
- when :not then inequality_operator(value)
- when :like then "LIKE #{quote_value(value)}"
- when :gt then "> #{quote_value(value)}"
- when :gte then ">= #{quote_value(value)}"
- when :lt then "< #{quote_value(value)}"
- when :lte then "<= #{quote_value(value)}"
- else raise "CAN HAS CRASH?"
- end
- case prop
- when DataMapper::Property
- "#{prop.field} #{operator}"
- when DataMapper::Query::Path
- rels = prop.relationships
- names = rels.map {|r| storage_name(r, repository) }.join(".")
- "#{names}.#{prop.field} #{operator}"
- end
- end
-
- def storage_name(rel, repository)
- rel.parent_model.storage_name(repository.name)
- end
-
- def order(direction)
- "#{direction.target.field} #{direction.operator.to_s.upcase}"
- end
-
- private
- def equality_operator(value)
- case value
- when Array then "IN #{quote_value(value)}"
- else "= #{quote_value(value)}"
- end
- end
-
- def inequality_operator(value)
- case value
- when Array then "NOT IN #{quote_value(value)}"
- else "!= #{quote_value(value)}"
- end
- end
-
- def quote_value(value)
- case value
- when Array then "(#{value.map {|v| quote_value(v)}.join(", ")})"
- when NilClass then "NULL"
- when String then "'#{value.gsub(/'/, "\\'").gsub(/\\/, %{\\\\})}'"
- else "#{value}"
- end
- end
- end
- end
-end
View
2  spec/fixtures/account.rb
@@ -11,4 +11,6 @@ def self.salesforce_id_properties
property :id, String, :serial => true
property :name, String
+
+ has n, :contacts
end
View
4 spec/models/contact_spec.rb
@@ -96,8 +96,8 @@
describe "when an account is specified" do
it "correctly connects the account when its relationship object is specified" do
a = Account.create(:name => "Puma Shoes Unlimited")
- c = Contact.create(:first_name => 'Per', :last_name => 'Son', :email => "person@company.com", :account => a)
- c.reload.account.should == a
+ c = Contact.get Contact.create(:first_name => 'Per', :last_name => 'Son', :email => "person@company.com", :account => a).id
+ c.account.should == a
end
it "correctly connects the account when its foreign key is specified" do
Please sign in to comment.
Something went wrong with that request. Please try again.