Class: Lutaml::Qea::Infrastructure::SchemaReader

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

Overview

SchemaReader reads database schema information from a QEA SQLite database.

This class is responsible for introspecting the database schema, including table names, column definitions, and metadata.

Examples:

Read schema information

reader = SchemaReader.new(db_connection)
tables = reader.tables
columns = reader.columns("t_object")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(database) ⇒ SchemaReader

Initialize a new schema reader

Parameters:

  • database (SQLite3::Database)

    The database connection

Raises:

  • (ArgumentError)

    if database is nil



23
24
25
26
27
# File 'lib/lutaml/qea/infrastructure/schema_reader.rb', line 23

def initialize(database)
  raise ArgumentError, "database cannot be nil" if database.nil?

  @database = database
end

Instance Attribute Details

#databaseObject (readonly)

Returns the value of attribute database.



17
18
19
# File 'lib/lutaml/qea/infrastructure/schema_reader.rb', line 17

def database
  @database
end

Instance Method Details

#column_names(table_name) ⇒ Array<String>

Get just column names for a specific table

Parameters:

  • table_name (String)

    The table name

Returns:

  • (Array<String>)

    List of column names



63
64
65
# File 'lib/lutaml/qea/infrastructure/schema_reader.rb', line 63

def column_names(table_name)
  columns(table_name).map { |col| col["name"] }
end

#columns(table_name) ⇒ Array<Hash>

Get column information for a specific table

Examples:

columns = reader.columns("t_object")
# => [
#   {"cid"=>0, "name"=>"Object_ID", "type"=>"INTEGER",
#    "notnull"=>1, "dflt_value"=>nil, "pk"=>1},
#   ...
# ]

Parameters:

  • table_name (String)

    The table name

Returns:

  • (Array<Hash>)

    Array of column information hashes Each hash contains: name, type, notnull, dflt_value, pk



55
56
57
# File 'lib/lutaml/qea/infrastructure/schema_reader.rb', line 55

def columns(table_name)
  @database.execute("PRAGMA table_info(#{table_name})")
end

#indexes(table_name) ⇒ Array<Hash>

Get index information for a table

Parameters:

  • table_name (String)

    The table name

Returns:

  • (Array<Hash>)

    Array of index information



106
107
108
109
110
111
112
# File 'lib/lutaml/qea/infrastructure/schema_reader.rb', line 106

def indexes(table_name)
  @database.execute(
    "SELECT name, sql FROM sqlite_master " \
    "WHERE type='index' AND tbl_name=?",
    table_name,
  )
end

#primary_key(table_name) ⇒ String?

Get primary key column name for a table

or nil if no primary key

Parameters:

  • table_name (String)

    The table name

Returns:

  • (String, nil)

    The primary key column name,



84
85
86
87
# File 'lib/lutaml/qea/infrastructure/schema_reader.rb', line 84

def primary_key(table_name)
  pk_column = columns(table_name).find { |col| col["pk"] == 1 }
  pk_column&.fetch("name", nil)
end

#row_count(table_name) ⇒ Integer

Get row count for a table

Parameters:

  • table_name (String)

    The table name

Returns:

  • (Integer)

    Number of rows in the table



118
119
120
121
122
123
# File 'lib/lutaml/qea/infrastructure/schema_reader.rb', line 118

def row_count(table_name)
  result = @database.execute(
    "SELECT COUNT(*) as count FROM #{table_name}",
  )
  result.first["count"]
end

#statisticsHash

Get schema statistics for all tables

Returns:

  • (Hash)

    Hash mapping table names to row counts



128
129
130
131
132
# File 'lib/lutaml/qea/infrastructure/schema_reader.rb', line 128

def statistics
  tables.to_h do |table_name|
    [table_name, row_count(table_name)]
  end
end

#table_exists?(table_name) ⇒ Boolean

Check if a table exists in the database

Parameters:

  • table_name (String)

    The table name to check

Returns:

  • (Boolean)

    true if table exists



71
72
73
74
75
76
77
# File 'lib/lutaml/qea/infrastructure/schema_reader.rb', line 71

def table_exists?(table_name)
  result = @database.execute(
    "SELECT name FROM sqlite_master WHERE type='table' AND name=?",
    table_name,
  )
  !result.empty?
end

#table_schema(table_name) ⇒ String?

Get table schema as CREATE TABLE statement

or nil if table doesn’t exist

Parameters:

  • table_name (String)

    The table name

Returns:

  • (String, nil)

    The CREATE TABLE SQL statement,



94
95
96
97
98
99
100
# File 'lib/lutaml/qea/infrastructure/schema_reader.rb', line 94

def table_schema(table_name)
  result = @database.execute(
    "SELECT sql FROM sqlite_master WHERE type='table' AND name=?",
    table_name,
  )
  result.first&.fetch("sql", nil)
end

#tables(exclude_system: true) ⇒ Array<String>

Get list of all table names in the database

Exclude SQLite system tables (default: true)

Parameters:

  • exclude_system (Boolean) (defaults to: true)

Returns:

  • (Array<String>)

    List of table names



34
35
36
37
38
39
40
# File 'lib/lutaml/qea/infrastructure/schema_reader.rb', line 34

def tables(exclude_system: true)
  query = "SELECT name FROM sqlite_master WHERE type='table'"
  query += " AND name NOT LIKE 'sqlite_%'" if exclude_system
  query += " ORDER BY name"

  @database.execute(query).map { |row| row["name"] }
end