module Sequel::MySQL::PreparedStatements::DatabaseMethods

Private Instance Methods

execute_prepared_statement(ps_name, opts, &block) click to toggle source

Executes a prepared statement on an available connection. If the prepared statement already exists for the connection and has the same SQL, reuse it, otherwise, prepare the new statement. Because of the usual MySQL stupidity, we are forced to name arguments via separate SET queries. Use @sequel_arg_N (for N starting at 1) for these arguments.

# File lib/sequel/adapters/utils/mysql_prepared_statements.rb, line 15
def execute_prepared_statement(ps_name, opts, &block)
  args = opts[:arguments]
  ps = prepared_statement(ps_name)
  sql = ps.prepared_sql
  synchronize(opts[:server]) do |conn|
    unless conn.prepared_statements[ps_name] == sql
      _execute(conn, "PREPARE #{ps_name} FROM #{literal(sql)}", opts)
      conn.prepared_statements[ps_name] = sql
    end
    i = 0
    _execute(conn, "SET " + args.map {|arg| "@sequel_arg_#{i+=1} = #{literal(arg)}"}.join(", "), opts) unless args.empty?
    opts = opts.merge(:log_sql=>" (#{sql})") if ps.log_sql
    _execute(conn, "EXECUTE #{ps_name}#{" USING #{(1..i).map{|j| "@sequel_arg_#{j}"}.join(', ')}" unless i == 0}", opts, &block)
  end
end