Class: RubynCode::DB::Connection
- Inherits:
-
Object
- Object
- RubynCode::DB::Connection
- Includes:
- MonitorMixin
- Defined in:
- lib/rubyn_code/db/connection.rb
Overview
Manages a singleton SQLite3 database connection with WAL mode, foreign keys, and thread-safe access.
Class Method Summary collapse
-
.execute(sql, params = []) ⇒ void
Executes a write statement (INSERT, UPDATE, DELETE, DDL).
-
.instance(path = nil) ⇒ Connection
Returns the singleton Connection instance, optionally initializing it with the given database path on first call.
-
.query(sql, params = []) ⇒ Array<Hash>
Executes a read query and returns rows as hashes.
-
.reset! ⇒ void
Tears down the singleton instance.
-
.transaction { ... } ⇒ Object
Wraps a block in a database transaction with automatic commit/rollback semantics.
Instance Method Summary collapse
-
#close ⇒ void
Closes the underlying database connection.
-
#execute(sql, params = []) ⇒ void
Executes a write statement with bind parameters.
-
#initialize(path) ⇒ Connection
constructor
A new instance of Connection.
-
#open? ⇒ Boolean
Returns whether the connection is open.
-
#query(sql, params = []) ⇒ Array<Hash>
Executes a read query and returns all matching rows.
-
#transaction { ... } ⇒ Object
Wraps a block in a transaction.
Constructor Details
#initialize(path) ⇒ Connection
Returns a new instance of Connection.
74 75 76 77 78 79 80 |
# File 'lib/rubyn_code/db/connection.rb', line 74 def initialize(path) super() # MonitorMixin @path = path @db = SQLite3::Database.new(path) @transaction_depth = 0 configure_connection end |
Class Method Details
.execute(sql, params = []) ⇒ void
This method returns an undefined value.
Executes a write statement (INSERT, UPDATE, DELETE, DDL).
37 38 39 |
# File 'lib/rubyn_code/db/connection.rb', line 37 def execute(sql, params = []) instance.execute(sql, params) end |
.instance(path = nil) ⇒ Connection
Returns the singleton Connection instance, optionally initializing it with the given database path on first call.
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/rubyn_code/db/connection.rb', line 20 def instance(path = nil) @mutex ||= Mutex.new @mutex.synchronize do if @instance.nil? path ||= Config::Defaults::DB_FILE FileUtils.mkdir_p(File.dirname(path)) @instance = new(path) end @instance end end |
.query(sql, params = []) ⇒ Array<Hash>
Executes a read query and returns rows as hashes.
46 47 48 |
# File 'lib/rubyn_code/db/connection.rb', line 46 def query(sql, params = []) instance.query(sql, params) end |
.reset! ⇒ void
This method returns an undefined value.
Tears down the singleton instance. Intended for test cleanup.
62 63 64 65 66 67 68 69 70 |
# File 'lib/rubyn_code/db/connection.rb', line 62 def reset! @mutex ||= Mutex.new @mutex.synchronize do if @instance @instance.close @instance = nil end end end |
.transaction { ... } ⇒ Object
Wraps a block in a database transaction with automatic commit/rollback semantics. Supports nested calls via SAVEPOINTs.
55 56 57 |
# File 'lib/rubyn_code/db/connection.rb', line 55 def transaction(&) instance.transaction(&) end |
Instance Method Details
#close ⇒ void
This method returns an undefined value.
Closes the underlying database connection.
120 121 122 123 124 |
# File 'lib/rubyn_code/db/connection.rb', line 120 def close synchronize do @db.close unless @db.closed? end end |
#execute(sql, params = []) ⇒ void
This method returns an undefined value.
Executes a write statement with bind parameters.
87 88 89 90 91 |
# File 'lib/rubyn_code/db/connection.rb', line 87 def execute(sql, params = []) synchronize do @db.execute(sql, params) end end |
#open? ⇒ Boolean
Returns whether the connection is open.
129 130 131 |
# File 'lib/rubyn_code/db/connection.rb', line 129 def open? !@db.closed? end |
#query(sql, params = []) ⇒ Array<Hash>
Executes a read query and returns all matching rows.
98 99 100 101 102 |
# File 'lib/rubyn_code/db/connection.rb', line 98 def query(sql, params = []) synchronize do @db.execute(sql, params) end end |
#transaction { ... } ⇒ Object
Wraps a block in a transaction. Nested calls use SAVEPOINTs to avoid SQLite “cannot start a transaction within a transaction” errors.
109 110 111 112 113 114 115 |
# File 'lib/rubyn_code/db/connection.rb', line 109 def transaction(&block) synchronize do @transaction_depth.zero? ? begin_top_level_transaction : begin_savepoint @transaction_depth += 1 execute_transaction_body(&block) end end |