Skip to content

Internals Database Creator Table TableRegistry

Stefan Crkvenjakov edited this page Oct 7, 2026 · 1 revision

database\creator\table\TableRegistry

Framework Internals · Subsystem overview · Developer guide

Source: awt_src/classes/database/creator/table/TableRegistry.php · Snapshot 8411f4287706.

DatabaseManager delegates SQL generation to QueryBuilder, execution to IProvider, and result caching to ICache. Schema builders use MySQL DDL and the framework table registry.

Imports and dependencies

use database\creator\interface\ITableRegistry;
use database\DatabaseManager;

TableRegistry

Declaration and architectural role

class TableRegistry implements ITableRegistry

Source description (behavioral corrections are recorded in Known discrepancies):

TableRegistry

Manages the framework's schema registry: the awt_table and awt_table_structure tables that record every table and column created by the framework.

All reads and writes go through DatabaseManager so they participate in the same read-through cache and condition-aware invalidation as the rest of the application.

This class handles only registry data. It never issues DDL or touches the real database schema; that is TableSchemaProvider's responsibility.

Constants, enum cases, traits and properties

Declarations below preserve visibility, type, default and modifiers exactly. A typed property without an initializer remains uninitialized until its constructor or another method assigns it. Mutable fields belong to this instance unless marked static; readonly fields can be initialized once. Constructor-promoted properties are also listed below.

Member Declaration Purpose or source contract
Constructor-promoted private readonly DatabaseManager $db Initialized from the constructor argument; default expression is evaluated when the argument is omitted.

Method index

Method Signature
__construct public function __construct( private readonly DatabaseManager $db, )
tableExists public function tableExists(string $name): bool
columnExists public function columnExists(string $table, string $column): bool
getTableId public function getTableId(string $name): ?int
registerTable public function registerTable(string $name, int $creatorId): int
registerColumn public function registerColumn(int $tableId, string $columnName, string $columnType): void
unregisterTable public function unregisterTable(string $name): void
unregisterColumn public function unregisterColumn(string $table, string $column): void
updateColumnType public function updateColumnType(string $table, string $column, string $newType): void
renameColumn public function renameColumn(string $table, string $oldName, string $newName): void
findByOwner public function findByOwner(int $ownerId): array

__construct

Source line 27

public function __construct(
        private readonly DatabaseManager $db,
    );

Parameters

Parameter Type, modifiers and default Purpose
$db private readonly DatabaseManager $db The database manager used for all registry queries.

@param DatabaseManager $db The database manager used for all registry queries.

Concrete no-op/default hook: this method performs no work. Override it where the lifecycle or extension contract calls for behavior.

tableExists

Source line 36

public function tableExists(string $name): bool

Parameters

Parameter Type, modifiers and default Purpose
$name string $name Named key/label selected by this API; its exact scope is shown by the implementation trace.

{@inheritdoc}

Return expressions (including local callbacks): !empty($rows).

Implementation, validation guards and interaction order
// =========================================================================
    // ITableRegistry
    // =========================================================================

    /** {@inheritdoc} */
    public function tableExists(string $name): bool
    {
        $rows = $this->db
            ->table('awt_table')
            ->select(['id'])
            ->where(['name' => $name])
            ->get();

        return !empty($rows);
    }

columnExists

Source line 48

public function columnExists(string $table, string $column): bool

Parameters

Parameter Type, modifiers and default Purpose
$table string $table Operation input; the exact use, validation and propagation are shown in the implementation trace.
$column string $column Operation input; the exact use, validation and propagation are shown in the implementation trace.

{@inheritdoc}

Return expressions (including local callbacks): false; !empty($rows).

Implementation, validation guards and interaction order
/** {@inheritdoc} */
    public function columnExists(string $table, string $column): bool
    {
        $tableId = $this->getTableId($table);

        if ($tableId === null) {
            return false;
        }

        $rows = $this->db
            ->table('awt_table_structure')
            ->select(['id'])
            ->where(['table_id' => $tableId, 'column_name' => $column])
            ->get();

        return !empty($rows);
    }

getTableId

Source line 66

public function getTableId(string $name): ?int

Parameters

Parameter Type, modifiers and default Purpose
$name string $name Named key/label selected by this API; its exact scope is shown by the implementation trace.

{@inheritdoc}

Return expressions (including local callbacks): !empty($rows) ? (int) $rows[0]['id'] : null.

Implementation, validation guards and interaction order
/** {@inheritdoc} */
    public function getTableId(string $name): ?int
    {
        $rows = $this->db
            ->table('awt_table')
            ->select(['id'])
            ->where(['name' => $name])
            ->get();

        return !empty($rows) ? (int) $rows[0]['id'] : null;
    }

registerTable

Source line 78

public function registerTable(string $name, int $creatorId): int

Parameters

Parameter Type, modifiers and default Purpose
$name string $name Named key/label selected by this API; its exact scope is shown by the implementation trace.
$creatorId int $creatorId Package ownership ID written into schema registry records.

{@inheritdoc}

Return expressions (including local callbacks): (int) $this->db ->table('awt_table') ->insert(['name' => $name, 'creator' => $creatorId]) ->executeInsert().

