Use a prepared statement to query the database for the row matching the given primary key.
# File lib/sequel/plugins/prepared_statements.rb, line 115 115: def primary_key_lookup(pk) 116: prepared_lookup.call(primary_key_hash(pk)) 117: end
Setup the datastructure used to hold the prepared statements in the subclass.
# File lib/sequel/plugins/prepared_statements.rb, line 39 39: def inherited(subclass) 40: super 41: subclass.instance_variable_set(:@prepared_statements, :insert=>{}, :insert_select=>{}, :update=>{}, :lookup_sql=>{}, :fixed=>{}) 42: end
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 123 123: def cached_prepared_statement(type, subtype) 124: h = @prepared_statements[type] 125: Sequel.synchronize do 126: if v = h[subtype] 127: return v end 128: end 129: ps = yield 130: Sequel.synchronize{h[subtype] = ps} 131: 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 48 48: def prepare_statement(ds, type, vals={}) 49: ps = ds.prepare(type, :"smpsp_#{NEXT.call}", vals) 50: ps.log_sql = true 51: ps 52: end
Return a sorted array of columns for use as a hash key.
# File lib/sequel/plugins/prepared_statements.rb, line 55 55: def prepared_columns(cols) 56: RUBY_VERSION >= '1.9' ? cols.sort : cols.sort_by{|c| c.to_s} 57: end
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 60 60: def prepared_delete 61: cached_prepared_statement(:fixed, :delete){prepare_statement(filter(prepared_statement_key_array(primary_key)), :delete)} 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, line 65 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, line 71 71: def prepared_insert_select(cols) 72: if dataset.supports_insert_select? 73: cached_prepared_statement(:insert_select, prepared_columns(cols)){prepare_statement(naked.clone(:server=>dataset.opts.fetch(:server, :default)), :insert_select, prepared_statement_key_hash(cols))} 74: end 75: end
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 78 78: def prepared_lookup 79: cached_prepared_statement(:fixed, :lookup){prepare_statement(filter(prepared_statement_key_array(primary_key)), :first)} 80: end
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 83 83: def prepared_refresh 84: cached_prepared_statement(:fixed, :refresh){prepare_statement(naked.clone(:server=>dataset.opts.fetch(:server, :default)).filter(prepared_statement_key_array(primary_key)), :first)} 85: 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 89 89: def prepared_statement_key_array(keys) 90: if dataset.requires_placeholder_type_specifiers? 91: sch = db_schema 92: Array(keys).map do |k| 93: if (s = sch[k]) && (t = s[:type]) 94: [k, :"$#{k}__#{t}"] 95: else 96: [k, :"$#{k}"] 97: end 98: end 99: else 100: Array(keys).map{|k| [k, :"$#{k}"]} 101: end 102: end
Return a hash mapping column symbols to placeholder symbols.
# File lib/sequel/plugins/prepared_statements.rb, line 105 105: def prepared_statement_key_hash(keys) 106: Hash[*(prepared_statement_key_array(keys).flatten)] 107: end
Return a prepared statement that can be used to update row using the given columns.
# File lib/sequel/plugins/prepared_statements.rb, line 110 110: def prepared_update(cols) 111: cached_prepared_statement(:update, prepared_columns(cols)){prepare_statement(filter(prepared_statement_key_array(primary_key)), :update, prepared_statement_key_hash(cols))} 112: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.