Skip to content

Internals Database Creator TableWizard

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

database\creator\TableWizard

Framework Internals · Subsystem overview · Developer guide

Source: awt_src/classes/database/creator/TableWizard.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\cache\DatabaseCache;
use database\creator\column\ColumnSQLBuilder;
use database\creator\interface\ITableRegistry;
use database\creator\interface\ITableSchemaProvider;
use database\creator\table\TableRegistry;
use database\creator\table\TableSchemaProvider;
use database\DatabaseManager;
use database\provider\DatabaseProvider;
use database\query\QueryBuilder;

TableWizard

Declaration and architectural role

class TableWizard

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

TableWizard

Facade for schema management. Exposes a high-level API for creating, modifying, and dropping tables and columns while keeping all collaborators separated by responsibility:

ITableSchemaProvider Executes DDL statements against the database. ITableRegistry Reads and writes the framework schema registry. ColumnSQLBuilder Generates SQL fragments from ColumnDefinition objects.

TableWizard no longer extends DatabaseManager. It receives a DatabaseManager instance as a dependency and uses it only through the registry, never accessing PDO directly.

Full operation list: createTable() Create a new table with all queued columns. dropTable() Drop a table and remove it from the registry. addColumn() Queue a column for the next createTable() call. addColumnToTable() Add a single column to an existing table. dropColumn() Remove a column from an existing table. modifyColumn() Change the definition of an existing column. renameColumn() Rename a column (MySQL 8+ / MariaDB 10.5+). renameTable() Rename a table and update the registry. addIndex() Add a named index to a column. dropIndex() Drop a named index. tableExists() Check whether a table is registered. columnExists() Check whether a column is registered.

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
$columns private array $columns = []; Columns queued for the next createTable() call.
$headless private bool $headless = false; Used by headless(), createTable().
$sqlBuilder private ColumnSQLBuilder $sqlBuilder; Used by __construct(), createTable(), addColumnToTable(), modifyColumn().
Constructor-promoted private readonly int $creatorId = 0 Initialized from the constructor argument; default expression is evaluated when the argument is omitted.
Constructor-promoted private readonly ITableSchemaProvider $schema = new TableSchemaProvider(new DatabaseProvider()) Initialized from the constructor argument; default expression is evaluated when the argument is omitted.
Constructor-promoted private readonly ITableRegistry $registry = new TableRegistry(new DatabaseManager()) Initialized from the constructor argument; default expression is evaluated when the argument is omitted.

Method index

Method Signature
__construct public function __construct( private readonly int $creatorId = 0, private readonly ITableSchemaProvider $schema = new TableSchemaProvider(new DatabaseProvider()), private readonly ITableRegistry $registry = new TableRegistry(new DatabaseManager()), )
headless public function headless(): self
create public static function create(int $creatorId = 0): self
addColumn public function addColumn(ColumnCreator $column): self
createTable public function createTable(string $tableName): bool
dropTable public function dropTable(string $tableName): bool
renameTable public function renameTable(string $from, string $to): bool
addColumnToTable public function addColumnToTable(string $tableName, ColumnCreator $column): bool
dropColumn public function dropColumn(string $tableName, string $columnName): bool
modifyColumn public function modifyColumn(string $tableName, ColumnCreator $column): bool
renameColumn public function renameColumn(string $tableName, string $from, string $to): bool
addIndex public function addIndex( string $tableName, string $columnName, string $indexType = 'INDEX', string $indexName = '', ): bool
dropIndex public function dropIndex(string $tableName, string $indexName): bool
tableExists public function tableExists(string $name): bool
columnExists public function columnExists(string $table, string $column): bool

__construct

Source line 56

public function __construct(
        private readonly int                  $creatorId = 0,
        private readonly ITableSchemaProvider $schema = new TableSchemaProvider(new DatabaseProvider()),
        private readonly ITableRegistry       $registry = new TableRegistry(new DatabaseManager()),
    )

Parameters

Parameter Type, modifiers and default Purpose
$creatorId private readonly int $creatorId = 0 The package/plugin ID recorded against new tables.
$schema private readonly ITableSchemaProvider $schema = new TableSchemaProvider(new DatabaseProvider()) Executes DDL against the database.
$registry private readonly ITableRegistry $registry = new TableRegistry(new DatabaseManager()) Reads and writes the framework schema registry.

@param int $creatorId The package/plugin ID recorded against new tables. @param ITableSchemaProvider $schema Executes DDL against the database. @param ITableRegistry $registry Reads and writes the framework schema registry.

State writes: $this->sqlBuilder.

