class DBI::DBD::Pg::Statement

See DBI::BaseStatement, and DBI::DBD::Pg::Tuples.

Constants

PG_STMT_NAME_PREFIX

Public Class Methods

new(db, sql) click to toggle source
Calls superclass method
# File lib/dbd/pg/statement.rb, line 13
def initialize(db, sql)
    super(db)
    @db  = db
    @sql = sql
    @stmt_name = PG_STMT_NAME_PREFIX + self.object_id.to_s + Time.now.to_f.to_s
    @result = nil
    @bindvars = []
    @prepared = false
rescue PGError => err
    raise DBI::ProgrammingError.new(err.message)
end

Public Instance Methods

[](attr) click to toggle source

Attributes:

If pg_row_count is requested and the statement has already executed, postgres will return what it believes is the row count.

# File lib/dbd/pg/statement.rb, line 100
def [](attr)
    case attr
    when 'pg_row_count'
        if @result
            @result.row_count
        else
            nil
        end
    else
        @attr[attr]
    end
end
bind_param(index, value, options) click to toggle source
# File lib/dbd/pg/statement.rb, line 25
def bind_param(index, value, options)
    @bindvars[index-1] = value
end
column_info() click to toggle source

See DBI::DBD::Pg::Tuples#column_info.

# File lib/dbd/pg/statement.rb, line 82
def column_info
    @result.column_info
end
execute() click to toggle source

See DBI::BaseDatabase#execute.

This method will make use of PostgreSQL's native BLOB support if DBI::Binary objects are passed in.

# File lib/dbd/pg/statement.rb, line 35
def execute
    # replace DBI::Binary object by oid returned by lo_import
    @bindvars.collect! do |var|
        if var.is_a? DBI::Binary then
            oid = @db.__blob_create(PGconn::INV_WRITE)
            @db.__blob_write(oid, var.to_s)
            oid
        else
            var
        end
    end

    internal_prepare

    if not @db['AutoCommit'] then
        #          if not SQL.query?(boundsql) and not @db['AutoCommit'] then
        @db.start_transaction unless @db.in_transaction?
    end

    if @db["pg_native_binding"]
        pg_result = @db._exec_prepared(@stmt_name, *@bindvars)
    else
        pg_result = @db._exec_prepared(@stmt_name)
    end

    @result = DBI::DBD::Pg::Tuples.new(@db, pg_result)
rescue PGError, RuntimeError => err
    raise DBI::ProgrammingError.new(err.message)
end
fetch() click to toggle source
# File lib/dbd/pg/statement.rb, line 65
def fetch
    @result.fetchrow
end
fetch_scroll(direction, offset) click to toggle source
# File lib/dbd/pg/statement.rb, line 69
def fetch_scroll(direction, offset)
    @result.fetch_scroll(direction, offset)
end
finish() click to toggle source
# File lib/dbd/pg/statement.rb, line 73
def finish
    internal_finish
    @result = nil
    @db = nil
end
rows() click to toggle source
# File lib/dbd/pg/statement.rb, line 86
def rows
    if @result
        @result.rows_affected
    else
        nil
    end
end

Private Instance Methods

internal_finish() click to toggle source

finish the statement at a lower level

# File lib/dbd/pg/statement.rb, line 126
def internal_finish
    @result.finish if @result
    @db._exec("DEALLOCATE \"#{@stmt_name}\"") if @prepared rescue nil
end
internal_prepare() click to toggle source

prepare the statement at a lower level.

# File lib/dbd/pg/statement.rb, line 132
def internal_prepare
    if @db["pg_native_binding"]
        unless @prepared
            @stmt = @db._prepare(@stmt_name, translate_param_markers(@sql))
        end
    else
        internal_finish
        @stmt = @db._prepare(@stmt_name, DBI::SQL::PreparedStatement.new(DBI::DBD::Pg, @sql).bind(@bindvars))
    end
    @prepared = true
end
translate_param_markers(sql) click to toggle source

Prepare the given SQL statement, returning its PostgreSQL string handle. ?-style parameters are translated to $1, $2, etc.

# File lib/dbd/pg/statement.rb, line 152
def translate_param_markers(sql)
    translator = DBI::SQL::PreparedStatement.new(DummyQuoter.new, sql)
    if translator.unbound.size > 0
        arr = (1..(translator.unbound.size)).collect{|i| "$#{i}"}
        sql = translator.bind( arr )
    end
    sql
end