File: statement.rb

package info (click to toggle)
ruby-dbd-sqlite3 1.2.5%2Bgem2deb-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 268 kB
  • sloc: ruby: 1,179; sql: 33; makefile: 2
file content (78 lines) | stat: -rw-r--r-- 2,014 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#
# See DBI::BaseStatement.
#
class DBI::DBD::SQLite3::Statement < DBI::BaseStatement
    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

    #
    # See DBI::BaseStatement#bind_param. This method will also raise
    # DBI::InterfaceError if +param+ is not a Fixnum, to prevent incorrect
    # binding.
    #
    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

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

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

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

    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

    def rows()
        @rows
    end

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

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