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 104 def cached_prepared_statement(type, subtype) 105 h = @prepared_statements[type] 106 Sequel.synchronize do 107 if v = h[subtype] 108 return v 109 end 110 end 111 ps = yield 112 Sequel.synchronize{h[subtype] = ps} 113 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 44 def prepare_explicit_statement(ds, type, vals=OPTS) 45 s = ds.opts[:returning] 46 if !s || s.empty? 47 ds = ds.returning(*columns.map{|c| Sequel.identifier(c)}) 48 end 49 50 prepare_statement(ds, type, vals) 51 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 55 def prepare_statement(ds, type, vals=OPTS) 56 ds.clone(:log_sql=>true).prepare(type, :"smpsp_#{NEXT.call}", vals) 57 end
Return a sorted array of columns for use as a hash key.
# File lib/sequel/plugins/prepared_statements.rb 60 def prepared_columns(cols) 61 cols.sort 62 end
Return a prepared statement that can be used to insert a row using the given columns.
# File lib/sequel/plugins/prepared_statements.rb 65 def prepared_insert(cols) 66 cached_prepared_statement(:insert, prepared_columns(cols)){prepare_statement(dataset, :insert, prepared_statement_key_hash(cols))} 67 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 71 def prepared_insert_select(cols) 72 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))} 73 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 77 def prepared_statement_key_array(keys) 78 if dataset.requires_placeholder_type_specifiers? 79 sch = db_schema 80 Array(keys).map do |k| 81 if (s = sch[k]) && (t = s[:type]) 82 [k, :"$#{k}__#{t}"] 83 else 84 [k, :"$#{k}"] 85 end 86 end 87 else 88 Array(keys).map{|k| [k, :"$#{k}"]} 89 end 90 end
Return a hash mapping column symbols to placeholder symbols.
# File lib/sequel/plugins/prepared_statements.rb 93 def prepared_statement_key_hash(keys) 94 Hash[*(prepared_statement_key_array(keys).flatten)] 95 end
Return a prepared statement that can be used to update row using the given columns.
# File lib/sequel/plugins/prepared_statements.rb 98 def prepared_update(cols) 99 cached_prepared_statement(:update, prepared_columns(cols)){prepare_statement(where(prepared_statement_key_array(primary_key)), :update, prepared_statement_key_hash(cols))} 100 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 117 def use_prepared_statements_for_pk_lookup? 118 !@fast_pk_lookup_sql && !dataset.joined_dataset? 119 end