module Sequel::Plugins::PreparedStatements::ClassMethods
Private Instance Methods
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 103 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
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 43 def prepare_explicit_statement(ds, type, vals=OPTS) s = ds.opts[:returning] if !s || s.empty? ds = ds.returning(*columns.map{|c| Sequel.identifier(c)}) end prepare_statement(ds, type, vals) end
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 54 def prepare_statement(ds, type, vals=OPTS) ds.clone(:log_sql=>true).prepare(type, :"smpsp_#{NEXT.call}", vals) end
Return a sorted array of columns for use as a hash key.
# File lib/sequel/plugins/prepared_statements.rb, line 59 def prepared_columns(cols) cols.sort end
Return a prepared statement that can be used to insert a row using the given columns.
# File lib/sequel/plugins/prepared_statements.rb, line 64 def prepared_insert(cols) cached_prepared_statement(:insert, prepared_columns(cols)){prepare_statement(dataset, :insert, prepared_statement_key_hash(cols))} end
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 70 def prepared_insert_select(cols) 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
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 76 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
Return a hash mapping column symbols to placeholder symbols.
# File lib/sequel/plugins/prepared_statements.rb, line 92 def prepared_statement_key_hash(keys) Hash[*(prepared_statement_key_array(keys).flatten)] end
Return a prepared statement that can be used to update row using the given columns.
# File lib/sequel/plugins/prepared_statements.rb, line 97 def prepared_update(cols) cached_prepared_statement(:update, prepared_columns(cols)){prepare_statement(where(prepared_statement_key_array(primary_key)), :update, prepared_statement_key_hash(cols))} end
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 116 def use_prepared_statements_for_pk_lookup? !@fast_pk_lookup_sql && !dataset.joined_dataset? end