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.
Methods
Public Instance
- call
- columns
- delayed_evaluation_sql_append
- inspect
- literal_symbol_append
- log_sql
- orig_dataset
- prepare
- prepared_args
- prepared_modify_values
- prepared_sql
- prepared_type
Protected Instance
Public Instance methods
Sets the prepared_args
to the given hash and runs the prepared statement.
# File lib/sequel/dataset/prepared_statements.rb 118 def call(bind_vars=OPTS, &block) 119 bind(bind_vars).run(&block) 120 end
Send the columns to the original dataset, as calling it on the prepared statement can cause problems.
# File lib/sequel/dataset/prepared_statements.rb 131 def columns 132 orig_dataset.columns 133 end
Disallow use of delayed evaluations in prepared statements.
# File lib/sequel/dataset/prepared_statements.rb 136 def delayed_evaluation_sql_append(sql, delay) 137 raise Error, "delayed evaluations cannot be used in prepared statements" if @opts[:no_delayed_evaluations] 138 super 139 end
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 177 def inspect 178 "<#{visible_class_name}/PreparedStatement #{prepared_sql.inspect}>" 179 end
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 166 def literal_symbol_append(sql, v) 167 if @opts[:bind_vars] && /\A\$(.*)\z/ =~ v 168 literal_append(sql, prepared_arg($1.to_sym)) 169 else 170 super 171 end 172 end
Whether to log the full SQL
query. By default, just the prepared statement name is generally logged on adapters that support native prepared statements.
# File lib/sequel/dataset/prepared_statements.rb 90 def log_sql 91 @opts[:log_sql] 92 end
The dataset that created this prepared statement.
# File lib/sequel/dataset/prepared_statements.rb 106 def orig_dataset 107 @opts[:orig_dataset] 108 end
Raise an error if attempting to call prepare on an already prepared statement.
# File lib/sequel/dataset/prepared_statements.rb 124 def prepare(*) 125 raise Error, "cannot prepare an already prepared statement" unless allow_preparing_prepared_statements? 126 super 127 end
The array/hash of bound variable placeholder names.
# File lib/sequel/dataset/prepared_statements.rb 101 def prepared_args 102 @opts[:prepared_args] 103 end
The argument to supply to insert and update, which may use placeholders specified by prepared_args
# File lib/sequel/dataset/prepared_statements.rb 112 def prepared_modify_values 113 @opts[:prepared_modify_values] 114 end
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 143 def prepared_sql 144 case prepared_type 145 when :select, :all, :each 146 # Most common scenario, so listed first. 147 select_sql 148 when :first, :single_value 149 clone(:limit=>1).select_sql 150 when :insert_select 151 insert_select_sql(*prepared_modify_values) 152 when :insert, :insert_pk 153 insert_sql(*prepared_modify_values) 154 when :update 155 update_sql(*prepared_modify_values) 156 when :delete 157 delete_sql 158 else 159 select_sql 160 end 161 end
The type of prepared statement, should be one of :select, :first, :insert, :update, :delete, or :single_value
# File lib/sequel/dataset/prepared_statements.rb 96 def prepared_type 97 @opts[:prepared_type] 98 end
Protected Instance methods
Run the method based on the type of prepared statement.
# File lib/sequel/dataset/prepared_statements.rb 184 def run(&block) 185 case prepared_type 186 when :select, :all 187 all(&block) 188 when :each 189 each(&block) 190 when :insert_select 191 with_sql(prepared_sql).first 192 when :first 193 first 194 when :insert, :update, :delete 195 if opts[:returning] && supports_returning?(prepared_type) 196 returning_fetch_rows(prepared_sql) 197 elsif prepared_type == :delete 198 delete 199 else 200 public_send(prepared_type, *prepared_modify_values) 201 end 202 when :insert_pk 203 fetch_rows(prepared_sql){|r| return r.values.first} 204 when Array 205 # :nocov: 206 case prepared_type[0] 207 # :nocov: 208 when :map, :as_hash, :to_hash, :to_hash_groups 209 public_send(*prepared_type, &block) 210 end 211 when :single_value 212 single_value 213 else 214 raise Error, "unsupported prepared statement type used: #{prepared_type.inspect}" 215 end 216 end