Class: FluidDb::SQLite

Inherits:
Base
  • Object
show all
Defined in:
lib/FluidDb/SQLite.rb

Instance Attribute Summary

Attributes inherited from Base

#connection, #verbose

Instance Method Summary collapse

Methods inherited from Base

#convertTupleToHash, #escape_string, #format_to_sql, #initialize, #reconnect, #splice_sql, #verboseLog

Constructor Details

This class inherits a constructor from FluidDb::Base

Instance Method Details

#BeginObject

Transaction Semantics



113
114
115
# File 'lib/FluidDb/SQLite.rb', line 113

def Begin
    @connection.transaction
end

#closeObject



16
17
18
# File 'lib/FluidDb/SQLite.rb', line 16

def close
    @connection.close
end

#CommitObject

Transaction Semantics



118
119
120
# File 'lib/FluidDb/SQLite.rb', line 118

def Commit
    @connection.commit
end

#connectObject

Connect to Db.

Parameters:

  • uri (String)

    a location for the resource to which we will attach, eg mysql://user:[email protected]/foo



11
12
13
14
# File 'lib/FluidDb/SQLite.rb', line 11

def connect()
    uri = @uri
    @connection = SQLite3::Database.new uri.path
end

#exec_params(sql, params = [], expected_affected_rows = nil) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/FluidDb/SQLite.rb', line 97

def exec_params( sql, params=[], expected_affected_rows=nil )
    parts = sql.split( "?" )
    sql = ""
    parts.each_with_index do |p,idx|
        sql = sql + p;
        sql = sql + "$#{idx+1}" if idx < parts.length - 1
    end
    r = @connection.exec_params( sql, params );

    if !expected_affected_rows.nil? and
        r.changes != expected_affected_rows then
        raise ExpectedAffectedRowsError.new( "Expected affected rows, #{expected_affected_rows}, Actual affected rows, #{r.cmd_tuples}")
    end
end

#execute(sql, params = [], expected_affected_rows = nil) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/FluidDb/SQLite.rb', line 85

def execute( sql, params=[], expected_affected_rows=nil )
    sql = self.format_to_sql( sql, params )

    self.verboseLog( "#{self.class.name}.execute. #{sql}" )
    r = @connection.execute(sql)

    if !expected_affected_rows.nil? and
        r.changes != expected_affected_rows then
        raise ExpectedAffectedRowsError.new( "Expected affected rows, #{expected_affected_rows}, Actual affected rows, #{r.cmd_tuples}")
    end
end

#insert(sql, params) ⇒ Object



127
128
129
130
131
# File 'lib/FluidDb/SQLite.rb', line 127

def insert( sql, params )
    raise "SQLite uses SEQUENCES, so possibly easier to use 2 executes"
    #            self.execute( sql, params )
    #return @connection.last_id
end

#queryForArray(sql, params = []) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/FluidDb/SQLite.rb', line 20

def queryForArray( sql, params=[] )
    sql = self.format_to_sql( sql, params )
    @connection.results_as_hash = true
    results = @connection.execute(sql)

    #        if ( $result === false ) then
    #    $message = pg_last_error( $this->connection );
    #    throw new Fluid_ConnectionException( $message );
    #end

    case results.length
        when -1
        raise FluidDb::ConnectionError.new
        when 0
        raise FluidDb::NoDataFoundError.new
        when 1
        return results[0]
        else
        raise FluidDb::TooManyRowsError.new
    end
end

#queryForResultset(sql, params = []) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/FluidDb/SQLite.rb', line 66

def queryForResultset( sql, params=[] )
    sql = self.format_to_sql( sql, params )
    @connection.results_as_hash = true
    results = @connection.execute(sql)

    #        if ( $result === false ) then
    #    $message = pg_last_error( $this->connection );
    #    throw new Fluid_ConnectionException( $message );
    #end

    case results.length
        when -1
        raise FluidDb::ConnectionError.new
        else
        return results
    end
end

#queryForValue(sql, params = []) ⇒ Object



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

def queryForValue( sql, params=[] )
    sql = self.format_to_sql( sql, params )
    @connection.results_as_hash = false
    results = @connection.execute(sql)

    #        if ( $result === false ) then
    #    $message = pg_last_error( $this->connection );
    #    throw new Fluid_ConnectionException( $message );
    #end

    case results.length
        when -1
        raise FluidDb::ConnectionError.new
        when 0
        raise FluidDb::NoDataFoundError.new
        when 1
        return results[0][0]
        else
        raise FluidDb::TooManyRowsError.new
    end

end

#RollbackObject

Transaction Semantics



123
124
125
# File 'lib/FluidDb/SQLite.rb', line 123

def Rollback
    @connection.rollback
end