Class: DBI::SQL::PreparedStatement

Inherits:
Object
  • Object
show all
Defined in:
lib/dbi/sql/preparedstatement.rb

Overview

The PreparedStatement class attempts to provide binding functionality for database systems that do not have this built-in. This package emulates the whole concept of a statement.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(quoter, sql) ⇒ PreparedStatement

“prepare” a statement.

quoter is deprecated and will eventually disappear, it is kept currently for compatibility. It is safe to pass nil to this parameter.

sql is the statement itself.



25
26
27
28
# File 'lib/dbi/sql/preparedstatement.rb', line 25

def initialize(quoter, sql)
    @quoter, @sql = quoter, sql
    prepare
end

Instance Attribute Details

#unboundObject

Returns the value of attribute unbound



9
10
11
# File 'lib/dbi/sql/preparedstatement.rb', line 9

def unbound
  @unbound
end

Class Method Details

.tokens(sql) ⇒ Object

Convenience method for consumers that just need the tokens method.



13
14
15
# File 'lib/dbi/sql/preparedstatement.rb', line 13

def self.tokens(sql)
    self.new(nil, sql).tokens
end

Instance Method Details

#bind(args) ⇒ Object

attempts to bind the arguments in args to this statement. Will raise StandardError if there are any extents issues.



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/dbi/sql/preparedstatement.rb', line 62

def bind(args)
    if @arg_index < args.size
        raise "Too many SQL parameters"
    elsif @arg_index > args.size
        raise "Not enough SQL parameters"
    end

    @unbound.each do |res_pos, arg_pos|
        @result[res_pos] = args[arg_pos]
    end

    @result.join("")
end

#tokensObject

Break the sql string into parts.

This is NOT a full lexer for SQL. It just breaks up the SQL string enough so that question marks, double question marks and quoted strings are separated. This is used when binding arguments to “?” in the SQL string.

C-style (/* */) and Ada-style (–) comments are handled.

Note

Nested C-style comments are NOT handled!



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/dbi/sql/preparedstatement.rb', line 40

def tokens
    @sql.scan(%r{
        (
            -- .*                               (?# matches "--" style comments to the end of line or string )
            |   -                                   (?# matches single "-" )
            |
            /[*] .*? [*]/                       (?# matches C-style comments )
            |   /                                   (?# matches single slash )    
            |
            ' ( [^'\\]  |  ''  |  \\. )* '  (?# match strings surrounded by apostophes )
            |
            " ( [^"\\]  |  ""  |  \\. )* "      (?# match strings surrounded by " )
            |
            \?\??                               (?# match one or two question marks )
            |
            [^-/'"?]+                           (?# match all characters except ' " ? - and / )

    )}x).collect {|t| t.first}
end