Class: Lutaml::Qea::Database

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

Overview

Database container for all loaded EA models

This class provides a unified container for all EA table collections loaded from a QEA database. It stores collections by name and provides accessor methods, statistics, and lookup functionality.

Examples:

Load and access database

database = Lutaml::Qea::Services::DatabaseLoader.new("file.qea").load
puts database.stats
# => {"objects" => 693, "attributes" => 1910, ...}

classes = database.objects.find_by_type("Class")
obj = database.find_object(123)

Constant Summary collapse

COLLECTION_ACCESSORS =
%i[
  attributes operations operation_params connectors packages
  diagrams diagram_objects diagram_links object_constraints
  tagged_values object_properties attribute_tags xrefs
  stereotypes datatypes constraint_types connector_types
  diagram_types object_types status_types complexity_types
  documents scripts
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(qea_path, connection = nil) ⇒ Database

Returns a new instance of Database.



28
29
30
31
32
33
# File 'lib/lutaml/qea/database.rb', line 28

def initialize(qea_path, connection = nil)
  @qea_path = qea_path
  @connection = connection
  @collections = {}
  @mutex = Mutex.new
end

Instance Attribute Details

#collectionsHash<Symbol, Array> (readonly)

Returns Collections of records by name.

Returns:

  • (Hash<Symbol, Array>)

    Collections of records by name



20
21
22
# File 'lib/lutaml/qea/database.rb', line 20

def collections
  @collections
end

#connectionSQLite3::Database?

Returns Database connection.

Returns:

  • (SQLite3::Database, nil)

    Database connection



26
27
28
# File 'lib/lutaml/qea/database.rb', line 26

def connection
  @connection
end

#qea_pathString (readonly)

Returns Path to the QEA file.

Returns:

  • (String)

    Path to the QEA file



23
24
25
# File 'lib/lutaml/qea/database.rb', line 23

def qea_path
  @qea_path
end

Instance Method Details

#add_collection(name, records) ⇒ void

This method returns an undefined value.

Add a collection to the database

Parameters:

  • name (Symbol, String)

    Collection name (e.g., :objects)

  • records (Array)

    Array of model instances



48
49
50
51
52
# File 'lib/lutaml/qea/database.rb', line 48

def add_collection(name, records)
  @mutex.synchronize do
    @collections[name.to_sym] = records.freeze
  end
end

#attributes_for_object(id) ⇒ Object



125
126
127
128
# File 'lib/lutaml/qea/database.rb', line 125

def attributes_for_object(id)
  ensure_lookup_indexes
  @attributes_by_object_id[id] || []
end

#child_packages_for(id) ⇒ Object



140
141
142
143
# File 'lib/lutaml/qea/database.rb', line 140

def child_packages_for(id)
  ensure_lookup_indexes
  @packages_by_parent[id] || []
end

#collection_namesArray<Symbol>

Get collection names

Returns:

  • (Array<Symbol>)

    Array of collection names



193
194
195
# File 'lib/lutaml/qea/database.rb', line 193

def collection_names
  @collections.keys
end

#connectors_for_object(object_id) ⇒ Object

Get connectors involving a specific object (start or end)



177
178
179
180
181
# File 'lib/lutaml/qea/database.rb', line 177

def connectors_for_object(object_id)
  ensure_lookup_indexes
  (@connectors_by_start[object_id] || []) +
    (@connectors_by_end[object_id] || [])
end


160
161
162
163
# File 'lib/lutaml/qea/database.rb', line 160

def diagram_links_for(id)
  ensure_lookup_indexes
  @diagram_links_by_id[id] || []
end

#diagram_objects_for(id) ⇒ Object



155
156
157
158
# File 'lib/lutaml/qea/database.rb', line 155

def diagram_objects_for(id)
  ensure_lookup_indexes
  @diagram_objects_by_id[id] || []
end

#diagrams_in_package(id) ⇒ Object



150
151
152
153
# File 'lib/lutaml/qea/database.rb', line 150

def diagrams_in_package(id)
  ensure_lookup_indexes
  @diagrams_by_package_id[id] || []
end

#empty?Boolean

Check if database is empty

Returns:

  • (Boolean)

    true if no collections loaded



186
187
188
# File 'lib/lutaml/qea/database.rb', line 186

def empty?
  @collections.empty? || total_records.zero?
end

#find_attribute(id) ⇒ Object



110
111
112
113
# File 'lib/lutaml/qea/database.rb', line 110

def find_attribute(id)
  ensure_lookup_indexes
  @attributes_by_id[id]
end

#find_connector(id) ⇒ Object



115
116
117
118
# File 'lib/lutaml/qea/database.rb', line 115

def find_connector(id)
  ensure_lookup_indexes
  @connectors_by_id[id]
end

#find_diagram(id) ⇒ Object



120
121
122
123
# File 'lib/lutaml/qea/database.rb', line 120

def find_diagram(id)
  ensure_lookup_indexes
  @diagrams_by_id[id]
end

#find_object(id) ⇒ Object

Find an object by ID



166
167
168
# File 'lib/lutaml/qea/database.rb', line 166

def find_object(id)
  objects.find_by_key(:ea_object_id, id)
end

#find_object_by_guid(ea_guid) ⇒ Object

Find object by ea_guid



171
172
173
174
# File 'lib/lutaml/qea/database.rb', line 171

def find_object_by_guid(ea_guid)
  ensure_lookup_indexes
  @objects_by_guid[ea_guid]
end

#find_package(id) ⇒ Object



105
106
107
108
# File 'lib/lutaml/qea/database.rb', line 105

def find_package(id)
  ensure_lookup_indexes
  @packages_by_id[id]
end

#freezeself

Freeze all collections to make database immutable

Returns:

  • (self)


200
201
202
203
204
205
# File 'lib/lutaml/qea/database.rb', line 200

def freeze
  objects
  ensure_lookup_indexes
  @collections.freeze
  super
end

#objectsRepositories::ObjectRepository

Get objects collection (special: wrapped in ObjectRepository)

Returns:



72
73
74
75
76
77
78
# File 'lib/lutaml/qea/database.rb', line 72

def objects
  return @objects if defined?(@objects)

  @objects = Repositories::ObjectRepository.new(
    @collections[:objects] || [],
  )
end

#objects_in_package(id) ⇒ Object



145
146
147
148
# File 'lib/lutaml/qea/database.rb', line 145

def objects_in_package(id)
  ensure_lookup_indexes
  @objects_by_package_id[id] || []
end

#operation_params_for(id) ⇒ Object



135
136
137
138
# File 'lib/lutaml/qea/database.rb', line 135

def operation_params_for(id)
  ensure_lookup_indexes
  @operation_params_by_id[id] || []
end

#operations_for_object(id) ⇒ Object



130
131
132
133
# File 'lib/lutaml/qea/database.rb', line 130

def operations_for_object(id)
  ensure_lookup_indexes
  @operations_by_object_id[id] || []
end

#statsHash<String, Integer>

Get statistics for all collections

Examples:

database.stats
# => {
#   "objects" => 693,
#   "attributes" => 1910,
#   "connectors" => 908,
#   ...
# }

Returns:

  • (Hash<String, Integer>)

    Record counts by collection name



92
93
94
95
96
# File 'lib/lutaml/qea/database.rb', line 92

def stats
  @collections.each_with_object({}) do |(name, records), hash|
    hash[name.to_s] = records.size
  end
end

#total_recordsInteger

Get total number of records across all collections

Returns:

  • (Integer)

    Total record count



101
102
103
# File 'lib/lutaml/qea/database.rb', line 101

def total_records
  @collections.values.sum(&:size)
end