module Sequel::Dataset::PreparedStatementMethods

  1. lib/sequel/dataset/prepared_statements.rb

Backbone of the prepared statement support. Grafts bind variable support into datasets by hijacking literal 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.

Public Instance methods

call(bind_vars=OPTS, &block)

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

[show source]
    # File lib/sequel/dataset/prepared_statements.rb
124 def call(bind_vars=OPTS, &block)
125   bind(bind_vars).run(&block)
126 end
columns()

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

[show source]
    # File lib/sequel/dataset/prepared_statements.rb
137 def columns
138   orig_dataset.columns
139 end
delayed_evaluation_sql_append(sql, delay)

Disallow use of delayed evaluations in prepared statements.

[show source]
    # File lib/sequel/dataset/prepared_statements.rb
142 def delayed_evaluation_sql_append(sql, delay)
143   raise Error, "delayed evaluations cannot be used in prepared statements" if @opts[:no_delayed_evaluations]
144   super
145 end
inspect()

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

[show source]
    # File lib/sequel/dataset/prepared_statements.rb
183 def inspect
184   "<#{visible_class_name}/PreparedStatement #{prepared_sql.inspect}>"
185 end
literal_symbol_append(sql, v)

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.

[show source]
    # File lib/sequel/dataset/prepared_statements.rb
172 def literal_symbol_append(sql, v)
173   if @opts[:bind_vars] && /\A\$(.*)\z/ =~ v
174     literal_append(sql, prepared_arg($1.to_sym))
175   else
176     super
177   end
178 end
log_sql()

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

[show source]
   # File lib/sequel/dataset/prepared_statements.rb
90 def log_sql
91   @opts[:log_sql]
92 end
orig_dataset()

The dataset that created this prepared statement.

[show source]
    # File lib/sequel/dataset/prepared_statements.rb
112 def orig_dataset
113   @opts[:orig_dataset]
114 end
prepare(*)

Raise an error if attempting to call prepare on an already prepared statement.

[show source]
    # File lib/sequel/dataset/prepared_statements.rb
130 def prepare(*)
131   raise Error, "cannot prepare an already prepared statement" unless allow_preparing_prepared_statements?
132   super
133 end
prepared_args()

The array/hash of bound variable placeholder names.

[show source]
    # File lib/sequel/dataset/prepared_statements.rb
107 def prepared_args
108   @opts[:prepared_args]
109 end
prepared_modify_values()

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

[show source]
    # File lib/sequel/dataset/prepared_statements.rb
118 def prepared_modify_values
119   @opts[:prepared_modify_values]
120 end
prepared_sql()

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

[show source]
    # File lib/sequel/dataset/prepared_statements.rb
149 def prepared_sql
150   case prepared_sql_type
151   when :select, :all, :each
152     # Most common scenario, so listed first.
153     select_sql
154   when :first, :single_value
155     clone(:limit=>1).select_sql
156   when :insert_select
157     insert_select_sql(*prepared_modify_values)
158   when :insert, :insert_pk
159     insert_sql(*prepared_modify_values)
160   when :update
161     update_sql(*prepared_modify_values)
162   when :delete
163     delete_sql
164   else
165     select_sql
166   end
167 end
prepared_sql_type()

The type of SQL to generate for the prepared statement. Generally the same as prepared_type, but can be different.

[show source]
   # File lib/sequel/dataset/prepared_statements.rb
96 def prepared_sql_type
97   @opts[:prepared_sql_type] || prepared_type
98 end
prepared_type()

The type of prepared statement, which controls how the prepared statement handles results from the database.

[show source]
    # File lib/sequel/dataset/prepared_statements.rb
102 def prepared_type
103   @opts[:prepared_type]
104 end

Protected Instance methods

run(&block)

Run the method based on the type of prepared statement.

[show source]
    # File lib/sequel/dataset/prepared_statements.rb
190 def run(&block)
191   case type = prepared_type
192   when :select, :all
193     with_sql_all(prepared_sql, &block)
194   when :each
195     with_sql_each(prepared_sql, &block)
196   when :insert_select, :first
197     with_sql_first(prepared_sql)
198   when :insert, :update, :delete
199     if opts[:returning] && supports_returning?(prepared_type)
200       returning_fetch_rows(prepared_sql)
201     elsif type == :delete
202       with_sql_delete(prepared_sql)
203     else
204       force_prepared_sql.public_send(type, *prepared_modify_values)
205     end
206   when :insert_pk, :single_value
207     with_sql_single_value(prepared_sql)
208   when Array
209     # :nocov:
210     case type[0]
211     # :nocov:
212     when :map, :as_hash, :to_hash, :to_hash_groups
213       force_prepared_sql.public_send(*type, &block) 
214     end
215   else
216     raise Error, "unsupported prepared statement type used: #{prepared_type.inspect}"
217   end
218 end