SIMPLE_SQL_SCHEMA

Schema introspection for examining database structure. Query tables, columns, indexes, and foreign keys.

View Source

Creation

make (a_db: SIMPLE_SQL_DATABASE)
Create schema inspector attached to database.

Tables

tables: ARRAYED_LIST [STRING_8]
List of all table names.
table_exists (a_name: READABLE_STRING_8): BOOLEAN
Check if table exists.
table_info (a_table: READABLE_STRING_8): SIMPLE_SQL_TABLE_INFO
Get detailed table information.

Columns

columns (a_table: READABLE_STRING_8): ARRAYED_LIST [SIMPLE_SQL_COLUMN_INFO]
Get column definitions for table.
column_names (a_table: READABLE_STRING_8): ARRAYED_LIST [STRING_8]
Get just column names.
primary_key (a_table: READABLE_STRING_8): detachable STRING_8
Get primary key column name(s).

Indexes

indexes (a_table: READABLE_STRING_8): ARRAYED_LIST [STRING_8]
List index names for table.
index_columns (a_index: READABLE_STRING_8): ARRAYED_LIST [STRING_8]
Get columns in an index.

Foreign Keys

foreign_keys (a_table: READABLE_STRING_8): SIMPLE_SQL_RESULT
Get foreign key definitions.

SIMPLE_SQL_TABLE_INFO

name: STRING_8
Table name.
columns: ARRAYED_LIST [SIMPLE_SQL_COLUMN_INFO]
Column definitions.
row_count: INTEGER_64
Number of rows.
sql: STRING_8
CREATE TABLE statement.

SIMPLE_SQL_COLUMN_INFO

name: STRING_8
Column name.
data_type: STRING_8
SQLite type (TEXT, INTEGER, REAL, BLOB).
is_nullable: BOOLEAN
Can column contain NULL?
is_primary_key: BOOLEAN
Is this the primary key?
default_value: detachable STRING_8
Default value expression.

Examples

List All Tables

local
    schema: SIMPLE_SQL_SCHEMA
do
    create schema.make (db)

    print ("Tables in database:%N")
    across schema.tables as t loop
        print ("  - " + t + "%N")
    end
end

Examine Table Structure

local
    schema: SIMPLE_SQL_SCHEMA
    info: SIMPLE_SQL_TABLE_INFO
do
    create schema.make (db)
    info := schema.table_info ("users")

    print ("Table: " + info.name + "%N")
    print ("Rows: " + info.row_count.out + "%N")
    print ("Columns:%N")

    across info.columns as col loop
        print ("  " + col.name + ": " + col.data_type)
        if col.is_primary_key then
            print (" [PK]")
        end
        if not col.is_nullable then
            print (" NOT NULL")
        end
        print ("%N")
    end
end

Check for Required Tables

local
    schema: SIMPLE_SQL_SCHEMA
    required: ARRAY [STRING_8]
do
    create schema.make (db)
    required := <<"users", "sessions", "settings">>

    across required as table loop
        if not schema.table_exists (table) then
            print ("Missing table: " + table + "%N")
        end
    end
end