Files
ulmus/src/Entity/InformationSchema/Column.php
T

117 lines
3.5 KiB
PHP

<?php
namespace Ulmus\Entity\InformationSchema;
use Notes\Common\ReflectedProperty;
use Ulmus\Entity\Field\Datetime;
use Ulmus\{Attribute\Obj\Table, Entity\EntityInterface};
use Ulmus\Attribute\Property\{Field, Filter, FilterJoin, Relation, Join, Virtual, Where};
#[Table(name: "columns", database: "information_schema")]
class Column implements EntityInterface
{
use \Ulmus\EntityTrait;
#[Field(name: "TABLE_CATALOG", length: 512)]
public string $tableCatalog;
#[Field(name: "TABLE_SCHEMA", length: 64)]
public string $tableSchema;
#[Field(name: "TABLE_NAME", length: 64)]
public string $tableName;
#[Field(name: "COLUMN_NAME", length: 64, attributes: [ 'unsigned' => true, ])]
public string $name;
#[Field(name: "ORDINAL_POSITION", type: "bigint", length: 21, attributes: [ 'unsigned' => true, ])]
public int $ordinalPosition;
#[Field(name: "COLUMN_DEFAULT", type: "longtext")]
public ? string $default;
#[Field(name: "IS_NULLABLE", length: 3)]
public string $nullable;
#[Field(name: "DATA_TYPE", length: 64)]
public string $dataType;
#[Field(name: "CHARACTER_MAXIMUM_LENGTH", type: "bigint", length: 21, attributes: [ 'unsigned' => true, ])]
public ? int $characterMaximumLength;
#[Field(name: "CHARACTER_OCTET_LENGTH", type: "bigint", length: 21, attributes: [ 'unsigned' => true, ])]
public ? int $characterOctetLength;
#[Field(name: "NUMERIC_PRECISION", type: "bigint", length: 21, attributes: [ 'unsigned' => true, ])]
public ? int $numericPrecision;
#[Field(name: "NUMERIC_SCALE", type: "bigint", length: 21, attributes: [ 'unsigned' => true, ])]
public ? int $numericScale;
#[Field(name: "DATETIME_PRECISION", type: "bigint", length: 21, attributes: [ 'unsigned' => true, ])]
public ? int $datetimePrecision;
#[Field(name: "CHARACTER_SET_NAME", length: 32)]
public ? string $characterSetName;
#[Field(name: "COLLATION_NAME", length: 32)]
public ? string $collationName;
# #[Field(name: "COLLATION_TYPE", type: "longtext")]
# public string $type;
# #[Field(name: "IS_GENERATED", length: 6)]
# public string $generated;
public function matchFieldDefinition(ReflectedProperty $definition) : bool
{
$nullable = $this->nullable === 'YES';
$definitionValue = $this->getDefinitionValue($definition);
if ($nullable !== $definition->allowsNull()) {
return false;
}
if ( isset($definitionValue) && $this->canHaveDefaultValue() ) {
if ( $definitionValue !== $this->defaultValue() ) {
# dump($definition->value, $this->defaultValue(), $definition->name);
return false;
}
}
elseif (! isset($definitionValue)) {
if ( ! $this->defaultValueIsNull() ) {
return false;
}
}
return true;
}
protected function getDefinitionValue(ReflectedProperty $definition) : mixed
{
# Attribute's value first, then defined in class value
return $definition->getAttribute(Field::class)->object->default ?? $definition->value ?? null;
}
protected function defaultValueIsNull() : bool
{
return $this->defaultValue() === null;
}
protected function defaultValue() : mixed
{
if (is_numeric($this->default)) {
return (int) $this->default;
}
return $this->default;
}
protected function canHaveDefaultValue() : bool
{
return true;
}
}