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.

Methods

Included Modules

Sequel::Dataset::UnnumberedArgumentMapper BindArgumentMethods BindArgumentMethods

Constants

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

Attributes

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

Raise a more obvious error if you attempt to call a unnamed prepared statement.

[Source]

     # File lib/sequel/adapters/postgres.rb, line 560
560:           def call(*)
561:             raise Error, "Cannot call prepared statement without a name" if prepared_statement_name.nil?
562:             super
563:           end

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

[Source]

    # File lib/sequel/dataset/prepared_statements.rb, line 65
65:       def call(bind_vars={}, &block)
66:         bind(bind_vars).run(&block)
67:       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 71
71:       def columns
72:         orig_dataset.columns
73:       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 116
116:       def inspect
117:         "<#{self.class.name}/PreparedStatement #{prepared_sql.inspect}>"
118:       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 100
100:       def literal_symbol_append(sql, v)
101:         if @opts[:bind_vars] and match = PLACEHOLDER_RE.match(v.to_s)
102:           s = match[1].to_sym
103:           if prepared_arg?(s)
104:             literal_append(sql, prepared_arg(s))
105:           else
106:             sql << v.to_s
107:           end
108:         else
109:           super
110:         end
111:       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 77
77:       def prepared_sql
78:         case @prepared_type
79:         when :select, :all
80:           # Most common scenario, so listed first.
81:           select_sql
82:         when :first
83:           clone(:limit=>1).select_sql
84:         when :insert_select
85:           returning.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:         else
93:           select_sql
94:         end
95:       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 125
125:       def run(&block)
126:         case @prepared_type
127:         when :select, :all
128:           # Most common scenario, so listed first
129:           all(&block)
130:         when :insert_select
131:           with_sql(prepared_sql).first
132:         when :first
133:           first
134:         when :insert
135:           insert(*@prepared_modify_values)
136:         when :update
137:           update(*@prepared_modify_values)
138:         when :delete
139:           delete
140:         when Array
141:           case @prepared_type.at(0)
142:           when :map, :to_hash
143:             send(*@prepared_type, &block) 
144:           end
145:         else
146:           all(&block)
147:         end
148:       end

[Validate]