### Home Classes Methods

### In Files

- lib/dbi/sql/preparedstatement.rb

### Parent

Object

### Methods

- ::new
- ::tokens
- #bind
- #tokens

### Files

- README

### Class/Module Index

Quicksearch

- DBI
- DBI::BaseDatabase
- DBI::BaseDriver
- DBI::BaseStatement
- DBI::Binary
- DBI::ColumnInfo
- DBI::DataError
- DBI::DatabaseError
- DBI::DatabaseHandle
- DBI::Date
- DBI::DriverHandle
- DBI::Error
- DBI::Handle
- DBI::HandleTracer
- DBI::IntegrityError
- DBI::InterfaceError
- DBI::InternalError
- DBI::NotImplementedError
- DBI::NotSupportedError
- DBI::OperationalError
- DBI::ProgrammingError
- DBI::Row
- DBI::SQL
- DBI::SQL::PreparedStatement
- DBI::StatementHandle
- DBI::Time
- DBI::Timestamp
- DBI::Type
- DBI::Type::Boolean
- DBI::Type::Decimal
- DBI::Type::Float
- DBI::Type::Integer
- DBI::Type::Null
- DBI::Type::Timestamp
- DBI::Type::Varchar
- DBI::TypeUtil
- DBI::Utils
- DBI::Utils::ConvParam
- DBI::Utils::TableFormatter
- DBI::Utils::XMLFormatter
- DBI::Warning
- Class

No matching classes.

# DBI::SQL::PreparedStatement

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.

### Attributes

unbound[RW]

### Public Class Methods

new(quoter, sql) click to toggle source

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

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

tokens(sql) click to toggle source

Convenience method for consumers that just need the tokens method.

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

### Public Instance Methods

bind(args) click to toggle source

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

    # 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

tokens() click to toggle source

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!

    # File lib/dbi/sql/preparedstatement.rb, line 40
    def tokens
        @sql.scan(%{
            (
                -- .*                               (?# 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 / )

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

[Validate]

Generated with the Darkfish Rdoc Generator 2.