Class: SQLite::ParsedStatement

Inherits:
Object
  • Object
show all
Defined in:
lib/sqlite/parsed_statement.rb

Overview

A ParsedStatement instance represents a tokenized version of an SQL statement. This makes it possible to do bind variable replacements multiple times, fairly efficiently.

Within the SQLite interfaces, this is used only by the Statement class. However, it could be reused by other SQL-reliant classes easily.

Defined Under Namespace

Classes: BindVariable, Token

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sql) ⇒ ParsedStatement

Create a new ParsedStatement. This will tokenize the given buffer. As an optimization, the tokenization is only performed if the string matches /[?:;]/, otherwise the string is used as-is.



114
115
116
117
118
119
120
121
122
# File 'lib/sqlite/parsed_statement.rb', line 114

def initialize( sql )
  @bind_values = Hash.new

  if sql.index( /[?:;]/ )
    @tokens, @trailing = tokenize( sql )
  else
    @tokens, @trailing = [ Token.new(sql) ], ""
  end
end

Instance Attribute Details

#trailingObject (readonly)

The text trailing the first recognized SQL statement that was parsed from the buffer given to this object. If there was no trailing SQL statement, this property will be the empty string.



109
110
111
# File 'lib/sqlite/parsed_statement.rb', line 109

def trailing
  @trailing
end

Instance Method Details

#bind_param(param, value) ⇒ Object

Binds the given value to the placeholder indicated by param, which may be either a Fixnum or a String. If the indicated placeholder does not exist in the statement, this method does nothing.



166
167
168
169
# File 'lib/sqlite/parsed_statement.rb', line 166

def bind_param( param, value )
  return unless @bind_values.has_key?( param )
  @bind_values[ param ] = value
end

#bind_params(*bind_vars) ⇒ Object

Binds the given parameters to the placeholders in the statement. It does this by iterating over each argument and calling #bind_param with the corresponding index (starting at 1). However, if any element is a hash, the hash is iterated through and #bind_param called for each key/value pair. Hash’s do not increment the index.



150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/sqlite/parsed_statement.rb', line 150

def bind_params( *bind_vars )
  index = 1
  bind_vars.each do |value|
    if value.is_a?( Hash )
      value.each_pair { |key, value| bind_param( key, value ) }
    else
      bind_param index, value
      index += 1
    end
  end
  self
end

#placeholdersObject

Returns an array of the placeholders known to this statement. This will either be empty (if the statement has no placeholders), or will contain numbers (indexes) and strings (names).



127
128
129
# File 'lib/sqlite/parsed_statement.rb', line 127

def placeholders
  @bind_values.keys
end

#sqlObject

Returns the SQL that was given to this parsed statement when it was created, with bind placeholders intact.



133
134
135
# File 'lib/sqlite/parsed_statement.rb', line 133

def sql
  @tokens.inject( "" ) { |sql,tok| sql << tok.to_s }
end

#to_sObject Also known as: to_str

Returns the statement as an SQL string, with all placeholders bound to their corresponding values.



139
140
141
# File 'lib/sqlite/parsed_statement.rb', line 139

def to_s
  @tokens.inject( "" ) { |sql,tok| sql << tok.to_s( @bind_values ) }
end