Class: Lutaml::Qea::FileDetector

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

Overview

Utility for detecting and validating QEA files

Constant Summary collapse

SQLITE_MAGIC =

SQLite magic bytes

"SQLite format 3\x00"
REQUIRED_EA_TABLES =

Required EA tables for valid QEA file

%w[
  t_object
  t_attribute
  t_connector
  t_package
].freeze

Class Method Summary collapse

Class Method Details

.file_info(path) ⇒ Hash

Get file information

Examples:

info = FileDetector.file_info("model.qea")
puts "Size: #{info[:size_mb]} MB"
puts "Tables: #{info[:table_count]}"

Parameters:

  • path (String)

    File path

Returns:

  • (Hash)

    File information



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/lutaml/qea/file_detector.rb', line 110

def file_info(path) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
  return { error: "File not found" } unless File.exist?(path)

  info = {
    path: path,
    size_bytes: File.size(path),
    size_mb: (File.size(path) / 1024.0 / 1024.0).round(2),
    modified: File.mtime(path),
    is_qea: qea_file?(path),
  }

  if sqlite_file?(path)
    begin
      db = SQLite3::Database.new(path, readonly: true)
      tables = get_table_names(db)
      info[:is_sqlite] = true
      info[:table_count] = tables.size
      info[:has_ea_tables] = REQUIRED_EA_TABLES.all? do |t|
        tables.include?(t)
      end

      # Get record counts for key tables
      if tables.include?("t_object")
        info[:object_count] =
          db.execute("SELECT COUNT(*) FROM t_object").first.first
      end
      if tables.include?("t_package")
        info[:package_count] =
          db.execute("SELECT COUNT(*) FROM t_package").first.first
      end
    rescue SQLite3::Exception => e
      info[:error] = e.message
    ensure
      db&.close
    end
  else
    info[:is_sqlite] = false
  end

  info
end

.qea_file?(path) ⇒ Boolean

Check if file is a QEA file

Parameters:

  • path (String)

    File path

Returns:

  • (Boolean)

    True if file appears to be QEA



25
26
27
28
29
30
31
32
# File 'lib/lutaml/qea/file_detector.rb', line 25

def qea_file?(path)
  return false unless File.exist?(path)
  return false unless File.file?(path)
  return false unless path.end_with?(".qea")

  # Quick check: is it SQLite?
  sqlite_file?(path)
end

.validate_qea(path) ⇒ Hash

Validate QEA file structure

Examples:

result = FileDetector.validate_qea("model.qea")
if result[:valid]
  puts "Valid QEA file"
else
  puts "Errors: #{result[:errors]}"
end

Parameters:

  • path (String)

    File path

Returns:

  • (Hash)

    Validation result with :valid, :errors, :warnings



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/lutaml/qea/file_detector.rb', line 46

def validate_qea(path) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
  errors = []
  warnings = []

  # Check file exists
  unless File.exist?(path)
    return {
      valid: false,
      errors: ["File not found: #{path}"],
      warnings: [],
    }
  end

  # Check file extension
  unless path.end_with?(".qea")
    warnings << "File does not have .qea extension"
  end

  # Check SQLite format
  unless sqlite_file?(path)
    errors << "File is not a valid SQLite database"
    return { valid: false, errors: errors, warnings: warnings }
  end

  # Check for EA tables
  begin
    db = SQLite3::Database.new(path, readonly: true)
    tables = get_table_names(db)

    REQUIRED_EA_TABLES.each do |required_table|
      unless tables.include?(required_table)
        errors << "Missing required EA table: #{required_table}"
      end
    end

    # Check for data
    if tables.include?("t_object")
      count = db.execute("SELECT COUNT(*) FROM t_object").first.first
      if count.zero?
        warnings << "No objects found in t_object table"
      end
    end
  rescue SQLite3::Exception => e
    errors << "Failed to open database: #{e.message}"
  ensure
    db&.close
  end

  {
    valid: errors.empty?,
    errors: errors,
    warnings: warnings,
  }
end