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 bind variable hash to use when substituting |
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 67 67: def call(hash, &block) 68: ds = clone 69: ds.prepared_args = hash 70: ds.run(&block) 71: 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 109 109: def inspect 110: "<#{self.class.name}/PreparedStatement #{prepared_sql.inspect}>" 111: 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 93 93: def literal(v) 94: case v 95: when Symbol 96: if match = PLACEHOLDER_RE.match(v.to_s) and @prepared_args 97: super(prepared_arg(match[1].to_sym)) 98: else 99: super 100: end 101: else 102: super 103: end 104: 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 75 75: def prepared_sql 76: case @prepared_type 77: when :select, :all 78: select_sql 79: when :first 80: clone(:limit=>1).select_sql 81: when :insert 82: insert_sql(@prepared_modify_values) 83: when :update 84: update_sql(@prepared_modify_values) 85: when :delete 86: delete_sql 87: end 88: 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 118 118: def run(&block) 119: case @prepared_type 120: when :select, :all 121: all(&block) 122: when :first 123: first 124: when :insert 125: insert(@prepared_modify_values) 126: when :update 127: update(@prepared_modify_values) 128: when :delete 129: delete 130: end 131: end