Module: Sequel::MySQL::PreparedStatements::DatasetMethods

Includes:
Dataset::StoredProcedures
Included in:
Dataset, Sequel::Mysql2::Dataset
Defined in:
lib/sequel/adapters/shared/mysql_prepared_statements.rb

Defined Under Namespace

Modules: CallableStatementMethods

Constant Summary collapse

PreparedStatementMethods =
Sequel::Dataset.send(:prepared_statements_module,
  :prepare_bind,
  Sequel::Dataset::UnnumberedArgumentMapper) do
    # Raise a more obvious error if you attempt to call a unnamed prepared statement.
    def call(*)
      raise Error, "Cannot call prepared statement without a name" if prepared_statement_name.nil?
      super
    end
end
StoredProcedureMethods =
Sequel::Dataset.send(:prepared_statements_module,
"sql = @sproc_name; opts = Hash[opts]; opts[:args] = @sproc_args; opts[:sproc] = true",
Sequel::Dataset::StoredProcedureMethods, %w'execute execute_dui')

Instance Method Summary collapse

Methods included from Dataset::StoredProcedures

#call_sproc, #prepare_sproc

Instance Method Details

#call(type, bind_arguments = {}, *values, &block) ⇒ Object

MySQL is different in that it supports prepared statements but not bound variables outside of prepared statements. The default implementation breaks the use of subselects in prepared statements, so extend the temporary prepared statement that this creates with a module that fixes it.



122
123
124
125
126
# File 'lib/sequel/adapters/shared/mysql_prepared_statements.rb', line 122

def call(type, bind_arguments={}, *values, &block)
  ps = to_prepared_statement(type, values)
  ps.extend(CallableStatementMethods)
  ps.call(bind_arguments, &block)
end

#prepare(type, name = nil, *values) ⇒ Object

Store the given type of prepared statement in the associated database with the given name.



130
131
132
133
134
135
136
137
138
# File 'lib/sequel/adapters/shared/mysql_prepared_statements.rb', line 130

def prepare(type, name=nil, *values)
  ps = to_prepared_statement(type, values)
  ps.extend(PreparedStatementMethods)
  if name
    ps.prepared_statement_name = name
    db.set_prepared_statement(name, ps)
  end
  ps
end