Included Modules

Class Index [+]

Quicksearch

Sequel::Dataset::PreparedStatementMethods

Allow use of server side prepared statements for PostgreSQL using the pg driver.


Backbone of the prepared statement support. Grafts bind variable support into datasets by hijacking # and using placeholders. By default, emulates prepared statements and bind variables by taking the hash of bind variables and directly substituting them into the query, which works on all databases, as it is no different from using the dataset without bind variables.

Constants

PLACEHOLDER_RE

Attributes

log_sql[RW]

Whether to log the full SQL query. By default, just the prepared statement name is generally logged on adapters that support native prepared statements.

prepared_type[RW]

The type of prepared statement, should be one of :select, :first, :insert, :update, or :delete

prepared_args[RW]

The array/hash of bound variable placeholder names.

orig_dataset[RW]

The dataset that created this prepared statement.

prepared_modify_values[RW]

The argument to supply to insert and update, which may use placeholders specified by prepared_args

Public Instance Methods

call(*) click to toggle source

Raise a more obvious error if you attempt to call a unnamed prepared statement.

     # File lib/sequel/adapters/postgres.rb, line 653
653:           def call(*)
654:             raise Error, "Cannot call prepared statement without a name" if prepared_statement_name.nil?
655:             super
656:           end
call(bind_vars={}, &block) click to toggle source

Sets the prepared_args to the given hash and runs the prepared statement.

    # File lib/sequel/dataset/prepared_statements.rb, line 69
69:       def call(bind_vars={}, &block)
70:         bind(bind_vars).run(&block)
71:       end
columns() click to toggle source

Send the columns to the original dataset, as calling it on the prepared statement can cause problems.

    # File lib/sequel/dataset/prepared_statements.rb, line 75
75:       def columns
76:         orig_dataset.columns
77:       end
inspect() click to toggle source

Programmer friendly string showing this is a prepared statement, with the prepared SQL it represents (which in general won’t have substituted variables).

     # File lib/sequel/dataset/prepared_statements.rb, line 120
120:       def inspect
121:         "<#{self.class.name}/PreparedStatement #{prepared_sql.inspect}>"
122:       end
literal_symbol_append(sql, v) click to toggle source

Changes the values of symbols if they start with $ and prepared_args is present. If so, they are considered placeholders, and they are substituted using prepared_arg.

     # File lib/sequel/dataset/prepared_statements.rb, line 104
104:       def literal_symbol_append(sql, v)
105:         if @opts[:bind_vars] and match = PLACEHOLDER_RE.match(v.to_s)
106:           s = match[1].to_sym
107:           if prepared_arg?(s)
108:             literal_append(sql, prepared_arg(s))
109:           else
110:             sql << v.to_s
111:           end
112:         else
113:           super
114:         end
115:       end
prepared_sql() click to toggle source

Returns the SQL for the prepared statement, depending on the type of the statement and the prepared_modify_values.

    # File lib/sequel/dataset/prepared_statements.rb, line 81
81:       def prepared_sql
82:         case @prepared_type
83:         when :select, :all, :each
84:           # Most common scenario, so listed first.
85:           select_sql
86:         when :first
87:           clone(:limit=>1).select_sql
88:         when :insert_select
89:           returning.insert_sql(*@prepared_modify_values)
90:         when :insert
91:           insert_sql(*@prepared_modify_values)
92:         when :update
93:           update_sql(*@prepared_modify_values)
94:         when :delete
95:           delete_sql
96:         else
97:           select_sql
98:         end
99:       end

Protected Instance Methods

run(&block) click to toggle source

Run the method based on the type of prepared statement, with :select running # to get all of the rows, and the other types running the method with the same name as the type.

     # File lib/sequel/dataset/prepared_statements.rb, line 129
129:       def run(&block)
130:         case @prepared_type
131:         when :select, :all
132:           # Most common scenario, so listed first
133:           all(&block)
134:         when :each
135:           each(&block)
136:         when :insert_select
137:           with_sql(prepared_sql).first
138:         when :first
139:           first
140:         when :insert
141:           insert(*@prepared_modify_values)
142:         when :update
143:           update(*@prepared_modify_values)
144:         when :delete
145:           delete
146:         when Array
147:           case @prepared_type.at(0)
148:           when :map, :to_hash, :to_hash_groups
149:             send(*@prepared_type, &block) 
150:           end
151:         else
152:           all(&block)
153:         end
154:       end

Private Instance Methods

execute(sql, opts={}, &block) click to toggle source

Execute the stored prepared statement name and the stored bind arguments instead of the SQL given.

     # File lib/sequel/adapters/oracle.rb, line 351
351:         def execute(sql, opts={}, &block)
352:           super(prepared_statement_name, opts, &block)
353:         end
execute(sql, opts={}, &block) click to toggle source

Execute the stored prepared statement name and the stored bind arguments instead of the SQL given.

     # File lib/sequel/adapters/postgres.rb, line 662
662:           def execute(sql, opts={}, &block)
663:             super(prepared_statement_name, opts, &block)
664:           end
execute_dui(sql, opts={}, &block) click to toggle source

Same as execute, explicit due to intricacies of alias and super.

     # File lib/sequel/adapters/postgres.rb, line 667
667:           def execute_dui(sql, opts={}, &block)
668:             super(prepared_statement_name, opts, &block)
669:           end
execute_dui(sql, opts={}, &block) click to toggle source

Same as execute, explicit due to intricacies of alias and super.

     # File lib/sequel/adapters/oracle.rb, line 356
356:         def execute_dui(sql, opts={}, &block)
357:           super(prepared_statement_name, opts, &block)
358:         end
execute_insert(sql, opts={}, &block) click to toggle source

Same as execute, explicit due to intricacies of alias and super.

     # File lib/sequel/adapters/oracle.rb, line 361
361:         def execute_insert(sql, opts={}, &block)
362:           super(prepared_statement_name, opts, &block)
363:         end
prepared_arg(k) click to toggle source

Returns the value of the prepared_args hash for the given key.

     # File lib/sequel/dataset/prepared_statements.rb, line 159
159:       def prepared_arg(k)
160:         @opts[:bind_vars][k]
161:       end
prepared_arg?(k) click to toggle source

Whether there is a bound value for the given key.

     # File lib/sequel/dataset/prepared_statements.rb, line 164
164:       def prepared_arg?(k)
165:         @opts[:bind_vars].has_key?(k)
166:       end
subselect_sql_append(sql, ds) click to toggle source

Use a clone of the dataset extended with prepared statement support and using the same argument hash so that you can use bind variables/prepared arguments in subselects.

     # File lib/sequel/dataset/prepared_statements.rb, line 171
171:       def subselect_sql_append(sql, ds)
172:         ps = ds.clone(:append_sql=>sql).prepare(:select)
173:         ps = ps.bind(@opts[:bind_vars]) if @opts[:bind_vars]
174:         ps.prepared_args = prepared_args
175:         ps.prepared_sql
176:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.