class Lutaml::Qea::Infrastructure::DatabaseConnection
end
# … use db
conn.with_connection do |db|
conn = DatabaseConnection.new(“model.qea”)
@example Using with_connection block
conn.close
# … use connection
conn.connect
conn = DatabaseConnection.new(“model.qea”)
@example Connect to a QEA file
for QEA files (Enterprise Architect SQLite databases).
DatabaseConnection manages the SQLite database connection lifecycle
def close
-
(void)-
def close return unless @connection @connection.close @connection = nil end
def connect
-
(SQLite3::Exception)- if connection fails -
(Errno::ENOENT)- if the file does not exist
Returns:
-
(SQLite3::Database)- The database connection
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
def connected?
-
(Boolean)- true if connection is open
def connected? !@connection.nil? && !@connection.closed? end
def initialize(file_path)
-
(ArgumentError)- if file_path is nil or empty
Parameters:
-
file_path(String) -- Path to the .qea file
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
def with_connection
-
(SQLite3::Exception)- if connection fails -
(Errno::ENOENT)- if the file does not exist
Returns:
-
(Object)- The result of the block
Other tags:
- Yield: - The database connection
def with_connection should_close = !connected? begin connect unless connected? yield @connection ensure close if should_close end end