Class: Lutaml::Qea::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/qea/database.rb,
lib/lutaml/qea/lookup_indexes.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.



31
32
33
34
35
36
# File 'lib/lutaml/qea/database.rb', line 31

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



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

def collections
  @collections
end

#connectionSQLite3::Database?

Returns Database connection.

Returns:

  • (SQLite3::Database, nil)

    Database connection



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

def connection
  @connection
end

#qea_pathString (readonly)

Returns Path to the QEA file.

Returns:

  • (String)

    Path to the QEA file



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

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



51
52
53
54
55
# File 'lib/lutaml/qea/database.rb', line 51

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

#attributes_for_object(id) ⇒ Object



128
129
130
131
# File 'lib/lutaml/qea/database.rb', line 128

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

#child_packages_for(id) ⇒ Object



143
144
145
146
# File 'lib/lutaml/qea/database.rb', line 143

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



196
197
198
# File 'lib/lutaml/qea/database.rb', line 196

def collection_names
  @collections.keys
end

#connectors_for_object(object_id) ⇒ Object

Get connectors involving a specific object (start or end)



180
181
182
183
184
# File 'lib/lutaml/qea/database.rb', line 180

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


163
164
165
166
# File 'lib/lutaml/qea/database.rb', line 163

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

#diagram_objects_for(id) ⇒ Object



158
159
160
161
# File 'lib/lutaml/qea/database.rb', line 158

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

#diagrams_in_package(id) ⇒ Object



153
154
155
156
# File 'lib/lutaml/qea/database.rb', line 153

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



189
190
191
# File 'lib/lutaml/qea/database.rb', line 189

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

#find_attribute(id) ⇒ Object



113
114
115
116
# File 'lib/lutaml/qea/database.rb', line 113

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

#find_connector(id) ⇒ Object



118
119
120
121
# File 'lib/lutaml/qea/database.rb', line 118

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

#find_diagram(id) ⇒ Object



123
124
125
126
# File 'lib/lutaml/qea/database.rb', line 123

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

#find_object(id) ⇒ Object

Find an object by ID



169
170
171
# File 'lib/lutaml/qea/database.rb', line 169

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



174
175
176
177
# File 'lib/lutaml/qea/database.rb', line 174

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

#find_package(id) ⇒ Object



108
109
110
111
# File 'lib/lutaml/qea/database.rb', line 108

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

#freezeself

Freeze all collections to make database immutable

Returns:

  • (self)


203
204
205
206
207
208
# File 'lib/lutaml/qea/database.rb', line 203

def freeze
  objects
  ensure_lookup_indexes
  @collections.freeze
  super
end

#objectsRepositories::ObjectRepository

Get objects collection (special: wrapped in ObjectRepository)

Returns:



75
76
77
78
79
80
81
# File 'lib/lutaml/qea/database.rb', line 75

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

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

#objects_in_package(id) ⇒ Object



148
149
150
151
# File 'lib/lutaml/qea/database.rb', line 148

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

#operation_params_for(id) ⇒ Object



138
139
140
141
# File 'lib/lutaml/qea/database.rb', line 138

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

#operations_for_object(id) ⇒ Object



133
134
135
136
# File 'lib/lutaml/qea/database.rb', line 133

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



95
96
97
98
99
# File 'lib/lutaml/qea/database.rb', line 95

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



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

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