Implementation, validation guards and interaction order
/**
     * @param int                  $creatorId  The package/plugin ID recorded against new tables.
     * @param ITableSchemaProvider $schema     Executes DDL against the database.
     * @param ITableRegistry       $registry   Reads and writes the framework schema registry.
     */
    public function __construct(
        private readonly int                  $creatorId = 0,
        private readonly ITableSchemaProvider $schema = new TableSchemaProvider(new DatabaseProvider()),
        private readonly ITableRegistry       $registry = new TableRegistry(new DatabaseManager()),
    ) {
        $this->sqlBuilder = new ColumnSQLBuilder();
    }

headless

Source line 64

public function headless(): self

State writes: $this->headless.

Return expressions (including local callbacks): $this.

Implementation, validation guards and interaction order
public function headless(): self
    {
        $this->headless = true;
        return $this;
    }

create

Source line 77

public static function create(int $creatorId = 0): self

Parameters

Parameter Type, modifiers and default Purpose
$creatorId int $creatorId = 0 The package/plugin ID for the new tables.

Convenience static constructor that wires the default concrete collaborators automatically.

@param int $creatorId The package/plugin ID for the new tables.

Return expressions (including local callbacks): new self( creatorId: $creatorId, schema: new TableSchemaProvider($provider), registry: new TableRegistry($db), ).

Implementation, validation guards and interaction order
/**
     * Convenience static constructor that wires the default concrete
     * collaborators automatically.
     *
     * @param int $creatorId The package/plugin ID for the new tables.
     */
    public static function create(int $creatorId = 0): self
    {
        $provider = new DatabaseProvider();
        $db       = new DatabaseManager($provider, new QueryBuilder(), new DatabaseCache());

        return new self(
            creatorId: $creatorId,
            schema:    new TableSchemaProvider($provider),
            registry:  new TableRegistry($db),
        );
    }

addColumn

Source line 99

public function addColumn(ColumnCreator $column): self

Parameters

Parameter Type, modifiers and default Purpose
$column ColumnCreator $column The column definition to queue.

Queue a column to be included in the next createTable() call. Has no effect on existing tables; use addColumnToTable() for that.

@param ColumnCreator $column The column definition to queue.

State writes: $this->columns[].

Return expressions (including local callbacks): $this.

Implementation, validation guards and interaction order
// =========================================================================
    // Column queue
    // =========================================================================

    /**
     * Queue a column to be included in the next createTable() call.
     * Has no effect on existing tables; use addColumnToTable() for that.
     *
     * @param ColumnCreator $column The column definition to queue.
     */
    public function addColumn(ColumnCreator $column): self
    {
        $this->columns[] = $column;
        return $this;
    }

createTable

Source line 125

public function createTable(string $tableName): bool

Parameters

Parameter Type, modifiers and default Purpose
$tableName string $tableName The name of the table to create.

Create a new database table from the columns queued via addColumn().

The table is built with ENGINE=InnoDB and utf8mb4_general_ci collation. Foreign key constraints are appended as trailing clauses inside the CREATE TABLE statement, separate from the column definitions. Regular (non-unique) INDEX columns are created as standalone statements after the table itself has been created.

On success, the table and all its columns are recorded in the registry. The column queue is cleared after each call regardless of outcome.

@param string $tableName The name of the table to create.

@return bool False if the table is already registered; true on success.

State writes: $this->columns.

Return expressions (including local callbacks): false; $result.

Implementation, validation guards and interaction order
// =========================================================================
    // Table operations
    // =========================================================================

    /**
     * Create a new database table from the columns queued via addColumn().
     *
     * The table is built with ENGINE=InnoDB and utf8mb4_general_ci collation.
     * Foreign key constraints are appended as trailing clauses inside the
     * CREATE TABLE statement, separate from the column definitions.
     * Regular (non-unique) INDEX columns are created as standalone statements
     * after the table itself has been created.
     *
     * On success, the table and all its columns are recorded in the registry.
     * The column queue is cleared after each call regardless of outcome.
     *
     * @param string $tableName The name of the table to create.
     *
     * @return bool False if the table is already registered; true on success.
     */
    public function createTable(string $tableName): bool
    {
        if (!$this->headless && $this->registry->tableExists($tableName)) {
            return false;
        }

        $columnClauses     = [];
        $foreignKeyClauses = [];

        foreach ($this->columns as $column) {
            $def                = $column->build();
            $columnClauses[]    = $this->sqlBuilder->buildColumnSQL($def);

            $fk = $this->sqlBuilder->buildForeignKeySQL($def);
            if ($fk !== null) {
                $foreignKeyClauses[] = $fk;
            }
        }

        $allClauses = implode(",\n    ", array_merge($columnClauses, $foreignKeyClauses));
        $sql        = "CREATE TABLE `{$tableName}` (\n    {$allClauses}\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;";

        $result = $this->schema->executeDDL($sql);

        if ($result) {

            $tableId = null;

            if(!$this->headless) {
                $tableId = $this->registry->registerTable($tableName, $this->creatorId);
            }


            foreach ($this->columns as $column) {
                $def = $column->build();

                if(!$this->headless) {
                    $this->registry->registerColumn($tableId, $def->name, $def->type);
                }


                // Standalone index statements are run after the table exists.
                $indexSQL = $this->sqlBuilder->buildIndexSQL($tableName, $def);
                if ($indexSQL !== null) {
                    $this->schema->executeDDL($indexSQL);
                }
            }
        }

        $this->columns = [];
        return $result;
    }

