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

Instance Method Details

#call(bind_vars = {}, &block) ⇒ Object

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


109
110
111
# File 'lib/sequel/dataset/prepared_statements.rb', line 109

def call(bind_vars={}, &block)
  bind(bind_vars).run(&block)
end

#columnsObject

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


122
123
124
# File 'lib/sequel/dataset/prepared_statements.rb', line 122

def columns
  orig_dataset.columns
end

#delayed_evaluation_sql_append(sql, delay) ⇒ Object

Disallow use of delayed evaluations in prepared statements.

Raises:


127
128
129
130
# File 'lib/sequel/dataset/prepared_statements.rb', line 127

def delayed_evaluation_sql_append(sql, delay)
  raise Error, "delayed evaluations cannot be used in prepared statements" if @opts[:no_delayed_evaluations]
  super
end

#inspectObject

Programmer friendly string showing this is a prepared statement, with the prepared SQL it represents (which in general won't have substituted variables).


173
174
175
# File 'lib/sequel/dataset/prepared_statements.rb', line 173

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.


157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/sequel/dataset/prepared_statements.rb', line 157

def literal_symbol_append(sql, v)
  if @opts[:bind_vars] and match = /\A\$(.*)\z/.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

#log_sqlObject

Whether to log the full SQL query. By default, just the prepared statement name is generally logged on adapters that support native prepared statements.


81
82
83
# File 'lib/sequel/dataset/prepared_statements.rb', line 81

def log_sql
  @opts[:log_sql]
end

#orig_datasetObject

The dataset that created this prepared statement.


97
98
99
# File 'lib/sequel/dataset/prepared_statements.rb', line 97

def orig_dataset
  @opts[:orig_dataset]
end

#prepareObject

Raise an error if attempting to call prepare on an already prepared statement.

Raises:


115
116
117
118
# File 'lib/sequel/dataset/prepared_statements.rb', line 115

def prepare(*)
  raise Error, "cannot prepare an already prepared statement" unless allow_preparing_prepared_statements?
  super
end

#prepared_argsObject

The array/hash of bound variable placeholder names.


92
93
94
# File 'lib/sequel/dataset/prepared_statements.rb', line 92

def prepared_args
  @opts[:prepared_args]
end

#prepared_modify_valuesObject

The argument to supply to insert and update, which may use placeholders specified by prepared_args


103
104
105
# File 'lib/sequel/dataset/prepared_statements.rb', line 103

def prepared_modify_values
  @opts[:prepared_modify_values]
end

#prepared_sqlObject

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


134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/sequel/dataset/prepared_statements.rb', line 134

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
    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_typeObject

The type of prepared statement, should be one of :select, :first, :insert, :update, or :delete


87
88
89
# File 'lib/sequel/dataset/prepared_statements.rb', line 87

def prepared_type
  @opts[:prepared_type]
end