Class: RubynCode::DB::Connection

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Connection

Returns a new instance of Connection.

Parameters:

  • path (String)

    path to the SQLite3 database file



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).

Parameters:

  • sql (String)

    the SQL statement

  • params (Array) (defaults to: [])

    bind parameters



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.

Parameters:

  • path (String) (defaults to: nil)

    path to the SQLite3 database file

Returns:



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.

Parameters:

  • sql (String)

    the SQL query

  • params (Array) (defaults to: [])

    bind parameters

Returns:

  • (Array<Hash>)


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.

Yields:

  • the block to execute within the transaction

Returns:

  • (Object)

    the return value of the block



55
56
57
# File 'lib/rubyn_code/db/connection.rb', line 55

def transaction(&)
  instance.transaction(&)
end

Instance Method Details

#closevoid

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.

Parameters:

  • sql (String)
  • params (Array) (defaults to: [])


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.

Returns:

  • (Boolean)


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.

Parameters:

  • sql (String)
  • params (Array) (defaults to: [])

Returns:

  • (Array<Hash>)


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.

Yields:

  • the block to execute

Returns:

  • (Object)

    the block’s return value



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