dropTable

Source line 186

public function dropTable(string $tableName): bool

Parameters

Parameter Type, modifiers and default Purpose
$tableName string $tableName The table to drop.

Drop a table from the database and remove all of its entries from the framework schema registry.

@param string $tableName The table to drop.

@return bool False if the table is not registered; true on success.

Return expressions (including local callbacks): false; $result.

Implementation, validation guards and interaction order
/**
     * Drop a table from the database and remove all of its entries from the
     * framework schema registry.
     *
     * @param string $tableName The table to drop.
     *
     * @return bool False if the table is not registered; true on success.
     */
    public function dropTable(string $tableName): bool
    {
        if (!$this->registry->tableExists($tableName)) {
            return false;
        }

        $result = $this->schema->executeDDL("DROP TABLE `{$tableName}`");

        if ($result) {
            $this->registry->unregisterTable($tableName);
        }

        return $result;
    }

renameTable

Source line 209

public function renameTable(string $from, string $to): bool

Parameters

Parameter Type, modifiers and default Purpose
$from string $from Current table name.
$to string $to New table name.

Rename an existing table and update its entry in the registry.

@param string $from Current table name. @param string $to New table name.

@return bool False if the source table is not registered; true on success.

Return expressions (including local callbacks): false; $result.

Implementation, validation guards and interaction order
/**
     * Rename an existing table and update its entry in the registry.
     *
     * @param string $from Current table name.
     * @param string $to   New table name.
     *
     * @return bool False if the source table is not registered; true on success.
     */
    public function renameTable(string $from, string $to): bool
    {
        if (!$this->registry->tableExists($from)) {
            return false;
        }

        $result = $this->schema->executeDDL("RENAME TABLE `{$from}` TO `{$to}`");

        if ($result) {
            // Re-register the table under the new name, preserving the creator ID.
            $tableId = $this->registry->getTableId($from);
            $this->registry->unregisterTable($from);
            // registerTable creates a new entry; we pass the original creator ID.
            $this->registry->registerTable($to, $this->creatorId);
        }

        return $result;
    }

addColumnToTable

Source line 247

public function addColumnToTable(string $tableName, ColumnCreator $column): bool

Parameters

Parameter Type, modifiers and default Purpose
$tableName string $tableName The table to alter.
$column ColumnCreator $column The column to add.

Add a new column to an existing table.

When the column carries a foreign key, the ADD COLUMN and ADD FOREIGN KEY statements are issued as two separate ALTER TABLE calls. Concatenating a FOREIGN KEY clause directly onto ADD COLUMN is invalid SQL.

When the column carries a regular INDEX, a standalone CREATE INDEX statement is issued after the column is added.

@param string $tableName The table to alter. @param ColumnCreator $column The column to add.

@return bool False if the column is already registered; true on success.

Return expressions (including local callbacks): false; true.

