- Field mapper is now removed from Adapters and set into it's own object
- Added a PK to Column (IS)
This commit is contained in:
1 parent
9977a25cf5
commit
3c2ae86653
7 files changed
+168
-57
No files matched your search
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
namespace Ulmus\Adapter;
|
||||
|
||||
use Ulmus\Migration\FieldDefinition;
|
||||
|
||||
class SqlFieldMapper
|
||||
{
|
||||
public readonly string $type;
|
||||
|
||||
public readonly 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";
|
||||
$length = 1;
|
||||
break;
|
||||
|
||||
case "array":
|
||||
case "string":
|
||||
if ($length && $length <= 255) {
|
||||
$this->type = "VARCHAR";
|
||||
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";
|
||||
break;
|
||||
|
||||
default:
|
||||
$this->type = strtoupper($type);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public function render() : string
|
||||
{
|
||||
return $this->type . ( isset($this->length) ? "($this->length)" : "" );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user