Module Sequel::Dataset::PreparedStatementMethods
In: lib/sequel/dataset/prepared_statements.rb
lib/sequel/adapters/jdbc.rb
lib/sequel/adapters/sqlite.rb

Use JDBC PreparedStatements instead of emulated ones. Statements created using prepare are cached at the connection level to allow reuse. This also supports bind variables by using unnamed prepared statements created using call.

Methods

Included Modules

Sequel::Dataset::UnnumberedArgumentMapper BindArgumentMethods

Constants

PLACEHOLDER_RE = /\A\$(.*)\z/

Attributes

log_sql  [RW]  Whether to log the full SQL query. By default, just the prepared statement name is generally logged on adapters that support native prepared statements.
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

Public Instance methods

Sets the prepared_args to the given hash and runs the prepared statement.

[Source]

    # File lib/sequel/dataset/prepared_statements.rb, line 69
69:       def call(bind_vars={}, &block)
70:         bind(bind_vars).run(&block)
71:       end

Send the columns to the original dataset, as calling it on the prepared statement can cause problems.

[Source]

    # File lib/sequel/dataset/prepared_statements.rb, line 75
75:       def columns
76:         orig_dataset.columns
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).

[Source]

     # File lib/sequel/dataset/prepared_statements.rb, line 120
120:       def inspect
121:         c = self.class
122:         c = c.superclass while c.name.nil? || c.name == ''
123:         "<#{c.name}/PreparedStatement #{prepared_sql.inspect}>"
124:       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.

[Source]

     # File lib/sequel/dataset/prepared_statements.rb, line 104
104:       def literal_symbol_append(sql, v)
105:         if @opts[:bind_vars] and match = PLACEHOLDER_RE.match(v.to_s)
106:           s = match[1].to_sym
107:           if prepared_arg?(s)
108:             literal_append(sql, prepared_arg(s))
109:           else
110:             sql << v.to_s
111:           end
112:         else
113:           super
114:         end
115:       end

Returns the SQL for the prepared statement, depending on the type of the statement and the prepared_modify_values.

[Source]

    # File lib/sequel/dataset/prepared_statements.rb, line 81
81:       def prepared_sql
82:         case @prepared_type
83:         when :select, :all, :each
84:           # Most common scenario, so listed first.
85:           select_sql
86:         when :first
87:           clone(:limit=>1).select_sql
88:         when :insert_select
89:           returning.insert_sql(*@prepared_modify_values)
90:         when :insert
91:           insert_sql(*@prepared_modify_values)
92:         when :update
93:           update_sql(*@prepared_modify_values)
94:         when :delete
95:           delete_sql
96:         else
97:           select_sql
98:         end
99:       end

Protected Instance methods

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.

[Source]

     # File lib/sequel/dataset/prepared_statements.rb, line 131
131:       def run(&block)
132:         case @prepared_type
133:         when :select, :all
134:           # Most common scenario, so listed first
135:           all(&block)
136:         when :each
137:           each(&block)
138:         when :insert_select
139:           with_sql(prepared_sql).first
140:         when :first
141:           first
142:         when :insert
143:           insert(*@prepared_modify_values)
144:         when :update
145:           update(*@prepared_modify_values)
146:         when :delete
147:           delete
148:         when Array
149:           case @prepared_type.at(0)
150:           when :map, :to_hash, :to_hash_groups
151:             send(*@prepared_type, &block) 
152:           end
153:         else
154:           all(&block)
155:         end
156:       end

[Validate]