99 lines
2.6 KiB
PHP
99 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace Ulmus\Adapter;
|
|
|
|
use Ulmus\Migration\FieldDefinition;
|
|
use Ulmus\Entity;
|
|
|
|
class SqlFieldMapper
|
|
{
|
|
public string $type;
|
|
|
|
public string $length;
|
|
|
|
public function __construct(
|
|
public FieldDefinition $field,
|
|
) {
|
|
$this->map();
|
|
}
|
|
|
|
public function map() : void
|
|
{
|
|
$type = $this->field->type;
|
|
$length = $this->field->length;
|
|
|
|
if ( is_a($type, Entity\Field\Date::class, true) ) {
|
|
$type = "DATE";
|
|
}
|
|
elseif ( is_a($type, Entity\Field\Time::class, true) ) {
|
|
$type = "TIME";
|
|
}
|
|
elseif ( is_a($type, \DateTime::class, true) ) {
|
|
$type = "DATETIME";
|
|
}
|
|
elseif ( enum_exists($type) ) {
|
|
# Haven't found a better way yet to check for BackendEnum without an instance of the object
|
|
if ( ! method_exists($type, 'tryFrom') ) {
|
|
throw new \Ulmus\Exception\BackedEnumRequired(sprintf("You must define your enum as a BackedEnum instead of an UnitEnum for field '%s'.", $this->field->getColumnName()));
|
|
}
|
|
|
|
$type = "string";
|
|
}
|
|
|
|
switch($type) {
|
|
case "bool":
|
|
$this->type = "TINYINT";
|
|
$this->length = 1;
|
|
break;
|
|
|
|
case "array":
|
|
$this->type = "JSON";
|
|
break;
|
|
|
|
case "string":
|
|
if ($length && $length <= 255) {
|
|
$this->type = "VARCHAR";
|
|
$this->length = $length;
|
|
break;
|
|
}
|
|
elseif (! $length || ( $length <= 65535 ) ) {
|
|
$this->type = "TEXT";
|
|
}
|
|
elseif ( $length <= 16777215 ) {
|
|
$this->type = "MEDIUMTEXT";
|
|
}
|
|
elseif ($length <= 4294967295) {
|
|
$this->type = "LONGTEXT";
|
|
}
|
|
else {
|
|
throw new \Exception("A column with a length bigger than 4GB cannot be created.");
|
|
}
|
|
|
|
# Length is unnecessary on TEXT fields
|
|
unset($length);
|
|
|
|
break;
|
|
|
|
case "float":
|
|
$this->type = "DOUBLE";
|
|
|
|
default:
|
|
if ($length) {
|
|
$this->length = $length;
|
|
}
|
|
|
|
$this->type ??= strtoupper($type);
|
|
}
|
|
|
|
$this->postProcess();
|
|
}
|
|
|
|
public function render() : string
|
|
{
|
|
return $this->type . ( isset($this->length) ? "($this->length)" : "" );
|
|
}
|
|
|
|
public function postProcess() : void
|
|
{
|
|
}
|
|
} |