Object
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.
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
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
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
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
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
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
Generated with the Darkfish Rdoc Generator 2.