Class: DBI::DBD::SQLite3::Statement

Inherits:
BaseStatement show all
Defined in:
lib/dbd/sqlite3/statement.rb

Overview

See DBI::BaseStatement.

Instance Attribute Summary

Attributes inherited from BaseStatement

#raise_error

Instance Method Summary collapse

Methods inherited from BaseStatement

#[], #[]=, #fetch_all, #fetch_many, #fetch_scroll

Constructor Details

#initialize(sql, db) ⇒ Statement

Returns a new instance of Statement.



5
6
7
8
9
10
11
12
13
# File 'lib/dbd/sqlite3/statement.rb', line 5

def initialize(sql, db)
    sql.gsub!(/\\\\/) { '\\' } # sqlite underneath does this for us automatically, and it's causing trouble with the rest of the system.
    @sql = sql
    @db = db
    @stmt = db.prepare(sql)
    @result = nil
rescue ::SQLite3::Exception, RuntimeError => err
    raise DBI::ProgrammingError.new(err.message)
end

Instance Method Details

#bind_param(param, value, attribs = nil) ⇒ Object

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



20
21
22
23
# File 'lib/dbd/sqlite3/statement.rb', line 20

def bind_param(param, value, attribs=nil)
    raise DBI::InterfaceError, "Bound parameter must be an integer" unless param.kind_of? Fixnum 
    @stmt.bind_param(param, value)
end

#bind_params(*bindvars) ⇒ Object



70
71
72
# File 'lib/dbd/sqlite3/statement.rb', line 70

def bind_params(*bindvars)
    @stmt.bind_params(bindvars)
end

#cancelObject



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

def cancel()
    @result = nil
    @index = 0
end

#column_infoObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/dbd/sqlite3/statement.rb', line 41

def column_info()
    @stmt.columns.zip(@stmt.types).map{|name, type_name|
        m = DBI::DBD::SQLite3.parse_type(type_name)
        h = { 
          'name' => name,
          'type_name' => m[1],
          'sql_type' => 
                begin
                    DBI.const_get('SQL_'+m[1].upcase)
                rescue NameError
                    DBI::SQL_OTHER
                end,
        }
        h['precision'] = m[3].to_i if m[3]
        h['scale']     = m[5].to_i if m[5]

        case h['type_name']
        when 'double'
            h['dbi_type'] = DBI::Type::Float
        end

        h
    }
end

#executeObject



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

def execute()
    @result = @stmt.execute
    @rows = DBI::SQL.query?(@sql) ? 0 : @db.changes
end

#fetchObject



35
36
37
38
39
# File 'lib/dbd/sqlite3/statement.rb', line 35

def fetch()
    ret = @result.next
    return ret unless ret
    [ret].flatten
end

#finishObject



30
31
32
33
# File 'lib/dbd/sqlite3/statement.rb', line 30

def finish()
    @stmt.close rescue nil
    @result = nil
end

#rowsObject



66
67
68
# File 'lib/dbd/sqlite3/statement.rb', line 66

def rows()
    @rows
end