Parent

Files

DBI::BaseStatement

StatementHandles are used to encapsulate the process of managing a statement (DDL or DML) and its parameters, sending it to the database, and gathering any results from the execution of that statement.

As with the other `Base` classes, the terms "DBD Required" and "DBD Optional" are defined in DBI::BaseDatabase.

Attributes

raise_error[RW]

Public Class Methods

new(attr=nil) click to toggle source
# File lib/dbi/base_classes/statement.rb, line 14
def initialize(attr=nil)
    @attr = attr || {}
end

Public Instance Methods

[](attr) click to toggle source

Get statement attributes.

# File lib/dbi/base_classes/statement.rb, line 159
def [](attr)
    @attr ||= { }
    @attr[attr]
end
[]=(attr, value) click to toggle source

Set statement attributes. DBD Optional.

# File lib/dbi/base_classes/statement.rb, line 167
def []=(attr, value)
    raise NotSupportedError
end
bind_param(param, value, attribs) click to toggle source

Bind a parameter to the statement. DBD Required.

The parameter number is numeric and indexes starting at 1. This corresponds to the question marks (?) in the statement from the left-most part of the statement moving forward.

The value may be any ruby type. How these are handled is DBD-dependent, but the vast majority of DBDs will convert these to string inside the query.

# File lib/dbi/base_classes/statement.rb, line 29
def bind_param(param, value, attribs)
    raise NotImplementedError
end
bind_params(*bindvars) click to toggle source

Take a list of bind variables and bind them successively using bind_param.

# File lib/dbi/base_classes/statement.rb, line 74
def bind_params(*bindvars)
    bindvars.each_with_index {|val,i| bind_param(i+1, val, nil) }
    self
end
cancel() click to toggle source

Cancel any result cursors. DBD Optional, but intentionally does not raise any exception as it's used internally to maintain consistency.

# File lib/dbi/base_classes/statement.rb, line 83
def cancel
end
column_info() click to toggle source

returns result-set column information as array of hashs, where each hash represents one column. See BaseDatabase#columns. DBD Required.

# File lib/dbi/base_classes/statement.rb, line 63
def column_info
    raise NotImplementedError
end
execute() click to toggle source

Execute the statement with the known binds. DBD Required.

# File lib/dbi/base_classes/statement.rb, line 36
def execute
    raise NotImplementedError
end
fetch() click to toggle source

Fetch the next row in the result set. DBD Required.

DBI::Row is responsible for formatting the data the DBD provides it.

# File lib/dbi/base_classes/statement.rb, line 54
def fetch
    raise NotImplementedError
end
fetch_all() click to toggle source

Fetch all available rows. Result is Array of DBI::Row.

# File lib/dbi/base_classes/statement.rb, line 141
def fetch_all
    rows = []
    loop do
        row = fetch
        break if row.nil?
        rows << row.dup
    end

    if rows.empty?
        nil
    else
        rows
    end
end
fetch_many(cnt) click to toggle source

fetch x rows. The result is Array of DBI::Row.

# File lib/dbi/base_classes/statement.rb, line 123
def fetch_many(cnt)
    rows = []
    cnt.times do
        row = fetch
        break if row.nil?
        rows << row.dup
    end

    if rows.empty?
        nil
    else
        rows
    end
end
fetch_scroll(direction, offset) click to toggle source

fetch_scroll is provided with a direction and offset and works similar to how seek() is used on files.

The constants available for direction are as follows:

  • SQL_FETCH_NEXT: fetch the next result.

  • SQL_FETCH_LAST: fetch the last result, period.

  • SQL_FETCH_RELATIVE: fetch the result at the offset.

Other constants can be used, but if this method is not supplied by the driver, they will result in a raise of DBI::NotSupportedError.

# File lib/dbi/base_classes/statement.rb, line 100
def fetch_scroll(direction, offset)
    case direction
    when SQL_FETCH_NEXT
        return fetch
    when SQL_FETCH_LAST
        last_row = nil
        while (row=fetch) != nil
            last_row = row
        end
        return last_row
    when SQL_FETCH_RELATIVE
        raise NotSupportedError if offset <= 0
        row = nil
        offset.times { row = fetch; break if row.nil? }
        return row
    else
        raise NotSupportedError
    end
end
finish() click to toggle source

Close the statement and any result cursors. DBD Required.

Note

Most implementations will fail miserably if you forget to finish your statement handles.

# File lib/dbi/base_classes/statement.rb, line 45
def finish
    raise NotImplementedError
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.