module Sequel::Plugins::PreparedStatements::ClassMethods

Private Instance Methods

cached_prepared_statement(type, subtype) { || ... } click to toggle source

If a prepared statement has already been cached for the given type and subtype, return it. Otherwise, yield to the block to get the prepared statement, and cache it.

# File lib/sequel/plugins/prepared_statements.rb, line 135
def cached_prepared_statement(type, subtype)
  h = @prepared_statements[type]
  Sequel.synchronize do
    if v = h[subtype]
      return v
    end
  end
  ps = yield
  Sequel.synchronize{h[subtype] = ps}
end
prepare_explicit_statement(ds, type, vals=OPTS) click to toggle source

Create a prepared statement, but modify the SQL used so that the model's columns are explicitly selected instead of using *, assuming that the dataset selects from a single table.

# File lib/sequel/plugins/prepared_statements.rb, line 46
def prepare_explicit_statement(ds, type, vals=OPTS)
  f = ds.opts[:from]
  meth = type == :insert_select ? :returning : :select
  s = ds.opts[meth]
  if f && f.length == 1 && !ds.opts[:join] && (!s || s.empty?)
    ds = ds.send(meth, *columns.map{|c| Sequel.identifier(c)})
  end 
  
  prepare_statement(ds, type, vals)
end
prepare_statement(ds, type, vals=OPTS) click to toggle source

Create a prepared statement based on the given dataset with a unique name for the given type of query and values.

# File lib/sequel/plugins/prepared_statements.rb, line 59
def prepare_statement(ds, type, vals=OPTS)
  ds.clone(:log_sql=>true).prepare(type, :"smpsp_#{NEXT.call}", vals)
end
prepared_columns(cols) click to toggle source

Return a sorted array of columns for use as a hash key.

# File lib/sequel/plugins/prepared_statements.rb, line 64
def prepared_columns(cols)
  RUBY_VERSION >= '1.9' ? cols.sort : cols.sort_by(&:to_s)
end
prepared_delete() click to toggle source

Return a prepared statement that can be used to delete a row from this model's dataset.

# File lib/sequel/plugins/prepared_statements.rb, line 69
def prepared_delete
  # SEQUEL5: Remove
  cached_prepared_statement(:fixed, :delete){prepare_statement(filter(prepared_statement_key_array(primary_key)), :delete)}
end
prepared_insert(cols) click to toggle source

Return a prepared statement that can be used to insert a row using the given columns.

# File lib/sequel/plugins/prepared_statements.rb, line 75
def prepared_insert(cols)
  cached_prepared_statement(:insert, prepared_columns(cols)){prepare_statement(dataset, :insert, prepared_statement_key_hash(cols))}
end
prepared_insert_select(cols) click to toggle source

Return a prepared statement that can be used to insert a row using the given columns and return that column values for the row created.

# File lib/sequel/plugins/prepared_statements.rb, line 81
def prepared_insert_select(cols)
  if dataset.supports_insert_select?
    cached_prepared_statement(:insert_select, prepared_columns(cols)){prepare_explicit_statement(naked.clone(:server=>dataset.opts.fetch(:server, :default)), :insert_select, prepared_statement_key_hash(cols))}
  end
end
prepared_lookup() click to toggle source

Return a prepared statement that can be used to lookup a row solely based on the primary key.

# File lib/sequel/plugins/prepared_statements.rb, line 88
def prepared_lookup
  # SEQUEL5: Remove
  cached_prepared_statement(:fixed, :lookup){prepare_explicit_statement(filter(prepared_statement_key_array(primary_key)), :first)}
end
prepared_refresh() click to toggle source

Return a prepared statement that can be used to refresh a row to get new column values after insertion.

# File lib/sequel/plugins/prepared_statements.rb, line 94
def prepared_refresh
  # SEQUEL5: Remove
  cached_prepared_statement(:fixed, :refresh){prepare_explicit_statement(naked.clone(:server=>dataset.opts.fetch(:server, :default)).where(prepared_statement_key_array(primary_key)), :first)}
end
prepared_statement_key_array(keys) click to toggle source

Return an array of two element arrays with the column symbol as the first entry and the placeholder symbol as the second entry.

# File lib/sequel/plugins/prepared_statements.rb, line 101
def prepared_statement_key_array(keys)
  if dataset.requires_placeholder_type_specifiers?
    sch = db_schema
    Array(keys).map do |k|
      if (s = sch[k]) && (t = s[:type])
        [k, :"$#{k}__#{t}"]
      else
        [k, :"$#{k}"]
      end
    end
  else
    Array(keys).map{|k| [k, :"$#{k}"]}
  end
end
prepared_statement_key_hash(keys) click to toggle source

Return a hash mapping column symbols to placeholder symbols.

# File lib/sequel/plugins/prepared_statements.rb, line 117
def prepared_statement_key_hash(keys)
  Hash[*(prepared_statement_key_array(keys).flatten)]
end
prepared_update(cols) click to toggle source

Return a prepared statement that can be used to update row using the given columns.

# File lib/sequel/plugins/prepared_statements.rb, line 122
def prepared_update(cols)
  cached_prepared_statement(:update, prepared_columns(cols)){prepare_statement(filter(prepared_statement_key_array(primary_key)), :update, prepared_statement_key_hash(cols))}
end
primary_key_lookup(pk) click to toggle source

Use a prepared statement to query the database for the row matching the given primary key.

Calls superclass method
# File lib/sequel/plugins/prepared_statements.rb, line 127
def primary_key_lookup(pk)
  return super unless use_prepared_statements_for_pk_lookup?
  # SEQUEL5: Remove
  prepared_lookup.call(primary_key_hash(pk))
end
use_prepared_statements_for_pk_lookup?() click to toggle source

Whether to use prepared statements for lookups by primary key. True if the default primary key lookup isn't optimized.

# File lib/sequel/plugins/prepared_statements.rb, line 148
def use_prepared_statements_for_pk_lookup?
  !@fast_pk_lookup_sql && !dataset.joined_dataset?
end