Module | Sequel::Dataset::PreparedStatementMethods |
In: |
lib/sequel/adapters/jdbc.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/ |
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 75 75: def call(bind_vars={}, &block) 76: bind(bind_vars).run(&block) 77: 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 123 123: def inspect 124: "<#{self.class.name}/PreparedStatement #{prepared_sql.inspect}>" 125: 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 107 107: def literal_symbol_append(sql, v) 108: if @opts[:bind_vars] and match = PLACEHOLDER_RE.match(v.to_s) 109: s = match[1].to_sym 110: if prepared_arg?(s) 111: literal_append(sql, prepared_arg(s)) 112: else 113: sql << v.to_s 114: end 115: else 116: super 117: end 118: 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 87 87: def prepared_sql 88: case @prepared_type 89: when :select, :all 90: select_sql 91: when :first 92: clone(:limit=>1).select_sql 93: when :insert_select 94: returning.insert_sql(*@prepared_modify_values) 95: when :insert 96: insert_sql(*@prepared_modify_values) 97: when :update 98: update_sql(*@prepared_modify_values) 99: when :delete 100: delete_sql 101: end 102: 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 132 132: def run(&block) 133: case @prepared_type 134: when :select, :all 135: all(&block) 136: when :insert_select 137: with_sql(prepared_sql).first 138: when :first 139: first 140: when :insert 141: insert(*@prepared_modify_values) 142: when :update 143: update(*@prepared_modify_values) 144: when :delete 145: delete 146: end 147: end