Implementation, validation guards and interaction order
// =========================================================================
    // Column operations
    // =========================================================================

    /**
     * Add a new column to an existing table.
     *
     * When the column carries a foreign key, the ADD COLUMN and ADD FOREIGN KEY
     * statements are issued as two separate ALTER TABLE calls. Concatenating a
     * FOREIGN KEY clause directly onto ADD COLUMN is invalid SQL.
     *
     * When the column carries a regular INDEX, a standalone CREATE INDEX
     * statement is issued after the column is added.
     *
     * @param string        $tableName The table to alter.
     * @param ColumnCreator $column    The column to add.
     *
     * @return bool False if the column is already registered; true on success.
     */
    public function addColumnToTable(string $tableName, ColumnCreator $column): bool
    {
        if ($this->registry->columnExists($tableName, $column->getName())) {
            return false;
        }

        $def    = $column->build();
        $result = $this->schema->executeDDL(
            "ALTER TABLE `{$tableName}` ADD COLUMN " . $this->sqlBuilder->buildColumnSQL($def)
        );

        if (!$result) {
            return false;
        }

        // Foreign key must be a separate ALTER TABLE statement.
        $fkSQL = $this->sqlBuilder->buildForeignKeySQL($def);
        if ($fkSQL !== null) {
            $this->schema->executeDDL("ALTER TABLE `{$tableName}` ADD {$fkSQL}");
        }

        // Standalone index statement when the column is marked INDEX.
        $indexSQL = $this->sqlBuilder->buildIndexSQL($tableName, $def);
        if ($indexSQL !== null) {
            $this->schema->executeDDL($indexSQL);
        }

        $tableId = $this->registry->getTableId($tableName);
        if ($tableId !== null) {
            $this->registry->registerColumn($tableId, $def->name, $def->type);
        }

        return true;
    }

dropColumn

Source line 290

public function dropColumn(string $tableName, string $columnName): bool

Parameters

Parameter Type, modifiers and default Purpose
$tableName string $tableName The table to alter.
$columnName string $columnName The column to drop.

Remove a column from an existing table.

@param string $tableName The table to alter. @param string $columnName The column to drop.

@return bool False if the column is not registered; true on success.

Return expressions (including local callbacks): false; $result.

Implementation, validation guards and interaction order
/**
     * Remove a column from an existing table.
     *
     * @param string $tableName  The table to alter.
     * @param string $columnName The column to drop.
     *
     * @return bool False if the column is not registered; true on success.
     */
    public function dropColumn(string $tableName, string $columnName): bool
    {
        if (!$this->registry->columnExists($tableName, $columnName)) {
            return false;
        }

        $result = $this->schema->executeDDL(
            "ALTER TABLE `{$tableName}` DROP COLUMN `{$columnName}`"
        );

        if ($result) {
            $this->registry->unregisterColumn($tableName, $columnName);
        }

        return $result;
    }

modifyColumn

Source line 319

public function modifyColumn(string $tableName, ColumnCreator $column): bool

Parameters

Parameter Type, modifiers and default Purpose
$tableName string $tableName The table to alter.
$column ColumnCreator $column The new column definition. The name must

Change the definition of an existing column (type, length, nullability, default, etc.) using MODIFY COLUMN.

The column must already exist. To rename a column, use renameColumn().

@param string $tableName The table to alter. @param ColumnCreator $column The new column definition. The name must match the existing column.

@return bool False if the column is not registered; true on success.

Return expressions (including local callbacks): false; $result.

Implementation, validation guards and interaction order
/**
     * Change the definition of an existing column (type, length, nullability,
     * default, etc.) using MODIFY COLUMN.
     *
     * The column must already exist. To rename a column, use renameColumn().
     *
     * @param string        $tableName The table to alter.
     * @param ColumnCreator $column    The new column definition. The name must
     *                                 match the existing column.
     *
     * @return bool False if the column is not registered; true on success.
     */
    public function modifyColumn(string $tableName, ColumnCreator $column): bool
    {
        if (!$this->registry->columnExists($tableName, $column->getName())) {
            return false;
        }

        $def    = $column->build();
        $result = $this->schema->executeDDL(
            "ALTER TABLE `{$tableName}` MODIFY COLUMN " . $this->sqlBuilder->buildColumnSQL($def)
        );

        if ($result) {
            $this->registry->updateColumnType($tableName, $def->name, $def->type);
        }

        return $result;
    }

renameColumn

Source line 349

public function renameColumn(string $tableName, string $from, string $to): bool

Parameters

Parameter Type, modifiers and default Purpose
$tableName string $tableName The table to alter.
$from string $from The current column name.
$to string $to The new column name.

Rename a column using ALTER TABLE RENAME COLUMN.

Requires MySQL 8.0+ or MariaDB 10.5.2+. On older engines, use modifyColumn() with the new name and full type definition instead.

@param string $tableName The table to alter. @param string $from The current column name. @param string $to The new column name.

@return bool False if the source column is not registered; true on success.

Return expressions (including local callbacks): false; $result.

