Module | Sequel::Dataset::PreparedStatementMethods |
In: |
lib/sequel/adapters/jdbc.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 66 66: def call(bind_vars={}, &block) 67: bind(bind_vars).run(&block) 68: 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 102 102: def inspect 103: "<#{self.class.name}/PreparedStatement #{prepared_sql.inspect}>" 104: 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 90 90: def literal_symbol(v) 91: if @opts[:bind_vars] and match = PLACEHOLDER_RE.match(v.to_s) 92: v2 = prepared_arg(match[1].to_sym) 93: v2 ? literal(v2) : v 94: else 95: super 96: end 97: 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 72 72: def prepared_sql 73: case @prepared_type 74: when :select, :all 75: select_sql 76: when :first 77: clone(:limit=>1).select_sql 78: when :insert 79: insert_sql(*@prepared_modify_values) 80: when :update 81: update_sql(*@prepared_modify_values) 82: when :delete 83: delete_sql 84: end 85: 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 111 111: def run(&block) 112: case @prepared_type 113: when :select, :all 114: all(&block) 115: when :first 116: first 117: when :insert 118: insert(*@prepared_modify_values) 119: when :update 120: update(*@prepared_modify_values) 121: when :delete 122: delete 123: end 124: end