cross repository query path?

3 views
Skip to first unread message

Lin Jen-Shin

unread,
Dec 30, 2008, 9:46:42 AM12/30/08
to DataMapper, god...@godfat.org
Hi,

I have used a query path to lookup some data with dm-core 0.9.7,
and wonder if the path could use different repository.

original code:

class Photo
include DataMapper::Resource
belongs_to :permission
def self.for_public
all(permission.guest.not => :none)
end
end

I would like to do this:

[...]
all(permission(:repository => :cache).guest.not => :none)
[...]

and it seems impossible since INNER JOIN was created in
the form of:

statement <<
" INNER JOIN #{quote_table_name(join_table_name)} ON "

I am not familiar with database, and have no idea if this could
be implemented or not. Then I tried this with MySQL:

Photo.find_by_sql('SELECT * FROM `cache`.`photos` INNER JOIN
`core`.`permissions` ON (`permissions`.`id` =
`photos`.`permission_id`) WHERE `permissions`.`guest` <> 1;')

It's slow but seems working.

Then I rewrite `for_public' method mentioned above to:

def Photo.for_public
# extracted from DataMapper::Model#method_missing
rel = relationships(:default)[:permission]
klass = self == rel.child_model ? rel.parent_model : rel.child_model

per = DataMapper::Query::Path.new(repository(:default), [rel],
klass)
all(per.guest.not => :none)
end

and monkey patch dm-core/adapters/data_object_adapter.rb
DataObjectsAdapter#SQL#links_statement:

statement << " INNER JOIN #{quote_table_name
(query.repository.adapter.uri.path[1..-1])}.#{quote_table_name
(join_table_name)} ON "

then calling:

Photo.repository(:cache) do
Photo.for_public
end

and it produced:

INNER JOIN `cache`.`permissions`

It seems it didn't preserved :default repo but :cache repo instead.
Could this be implemented? If so, perhaps I could spend some
time to figure out where to start.

Many thanks!

cheers,
Jen-Shin
Reply all
Reply to author
Forward
0 new messages