Class: Rubino::Database::Connection

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

Overview

Manages the SQLite database connection via Sequel. Handles connection creation, WAL mode setup, and provides access to the underlying Sequel::Database instance.

Constant Summary collapse

MEMORY_PATHS =

SQLite path values that resolve to an ephemeral, in-memory database rather than an on-disk file. These must skip File.expand_path (which would turn “:memory:” into a literal “./:memory:” file) and FileUtils.mkdir_p on the parent directory.

[":memory:", "file::memory:"].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db_path) ⇒ Connection

Returns a new instance of Connection.



20
21
22
# File 'lib/rubino/database/connection.rb', line 20

def initialize(db_path)
  @db_path = memory_path?(db_path) ? db_path : File.expand_path(db_path)
end

Instance Attribute Details

#db_pathObject (readonly)

Returns the value of attribute db_path.



18
19
20
# File 'lib/rubino/database/connection.rb', line 18

def db_path
  @db_path
end

Instance Method Details

#closeObject

Closes the database connection



38
39
40
41
# File 'lib/rubino/database/connection.rb', line 38

def close
  @db&.disconnect
  @db = nil
end

#dbObject

Returns the Sequel database connection (lazy-initialized)



25
26
27
# File 'lib/rubino/database/connection.rb', line 25

def db
  @db ||= connect!
end

#healthy?Boolean

Tests if the database is accessible

Returns:

  • (Boolean)


30
31
32
33
34
35
# File 'lib/rubino/database/connection.rb', line 30

def healthy?
  db.execute("SELECT 1")
  true
rescue StandardError
  false
end

#memory?Boolean

True when @db_path refers to an in-memory SQLite instance.

Returns:

  • (Boolean)


44
45
46
# File 'lib/rubino/database/connection.rb', line 44

def memory?
  memory_path?(@db_path)
end