Class: Lutaml::Qea::Infrastructure::DatabaseConnection
- Inherits:
-
Object
- Object
- Lutaml::Qea::Infrastructure::DatabaseConnection
- Defined in:
- lib/lutaml/qea/infrastructure/database_connection.rb
Overview
DatabaseConnection manages the SQLite database connection lifecycle for QEA files (Enterprise Architect SQLite databases).
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#file_path ⇒ Object
readonly
Returns the value of attribute file_path.
Instance Method Summary collapse
-
#close ⇒ void
Close the database connection.
-
#connect ⇒ SQLite3::Database
Connect to the database.
-
#connected? ⇒ Boolean
Check if the connection is open.
-
#initialize(file_path) ⇒ DatabaseConnection
constructor
Initialize a new database connection.
-
#with_connection {|SQLite3::Database| ... } ⇒ Object
Execute a block with an active connection.
Constructor Details
#initialize(file_path) ⇒ DatabaseConnection
Initialize a new database connection
29 30 31 32 33 34 35 36 37 |
# File 'lib/lutaml/qea/infrastructure/database_connection.rb', line 29 def initialize(file_path) if file_path.nil? || file_path.empty? raise ArgumentError, "file_path cannot be nil or empty" end @file_path = file_path @connection = nil end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
23 24 25 |
# File 'lib/lutaml/qea/infrastructure/database_connection.rb', line 23 def connection @connection end |
#file_path ⇒ Object (readonly)
Returns the value of attribute file_path.
23 24 25 |
# File 'lib/lutaml/qea/infrastructure/database_connection.rb', line 23 def file_path @file_path end |
Instance Method Details
#close ⇒ void
This method returns an undefined value.
Close the database connection
57 58 59 60 61 62 |
# File 'lib/lutaml/qea/infrastructure/database_connection.rb', line 57 def close return unless @connection @connection.close @connection = nil end |
#connect ⇒ SQLite3::Database
Connect to the database
44 45 46 47 48 49 50 51 52 |
# File 'lib/lutaml/qea/infrastructure/database_connection.rb', line 44 def connect unless File.exist?(@file_path) raise Errno::ENOENT, "QEA file not found: #{@file_path}" end @connection = SQLite3::Database.new(@file_path, readonly: true) @connection.results_as_hash = true @connection end |
#connected? ⇒ Boolean
Check if the connection is open
67 68 69 |
# File 'lib/lutaml/qea/infrastructure/database_connection.rb', line 67 def connected? !@connection.nil? && !@connection.closed? end |
#with_connection {|SQLite3::Database| ... } ⇒ Object
Execute a block with an active connection
This method ensures the connection is properly opened and closed. If a connection already exists, it reuses it. Otherwise, it creates a new connection and closes it after the block executes.
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/lutaml/qea/infrastructure/database_connection.rb', line 87 def with_connection should_close = !connected? begin connect unless connected? yield @connection ensure close if should_close end end |