Skip to content

Instantly share code, notes, and snippets.

@knowtheory
Created June 29, 2010 04:27
Show Gist options
  • Select an option

  • Save knowtheory/456796 to your computer and use it in GitHub Desktop.

Select an option

Save knowtheory/456796 to your computer and use it in GitHub Desktop.
module DataMapper
class Query
OPTIONS << :join
def assert_valid_options(options)
assert_kind_of 'options', options, Hash
options.each do |attribute, value|
case attribute
when :fields then assert_valid_fields(value, options[:unique])
when :links then assert_valid_links(value)
when :conditions then assert_valid_conditions(value)
when :offset then assert_valid_offset(value, options[:limit])
when :limit then assert_valid_limit(value)
when :order then assert_valid_order(value, options[:fields])
when :unique, :add_reversed, :reload then assert_valid_boolean("options[:#{attribute}]", value)
when :join then assert_valid_join(value)
else
assert_valid_conditions(attribute => value)
end
end
end
def assert_valid_join(join)
unless [:left_outer, :inner, :right_outer].include? join
raise ArgumentError, "+options[:fields]+ entry #{inspect} of an unsupported object #{field.class}"
end
end
end
module Adapters
class DataObjectsAdapter < AbstractAdapter
def join_statement(query, qualify)
statement = ''
join = {:left_outer => "LEFT OUTER JOIN", :inner => "INNER JOIN", :right_outer => "RIGHT OUTER JOIN"}[query.options.fetch(:join,:inner?)]
query.links.reverse_each do |relationship|
statement << " #{join} #{quote_name(relationship.source_model.storage_name(name))} ON "
statement << relationship.target_key.zip(relationship.source_key).map do |target_property, source_property|
"#{property_to_column_name(target_property, qualify)} = #{property_to_column_name(source_property, qualify)}"
end.join(' AND ')
end
statement
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment