Class DBI::SQL::PreparedStatement
In: lib/dbi/sql/preparedstatement.rb
Parent: Object

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.

Methods

bind   new   tokens   tokens  

Attributes

unbound  [RW] 

Public Class methods

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

[Source]

    # File lib/dbi/sql/preparedstatement.rb, line 25
25:             def initialize(quoter, sql)
26:                 @quoter, @sql = quoter, sql
27:                 prepare
28:             end

Convenience method for consumers that just need the tokens method.

[Source]

    # File lib/dbi/sql/preparedstatement.rb, line 13
13:             def self.tokens(sql)
14:                 self.new(nil, sql).tokens
15:             end

Public Instance methods

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

[Source]

    # File lib/dbi/sql/preparedstatement.rb, line 62
62:             def bind(args)
63:                 if @arg_index < args.size
64:                     raise "Too many SQL parameters"
65:                 elsif @arg_index > args.size
66:                     raise "Not enough SQL parameters"
67:                 end
68: 
69:                 @unbound.each do |res_pos, arg_pos|
70:                     @result[res_pos] = args[arg_pos]
71:                 end
72: 
73:                 @result.join("")
74:             end

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!

[Source]

    # File lib/dbi/sql/preparedstatement.rb, line 40
40:             def tokens
41:                 @sql.scan(%r{
42:                     (
43:                         -- .*                               (?# matches "--" style comments to the end of line or string )
44:                         |   -                                   (?# matches single "-" )
45:                         |
46:                         /[*] .*? [*]/                       (?# matches C-style comments )
47:                         |   /                                   (?# matches single slash )    
48:                         |
49:                         ' ( [^'\\]  |  ''  |  \\. )* '  (?# match strings surrounded by apostophes )
50:                         |
51:                         " ( [^"\\]  |  ""  |  \\. )* "      (?# match strings surrounded by " )
52:                         |
53:                         \?\??                               (?# match one or two question marks )
54:                         |
55:                         [^-/'"?]+                           (?# match all characters except ' " ? - and / )
56: 
57:                 )}x).collect {|t| t.first}
58:             end

[Validate]