Class: Lutaml::Qea::Services::DatabaseLoader

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

Overview

DatabaseLoader orchestrates loading all EA tables into a Database

This service loads all enabled tables from the QEA database, converts database rows to model instances, and populates a Database container with the results.

Examples:

Load a database

loader = DatabaseLoader.new("model.qea")
loader.on_progress do |table, current, total|
  puts "Loading #{table}: #{current}/#{total}"
end
database = loader.load

Load a single table

loader = DatabaseLoader.new("model.qea")
objects = loader.load_table("t_object")

Constant Summary collapse

MODEL_CLASSES =

Table name to model class mapping

{
  "t_object" => Models::EaObject,
  "t_attribute" => Models::EaAttribute,
  "t_operation" => Models::EaOperation,
  "t_operationparams" => Models::EaOperationParam,
  "t_connector" => Models::EaConnector,
  "t_package" => Models::EaPackage,
  "t_diagram" => Models::EaDiagram,
  "t_diagramobjects" => Models::EaDiagramObject,
  "t_diagramlinks" => Models::EaDiagramLink,
  "t_objectconstraint" => Models::EaObjectConstraint,
  "t_taggedvalue" => Models::EaTaggedValue,
  "t_objectproperties" => Models::EaObjectProperty,
  "t_attributetag" => Models::EaAttributeTag,
  "t_xref" => Models::EaXref,
  "t_document" => Models::EaDocument,
  "t_script" => Models::EaScript,
  "t_stereotypes" => Models::EaStereotype,
  "t_datatypes" => Models::EaDatatype,
  "t_constrainttypes" => Models::EaConstraintType,
  "t_connectortypes" => Models::EaConnectorType,
  "t_diagramtypes" => Models::EaDiagramType,
  "t_objecttypes" => Models::EaObjectType,
  "t_statustypes" => Models::EaStatusType,
  "t_complexitytypes" => Models::EaComplexityType,
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(qea_path, config = nil) ⇒ DatabaseLoader

Initialize loader

Parameters:

  • qea_path (String)

    Path to QEA file

  • config (Configuration, nil) (defaults to: nil)

    Optional configuration



90
91
92
93
94
95
# File 'lib/lutaml/qea/services/database_loader.rb', line 90

def initialize(qea_path, config = nil)
  @qea_path = qea_path
  @config = config || Configuration.load
  @progress_callback = nil
  @connection = Infrastructure::DatabaseConnection.new(qea_path)
end

Instance Attribute Details

#configConfiguration (readonly)

Returns Configuration instance.

Returns:



56
57
58
# File 'lib/lutaml/qea/services/database_loader.rb', line 56

def config
  @config
end

#qea_pathString (readonly)

Returns Path to QEA file.

Returns:

  • (String)

    Path to QEA file



53
54
55
# File 'lib/lutaml/qea/services/database_loader.rb', line 53

def qea_path
  @qea_path
end

Instance Method Details

#loadDatabase

Load entire database

Returns:

  • (Database)

    Populated database instance

Raises:

  • (Errno::ENOENT)

    if QEA file not found

  • (SQLite3::Exception)

    if database access fails



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/lutaml/qea/services/database_loader.rb', line 114

def load
  # Connect but don't use with_connection - we need to keep it open
  @connection.connect unless @connection.connected?
  db = @connection.connection

  database = Database.new(@qea_path, db)

  @config.enabled_tables.each do |table_def|
    table_name = table_def.table_name
    collection_name = table_def.collection_name

    records = load_table_records(db, table_name)
    database.add_collection(collection_name, records)
  end

  database.freeze
end

#load_table(table_name) ⇒ Array

Load a single table

Parameters:

  • table_name (String)

    Table name to load

Returns:

  • (Array)

    Array of model instances

Raises:

  • (ArgumentError)

    if table not configured or not enabled



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/lutaml/qea/services/database_loader.rb', line 137

def load_table(table_name) # rubocop:disable Metrics/MethodLength
  table_def = @config.table_config_for(table_name)
  unless table_def
    raise ArgumentError,
          "Table #{table_name} not configured"
  end
  unless table_def.enabled
    raise ArgumentError,
          "Table #{table_name} not enabled"
  end

  @connection.with_connection do |db|
    load_table_records(db, table_name)
  end
end

#on_progress {|table_name, current, total| ... } ⇒ self

Set progress callback

Yields:

  • (table_name, current, total)

    Progress information

Yield Parameters:

  • table_name (String)

    Current table being loaded

  • current (Integer)

    Records loaded so far

  • total (Integer)

    Total records in table

Returns:

  • (self)


104
105
106
107
# File 'lib/lutaml/qea/services/database_loader.rb', line 104

def on_progress(&block)
  @progress_callback = block
  self
end

#quick_statsHash<String, Integer>

Get quick statistics without full loading

Returns:

  • (Hash<String, Integer>)

    Table names to record counts



156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/lutaml/qea/services/database_loader.rb', line 156

def quick_stats
  stats = {}

  @connection.with_connection do |db|
    @config.enabled_tables.each do |table_def|
      reader = Infrastructure::TableReader.new(db, table_def.table_name)
      stats[table_def.collection_name] = reader.count
    end
  end

  stats
end