Class: DocVault::Database

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

Instance Method Summary collapse

Constructor Details

#initialize(db_name) ⇒ Database

Returns a new instance of Database.



7
8
9
10
11
# File 'lib/doc_vault/database.rb', line 7

def initialize(db_name)
  @db_name = db_name
  @db_path = DocVault.configuration.full_database_path(db_name)
  create_table_if_not_exists
end

Instance Method Details

#retrieve(id) ⇒ Object



20
21
22
23
24
25
# File 'lib/doc_vault/database.rb', line 20

def retrieve(id)
  result = db.execute("SELECT content FROM documents WHERE id = ?", [id])
  result.empty? ? nil : result[0][0]
rescue SQLite3::Exception => e
  raise DatabaseError, "Failed to retrieve document: #{e.message}"
end

#store(id, encrypted_document) ⇒ Object



13
14
15
16
17
18
# File 'lib/doc_vault/database.rb', line 13

def store(id, encrypted_document)
  result = db.execute("INSERT OR REPLACE INTO documents (id, content) VALUES (?, ?)", [id, encrypted_document])
  id if result
rescue SQLite3::Exception => e
  raise DatabaseError, "Failed to store document: #{e.message}"
end