Class: DBI::DBD::ODBC::Statement

Inherits:
BaseStatement
  • Object
show all
Defined in:
lib/dbd/odbc/statement.rb

Overview

See DBI::BaseStatement.

Instance Method Summary collapse

Constructor Details

#initialize(handle, statement) ⇒ Statement

Returns a new instance of Statement.



5
6
7
8
9
10
# File 'lib/dbd/odbc/statement.rb', line 5

def initialize(handle, statement)
    @statement = statement
    @handle = handle
    @params = []
    @arr = []
end

Instance Method Details

#bind_param(param, value, attribs) ⇒ Object

See DBI::BaseStatement#bind_param. This method will also raise DBI::InterfaceError if param is not a Fixnum, to prevent incorrect binding.

Raises:

  • (DBI::InterfaceError)


17
18
19
20
# File 'lib/dbd/odbc/statement.rb', line 17

def bind_param(param, value, attribs)
    raise DBI::InterfaceError, "only ? parameters supported" unless param.is_a? Fixnum
    @params[param-1] = value
end

#cancelObject



34
35
36
37
38
# File 'lib/dbd/odbc/statement.rb', line 34

def cancel
    @handle.cancel
rescue DBI::DBD::ODBC::ODBCErr => err
    raise DBI::DatabaseError.new(err.message)
end

#column_infoObject

See DBI::BaseStatement#column_info. These additional attributes are also supported:

  • table: the table this column came from, if available.

  • nullable: boolean, true if NULL is accepted as a value in this column.

  • searchable: FIXME DOCUMENT

  • length: FIXME DOCUMENT

  • unsigned: For numeric columns, whether or not the result value is signed.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/dbd/odbc/statement.rb', line 87

def column_info
    info = []
    @handle.columns(true).each do |col|
        info << {
            'name'       => col.name, 
            'table'      => col.table,
            'nullable'   => col.nullable,
            'searchable' => col.searchable,
            'precision'  => col.precision,
            'scale'      => col.scale,
            'sql_type'   => col.type,
            'type_name'  => DBI::SQL_TYPE_NAMES[col.type],
            'length'     => col.length,
            'unsigned'   => col.unsigned
        }
    end
    info
rescue DBI::DBD::ODBC::ODBCErr => err
    raise DBI::DatabaseError.new(err.message)
end

#executeObject



22
23
24
25
26
# File 'lib/dbd/odbc/statement.rb', line 22

def execute
    @handle.execute(*@params)
rescue DBI::DBD::ODBC::ODBCErr => err
    raise DBI::DatabaseError.new(err.message)
end

#fetchObject



40
41
42
43
44
# File 'lib/dbd/odbc/statement.rb', line 40

def fetch
    convert_row(@handle.fetch)
rescue DBI::DBD::ODBC::ODBCErr => err
    raise DBI::DatabaseError.new(err.message)
end

#fetch_scroll(direction, offset) ⇒ Object

See DBI::BaseStatement#fetch_scroll.

ODBC has a native version of this method and the constnats in the ODBC driver themselves are supported. If you’d prefer to use DBI constants (recommended), you can use these which map to the ODBC functionality:

  • DBI::SQL_FETCH_FIRST

  • DBI::SQL_FETCH_LAST

  • DBI::SQL_FETCH_NEXT

  • DBI::SQL_FETCH_PRIOR

  • DBI::SQL_FETCH_ABSOLUTE

  • DBI::SQL_FETCH_RELATIVE



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/dbd/odbc/statement.rb', line 60

def fetch_scroll(direction, offset)
    direction = case direction
                when DBI::SQL_FETCH_FIRST    then ::ODBC::SQL_FETCH_FIRST
                when DBI::SQL_FETCH_LAST     then ::ODBC::SQL_FETCH_LAST
                when DBI::SQL_FETCH_NEXT     then ::ODBC::SQL_FETCH_NEXT
                when DBI::SQL_FETCH_PRIOR    then ::ODBC::SQL_FETCH_PRIOR
                when DBI::SQL_FETCH_ABSOLUTE then ::ODBC::SQL_FETCH_ABSOLUTE
                when DBI::SQL_FETCH_RELATIVE then ::ODBC::SQL_FETCH_RELATIVE
                else
                    direction
                end

    convert_row(@handle.fetch_scroll(direction, offset))
rescue DBI::DBD::ODBC::ODBCErr => err
    raise DBI::DatabaseError.new(err.message)
end

#finishObject



28
29
30
31
32
# File 'lib/dbd/odbc/statement.rb', line 28

def finish
    @handle.drop
rescue DBI::DBD::ODBC::ODBCErr => err
    raise DBI::DatabaseError.new(err.message)
end

#rowsObject

See DBI::BaseStatement#rows.

For queries which DBI::SQL.query? returns true, will explicitly return 0. Otherwise, it will return the row processed count.



114
115
116
117
118
119
# File 'lib/dbd/odbc/statement.rb', line 114

def rows
    return 0 if DBI::SQL.query?(@statement)
    return @handle.nrows
rescue DBI::DBD::ODBC::ODBCErr => err
    raise DBI::DatabaseError.new(err.message)
end