2

I'm using the latest versions of postgresql, ruby and datamapper.

I create a query like so:

collection = Entry.all(:id => 2..4, :text => /test/)

collection is a DataMapper::Collection object. It's possible to get the DataMapper::Query object by simply calling query = collection.query. But how do I get the raw SQL of my query, as a String?

I need it because I have to customize the raw SQL (I need SELECT DISTINCT instead of only SELECT), but I don't want to write the SQL completely by myself, because I'm not sure if I can write a secure query.

I would also be happy to archive my goal another way, I'm open to completely new suggestions ;)

le_me
  • 3,089
  • 3
  • 26
  • 28
  • It is better to specify actual version numbers rather than saying "the latest". Think about someone looking at the question two or three years from now. – kgrittn Feb 03 '13 at 16:32

1 Answers1

6

This seems to be not so easyly possible, but this is kind of an hackish solution

collection = Entry.all(:id => 2..4, :text => /test/)

query = collection.query

DataMapper.repository.adapter.send(:select_statement,query)
Kristian Hildebrandt
  • 1,528
  • 1
  • 14
  • 16