Repository navigation
Internals Database Creator Column ColumnSQLBuilder
Framework Internals · Subsystem overview · Developer guide
Source: awt_src/classes/database/creator/column/ColumnSQLBuilder.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.
class ColumnSQLBuilderSource description (behavioral corrections are recorded in Known discrepancies):
ColumnSQLBuilder
Sole responsibility: generate SQL fragments from a ColumnDefinition. It holds no state of its own; every method is a pure transformation of the definition it receives.
Three kinds of SQL are produced:
buildColumnSQL() The column definition clause used inside CREATE TABLE or ALTER TABLE ADD COLUMN / MODIFY COLUMN.
buildForeignKeySQL() The FOREIGN KEY … REFERENCES … constraint clause, used as a separate statement in ALTER TABLE or as a trailing clause inside CREATE TABLE.
buildIndexSQL() A standalone CREATE INDEX statement for columns whose index type is INDEX (non-primary, non-unique). Primary and unique indexes are inlined by buildColumnSQL().
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 |
|---|---|---|
| const UNQUOTED_DEFAULTS | private const UNQUOTED_DEFAULTS = [ 'CURRENT_TIMESTAMP', 'CURRENT_DATE', 'CURRENT_TIME', 'NOW()', 'NULL', 'TRUE', 'FALSE', '0', '1', ]; |
SQL keyword defaults that must not be wrapped in single quotes. Comparisons are performed case-insensitively. |
| Method | Signature |
|---|---|
| buildColumnSQL | public function buildColumnSQL(ColumnDefinition $def): string |
| buildForeignKeySQL | public function buildForeignKeySQL(ColumnDefinition $def): ?string |
| buildIndexSQL | public function buildIndexSQL(string $table, ColumnDefinition $def): ?string |
| buildDefaultClause | private function buildDefaultClause(string $value, bool $asDefined): string |
| isNumericType | private function isNumericType(string $type): bool |
public function buildColumnSQL(ColumnDefinition $def): string| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$def |
ColumnDefinition $def |
The column to generate SQL for. |
Generate the column definition SQL fragment.
Covers: type, length, UNSIGNED, NULL/NOT NULL, DEFAULT, AUTO_INCREMENT, inline PRIMARY KEY / UNIQUE, COMMENT, and AFTER positioning.
For ENUM and SET types the $enumValues list on the definition is used to build the parenthesised value list instead of $length.
@param ColumnDefinition $def The column to generate SQL for.
@return string SQL fragment suitable for use in CREATE TABLE or ALTER TABLE.
Return expressions (including local callbacks): $sql.
Implementation, validation guards and interaction order
/**
* Generate the column definition SQL fragment.
*
* Covers: type, length, UNSIGNED, NULL/NOT NULL, DEFAULT, AUTO_INCREMENT,
* inline PRIMARY KEY / UNIQUE, COMMENT, and AFTER positioning.
*
* For ENUM and SET types the $enumValues list on the definition is used
* to build the parenthesised value list instead of $length.
*
* @param ColumnDefinition $def The column to generate SQL for.
*
* @return string SQL fragment suitable for use in CREATE TABLE or ALTER TABLE.
*/
public function buildColumnSQL(ColumnDefinition $def): string
{
$sql = "`{$def->name}` {$def->type}";
// ENUM and SET derive their parenthesised list from $enumValues.
if (in_array($def->type, ['ENUM', 'SET'], true) && !empty($def->enumValues)) {
$quoted = array_map(fn(string $v) => "'{$v}'", $def->enumValues);
$sql .= '(' . implode(', ', $quoted) . ')';
} elseif ($def->length !== '') {
$sql .= "({$def->length})";
}
if ($def->unsigned && $this->isNumericType($def->type)) {
$sql .= ' UNSIGNED';
}
$sql .= $def->nullable ? ' NULL' : ' NOT NULL';
if ($def->default !== '') {
$sql .= $this->buildDefaultClause($def->default, $def->defaultAsDefined);
}
if ($def->autoIncrement) {
$sql .= ' AUTO_INCREMENT';
}
if ($def->index === 'PRIMARY') {
$sql .= ' PRIMARY KEY';
} elseif ($def->index === 'UNIQUE') {
$sql .= ' UNIQUE';
}
if ($def->comment !== '') {
$escaped = str_replace("'", "''", $def->comment);
$sql .= " COMMENT '{$escaped}'";
}
if ($def->after !== '') {
$sql .= " AFTER `{$def->after}`";
}
return $sql;
}public function buildForeignKeySQL(ColumnDefinition $def): ?string| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$def |
ColumnDefinition $def |
The column whose foreign key to generate. |
Generate an ADD FOREIGN KEY constraint clause.
In a CREATE TABLE this is appended as a trailing element of the column list. In an ALTER TABLE it becomes a separate ADD CONSTRAINT statement.
Returns null when the definition carries no foreign key configuration.
@param ColumnDefinition $def The column whose foreign key to generate.
@return string|null The constraint clause, or null if none is defined.
Return expressions (including local callbacks): null; $sql.
Implementation, validation guards and interaction order
/**
* Generate an ADD FOREIGN KEY constraint clause.
*
* In a CREATE TABLE this is appended as a trailing element of the column
* list. In an ALTER TABLE it becomes a separate ADD CONSTRAINT statement.
*
* Returns null when the definition carries no foreign key configuration.
*
* @param ColumnDefinition $def The column whose foreign key to generate.
*
* @return string|null The constraint clause, or null if none is defined.
*/
public function buildForeignKeySQL(ColumnDefinition $def): ?string
{
if ($def->foreignKeyTable === null || $def->foreignKeyColumn === null) {
return null;
}
$sql = "FOREIGN KEY (`{$def->name}`) REFERENCES `{$def->foreignKeyTable}`(`{$def->foreignKeyColumn}`)";
if ($def->onDelete !== null) {
$sql .= " ON DELETE {$def->onDelete}";
}
if ($def->onUpdate !== null) {
$sql .= " ON UPDATE {$def->onUpdate}";
}
return $sql;
}public function buildIndexSQL(string $table, ColumnDefinition $def): ?string| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$table |
string $table |
The table the index belongs to. |
$def |
ColumnDefinition $def |
The column to index. |
Generate a standalone CREATE INDEX statement for non-inline indexes.
Only emits SQL when the definition's index type is INDEX; primary and unique indexes are already inlined by buildColumnSQL() and do not need a separate statement.
@param string $table The table the index belongs to. @param ColumnDefinition $def The column to index.
@return string|null A CREATE INDEX statement, or null when not applicable.
Return expressions (including local callbacks): null; "CREATE INDEX {$indexName} ON {$table} ({$def->name})".
Implementation, validation guards and interaction order
/**
* Generate a standalone CREATE INDEX statement for non-inline indexes.
*
* Only emits SQL when the definition's index type is INDEX; primary and
* unique indexes are already inlined by buildColumnSQL() and do not need
* a separate statement.
*
* @param string $table The table the index belongs to.
* @param ColumnDefinition $def The column to index.
*
* @return string|null A CREATE INDEX statement, or null when not applicable.
*/
public function buildIndexSQL(string $table, ColumnDefinition $def): ?string
{
if ($def->index !== 'INDEX') {
return null;
}
$indexName = "idx_{$table}_{$def->name}";
return "CREATE INDEX `{$indexName}` ON `{$table}` (`{$def->name}`)";
}private function buildDefaultClause(string $value, bool $asDefined): string| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$value |
string $value |
Operation input; the exact use, validation and propagation are shown in the implementation trace. |
$asDefined |
bool $asDefined |
Operation input; the exact use, validation and propagation are shown in the implementation trace. |
Build the DEFAULT … clause, choosing whether to quote the value based on whether it is a known SQL keyword expression.
Return expressions (including local callbacks): " DEFAULT {$value}"; " DEFAULT '{$escaped}'".
Implementation, validation guards and interaction order
// -------------------------------------------------------------------------
// Helpers
// -------------------------------------------------------------------------
/**
* Build the DEFAULT … clause, choosing whether to quote the value based
* on whether it is a known SQL keyword expression.
*/
private function buildDefaultClause(string $value, bool $asDefined): string
{
if ($asDefined || in_array(strtoupper($value), self::UNQUOTED_DEFAULTS, true)) {
return " DEFAULT {$value}";
}
$escaped = str_replace("'", "''", $value);
return " DEFAULT '{$escaped}'";
}private function isNumericType(string $type): bool| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$type |
string $type |
Expected class/type or enum selection according to the signature. |
Return true when the SQL type keyword accepts the UNSIGNED modifier.
Return expressions (including local callbacks): in_array(strtoupper($type), [ 'TINYINT', 'SMALLINT', 'MEDIUMINT', 'INT', 'BIGINT', 'FLOAT', 'DOUBLE', 'DECIMAL', ], true).
Implementation, validation guards and interaction order
/**
* Return true when the SQL type keyword accepts the UNSIGNED modifier.
*/
private function isNumericType(string $type): bool
{
return in_array(strtoupper($type), [
'TINYINT', 'SMALLINT', 'MEDIUMINT', 'INT', 'BIGINT',
'FLOAT', 'DOUBLE', 'DECIMAL',
], true);
}AWT Framework Wiki · Home · Installation Guide · Developer Guide · Framework Internals
Philosophy · Architecture · Known discrepancies · Verification
Installation Guide
Developer Guide
- Learning sequence
- First package
- Package structure
- Lifecycle
- Runtimes
- Events and objects
- Routing and controllers
- Views and resources
- Database and models
- Storage and caching
- Configuration and CLI
- Responses and errors
- Extension points
Framework Internals
- Architecture
- Bootstrap
- Packages
- Runtime
- Events and context
- Objects
- HTTP routing and controllers
- Rendering
- Blade integration
- Database
- Models
- Storage and caching
- Settings
- CLI
- Identity and sessions
- Responses
- Errors
- Complete source inventory
Documentation notes