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 104 104: def inspect 105: "<#{self.class.name}/PreparedStatement #{prepared_sql.inspect}>" 106: 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_symbol(v) 94: if match = PLACEHOLDER_RE.match(v.to_s) and @prepared_args 95: literal(prepared_arg(match[1].to_sym)) 96: else 97: super 98: end 99: 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 113 113: def run(&block) 114: case @prepared_type 115: when :select, :all 116: all(&block) 117: when :first 118: first 119: when :insert 120: insert(@prepared_modify_values) 121: when :update 122: update(@prepared_modify_values) 123: when :delete 124: delete 125: end 126: end