Class: SQLite::ParsedStatement::BindVariable

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

Overview

This represents a bind variable in a tokenized SQL stream. It is used only by ParsedStatement.

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ BindVariable

Create a new BindVariable token encapsulating the given name. The name is used when looking up a bind variable to bind to the place holder represented by this token. The name may be either a Fixnum (in which case it represents an positional placeholder) or a a String (in which case it represents a named placeholder).



75
76
77
# File 'lib/sqlite/parsed_statement.rb', line 75

def initialize( name )
  @name = name
end

Instance Method Details

#to_s(vars = nil) ⇒ Object

Convert the token to a string. If the vars parameter is nil, then the string will be in the format ?nnn (where nnn is the index that was used to initialize this token). Otherwise, the vars parameter must be a hash, and the value bound to this token is the element with the key given to this token when it was created. If that element is nil, this will return the string “NULL”. If the element is a String, then it will be quoted and escaped and returned. Otherwise, the “to_s” method of the element will be called and the result returned.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/sqlite/parsed_statement.rb', line 88

def to_s( vars=nil )
  if vars.nil?
    ":#{@name}"
  else
    var = vars[ @name ]
    case var
      when nil
        "NULL"
      when String
        "'#{var.gsub(/'/,"''")}'"
      else
        var.to_s
    end
  end
end