Class: Lutaml::Qea::Infrastructure::DatabaseConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/qea/infrastructure/database_connection.rb

Overview

DatabaseConnection manages the SQLite database connection lifecycle for QEA files (Enterprise Architect SQLite databases).

Examples:

Connect to a QEA file

conn = DatabaseConnection.new("model.qea")
conn.connect
# ... use connection
conn.close

Using with_connection block

conn = DatabaseConnection.new("model.qea")
conn.with_connection do |db|
  # ... use db
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ DatabaseConnection

Initialize a new database connection

Parameters:

  • file_path (String)

    Path to the .qea file

Raises:

  • (ArgumentError)

    if file_path is nil or empty



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

#connectionObject (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_pathObject (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

#closevoid

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

#connectSQLite3::Database

Connect to the database

Returns:

  • (SQLite3::Database)

    The database connection

Raises:

  • (Errno::ENOENT)

    if the file does not exist

  • (SQLite3::Exception)

    if connection fails



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

Returns:

  • (Boolean)

    true if 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.

Examples:

conn = DatabaseConnection.new("model.qea")
result = conn.with_connection do |db|
  db.execute("SELECT COUNT(*) FROM t_object")
end

Yields:

  • (SQLite3::Database)

    The database connection

Returns:

  • (Object)

    The result of the block

Raises:

  • (Errno::ENOENT)

    if the file does not exist

  • (SQLite3::Exception)

    if connection fails



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