4

I'm struggling with ruby's mysql gem and prepares statements.
I want to end up with the same as I would do with each_hash over the result, but it's nor supported in prepares statements. So I came with this horrible mess.

  stmt = @db.prepare("SELECT mat_id, name, qty FROM materials WHERE mat_id = ? ")

  #those 3 lines hurt my eyes
  res = stmt.execute(params[:id])
  mat_id, name, qty  =  res.bind_result(Integer, String, Integer).fetch
  @material = [mat_id: mat_id, name: name, qty: qty]

There has to be a better way to fetch the results and get an array of hashes.

A better mysql gem could be a valid answer. An ORM is NOT.

12
  • Have you tried mysql2 gem? Jun 13, 2013 at 9:17
  • 1
    An ORM might not be a valid answer, but you should still look into the Sequel gem. It's the only ORM I'm aware of that an SQL junky will feel comfortable with. Jun 13, 2013 at 9:26
  • Why is an ORM not a valid answer? Not all are alike and, though some are poorly done, others like Sequel, are amazingly flexible and powerful and can ease your programming burden significantly. Looking at your code I can see where it'd help. sequel.rubyforge.org/rdoc/files/doc/… Jun 13, 2013 at 10:06
  • @SergioTulentsev mysql2 doesn't support prepared statements Jun 13, 2013 at 10:08
  • 1
    @TheDisintegrator: Sequel isn't so much of an ORM as it is a tool to manipulate abstract syntax trees. Basically, it allows you to manipulate the SQL grammar using ruby, and it generates the query you want when you issue the fetch, insert, etc. the ORM part, which you referred to, is completely orthogonal to that functionality. Think of Sequel as a more advanced and rubyish SQL gem, and you'll see the light. It's an amazing piece of software, and I'm unaware of any equivalent in other languages. Jun 13, 2013 at 11:08

3 Answers 3

2

Seeing the comments, I'll still post the Sequel link as an answer:

http://sequel.rubyforge.org/

You don't need to use the model part of Sequel at all. In fact, the docs has an entire section dedicated to SQL junkies:

http://sequel.rubyforge.org/rdoc/files/doc/sql_rdoc.html

example query:

DB.fetch("SELECT * FROM albums WHERE name LIKE ?", 'A%') do |row|
  puts row[:name]
end
1
  • Ok, you win. Using sequel now. Still with a bitter taste for not being able to find a better way to express the three code lines of the question. Jun 15, 2013 at 8:01
2

Oneliner!

Hash[stmt.result_metadata.fetch_fields.map(&:name).zip( stmt.fetch )]

Or more robust

row = stmt.fetch
Hash[stmt.result_metadata.fetch_fields.map(&:name).zip( row )] if row
1

According to http://tmtm.org/en/mysql/ruby/, results have an "each_hash" method, but statements don't. What a pain in the ass...

#A proxy for the statement class
class Stmt

  def each_hash
    fields = @target.result_metadata.fetch_fields.map do |f| f.name.to_sym end 
    @target.execute.each do |x| 
      hash = {}
      fields.zip(x).each do |pair|
        hash[pair[0]] = pair[1]
      end 
      yield hash
    end 
  end 

  def initialize(target)
    @target = target
  end 

  def method_missing(name, *args, &block)
    @target.send(name, *args, &block)
  end 
end

Now, you can do this:

Stmt.new(@db.prepare(...).execute(...)).each_hash do |x|
  puts x
end

and you can loop through each row as a hash.

I still haven't tested this for multiple executions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.