Support generated columns #2596

This commit is contained in:
Kijin Sung 2025-09-09 17:18:31 +09:00
parent 5e4b48f19b
commit 40067c0b53
5 changed files with 73 additions and 16 deletions

View file

@ -70,8 +70,21 @@ class DBTableParser extends BaseParser
// Load columns.
foreach ($xml->column as $column_info)
{
// Is this column generated?
$is_generated = strval($column_info['generated'] ?? '') !== '';
if ($is_generated)
{
$column = new DBTable\GeneratedColumn;
$column->generated = strtolower($column_info['generated']);
$column->is_stored = strtolower($column_info['stored'] ?? '');
$column->is_stored = $column->is_stored !== 'virtual' && toBool($column->is_stored);
}
else
{
$column = new DBTable\Column;
}
// Get the column name and type.
$column = new DBTable\Column;
$column->name = strval($column_info['name']);
list($column->type, $column->xetype, $column->size) = self::getTypeAndSize(strval($column_info['type']), strval($column_info['size']));

View file

@ -0,0 +1,12 @@
<?php
namespace Rhymix\Framework\Parsers\DBTable;
/**
* Generated column class.
*/
class GeneratedColumn extends Column
{
public $generated = 'always';
public $is_stored = false;
}

View file

@ -53,29 +53,42 @@ class Table
$columndef .= ' CHARACTER SET ' . $column->charset . ' COLLATE ' . $column->charset . '_general_ci';
}
}
if ($column instanceof GeneratedColumn)
{
$columndef .= ' GENERATED ' . strtoupper($column->generated ?: 'always');
$columndef .= ' AS (' . $column->default_value . ')';
if ($column->is_stored)
{
$columndef .= ' STORED';
}
}
else
{
if ($column->default_value !== null)
{
if (preg_match('/(?:int|float|double|decimal|number)/i', $column->type) && is_numeric($column->default_value))
{
$columndef .= ' DEFAULT ' . $column->default_value;
}
elseif (preg_match('/^\w+\(\)$/', $column->default_value))
{
$columndef .= ' DEFAULT ' . $column->default_value;
}
else
{
$columndef .= ' DEFAULT \'' . $column->default_value . '\'';
}
}
}
if ($column->not_null)
{
$columndef .= ' NOT NULL';
}
if ($column->default_value !== null)
{
if (preg_match('/(?:int|float|double|decimal|number)/i', $column->type) && is_numeric($column->default_value))
{
$columndef .= ' DEFAULT ' . $column->default_value;
}
elseif (preg_match('/^\w+\(\)$/', $column->default_value))
{
$columndef .= ' DEFAULT ' . $column->default_value;
}
else
{
$columndef .= ' DEFAULT \'' . $column->default_value . '\'';
}
}
if ($column->auto_increment)
{
$columndef .= ' AUTO_INCREMENT';
}
$columns[] = $columndef;
}