Implementation, validation guards and interaction order
/**
     * Rename a column using ALTER TABLE RENAME COLUMN.
     *
     * Requires MySQL 8.0+ or MariaDB 10.5.2+. On older engines, use
     * modifyColumn() with the new name and full type definition instead.
     *
     * @param string $tableName The table to alter.
     * @param string $from      The current column name.
     * @param string $to        The new column name.
     *
     * @return bool False if the source column is not registered; true on success.
     */
    public function renameColumn(string $tableName, string $from, string $to): bool
    {
        if (!$this->registry->columnExists($tableName, $from)) {
            return false;
        }

        $result = $this->schema->executeDDL(
            "ALTER TABLE `{$tableName}` RENAME COLUMN `{$from}` TO `{$to}`"
        );

        if ($result) {
            $this->registry->renameColumn($tableName, $from, $to);
        }

        return $result;
    }

addIndex

Source line 381

public function addIndex(
        string $tableName,
        string $columnName,
        string $indexType = 'INDEX',
        string $indexName = '',
    ): bool

Parameters

Parameter Type, modifiers and default Purpose
$tableName string $tableName The table to add the index to.
$columnName string $columnName The column to index.
$indexType string $indexType = 'INDEX' INDEX or UNIQUE. Defaults to INDEX.
$indexName string $indexName = '' Optional custom index name. When empty, a name

Add a named index to a column on an existing table.

@param string $tableName The table to add the index to. @param string $columnName The column to index. @param string $indexType INDEX or UNIQUE. Defaults to INDEX. @param string $indexName Optional custom index name. When empty, a name is generated as idx_{table}_{column}.

@return bool True on success.

Return expressions (including local callbacks): $this->schema->executeDDL($sql).

Implementation, validation guards and interaction order
// =========================================================================
    // Index operations
    // =========================================================================

    /**
     * Add a named index to a column on an existing table.
     *
     * @param string $tableName  The table to add the index to.
     * @param string $columnName The column to index.
     * @param string $indexType  INDEX or UNIQUE. Defaults to INDEX.
     * @param string $indexName  Optional custom index name. When empty, a name
     *                           is generated as idx_{table}_{column}.
     *
     * @return bool True on success.
     */
    public function addIndex(
        string $tableName,
        string $columnName,
        string $indexType = 'INDEX',
        string $indexName = '',
    ): bool {
        $indexType = strtoupper($indexType);
        $name      = $indexName !== '' ? $indexName : "idx_{$tableName}_{$columnName}";

        $sql = match ($indexType) {
            'UNIQUE' => "CREATE UNIQUE INDEX `{$name}` ON `{$tableName}` (`{$columnName}`)",
            default  => "CREATE INDEX `{$name}` ON `{$tableName}` (`{$columnName}`)",
        };

        return $this->schema->executeDDL($sql);
    }

dropIndex

Source line 406

public function dropIndex(string $tableName, string $indexName): bool

Parameters

Parameter Type, modifiers and default Purpose
$tableName string $tableName The table that owns the index.
$indexName string $indexName The index name to drop.

Drop a named index from a table.

@param string $tableName The table that owns the index. @param string $indexName The index name to drop.

@return bool True on success.

Return expressions (including local callbacks): $this->schema->executeDDL( "DROP INDEX {$indexName} ON {$tableName}" ).

Implementation, validation guards and interaction order
/**
     * Drop a named index from a table.
     *
     * @param string $tableName The table that owns the index.
     * @param string $indexName The index name to drop.
     *
     * @return bool True on success.
     */
    public function dropIndex(string $tableName, string $indexName): bool
    {
        return $this->schema->executeDDL(
            "DROP INDEX `{$indexName}` ON `{$tableName}`"
        );
    }

tableExists

Source line 422

public function tableExists(string $name): bool

Parameters

Parameter Type, modifiers and default Purpose
$name string $name The table name to check.

Check whether a table is registered in the framework schema.

@param string $name The table name to check.

Return expressions (including local callbacks): $this->registry->tableExists($name).

Implementation, validation guards and interaction order
// =========================================================================
    // Registry inspection
    // =========================================================================

    /**
     * Check whether a table is registered in the framework schema.
     *
     * @param string $name The table name to check.
     */
    public function tableExists(string $name): bool
    {
        return $this->registry->tableExists($name);
    }

columnExists

Source line 433

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

Parameters

Parameter Type, modifiers and default Purpose
$table string $table The table to inspect.
$column string $column The column name to check.

Check whether a column is registered for a specific table.

@param string $table The table to inspect. @param string $column The column name to check.

Return expressions (including local callbacks): $this->registry->columnExists($table, $column).

Implementation, validation guards and interaction order
/**
     * Check whether a column is registered for a specific table.
     *
     * @param string $table  The table to inspect.
     * @param string $column The column name to check.
     */
    public function columnExists(string $table, string $column): bool
    {
        return $this->registry->columnExists($table, $column);
    }

Related types

Clone this wiki locally