Class: DBI::DBD::Pg::Statement

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

Overview

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

– Peculiar Statement responsibilities:

- Translate dbi params (?, ?, ...) to Pg params ($1, $2, ...)
- Translate DBI::Binary objects to Pg large objects (lo_*)

Defined Under Namespace

Classes: DummyQuoter

Constant Summary collapse

PG_STMT_NAME_PREFIX =
'ruby-dbi:Pg:'

Instance Method Summary collapse

Constructor Details

#initialize(db, sql) ⇒ Statement

Returns a new instance of Statement.



13
14
15
16
17
18
19
20
21
22
23
# 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

Instance Method Details

#[](attr) ⇒ Object

Attributes:

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



100
101
102
103
104
105
106
107
108
109
110
111
# 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) ⇒ Object



25
26
27
# File 'lib/dbd/pg/statement.rb', line 25

def bind_param(index, value, options)
    @bindvars[index-1] = value
end

#column_infoObject

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



82
83
84
# File 'lib/dbd/pg/statement.rb', line 82

def column_info
    @result.column_info
end

#executeObject

See DBI::BaseDatabase#execute.

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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

#fetchObject



65
66
67
# File 'lib/dbd/pg/statement.rb', line 65

def fetch
    @result.fetchrow
end

#fetch_scroll(direction, offset) ⇒ Object



69
70
71
# File 'lib/dbd/pg/statement.rb', line 69

def fetch_scroll(direction, offset)
    @result.fetch_scroll(direction, offset)
end

#finishObject



73
74
75
76
77
# File 'lib/dbd/pg/statement.rb', line 73

def finish
    internal_finish
    @result = nil
    @db = nil
end

#rowsObject



86
87
88
89
90
91
92
# File 'lib/dbd/pg/statement.rb', line 86

def rows
    if @result
        @result.rows_affected
    else
        nil
    end
end