Module | Sequel::Dataset::PreparedStatementMethods |
In: |
lib/sequel/dataset/prepared_statements.rb
lib/sequel/adapters/sqlite.rb lib/sequel/adapters/postgres.rb lib/sequel/adapters/jdbc.rb |
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 65 65: def call(bind_vars={}, &block) 66: bind(bind_vars).run(&block) 67: 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 113 113: def inspect 114: "<#{self.class.name}/PreparedStatement #{prepared_sql.inspect}>" 115: 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 97 97: def literal_symbol_append(sql, v) 98: if @opts[:bind_vars] and match = PLACEHOLDER_RE.match(v.to_s) 99: s = match[1].to_sym 100: if prepared_arg?(s) 101: literal_append(sql, prepared_arg(s)) 102: else 103: sql << v.to_s 104: end 105: else 106: super 107: end 108: 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 77 77: def prepared_sql 78: case @prepared_type 79: when :select, :all 80: select_sql 81: when :first 82: clone(:limit=>1).select_sql 83: when :insert_select 84: returning.insert_sql(*@prepared_modify_values) 85: when :insert 86: insert_sql(*@prepared_modify_values) 87: when :update 88: update_sql(*@prepared_modify_values) 89: when :delete 90: delete_sql 91: end 92: 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 122 122: def run(&block) 123: case @prepared_type 124: when :select, :all 125: all(&block) 126: when :insert_select 127: with_sql(prepared_sql).first 128: when :first 129: first 130: when :insert 131: insert(*@prepared_modify_values) 132: when :update 133: update(*@prepared_modify_values) 134: when :delete 135: delete 136: end 137: end