Class: SqLite3Wrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/aspera/sync/database.rb

Overview

A wrapper class that provides common API for sqlite in both Ruby and JRuby

Constant Summary collapse

READ_ONLY =

async may have the database open while it synchronizes: the connection is set read-only (PRAGMA query_only, a core SQLite feature, so it works the same with the JDBC driver), so that a query (for example option sql) cannot modify the live database (it blocks UPDATE, DELETE, INSERT and VACUUM INTO). The database is still opened read-write so that a database in WAL mode can be read (a read-only open needs to write the -shm index).

'PRAGMA query_only = ON'

Instance Method Summary collapse

Constructor Details

#initialize(db_path) ⇒ SqLite3Wrapper

Returns a new instance of SqLite3Wrapper.



16
17
18
# File 'lib/aspera/sync/database.rb', line 16

def initialize(db_path)
  @db_path = db_path
end

Instance Method Details

#execute(sql) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/aspera/sync/database.rb', line 26

def execute(sql)
  db = Sequel.connect("jdbc:sqlite:#{@db_path}")
  begin
    db.run(READ_ONLY)
    normalize_rows(db.fetch(sql).all)
  ensure
    db.disconnect
  end
end

#full_table(table_name, sql_suffix = nil) ⇒ Object



52
53
54
# File 'lib/aspera/sync/database.rb', line 52

def full_table(table_name, sql_suffix = nil)
  execute(["SELECT * FROM #{table_name}", sql_suffix].compact.join(' '))
end

#single_table(table_name, sql_suffix = nil) ⇒ Object

The table contains a single row



48
49
50
# File 'lib/aspera/sync/database.rb', line 48

def single_table(table_name, sql_suffix = nil)
  execute(["SELECT * FROM #{table_name}", sql_suffix, 'LIMIT 1'].compact.join(' ')).first
end