Implementation, validation guards and interaction order
/** {@inheritdoc} */
    public function registerTable(string $name, int $creatorId): int
    {
        return (int) $this->db
            ->table('awt_table')
            ->insert(['name' => $name, 'creator' => $creatorId])
            ->executeInsert();
    }

registerColumn

Source line 87

public function registerColumn(int $tableId, string $columnName, string $columnType): void

Parameters

Parameter Type, modifiers and default Purpose
$tableId int $tableId Operation input; the exact use, validation and propagation are shown in the implementation trace.
$columnName string $columnName Operation input; the exact use, validation and propagation are shown in the implementation trace.
$columnType string $columnType Operation input; the exact use, validation and propagation are shown in the implementation trace.

{@inheritdoc}

Computes or delegates the operation shown below; no explicit receiver property assignment or throw occurs in this method.

Implementation, validation guards and interaction order
/** {@inheritdoc} */
    public function registerColumn(int $tableId, string $columnName, string $columnType): void
    {
        $this->db
            ->table('awt_table_structure')
            ->insert([
                'table_id'    => $tableId,
                'column_name' => $columnName,
                'column_type' => strtolower($columnType),
            ])
            ->executeInsert();
    }

unregisterTable

Source line 100

public function unregisterTable(string $name): void

Parameters

Parameter Type, modifiers and default Purpose
$name string $name Named key/label selected by this API; its exact scope is shown by the implementation trace.

{@inheritdoc}

Explicit exceptions: \RuntimeException. The guards in the implementation below show when each is raised; exceptions from collaborators also propagate unless caught.

Implementation, validation guards and interaction order
/** {@inheritdoc} */
    public function unregisterTable(string $name): void
    {
        $tableId = $this->getTableId($name);

        if ($tableId === null) {
            return;
        }

        // Remove all column entries first to satisfy any foreign key constraints
        // on awt_table_structure that reference awt_table.id.
        $this->db->transaction(function () use ($tableId, $name): void {
            // Tables with no registered columns legitimately delete zero column rows.
            $this->db->table('awt_table_structure')->where(['table_id' => $tableId])->delete();
            if (!$this->db->table('awt_table')->where(['id' => $tableId])->delete()) {
                throw new \RuntimeException("Failed to unregister table: {$name}");
            }
        });
    }

unregisterColumn

Source line 120

public function unregisterColumn(string $table, string $column): void

Parameters

Parameter Type, modifiers and default Purpose
$table string $table Operation input; the exact use, validation and propagation are shown in the implementation trace.
$column string $column Operation input; the exact use, validation and propagation are shown in the implementation trace.

{@inheritdoc}

Delegates to getTableId().

Implementation, validation guards and interaction order
/** {@inheritdoc} */
    public function unregisterColumn(string $table, string $column): void
    {
        $tableId = $this->getTableId($table);

        if ($tableId === null) {
            return;
        }

        $this->db
            ->table('awt_table_structure')
            ->where(['table_id' => $tableId, 'column_name' => $column])
            ->delete();
    }

updateColumnType

Source line 135

public function updateColumnType(string $table, string $column, string $newType): void

Parameters

Parameter Type, modifiers and default Purpose
$table string $table Operation input; the exact use, validation and propagation are shown in the implementation trace.
$column string $column Operation input; the exact use, validation and propagation are shown in the implementation trace.
$newType string $newType Operation input; the exact use, validation and propagation are shown in the implementation trace.

{@inheritdoc}

Delegates to getTableId().

Implementation, validation guards and interaction order
/** {@inheritdoc} */
    public function updateColumnType(string $table, string $column, string $newType): void
    {
        $tableId = $this->getTableId($table);

        if ($tableId === null) {
            return;
        }

        $this->db
            ->table('awt_table_structure')
            ->where(['table_id' => $tableId, 'column_name' => $column])
            ->update(['column_type' => strtolower($newType)]);
    }

renameColumn

Source line 150

public function renameColumn(string $table, string $oldName, string $newName): void

Parameters

Parameter Type, modifiers and default Purpose
$table string $table Operation input; the exact use, validation and propagation are shown in the implementation trace.
$oldName string $oldName Operation input; the exact use, validation and propagation are shown in the implementation trace.
$newName string $newName Operation input; the exact use, validation and propagation are shown in the implementation trace.

{@inheritdoc}

Delegates to getTableId().

Implementation, validation guards and interaction order
/** {@inheritdoc} */
    public function renameColumn(string $table, string $oldName, string $newName): void
    {
        $tableId = $this->getTableId($table);

        if ($tableId === null) {
            return;
        }

        $this->db
            ->table('awt_table_structure')
            ->where(['table_id' => $tableId, 'column_name' => $oldName])
            ->update(['column_name' => $newName]);
    }

findByOwner

Source line 165

public function findByOwner(int $ownerId): array

Parameters

Parameter Type, modifiers and default Purpose
$ownerId int $ownerId Owning record ID or null.

{@inheritdoc}

Return expressions (including local callbacks): array_column($rows, 'name').

Implementation, validation guards and interaction order
/** {@inheritdoc} */
    public function findByOwner(int $ownerId): array
    {
        $rows = $this->db->table('awt_table')->select(['name'])
            ->where(['creator' => $ownerId])->get();

        return array_column($rows, 'name');
    }

Related types

Clone this wiki locally