Class: DBGeni::Connector::Sqlite

Inherits:
Object
  • Object
show all
Defined in:
lib/dbgeni/connectors/sqlite.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



14
15
16
# File 'lib/dbgeni/connectors/sqlite.rb', line 14

def connection
  @connection
end

#databaseObject (readonly)

Returns the value of attribute database.



15
16
17
# File 'lib/dbgeni/connectors/sqlite.rb', line 15

def database
  @database
end

Class Method Details

.connect(user, password, database) ⇒ Object



28
29
30
31
# File 'lib/dbgeni/connectors/sqlite.rb', line 28

def self.connect(user, password, database)
  raise DBGeni::DatabaseNotSpecified unless database
  self.new(database)
end

.db_file_path(base, file) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/dbgeni/connectors/sqlite.rb', line 17

def self.db_file_path(base, file)
  # starts with a ., eg ./filename, or does not
  # contain any forward or backslashes, eg db.sqlite3
  if file =~ /^\./ || file !~ /\/|\\/
    # it is a relative path, so join to base directory
    File.join(base, file)
  else
    file
  end
end

Instance Method Details

#commitObject



106
107
108
# File 'lib/dbgeni/connectors/sqlite.rb', line 106

def commit
  @connection.commit
end

#date_as_string(dtm) ⇒ Object



118
119
120
# File 'lib/dbgeni/connectors/sqlite.rb', line 118

def date_as_string(dtm)
  dtm.strftime '%Y-%m-%d %H:%M:%S'
end

#date_placeholder(bind_var) ⇒ Object



114
115
116
# File 'lib/dbgeni/connectors/sqlite.rb', line 114

def date_placeholder(bind_var)
  "time(:#{bind_var})"
end

#disconnectObject



33
34
35
# File 'lib/dbgeni/connectors/sqlite.rb', line 33

def disconnect
  @connection.close
end

#execute(sql, *binds) ⇒ Object



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
# File 'lib/dbgeni/connectors/sqlite.rb', line 37

def execute(sql, *binds)
#        unless @connection.transaction_active?
#          @connection.transaction
#        end
  if RUBY_PLATFORM == 'java'
    return execute_jdbc(sql, *binds)
  end
  begin
    # Why am I re-establishing the database connection? Well, something has changed
    # somewhere in the sqlite3 application or ruby drivers, and without this line
    # I get lots of errors in the test suite:
    #
    # sqlite 3.7.8 QLite3::SQLException: SQL logic error or missing database
    #
    # This line fixes it be reconnecting. I am not sure if this is a non-reentrant
    # issue, but it used to work just fine :-/
    # SQLITE + DBGeni is not really intended as a production combo, so this is probably
    # ok.
    @connection = SQLite3::Database.new(@database)
    query = @connection.prepare(sql)
    binds.each_with_index do |b, i|
      query.bind_param(i+1, b)
    end
    results = query.execute!
    # This shouldn't even be needed, as there are never transactions started.
    # by default everthing in sqlite is autocommit
    if @connection.transaction_active?
      @connection.commit
    end
    results
  ensure
    begin
      query.close
    rescue Exception => e
    end
  end
end

#pingObject



102
103
104
# File 'lib/dbgeni/connectors/sqlite.rb', line 102

def ping
  ! @connection.closed?
end

#rollbackObject



110
111
112
# File 'lib/dbgeni/connectors/sqlite.rb', line 110

def rollback
  @connection.rollback
end