- Fixed conflicts

This commit is contained in:
Dave Mc Nicoll committed 2022-04-13 03:38:57 +00:00
commit 325704260b
31 files changed
+632 -127

No files matched your search

+18 -65
View File
@@ -2,6 +2,7 @@
namespace Ulmus\Migration;
use Ulmus\Adapter\AdapterInterface;
use Ulmus\Annotation\Property\Field;
use Ulmus\Entity;
@@ -25,8 +26,12 @@ class FieldDefinition {
public ? string $update;
public function __construct(array $data)
public AdapterInterface $adapter;
public function __construct(AdapterInterface $adapter, array $data)
{
$this->adapter = $adapter;
$this->name = $data['name'];
$this->builtIn = $data['builtin'];
$this->tags = $data['tags'];
@@ -46,109 +51,57 @@ class FieldDefinition {
public function getSqlType(bool $typeOnly = false) : string
{
$type = $this->type;
$length = $this->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";
}
switch($type) {
case "bool":
$type = "TINYINT";
$length = 1;
break;
case "array":
case "string":
if ($length && $length <= 255) {
$type = "VARCHAR";
break;
}
elseif (! $length || ( $length <= 65535 ) ) {
$type = "TEXT";
}
elseif ( $length <= 16777215 ) {
$type = "MEDIUMTEXT";
}
elseif ($length <= 4294967295) {
$type = "LONGTEXT";
}
else {
throw new \Exception("A column with size bigger than 4GB cannot be created.");
}
# Length is unnecessary on TEXT fields
unset($length);
break;
case "float":
$type = "DOUBLE";
break;
default:
$type = strtoupper($type);
break;
}
return $typeOnly ? $type : $type . ( $length ? "($length" . ( $precision ? ",$precision" : "" ) . ")" : "" );
return $this->adapter->mapFieldType($this, $typeOnly);
}
public function getSqlParams() : string
{
$default = $this->getDefault();
$syntax = $this->adapter->tableSyntax();
return implode(' ', array_filter([
$this->isUnsigned() ? "UNSIGNED" : null,
$this->isUnsigned() ? $syntax['unsigned'] : null,
$this->nullable ? "NULL" : "NOT NULL",
$this->update ? "ON UPDATE {$this->update}" : null,
$default ? "DEFAULT $default" : null,
$this->isAutoIncrement() ? "AUTO_INCREMENT" : null,
$this->isPrimaryKey() ? "PRIMARY KEY" : null,
$this->isPrimaryKey() ? $syntax['pk'] : null,
$this->isAutoIncrement() ? $syntax['ai'] : null,
]));
}
protected function getFieldTag() : ? Field
public function getFieldTag() : ? Field
{
$field = array_filter($this->tags, fn($item) => $item['object'] instanceof Field);
return array_pop($field)['object'];
}
protected function getColumnName() : ? string
public function getColumnName() : ? string
{
return $this->getFieldTag()->name ?? $this->name;
}
protected function getDefault() : ? string
public function getDefault() : ? string
{
return $this->getFieldTag()->attributes['default'] ?? ( $this->nullable ? "NULL" : null ) ;
}
protected function isPrimaryKey() : bool
public function isPrimaryKey() : bool
{
return $this->getFieldTag()->attributes['primary_key'] ?? false;
}
protected function isAutoIncrement() : bool
public function isAutoIncrement() : bool
{
return $this->getFieldTag()->attributes['auto_increment'] ?? false;
}
protected function isUnique() : bool
public function isUnique() : bool
{
return $this->getFieldTag()->attributes['unique'] ?? false;
}
protected function isUnsigned() : bool
public function isUnsigned() : bool
{
return $this->getFieldTag()->attributes['unsigned'] ?? false;
}