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.
Constant Summary collapse
- PLACEHOLDER_RE =
/\A\$(.*)\z/
Instance Attribute Summary collapse
-
#log_sql ⇒ Object
Whether to log the full SQL query.
-
#orig_dataset ⇒ Object
The dataset that created this 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_type ⇒ Object
The type of prepared statement, should be one of :select, :first, :insert, :update, or :delete.
Instance Method Summary collapse
-
#call(bind_vars = {}, &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.
-
#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.
-
#prepared_sql ⇒ Object
Returns the SQL for the prepared statement, depending on the type of the statement and the prepared_modify_values.
Instance Attribute Details
#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.
51 52 53 |
# File 'lib/sequel/dataset/prepared_statements.rb', line 51 def log_sql @log_sql end |
#orig_dataset ⇒ Object
The dataset that created this prepared statement.
61 62 63 |
# File 'lib/sequel/dataset/prepared_statements.rb', line 61 def orig_dataset @orig_dataset end |
#prepared_args ⇒ Object
The array/hash of bound variable placeholder names.
58 59 60 |
# File 'lib/sequel/dataset/prepared_statements.rb', line 58 def prepared_args @prepared_args end |
#prepared_modify_values ⇒ Object
The argument to supply to insert and update, which may use placeholders specified by prepared_args
65 66 67 |
# File 'lib/sequel/dataset/prepared_statements.rb', line 65 def prepared_modify_values @prepared_modify_values end |
#prepared_type ⇒ Object
The type of prepared statement, should be one of :select, :first, :insert, :update, or :delete
55 56 57 |
# File 'lib/sequel/dataset/prepared_statements.rb', line 55 def prepared_type @prepared_type end |
Instance Method Details
#call(bind_vars = {}, &block) ⇒ Object
Sets the prepared_args to the given hash and runs the prepared statement.
69 70 71 |
# File 'lib/sequel/dataset/prepared_statements.rb', line 69 def call(bind_vars={}, &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.
75 76 77 |
# File 'lib/sequel/dataset/prepared_statements.rb', line 75 def columns orig_dataset.columns 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).
120 121 122 |
# File 'lib/sequel/dataset/prepared_statements.rb', line 120 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.
104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/sequel/dataset/prepared_statements.rb', line 104 def literal_symbol_append(sql, v) if @opts[:bind_vars] and match = PLACEHOLDER_RE.match(v.to_s) s = match[1].to_sym if prepared_arg?(s) literal_append(sql, prepared_arg(s)) else sql << v.to_s end else super end end |
#prepared_sql ⇒ Object
Returns the SQL for the prepared statement, depending on the type of the statement and the prepared_modify_values.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/sequel/dataset/prepared_statements.rb', line 81 def prepared_sql case @prepared_type when :select, :all, :each # Most common scenario, so listed first. select_sql when :first clone(:limit=>1).select_sql when :insert_select returning.insert_sql(*@prepared_modify_values) when :insert insert_sql(*@prepared_modify_values) when :update update_sql(*@prepared_modify_values) when :delete delete_sql else select_sql end end |