Module | Sequel::Dataset::PreparedStatementMethods |
In: |
lib/sequel/adapters/jdbc.rb
lib/sequel/adapters/mysql.rb lib/sequel/adapters/postgres.rb lib/sequel/adapters/sqlite.rb 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/ |
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 72 72: def call(bind_vars={}, &block) 73: bind(bind_vars).run(&block) 74: 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 108 108: def inspect 109: "<#{self.class.name}/PreparedStatement #{prepared_sql.inspect}>" 110: 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 96 96: def literal_symbol(v) 97: if @opts[:bind_vars] and match = PLACEHOLDER_RE.match(v.to_s) 98: v2 = prepared_arg(match[1].to_sym) 99: v2 ? literal(v2) : v 100: else 101: super 102: end 103: 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 78 78: def prepared_sql 79: case @prepared_type 80: when :select, :all 81: select_sql 82: when :first 83: clone(:limit=>1).select_sql 84: when :insert 85: insert_sql(*@prepared_modify_values) 86: when :update 87: update_sql(*@prepared_modify_values) 88: when :delete 89: delete_sql 90: end 91: 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 117 117: def run(&block) 118: case @prepared_type 119: when :select, :all 120: all(&block) 121: when :first 122: first 123: when :insert 124: insert(*@prepared_modify_values) 125: when :update 126: update(*@prepared_modify_values) 127: when :delete 128: delete 129: end 130: end