Class: Rubino::Database::Connection
- Inherits:
-
Object
- Object
- Rubino::Database::Connection
- 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
-
#db_path ⇒ Object
readonly
Returns the value of attribute db_path.
Instance Method Summary collapse
-
#close ⇒ Object
Closes the database connection.
-
#db ⇒ Object
Returns the Sequel database connection (lazy-initialized).
-
#healthy? ⇒ Boolean
Tests if the database is accessible.
-
#initialize(db_path) ⇒ Connection
constructor
A new instance of Connection.
-
#memory? ⇒ Boolean
True when @db_path refers to an in-memory SQLite instance.
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.(db_path) end |
Instance Attribute Details
#db_path ⇒ Object (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
#close ⇒ Object
Closes the database connection
38 39 40 41 |
# File 'lib/rubino/database/connection.rb', line 38 def close @db&.disconnect @db = nil end |
#db ⇒ Object
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
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.
44 45 46 |
# File 'lib/rubino/database/connection.rb', line 44 def memory? memory_path?(@db_path) end |