| Module | Sequel::Dataset::PreparedStatementMethods |
| In: |
lib/sequel/dataset/prepared_statements.rb
lib/sequel/adapters/mysql.rb lib/sequel/adapters/sqlite.rb lib/sequel/adapters/jdbc.rb lib/sequel/adapters/postgres.rb |
Allow use of server side prepared statements for PostgreSQL using the pg driver.
| 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 72
72: def call(bind_vars={}, &block)
73: bind(bind_vars).run(&block)
74: 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 110
110: def inspect
111: "<#{self.class.name}/PreparedStatement #{prepared_sql.inspect}>"
112: 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 98
98: def literal_symbol(v)
99: if @opts[:bind_vars] and match = PLACEHOLDER_RE.match(v.to_s)
100: s = match[1].to_sym
101: prepared_arg?(s) ? literal(prepared_arg(s)) : v
102: else
103: super
104: end
105: 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 78
78: def prepared_sql
79: case @prepared_type
80: when :select, :all
81: select_sql
82: when :first
83: clone(:limit=>1).select_sql
84: when :insert_select
85: clone(:returning=>nil).insert_sql(*@prepared_modify_values)
86: when :insert
87: insert_sql(*@prepared_modify_values)
88: when :update
89: update_sql(*@prepared_modify_values)
90: when :delete
91: delete_sql
92: end
93: 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 119
119: def run(&block)
120: case @prepared_type
121: when :select, :all
122: all(&block)
123: when :insert_select
124: meta_def(:select_sql){prepared_sql}
125: first
126: when :first
127: first
128: when :insert
129: insert(*@prepared_modify_values)
130: when :update
131: update(*@prepared_modify_values)
132: when :delete
133: delete
134: end
135: end