Class: Ea::Qea::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/ea/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 = Ea::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.



25
26
27
28
29
30
# File 'lib/ea/qea/database.rb', line 25

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/ea/qea/database.rb', line 20

def collections
  @collections
end

#qea_pathString (readonly)

Returns Path to the QEA file.

Returns:

  • (String)

    Path to the QEA file



23
24
25
# File 'lib/ea/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



53
54
55
56
57
# File 'lib/ea/qea/database.rb', line 53

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

#attributes_for_object(id) ⇒ Object



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

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

#child_packages_for(id) ⇒ Object



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

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

#close_connectionvoid

This method returns an undefined value.

Close the database connection if open



42
43
44
45
46
# File 'lib/ea/qea/database.rb', line 42

def close_connection
  return unless @connection && !@connection.closed?

  @connection.close
end

#collection_namesArray<Symbol>

Get collection names

Returns:

  • (Array<Symbol>)

    Array of collection names



217
218
219
# File 'lib/ea/qea/database.rb', line 217

def collection_names
  @collections.keys
end

#connection=(connection) ⇒ void

This method returns an undefined value.

Set database connection

Parameters:

  • connection (SQLite3::Database)

    Database connection



36
37
38
# File 'lib/ea/qea/database.rb', line 36

def connection=(connection)
  @connection = connection
end

#connectors_for_object(object_id) ⇒ Object

Get connectors involving a specific object (start or end)



182
183
184
185
# File 'lib/ea/qea/database.rb', line 182

def connectors_for_object(object_id)
  ensure_lookup_indexes
  @connectors_by_object[object_id] || []
end

#constraints_for_object(object_id) ⇒ Object



187
188
189
190
# File 'lib/ea/qea/database.rb', line 187

def constraints_for_object(object_id)
  ensure_lookup_indexes
  @constraints_by_object_id[object_id] || []
end


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

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

#diagram_objects_for(id) ⇒ Object



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

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

#diagrams_in_package(id) ⇒ Object



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

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



210
211
212
# File 'lib/ea/qea/database.rb', line 210

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

#find_attribute(id) ⇒ Object



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

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

#find_connector(id) ⇒ Object



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

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

#find_diagram(id) ⇒ Object



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

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

#find_object(id) ⇒ Object

Find an object by ID



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

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



176
177
178
179
# File 'lib/ea/qea/database.rb', line 176

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

#find_package(id) ⇒ Object



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

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

#freezeself

Freeze all collections to make database immutable

Returns:

  • (self)


224
225
226
227
228
229
# File 'lib/ea/qea/database.rb', line 224

def freeze
  objects
  ensure_lookup_indexes
  @collections.freeze
  super
end

#objectsRepositories::ObjectRepository

Get objects collection (special: wrapped in ObjectRepository)

Returns:



77
78
79
80
81
82
83
# File 'lib/ea/qea/database.rb', line 77

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

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

#objects_in_package(id) ⇒ Object



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

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

#operation_params_for(id) ⇒ Object



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

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

#operations_for_object(id) ⇒ Object



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

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

#properties_for_object(object_id) ⇒ Object



197
198
199
200
# File 'lib/ea/qea/database.rb', line 197

def properties_for_object(object_id)
  ensure_lookup_indexes
  @properties_by_object_id[object_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



97
98
99
100
101
# File 'lib/ea/qea/database.rb', line 97

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

#tagged_values_for_element(ea_guid) ⇒ Object



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

def tagged_values_for_element(ea_guid)
  ensure_lookup_indexes
  @tagged_values_by_element_id[ea_guid] || []
end

#total_recordsInteger

Get total number of records across all collections

Returns:

  • (Integer)

    Total record count



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

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

#xrefs_for_client(ea_guid) ⇒ Object



202
203
204
205
# File 'lib/ea/qea/database.rb', line 202

def xrefs_for_client(ea_guid)
  ensure_lookup_indexes
  @xrefs_by_client[ea_guid] || []
end