Parent

Files

SQLite::ParsedStatement

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.

Attributes

trailing[R]

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.

Public Class Methods

new( sql ) click to toggle source

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.

# 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

Public Instance Methods

bind_param( param, value ) click to toggle source

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.

# 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 ) click to toggle source

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.

# 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
placeholders() click to toggle source

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).

# File lib/sqlite/parsed_statement.rb, line 127
def placeholders
  @bind_values.keys
end
sql() click to toggle source

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

# File lib/sqlite/parsed_statement.rb, line 133
def sql
  @tokens.inject( "" ) { |sql,tok| sql << tok.to_s }
end
to_s() click to toggle source

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

# File lib/sqlite/parsed_statement.rb, line 139
def to_s
  @tokens.inject( "" ) { |sql,tok| sql << tok.to_s( @bind_values ) }
end
Also aliased as: to_str
to_str() click to toggle source
Alias for: to_s

[Validate]

Generated with the Darkfish Rdoc Generator 2.