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
107 def cached_prepared_statement(type, subtype)
108   h = @prepared_statements[type]
109   Sequel.synchronize do
110     if v = h[subtype]
111       return v
112     end
113   end
114   ps = yield
115   Sequel.synchronize{h[subtype] = ps}
116 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
43 def prepare_explicit_statement(ds, type, vals=OPTS)
44   f = ds.opts[:from]
45   meth = type == :insert_select ? :returning : :select
46   s = ds.opts[meth]
47   if f && f.length == 1 && !ds.opts[:join] && (!s || s.empty?)
48     ds = ds.public_send(meth, *columns.map{|c| Sequel.identifier(c)})
49   end 
50   
51   prepare_statement(ds, type, vals)
52 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
56 def prepare_statement(ds, type, vals=OPTS)
57   ds.clone(:log_sql=>true).prepare(type, :"smpsp_#{NEXT.call}", vals)
58 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
61 def prepared_columns(cols)
62   cols.sort
63 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
66 def prepared_insert(cols)
67   cached_prepared_statement(:insert, prepared_columns(cols)){prepare_statement(dataset, :insert, prepared_statement_key_hash(cols))}
68 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
72 def prepared_insert_select(cols)
73   if dataset.supports_insert_select?
74     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))}
75   end
76 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
80 def prepared_statement_key_array(keys)
81   if dataset.requires_placeholder_type_specifiers?
82     sch = db_schema
83     Array(keys).map do |k|
84       if (s = sch[k]) && (t = s[:type])
85         [k, :"$#{k}__#{t}"]
86       else
87         [k, :"$#{k}"]
88       end
89     end
90   else
91     Array(keys).map{|k| [k, :"$#{k}"]}
92   end
93 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
96 def prepared_statement_key_hash(keys)
97   Hash[*(prepared_statement_key_array(keys).flatten)]
98 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
101 def prepared_update(cols)
102   cached_prepared_statement(:update, prepared_columns(cols)){prepare_statement(where(prepared_statement_key_array(primary_key)), :update, prepared_statement_key_hash(cols))}
103 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
120 def use_prepared_statements_for_pk_lookup?
121   !@fast_pk_lookup_sql && !dataset.joined_dataset?
122 end