Search code examples
rubymysql2sequel

How do I create a prepared insert statement in Sequel?


I am attempting to create a prepared insert statement in Sequel and I am as far as

db[:registration].prepare(:insert)
=> <Sequel::Mysql2::Dataset/PreparedStatement "INSERT INTO `registration` () VALUES ()">

How do I create a statement that is something like the following:

INSERT INTO `registration` (`name`, `email`) VALUES (?, ?)

The documentation is a little bit obtuse and I can't find any examples online.


Solution

  • Figured this out looking at their rspecs:

    statement = db[:registration].prepare(:insert, :prepared_statement_name, :email => :$email, :name => :$name)
    statement.call(:name => "foo", :email => "foo@bar.com")
    

    NOTE

    The keys that are passed to .call correspond to the values passed in the hash in prepare. So this would work too:

    statement = db[:registration].prepare(:insert, :prepared_statement_name, :email => :$e, :name => :$n)
    statement.call(:n => "foo", :e => "foo@bar.com")