Module: Sequel::Dataset::PreparedStatementMethods
- Defined in:
- lib/sequel/dataset/prepared_statements.rb
Overview
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.
Instance Method Summary collapse
-
#call(bind_vars = OPTS, &block) ⇒ Object
Sets the prepared_args to the given hash and runs the prepared statement.
-
#columns ⇒ Object
Send the columns to the original dataset, as calling it on the prepared statement can cause problems.
-
#delayed_evaluation_sql_append(sql, delay) ⇒ Object
Disallow use of delayed evaluations in prepared statements.
-
#inspect ⇒ Object
Programmer friendly string showing this is a prepared statement, with the prepared SQL it represents (which in general won’t have substituted variables).
-
#literal_symbol_append(sql, v) ⇒ Object
Changes the values of symbols if they start with $ and prepared_args is present.
-
#log_sql ⇒ Object
Whether to log the full SQL query.
-
#orig_dataset ⇒ Object
The dataset that created this prepared statement.
-
#prepare ⇒ Object
Raise an error if attempting to call prepare on an already prepared statement.
-
#prepared_args ⇒ Object
The array/hash of bound variable placeholder names.
-
#prepared_modify_values ⇒ Object
The argument to supply to insert and update, which may use placeholders specified by prepared_args.
-
#prepared_sql ⇒ Object
Returns the SQL for the prepared statement, depending on the type of the statement and the prepared_modify_values.
-
#prepared_sql_type ⇒ Object
The type of SQL to generate for the prepared statement.
-
#prepared_type ⇒ Object
The type of prepared statement, which controls how the prepared statement handles results from the database.
Instance Method Details
#call(bind_vars = OPTS, &block) ⇒ Object
Sets the prepared_args to the given hash and runs the prepared statement.
124 125 126 |
# File 'lib/sequel/dataset/prepared_statements.rb', line 124 def call(bind_vars=OPTS, &block) bind(bind_vars).run(&block) end |
#columns ⇒ Object
Send the columns to the original dataset, as calling it on the prepared statement can cause problems.
137 138 139 |
# File 'lib/sequel/dataset/prepared_statements.rb', line 137 def columns orig_dataset.columns end |
#delayed_evaluation_sql_append(sql, delay) ⇒ Object
Disallow use of delayed evaluations in prepared statements.
142 143 144 145 |
# File 'lib/sequel/dataset/prepared_statements.rb', line 142 def delayed_evaluation_sql_append(sql, delay) raise Error, "delayed evaluations cannot be used in prepared statements" if @opts[:no_delayed_evaluations] super end |
#inspect ⇒ Object
Programmer friendly string showing this is a prepared statement, with the prepared SQL it represents (which in general won’t have substituted variables).
183 184 185 |
# File 'lib/sequel/dataset/prepared_statements.rb', line 183 def inspect "<#{visible_class_name}/PreparedStatement #{prepared_sql.inspect}>" end |
#literal_symbol_append(sql, v) ⇒ Object
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.
172 173 174 175 176 177 178 |
# File 'lib/sequel/dataset/prepared_statements.rb', line 172 def literal_symbol_append(sql, v) if @opts[:bind_vars] && /\A\$(.*)\z/ =~ v literal_append(sql, prepared_arg($1.to_sym)) else super end end |
#log_sql ⇒ Object
Whether to log the full SQL query. By default, just the prepared statement name is generally logged on adapters that support native prepared statements.
90 91 92 |
# File 'lib/sequel/dataset/prepared_statements.rb', line 90 def log_sql @opts[:log_sql] end |
#orig_dataset ⇒ Object
The dataset that created this prepared statement.
112 113 114 |
# File 'lib/sequel/dataset/prepared_statements.rb', line 112 def orig_dataset @opts[:orig_dataset] end |
#prepare ⇒ Object
Raise an error if attempting to call prepare on an already prepared statement.
130 131 132 133 |
# File 'lib/sequel/dataset/prepared_statements.rb', line 130 def prepare(*) raise Error, "cannot prepare an already prepared statement" unless allow_preparing_prepared_statements? super end |
#prepared_args ⇒ Object
The array/hash of bound variable placeholder names.
107 108 109 |
# File 'lib/sequel/dataset/prepared_statements.rb', line 107 def prepared_args @opts[:prepared_args] end |
#prepared_modify_values ⇒ Object
The argument to supply to insert and update, which may use placeholders specified by prepared_args
118 119 120 |
# File 'lib/sequel/dataset/prepared_statements.rb', line 118 def prepared_modify_values @opts[:prepared_modify_values] end |
#prepared_sql ⇒ Object
Returns the SQL for the prepared statement, depending on the type of the statement and the prepared_modify_values.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/sequel/dataset/prepared_statements.rb', line 149 def prepared_sql case prepared_sql_type when :select, :all, :each # Most common scenario, so listed first. select_sql when :first, :single_value clone(:limit=>1).select_sql when :insert_select insert_select_sql(*prepared_modify_values) when :insert, :insert_pk insert_sql(*prepared_modify_values) when :update update_sql(*prepared_modify_values) when :delete delete_sql else select_sql end end |
#prepared_sql_type ⇒ Object
The type of SQL to generate for the prepared statement. Generally the same as #prepared_type, but can be different.
96 97 98 |
# File 'lib/sequel/dataset/prepared_statements.rb', line 96 def prepared_sql_type @opts[:prepared_sql_type] || prepared_type end |
#prepared_type ⇒ Object
The type of prepared statement, which controls how the prepared statement handles results from the database.
102 103 104 |
# File 'lib/sequel/dataset/prepared_statements.rb', line 102 def prepared_type @opts[:prepared_type] end |