Class: Lutaml::Qea::Infrastructure::SchemaReader
- Inherits:
-
Object
- Object
- Lutaml::Qea::Infrastructure::SchemaReader
- 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.
Instance Attribute Summary collapse
-
#database ⇒ Object
readonly
Returns the value of attribute database.
Instance Method Summary collapse
-
#column_names(table_name) ⇒ Array<String>
Get just column names for a specific table.
-
#columns(table_name) ⇒ Array<Hash>
Get column information for a specific table.
-
#indexes(table_name) ⇒ Array<Hash>
Get index information for a table.
-
#initialize(database) ⇒ SchemaReader
constructor
Initialize a new schema reader.
-
#primary_key(table_name) ⇒ String?
Get primary key column name for a table.
-
#row_count(table_name) ⇒ Integer
Get row count for a table.
-
#statistics ⇒ Hash
Get schema statistics for all tables.
-
#table_exists?(table_name) ⇒ Boolean
Check if a table exists in the database.
-
#table_schema(table_name) ⇒ String?
Get table schema as CREATE TABLE statement.
-
#tables(exclude_system: true) ⇒ Array<String>
Get list of all table names in the database.
Constructor Details
#initialize(database) ⇒ SchemaReader
Initialize a new schema reader
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
#database ⇒ Object (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
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
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
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
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
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 |
#statistics ⇒ Hash
Get schema statistics for all tables
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
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
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)
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 |