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

Instance Method Summary collapse

Instance Attribute Details

#prepared_argsObject

The array/hash of bound variable placeholder names.



64
65
66
# File 'lib/sequel/dataset/prepared_statements.rb', line 64

def prepared_args
  @prepared_args
end

#prepared_modify_valuesObject

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



68
69
70
# File 'lib/sequel/dataset/prepared_statements.rb', line 68

def prepared_modify_values
  @prepared_modify_values
end

#prepared_typeObject

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



61
62
63
# File 'lib/sequel/dataset/prepared_statements.rb', line 61

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.



72
73
74
# File 'lib/sequel/dataset/prepared_statements.rb', line 72

def call(bind_vars={}, &block)
  bind(bind_vars).run(&block)
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).



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

def inspect
  "<#{self.class.name}/PreparedStatement #{prepared_sql.inspect}>"
end

#literal_symbol(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.



98
99
100
101
102
103
104
105
# File 'lib/sequel/dataset/prepared_statements.rb', line 98

def literal_symbol(v)
  if @opts[:bind_vars] and match = PLACEHOLDER_RE.match(v.to_s)
    s = match[1].to_sym
    prepared_arg?(s) ? literal(prepared_arg(s)) : v
  else
    super
  end
end

#prepared_sqlObject

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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/sequel/dataset/prepared_statements.rb', line 78

def prepared_sql
  case @prepared_type
  when :select, :all
    select_sql
  when :first
    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
  end
end