Skip to content

Commit

Permalink
Properly handle reading properties that are relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
larrytheliquid committed Aug 21, 2009
1 parent e72d737 commit 1c38b86
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 74 deletions.
1 change: 0 additions & 1 deletion lib/dm-salesforce.rb
Expand Up @@ -2,7 +2,6 @@


require 'dm-core' require 'dm-core'
require 'dm-validations' require 'dm-validations'
require 'dm-salesforce/sql'
require 'dm-salesforce/extensions' require 'dm-salesforce/extensions'
require 'dm-salesforce/adapter' require 'dm-salesforce/adapter'
require 'dm-salesforce/connection' require 'dm-salesforce/connection'
Expand Down
73 changes: 70 additions & 3 deletions lib/dm-salesforce/adapter.rb
Expand Up @@ -65,7 +65,7 @@ def update(attributes, collection)
[ make_salesforce_obj(query, attributes, key_condition) ] [ make_salesforce_obj(query, attributes, key_condition) ]
else else
read_many(query).map do |obj| read_many(query).map do |obj|
obj = make_salesforce_obj(query, attributes, x.key) obj = make_salesforce_obj(query, attributes, obj.key)
end end
end end
connection.update(arr).size connection.update(arr).size
Expand Down Expand Up @@ -136,11 +136,11 @@ def read(query)
repository = query.repository repository = query.repository
properties = query.fields properties = query.fields
properties_with_indexes = Hash[*properties.zip((0...properties.size).to_a).flatten] 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 = "SELECT #{query.fields.map {|f| f.field}.join(", ")} from #{query.model.storage_name(repository.name)}"
sql << " WHERE (#{conditions})" unless conditions.empty? 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 sql << " LIMIT #{query.limit}" if query.limit


DataMapper.logger.debug sql DataMapper.logger.debug sql
Expand Down Expand Up @@ -191,5 +191,72 @@ def normalize_id_value(klass, property, value)
end end
value value
end 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
end end
68 changes: 0 additions & 68 deletions lib/dm-salesforce/sql.rb

This file was deleted.

2 changes: 2 additions & 0 deletions spec/fixtures/account.rb
Expand Up @@ -11,4 +11,6 @@ def self.salesforce_id_properties


property :id, String, :serial => true property :id, String, :serial => true
property :name, String property :name, String

has n, :contacts
end end
4 changes: 2 additions & 2 deletions spec/models/contact_spec.rb
Expand Up @@ -96,8 +96,8 @@
describe "when an account is specified" do describe "when an account is specified" do
it "correctly connects the account when its relationship object is specified" do it "correctly connects the account when its relationship object is specified" do
a = Account.create(:name => "Puma Shoes Unlimited") a = Account.create(:name => "Puma Shoes Unlimited")
c = Contact.create(:first_name => 'Per', :last_name => 'Son', :email => "person@company.com", :account => a) c = Contact.get Contact.create(:first_name => 'Per', :last_name => 'Son', :email => "person@company.com", :account => a).id
c.reload.account.should == a c.account.should == a
end end


it "correctly connects the account when its foreign key is specified" do it "correctly connects the account when its foreign key is specified" do
Expand Down

0 comments on commit 1c38b86

Please sign in to comment.