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.
[R] | trailing | 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. |
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.
[ show source ]
# File lib/sqlite/parsed_statement.rb, line 114 114: def initialize( sql ) 115: @bind_values = Hash.new 116: 117: if sql.index( /[?:;]/ ) 118: @tokens, @trailing = tokenize( sql ) 119: else 120: @tokens, @trailing = [ Token.new(sql) ], "" 121: end 122: 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.
[ show source ]
# File lib/sqlite/parsed_statement.rb, line 166 166: def bind_param( param, value ) 167: return unless @bind_values.has_key?( param ) 168: @bind_values[ param ] = value 169: 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.
[ show source ]
# File lib/sqlite/parsed_statement.rb, line 150 150: def bind_params( *bind_vars ) 151: index = 1 152: bind_vars.each do |value| 153: if value.is_a?( Hash ) 154: value.each_pair { |key, value| bind_param( key, value ) } 155: else 156: bind_param index, value 157: index += 1 158: end 159: end 160: self 161: 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).
[ show source ]
# File lib/sqlite/parsed_statement.rb, line 127 127: def placeholders 128: @bind_values.keys 129: end
Returns the SQL that was given to this parsed statement when it was created, with bind placeholders intact.
[ show source ]
# File lib/sqlite/parsed_statement.rb, line 133 133: def sql 134: @tokens.inject( "" ) { |sql,tok| sql << tok.to_s } 135: end
Returns the statement as an SQL string, with all placeholders bound to their corresponding values.
[ show source ]
# File lib/sqlite/parsed_statement.rb, line 139 139: def to_s 140: @tokens.inject( "" ) { |sql,tok| sql << tok.to_s( @bind_values ) } 141: end
Alias for to_s