What is the proper way to execute a SQL statement via DataMapper?

1,526 views
Skip to first unread message

Johannes Held

unread,
May 3, 2012, 2:19:43 AM5/3/12
to datam...@googlegroups.com
Hello list,

I’m wondering what's the proper way to execute a SQL statement via DataMapper.

Assume I these models are created at runtime:

class Bar
  include DataMapper::Resource
  property :id, :Serial
end

class Foo
  include DataMapper::Resource
  property :a, :Serial
  property :b, :Serial
  belongs_to Bar, :key => true
end

Now I want to create a view (and a model to mirror that view):
create view foo_count as
select a, b, count(bar) as count
from foo
group by a, b


How can I use DataMapper to execute this statement?
Is there a way to use the name system of DataMapper?

--
Cheers, Johannes

Ted Han

unread,
Jul 25, 2012, 3:27:03 PM7/25/12
to datam...@googlegroups.com
Check out https://rubygems.org/gems/dm-ar-finders there's a find_by_sql method in there: http://rubydoc.info/gems/dm-ar-finders/1.2.0/frames

Beyond that you can also fire off queries directly at a repository's adapter as well:

1.9.3p194 :004 > DataMapper.repository.adapter.select('select * from candidates limit 1')
 => [#<struct id=1, name="Peter Kinder", slug="peter-kinder", incumbent=true, url=nil>] 

But as you can see, that doesn't give you wrapped models and a collection back.  (the find_by_sql method above does)

On Wed, Jul 25, 2012 at 12:22 PM, Mario Camou <mca...@tecnoguru.com> wrote:
I also need to do this, I want to issue a command to MySQL to set the Transaction Isolation level but haven't found out how to send raw SQL statements through DataMapper. Any takers?
--
You received this message because you are subscribed to the Google Groups "DataMapper" group.
To view this discussion on the web visit https://groups.google.com/d/msg/datamapper/-/BBN4m098lKsJ.

To post to this group, send email to datam...@googlegroups.com.
To unsubscribe from this group, send email to datamapper+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/datamapper?hl=en.



--
Ted Han
DocumentCloud
Investigative Reporters & Editors

Ted Han

unread,
Jul 25, 2012, 3:26:32 PM7/25/12
to datam...@googlegroups.com
Check out https://rubygems.org/gems/dm-ar-finders there's a find_by_sql method in there: http://rubydoc.info/gems/dm-ar-finders/1.2.0/frames

Beyond that you can also fire off queries directly at a repository's adapter as well:

1.9.3p194 :004 > DataMapper.repository.adapter.select('select * from candidates limit 1')
 => [#<struct id=1, name="Peter Kinder", slug="peter-kinder", incumbent=true, url=nil>] 

But as you can see, that doesn't give you wrapped models and a collection back.  (the find_by_sql method above does)
On Wed, Jul 25, 2012 at 12:22 PM, Mario Camou <mca...@tecnoguru.com> wrote:
I also need to do this, I want to issue a command to MySQL to set the Transaction Isolation level but haven't found out how to send raw SQL statements through DataMapper. Any takers?


On Thursday, May 3, 2012 8:19:43 AM UTC+2, Johannes Held wrote:
--
You received this message because you are subscribed to the Google Groups "DataMapper" group.
To view this discussion on the web visit https://groups.google.com/d/msg/datamapper/-/BBN4m098lKsJ.

To post to this group, send email to datam...@googlegroups.com.
To unsubscribe from this group, send email to datamapper+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/datamapper?hl=en.
Reply all
Reply to author
Forward
0 new messages