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. Issue a SET query with literalized values for each argument, then an EXECUTE to execute the query with the arguments.

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