Module | Sequel::Dataset::PreparedStatementMethods |
In: |
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.
PLACEHOLDER_RE | = | /\A\$(.*)\z/ |
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. |
orig_dataset | [RW] | The dataset that created this prepared statement. |
prepared_args | [RW] | The array/hash of bound variable placeholder names. |
prepared_modify_values | [RW] | The argument to supply to insert and update, which may use placeholders specified by prepared_args |
prepared_type | [RW] | The type of prepared statement, should be one of :select, :first, :insert, :update, or :delete |
Sets the prepared_args to the given hash and runs the prepared statement.
# File lib/sequel/dataset/prepared_statements.rb, line 98 98: def call(bind_vars={}, &block) 99: bind(bind_vars).run(&block) 100: 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, line 156 156: def inspect 157: "<#{visible_class_name}/PreparedStatement #{prepared_sql.inspect}>" 158: 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, line 140 140: def literal_symbol_append(sql, v) 141: if @opts[:bind_vars] and match = PLACEHOLDER_RE.match(v.to_s) 142: s = match[1].to_sym 143: if prepared_arg?(s) 144: literal_append(sql, prepared_arg(s)) 145: else 146: sql << v.to_s 147: end 148: else 149: super 150: end 151: 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, line 117 117: def prepared_sql 118: case @prepared_type 119: when :select, :all, :each 120: # Most common scenario, so listed first. 121: select_sql 122: when :first 123: clone(:limit=>1).select_sql 124: when :insert_select 125: insert_select_sql(*@prepared_modify_values) 126: when :insert 127: insert_sql(*@prepared_modify_values) 128: when :update 129: update_sql(*@prepared_modify_values) 130: when :delete 131: delete_sql 132: else 133: select_sql 134: end 135: end
Run the method based on the type of prepared statement, with :select running all 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 165 165: def run(&block) 166: case @prepared_type 167: when :select, :all 168: # Most common scenario, so listed first 169: all(&block) 170: when :each 171: each(&block) 172: when :insert_select 173: with_sql(prepared_sql).first 174: when :first 175: first 176: when :insert, :update, :delete 177: if opts[:returning] && supports_returning?(@prepared_type) 178: returning_fetch_rows(prepared_sql) 179: elsif @prepared_type == :delete 180: delete 181: else 182: send(@prepared_type, *@prepared_modify_values) 183: end 184: when Array 185: case @prepared_type.at(0) 186: when :map, :to_hash, :to_hash_groups 187: send(*@prepared_type, &block) 188: end 189: else 190: all(&block) 191: end 192: end