2022-06-17 20:53:35 +02:00
|
|
|
<?php
|
|
|
|
|
2022-11-20 17:13:53 +01:00
|
|
|
namespace Core\Objects\DatabaseEntity\Controller;
|
2022-11-18 18:06:46 +01:00
|
|
|
|
|
|
|
use Core\Driver\Logger\Logger;
|
|
|
|
use Core\Driver\SQL\Column\BoolColumn;
|
2022-11-20 17:13:53 +01:00
|
|
|
use Core\Driver\SQL\Column\Column;
|
2022-11-18 18:06:46 +01:00
|
|
|
use Core\Driver\SQL\Column\DateTimeColumn;
|
|
|
|
use Core\Driver\SQL\Column\EnumColumn;
|
|
|
|
use Core\Driver\SQL\Column\IntColumn;
|
|
|
|
use Core\Driver\SQL\Column\JsonColumn;
|
|
|
|
use Core\Driver\SQL\Column\StringColumn;
|
|
|
|
use Core\Driver\SQL\Condition\Compare;
|
2022-11-20 17:13:53 +01:00
|
|
|
use Core\Driver\SQL\Condition\CondAnd;
|
|
|
|
use Core\Driver\SQL\Condition\CondBool;
|
|
|
|
use Core\Driver\SQL\Condition\CondIn;
|
2022-11-18 18:06:46 +01:00
|
|
|
use Core\Driver\SQL\Condition\Condition;
|
|
|
|
use Core\Driver\SQL\Column\DoubleColumn;
|
|
|
|
use Core\Driver\SQL\Column\FloatColumn;
|
2022-11-20 17:13:53 +01:00
|
|
|
use Core\Driver\SQL\Condition\CondNot;
|
|
|
|
use Core\Driver\SQL\Condition\CondOr;
|
2022-11-18 18:06:46 +01:00
|
|
|
use Core\Driver\SQL\Constraint\ForeignKey;
|
2022-11-20 17:13:53 +01:00
|
|
|
use Core\Driver\SQL\Join;
|
|
|
|
use Core\Driver\SQL\Query\CreateProcedure;
|
2022-11-18 18:06:46 +01:00
|
|
|
use Core\Driver\SQL\Query\CreateTable;
|
2022-11-20 17:13:53 +01:00
|
|
|
use Core\Driver\SQL\Query\Insert;
|
2022-11-18 18:06:46 +01:00
|
|
|
use Core\Driver\SQL\Query\Select;
|
|
|
|
use Core\Driver\SQL\SQL;
|
|
|
|
use Core\Driver\SQL\Strategy\CascadeStrategy;
|
|
|
|
use Core\Driver\SQL\Strategy\SetNullStrategy;
|
2022-11-20 17:13:53 +01:00
|
|
|
use Core\Driver\SQL\Strategy\UpdateStrategy;
|
|
|
|
use Core\Driver\SQL\Type\CurrentColumn;
|
|
|
|
use Core\Driver\SQL\Type\CurrentTable;
|
2022-11-18 18:06:46 +01:00
|
|
|
use Core\Objects\DatabaseEntity\Attribute\Enum;
|
|
|
|
use Core\Objects\DatabaseEntity\Attribute\DefaultValue;
|
|
|
|
use Core\Objects\DatabaseEntity\Attribute\Json;
|
|
|
|
use Core\Objects\DatabaseEntity\Attribute\MaxLength;
|
2022-11-20 17:13:53 +01:00
|
|
|
use Core\Objects\DatabaseEntity\Attribute\Multiple;
|
2022-11-18 18:06:46 +01:00
|
|
|
use Core\Objects\DatabaseEntity\Attribute\Transient;
|
|
|
|
use Core\Objects\DatabaseEntity\Attribute\Unique;
|
2022-06-17 20:53:35 +02:00
|
|
|
use PHPUnit\Util\Exception;
|
|
|
|
|
2022-11-20 17:13:53 +01:00
|
|
|
class DatabaseEntityHandler implements Persistable {
|
2022-06-17 20:53:35 +02:00
|
|
|
|
|
|
|
private \ReflectionClass $entityClass;
|
|
|
|
private string $tableName;
|
|
|
|
private array $columns;
|
|
|
|
private array $properties;
|
2022-06-17 23:29:24 +02:00
|
|
|
private array $relations;
|
2022-06-20 19:52:31 +02:00
|
|
|
private array $constraints;
|
2022-11-20 17:13:53 +01:00
|
|
|
private array $nmRelations;
|
2022-06-17 22:58:42 +02:00
|
|
|
private SQL $sql;
|
|
|
|
private Logger $logger;
|
2022-06-17 20:53:35 +02:00
|
|
|
|
2022-06-17 22:58:42 +02:00
|
|
|
public function __construct(SQL $sql, \ReflectionClass $entityClass) {
|
|
|
|
$this->sql = $sql;
|
2022-06-17 20:53:35 +02:00
|
|
|
$className = $entityClass->getName();
|
2022-06-17 22:58:42 +02:00
|
|
|
$this->logger = new Logger($entityClass->getShortName(), $sql);
|
2022-06-17 20:53:35 +02:00
|
|
|
$this->entityClass = $entityClass;
|
2022-06-20 19:52:31 +02:00
|
|
|
if (!$this->entityClass->isSubclassOf(DatabaseEntity::class)) {
|
2022-06-17 22:58:42 +02:00
|
|
|
$this->raiseError("Cannot persist class '$className': Not an instance of DatabaseEntity or not instantiable.");
|
2022-06-17 20:53:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->tableName = $this->entityClass->getShortName();
|
2022-06-20 19:52:31 +02:00
|
|
|
$this->columns = []; // property name => database column name
|
|
|
|
$this->properties = []; // property name => \ReflectionProperty
|
2022-11-20 17:13:53 +01:00
|
|
|
$this->relations = []; // property name => DatabaseEntityHandler
|
2022-06-20 19:52:31 +02:00
|
|
|
$this->constraints = []; // \Driver\SQL\Constraint\Constraint
|
2022-11-20 17:13:53 +01:00
|
|
|
$this->nmRelations = []; // table name => NMRelation
|
2022-06-17 23:29:24 +02:00
|
|
|
|
2022-06-17 20:53:35 +02:00
|
|
|
foreach ($this->entityClass->getProperties() as $property) {
|
|
|
|
$propertyName = $property->getName();
|
2022-06-20 19:52:31 +02:00
|
|
|
if ($propertyName === "id") {
|
|
|
|
$this->properties[$propertyName] = $property;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-11-20 17:13:53 +01:00
|
|
|
if ($property->isStatic()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-06-17 20:53:35 +02:00
|
|
|
$propertyType = $property->getType();
|
|
|
|
$columnName = self::getColumnName($propertyName);
|
|
|
|
if (!($propertyType instanceof \ReflectionNamedType)) {
|
2022-06-17 22:58:42 +02:00
|
|
|
$this->raiseError("Cannot persist class '$className': Property '$propertyName' has no valid type");
|
2022-06-17 20:53:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$nullable = $propertyType->allowsNull();
|
|
|
|
$propertyTypeName = $propertyType->getName();
|
2022-06-20 19:52:31 +02:00
|
|
|
if (!empty($property->getAttributes(Transient::class))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$defaultValue = (self::getAttribute($property, DefaultValue::class))?->getValue();
|
|
|
|
$isUnique = !empty($property->getAttributes(Unique::class));
|
|
|
|
|
2022-06-17 20:53:35 +02:00
|
|
|
if ($propertyTypeName === 'string') {
|
2022-06-20 19:52:31 +02:00
|
|
|
$enum = self::getAttribute($property, Enum::class);
|
|
|
|
if ($enum) {
|
|
|
|
$this->columns[$propertyName] = new EnumColumn($columnName, $enum->getValues(), $nullable, $defaultValue);
|
|
|
|
} else {
|
|
|
|
$maxLength = self::getAttribute($property, MaxLength::class);
|
|
|
|
$this->columns[$propertyName] = new StringColumn($columnName, $maxLength?->getValue(), $nullable, $defaultValue);
|
|
|
|
}
|
2022-06-17 20:53:35 +02:00
|
|
|
} else if ($propertyTypeName === 'int') {
|
2022-06-20 19:52:31 +02:00
|
|
|
$this->columns[$propertyName] = new IntColumn($columnName, $nullable, $defaultValue);
|
2022-06-17 20:53:35 +02:00
|
|
|
} else if ($propertyTypeName === 'float') {
|
2022-06-20 19:52:31 +02:00
|
|
|
$this->columns[$propertyName] = new FloatColumn($columnName, $nullable, $defaultValue);
|
2022-06-17 20:53:35 +02:00
|
|
|
} else if ($propertyTypeName === 'double') {
|
2022-06-20 19:52:31 +02:00
|
|
|
$this->columns[$propertyName] = new DoubleColumn($columnName, $nullable, $defaultValue);
|
2022-06-17 20:53:35 +02:00
|
|
|
} else if ($propertyTypeName === 'bool') {
|
2022-06-20 19:52:31 +02:00
|
|
|
$this->columns[$propertyName] = new BoolColumn($columnName, $defaultValue ?? false);
|
2022-06-17 20:53:35 +02:00
|
|
|
} else if ($propertyTypeName === 'DateTime') {
|
2022-06-20 19:52:31 +02:00
|
|
|
$this->columns[$propertyName] = new DateTimeColumn($columnName, $nullable, $defaultValue);
|
2022-11-20 17:13:53 +01:00
|
|
|
/*} else if ($propertyName === 'array') {
|
|
|
|
$many = self::getAttribute($property, Many::class);
|
|
|
|
if ($many) {
|
|
|
|
$requestedType = $many->getValue();
|
|
|
|
if (isClass($requestedType)) {
|
|
|
|
$requestedClass = new \ReflectionClass($requestedType);
|
|
|
|
} else {
|
|
|
|
$this->raiseError("Cannot persist class '$className': Property '$propertyName' has non persist-able type: $requestedType");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->raiseError("Cannot persist class '$className': Property '$propertyName' has non persist-able type: $propertyTypeName");
|
|
|
|
}*/
|
|
|
|
} else if ($propertyTypeName === "array") {
|
|
|
|
$multiple = self::getAttribute($property, Multiple::class);
|
|
|
|
if (!$multiple) {
|
|
|
|
$this->raiseError("Cannot persist class '$className': Property '$propertyName' has non persist-able type: $propertyTypeName. " .
|
|
|
|
"Is the 'Multiple' attribute missing?");
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$refClass = $multiple->getClassName();
|
|
|
|
$requestedClass = new \ReflectionClass($refClass);
|
|
|
|
if ($requestedClass->isSubclassOf(DatabaseEntity::class)) {
|
|
|
|
$nmTableName = NMRelation::buildTableName($this->getTableName(), $requestedClass->getShortName());
|
|
|
|
$nmRelation = $this->nmRelations[$nmTableName] ?? null;
|
|
|
|
if (!$nmRelation) {
|
|
|
|
$otherHandler = DatabaseEntity::getHandler($this->sql, $requestedClass);
|
|
|
|
$otherNM = $otherHandler->getNMRelations();
|
|
|
|
$nmRelation = $otherNM[$nmTableName] ?? (new NMRelation($this, $otherHandler));
|
|
|
|
$this->nmRelations[$nmTableName] = $nmRelation;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->nmRelations[$nmTableName]->addProperty($this, $property);
|
2022-06-20 19:52:31 +02:00
|
|
|
} else {
|
2022-11-20 17:13:53 +01:00
|
|
|
$this->raiseError("Cannot persist class '$className': Property '$propertyName' of type multiple can " .
|
|
|
|
"only reference DatabaseEntity types, but got: $refClass");
|
2022-06-20 19:52:31 +02:00
|
|
|
}
|
2022-11-20 17:13:53 +01:00
|
|
|
} catch (\Exception $ex) {
|
|
|
|
$this->raiseError("Cannot persist class '$className' property '$propertyTypeName': " . $ex->getMessage());
|
|
|
|
}
|
2022-06-20 19:52:31 +02:00
|
|
|
} else if ($propertyTypeName !== "mixed") {
|
2022-06-17 20:53:35 +02:00
|
|
|
try {
|
|
|
|
$requestedClass = new \ReflectionClass($propertyTypeName);
|
|
|
|
if ($requestedClass->isSubclassOf(DatabaseEntity::class)) {
|
2022-06-20 19:52:31 +02:00
|
|
|
$columnName .= "_id";
|
2022-06-17 20:53:35 +02:00
|
|
|
$requestedHandler = ($requestedClass->getName() === $this->entityClass->getName()) ?
|
2022-06-17 22:58:42 +02:00
|
|
|
$this : DatabaseEntity::getHandler($this->sql, $requestedClass);
|
2022-06-17 20:53:35 +02:00
|
|
|
$strategy = $nullable ? new SetNullStrategy() : new CascadeStrategy();
|
2022-06-20 19:52:31 +02:00
|
|
|
$this->columns[$propertyName] = new IntColumn($columnName, $nullable, $defaultValue);
|
|
|
|
$this->constraints[] = new ForeignKey($columnName, $requestedHandler->tableName, "id", $strategy);
|
|
|
|
$this->relations[$propertyName] = $requestedHandler;
|
|
|
|
} else {
|
|
|
|
$this->raiseError("Cannot persist class '$className': Property '$propertyName' has non persist-able type: $propertyTypeName");
|
2022-06-17 20:53:35 +02:00
|
|
|
}
|
|
|
|
} catch (\Exception $ex) {
|
2022-06-20 19:52:31 +02:00
|
|
|
$this->raiseError("Cannot persist class '$className' property '$propertyTypeName': " . $ex->getMessage());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!empty($property->getAttributes(Json::class))) {
|
|
|
|
$this->columns[$propertyName] = new JsonColumn($columnName, $nullable, $defaultValue);
|
|
|
|
} else {
|
2022-06-17 22:58:42 +02:00
|
|
|
$this->raiseError("Cannot persist class '$className': Property '$propertyName' has non persist-able type: $propertyTypeName");
|
2022-06-17 20:53:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->properties[$propertyName] = $property;
|
2022-06-20 19:52:31 +02:00
|
|
|
|
|
|
|
if ($isUnique) {
|
2022-11-18 18:06:46 +01:00
|
|
|
$this->constraints[] = new \Core\Driver\SQL\Constraint\Unique($columnName);
|
2022-06-20 19:52:31 +02:00
|
|
|
}
|
2022-06-17 20:53:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-20 19:52:31 +02:00
|
|
|
private static function getAttribute(\ReflectionProperty $property, string $attributeClass): ?object {
|
|
|
|
$attributes = $property->getAttributes($attributeClass);
|
|
|
|
$attribute = array_shift($attributes);
|
|
|
|
return $attribute?->newInstance();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getColumnName(string $propertyName): string {
|
2022-06-17 20:53:35 +02:00
|
|
|
// abcTestLOL => abc_test_lol
|
|
|
|
return strtolower(preg_replace_callback("/([a-z])([A-Z]+)/", function ($m) {
|
|
|
|
return $m[1] . "_" . strtolower($m[2]);
|
|
|
|
}, $propertyName));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getReflection(): \ReflectionClass {
|
|
|
|
return $this->entityClass;
|
|
|
|
}
|
|
|
|
|
2022-06-17 22:58:42 +02:00
|
|
|
public function getLogger(): Logger {
|
|
|
|
return $this->logger;
|
|
|
|
}
|
|
|
|
|
2022-06-17 23:29:24 +02:00
|
|
|
public function getTableName(): string {
|
|
|
|
return $this->tableName;
|
|
|
|
}
|
|
|
|
|
2022-06-20 19:52:31 +02:00
|
|
|
public function getRelations(): array {
|
|
|
|
return $this->relations;
|
|
|
|
}
|
|
|
|
|
2022-11-20 17:13:53 +01:00
|
|
|
public function getNMRelations(): array {
|
|
|
|
return $this->nmRelations;
|
|
|
|
}
|
|
|
|
|
2022-06-20 19:52:31 +02:00
|
|
|
public function getColumnNames(): array {
|
|
|
|
$columns = ["$this->tableName.id"];
|
|
|
|
foreach ($this->columns as $column) {
|
|
|
|
$columns[] = $this->tableName . "." . $column->getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $columns;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getColumns(): array {
|
|
|
|
return $this->columns;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dependsOn(): array {
|
|
|
|
$foreignTables = array_map(function (DatabaseEntityHandler $relationHandler) {
|
|
|
|
return $relationHandler->getTableName();
|
|
|
|
}, $this->relations);
|
|
|
|
return array_unique($foreignTables);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getPrefixedRow(array $row, string $prefix): array {
|
|
|
|
$rel_row = [];
|
|
|
|
foreach ($row as $relKey => $relValue) {
|
|
|
|
if (startsWith($relKey, $prefix)) {
|
|
|
|
$rel_row[substr($relKey, strlen($prefix))] = $relValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $rel_row;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function entityFromRow(array $row): ?DatabaseEntity {
|
2022-06-17 22:58:42 +02:00
|
|
|
try {
|
2022-06-20 19:52:31 +02:00
|
|
|
|
|
|
|
$entity = call_user_func($this->entityClass->getName() . "::newInstance", $this->entityClass, $row);
|
|
|
|
if (!($entity instanceof DatabaseEntity)) {
|
|
|
|
$this->logger->error("Created Object is not of type DatabaseEntity");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-06-17 22:58:42 +02:00
|
|
|
foreach ($this->columns as $propertyName => $column) {
|
2022-06-20 19:52:31 +02:00
|
|
|
$columnName = $column->getName();
|
|
|
|
if (array_key_exists($columnName, $row)) {
|
|
|
|
$value = $row[$columnName];
|
|
|
|
$property = $this->properties[$propertyName];
|
|
|
|
|
|
|
|
if ($column instanceof DateTimeColumn) {
|
|
|
|
$value = new \DateTime($value);
|
|
|
|
} else if ($column instanceof JsonColumn) {
|
|
|
|
$value = json_decode($value);
|
|
|
|
} else if (isset($this->relations[$propertyName])) {
|
|
|
|
$relColumnPrefix = self::getColumnName($propertyName) . "_";
|
|
|
|
if (array_key_exists($relColumnPrefix . "id", $row)) {
|
|
|
|
$relId = $row[$relColumnPrefix . "id"];
|
|
|
|
if ($relId !== null) {
|
|
|
|
$relationHandler = $this->relations[$propertyName];
|
|
|
|
$value = $relationHandler->entityFromRow(self::getPrefixedRow($row, $relColumnPrefix));
|
|
|
|
} else if (!$column->notNull()) {
|
|
|
|
$value = null;
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2022-06-17 23:29:24 +02:00
|
|
|
|
2022-06-20 19:52:31 +02:00
|
|
|
$property->setAccessible(true);
|
|
|
|
$property->setValue($entity, $value);
|
2022-06-17 23:29:24 +02:00
|
|
|
}
|
2022-06-17 22:58:42 +02:00
|
|
|
}
|
2022-06-17 23:29:24 +02:00
|
|
|
|
2022-11-20 17:13:53 +01:00
|
|
|
// init n:m / 1:n properties with empty arrays
|
|
|
|
foreach ($this->nmRelations as $nmRelation) {
|
|
|
|
foreach ($nmRelation->getProperties($this) as $property) {
|
|
|
|
$property->setAccessible(true);
|
|
|
|
$property->setValue($entity, []);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-20 19:52:31 +02:00
|
|
|
$this->properties["id"]->setAccessible(true);
|
|
|
|
$this->properties["id"]->setValue($entity, $row["id"]);
|
|
|
|
$entity->postFetch($this->sql, $row);
|
2022-06-17 22:58:42 +02:00
|
|
|
return $entity;
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
$this->logger->error("Error creating entity from database row: " . $exception->getMessage());
|
2022-06-20 19:52:31 +02:00
|
|
|
return null;
|
2022-06-17 20:53:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-20 17:13:53 +01:00
|
|
|
public function updateNM(DatabaseEntity $entity): bool {
|
|
|
|
if (empty($this->nmRelations)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->nmRelations as $nmTable => $nmRelation) {
|
|
|
|
|
|
|
|
$thisIdColumn = $nmRelation->getIdColumn($this);
|
|
|
|
$thisTableName = $this->getTableName();
|
|
|
|
$dataColumns = $nmRelation->getDataColumns();
|
|
|
|
$otherHandler = $nmRelation->getOtherHandler($this);
|
|
|
|
$refIdColumn = $nmRelation->getIdColumn($otherHandler);
|
|
|
|
|
|
|
|
|
|
|
|
// delete from n:m table if no longer exists
|
|
|
|
$deleteStatement = $this->sql->delete($nmTable)
|
|
|
|
->where(new Compare($thisIdColumn, $entity->getId()));
|
|
|
|
|
|
|
|
if (!empty($dataColumns)) {
|
|
|
|
$conditions = [];
|
|
|
|
foreach ($dataColumns[$thisTableName] as $propertyName => $columnName) {
|
|
|
|
$property = $this->properties[$propertyName];
|
|
|
|
$entityIds = array_keys($property->getValue($entity));
|
|
|
|
if (!empty($entityIds)) {
|
|
|
|
$conditions[] = new CondAnd(
|
|
|
|
new CondBool($columnName),
|
|
|
|
new CondNot(new CondIn(new Column($refIdColumn), $entityIds)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($conditions)) {
|
|
|
|
$deleteStatement->where(new CondOr(...$conditions));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$property = next($nmRelation->getProperties($this));
|
|
|
|
$entityIds = array_keys($property->getValue($entity));
|
|
|
|
if (!empty($entityIds)) {
|
|
|
|
$deleteStatement->where(
|
|
|
|
new CondNot(new CondIn(new Column($refIdColumn), $entityIds))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$deleteStatement->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->insertNM($entity, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function insertNM(DatabaseEntity $entity, bool $ignoreExisting = true): bool {
|
|
|
|
|
|
|
|
if (empty($this->nmRelations)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$success = true;
|
|
|
|
foreach ($this->nmRelations as $nmTable => $nmRelation) {
|
|
|
|
$otherHandler = $nmRelation->getOtherHandler($this);
|
|
|
|
$thisIdColumn = $nmRelation->getIdColumn($this);
|
|
|
|
$thisTableName = $this->getTableName();
|
|
|
|
$refIdColumn = $nmRelation->getIdColumn($otherHandler);
|
|
|
|
$dataColumns = $nmRelation->getDataColumns();
|
|
|
|
|
|
|
|
$columns = [
|
|
|
|
$thisIdColumn,
|
|
|
|
$refIdColumn,
|
|
|
|
];
|
|
|
|
|
|
|
|
if (!empty($dataColumns)) {
|
|
|
|
$columns = array_merge($columns, array_values($dataColumns[$thisTableName]));
|
|
|
|
}
|
|
|
|
|
|
|
|
$statement = $this->sql->insert($nmTable, $columns);
|
|
|
|
if ($ignoreExisting) {
|
|
|
|
$statement->onDuplicateKeyStrategy(new UpdateStrategy($nmRelation->getAllColumns(), [
|
|
|
|
$thisIdColumn => $entity->getId()
|
|
|
|
]));
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($nmRelation->getProperties($this) as $property) {
|
|
|
|
$property->setAccessible(true);
|
|
|
|
$relEntities = $property->getValue($entity);
|
|
|
|
foreach ($relEntities as $relEntity) {
|
|
|
|
$nmRow = [$entity->getId(), $relEntity->getId()];
|
|
|
|
if (!empty($dataColumns)) {
|
|
|
|
foreach (array_keys($dataColumns[$thisTableName]) as $propertyName) {
|
|
|
|
$nmRow[] = $property->getName() === $propertyName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$statement->addRow(...$nmRow);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$success = $statement->execute() && $success;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $success;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function fetchNMRelations(array $entities, bool $recursive = false) {
|
|
|
|
|
|
|
|
if ($recursive) {
|
|
|
|
foreach ($entities as $entity) {
|
|
|
|
foreach ($this->relations as $propertyName => $relHandler) {
|
|
|
|
$relEntity = $this->properties[$propertyName]->getValue($entity);
|
|
|
|
if ($relEntity) {
|
|
|
|
$relHandler->fetchNMRelations([$relEntity->getId() => $relEntity], true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($this->nmRelations)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$entityIds = array_keys($entities);
|
|
|
|
foreach ($this->nmRelations as $nmTable => $nmRelation) {
|
|
|
|
$otherHandler = $nmRelation->getOtherHandler($this);
|
|
|
|
|
|
|
|
$thisIdColumn = $nmRelation->getIdColumn($this);
|
|
|
|
$thisProperties = $nmRelation->getProperties($this);
|
|
|
|
$thisTableName = $this->getTableName();
|
|
|
|
|
|
|
|
$refIdColumn = $nmRelation->getIdColumn($otherHandler);
|
|
|
|
$refProperties = $nmRelation->getProperties($otherHandler);
|
|
|
|
$refTableName = $otherHandler->getTableName();
|
|
|
|
|
|
|
|
$dataColumns = $nmRelation->getDataColumns();
|
|
|
|
|
|
|
|
$relEntityQuery = DatabaseEntityQuery::fetchAll($otherHandler)
|
|
|
|
->addJoin(new Join("INNER", $nmTable, "$nmTable.$refIdColumn", "$refTableName.id"))
|
|
|
|
->where(new CondIn(new Column($thisIdColumn), $entityIds))
|
|
|
|
->getQuery();
|
|
|
|
|
|
|
|
$relEntityQuery->addColumn($thisIdColumn);
|
|
|
|
foreach ($dataColumns as $tableDataColumns) {
|
|
|
|
foreach ($tableDataColumns as $columnName) {
|
|
|
|
$relEntityQuery->addColumn($columnName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$rows = $relEntityQuery->execute();
|
|
|
|
if (!is_array($rows)) {
|
|
|
|
$this->logger->error("Error fetching n:m relations from table: '$nmTable': " . $this->sql->getLastError());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$relEntities = [];
|
|
|
|
foreach ($rows as $row) {
|
|
|
|
$relId = $row["id"];
|
|
|
|
if (!isset($relEntities[$relId])) {
|
|
|
|
$relEntity = $otherHandler->entityFromRow($row);
|
|
|
|
$relEntities[$relId] = $relEntity;
|
|
|
|
}
|
|
|
|
|
|
|
|
$thisEntity = $entities[$row[$thisIdColumn]];
|
|
|
|
$relEntity = $relEntities[$relId];
|
|
|
|
$mappings = [
|
|
|
|
[$refProperties, $refTableName, $relEntity, $thisEntity],
|
|
|
|
[$thisProperties, $thisTableName, $thisEntity, $relEntity],
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($mappings as $mapping) {
|
|
|
|
list($properties, $tableName, $targetEntity, $entityToAdd) = $mapping;
|
|
|
|
foreach ($properties as $propertyName => $property) {
|
|
|
|
$addToProperty = empty($dataColumns);
|
|
|
|
if (!$addToProperty) {
|
|
|
|
$columnName = $dataColumns[$tableName][$propertyName] ?? null;
|
|
|
|
$addToProperty = ($columnName && $this->sql->parseBool($row[$columnName]));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($addToProperty) {
|
|
|
|
$targetArray = $property->getValue($targetEntity);
|
|
|
|
$targetArray[$entityToAdd->getId()] = $entityToAdd;
|
|
|
|
$property->setValue($targetEntity, $targetArray);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-20 19:52:31 +02:00
|
|
|
public function getSelectQuery(): Select {
|
|
|
|
return $this->sql->select(...$this->getColumnNames())
|
|
|
|
->from($this->tableName);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function fetchOne(int $id): DatabaseEntity|bool|null {
|
|
|
|
$res = $this->getSelectQuery()
|
|
|
|
->where(new Compare($this->tableName . ".id", $id))
|
2022-06-17 20:53:35 +02:00
|
|
|
->first()
|
|
|
|
->execute();
|
|
|
|
|
2022-11-20 17:13:53 +01:00
|
|
|
if ($res !== false && $res !== null) {
|
|
|
|
$res = $this->entityFromRow($res);
|
|
|
|
if ($res instanceof DatabaseEntity) {
|
|
|
|
$this->fetchNMRelations([$res->getId() => $res]);
|
|
|
|
}
|
2022-06-17 20:53:35 +02:00
|
|
|
}
|
2022-11-20 17:13:53 +01:00
|
|
|
|
|
|
|
return $res;
|
2022-06-17 20:53:35 +02:00
|
|
|
}
|
|
|
|
|
2022-06-17 22:58:42 +02:00
|
|
|
public function fetchMultiple(?Condition $condition = null): ?array {
|
2022-06-20 19:52:31 +02:00
|
|
|
$query = $this->getSelectQuery();
|
2022-06-17 20:53:35 +02:00
|
|
|
|
|
|
|
if ($condition) {
|
|
|
|
$query->where($condition);
|
|
|
|
}
|
|
|
|
|
|
|
|
$res = $query->execute();
|
|
|
|
if ($res === false) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
$entities = [];
|
|
|
|
foreach ($res as $row) {
|
2022-06-20 19:52:31 +02:00
|
|
|
$entity = $this->entityFromRow($row);
|
|
|
|
if ($entity) {
|
|
|
|
$entities[$entity->getId()] = $entity;
|
|
|
|
}
|
2022-06-17 20:53:35 +02:00
|
|
|
}
|
2022-11-20 17:13:53 +01:00
|
|
|
|
|
|
|
$this->fetchNMRelations($entities);
|
2022-06-17 20:53:35 +02:00
|
|
|
return $entities;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-20 17:13:53 +01:00
|
|
|
public function getCreateQueries(SQL $sql): array {
|
|
|
|
|
|
|
|
$queries = [];
|
|
|
|
$queries[] = $this->getTableQuery($sql);
|
|
|
|
|
|
|
|
$table = $this->getTableName();
|
|
|
|
$entityLogConfig = $this->entityClass->getProperty("entityLogConfig");
|
|
|
|
$entityLogConfig->setAccessible(true);
|
|
|
|
$entityLogConfig = $entityLogConfig->getValue();
|
|
|
|
|
|
|
|
if (isset($entityLogConfig["insert"]) && $entityLogConfig["insert"] === true) {
|
|
|
|
$queries[] = $sql->createTrigger("${table}_trg_insert")
|
|
|
|
->after()->insert($table)
|
|
|
|
->exec(new CreateProcedure($sql, "InsertEntityLog"), [
|
|
|
|
"tableName" => new CurrentTable(),
|
|
|
|
"entityId" => new CurrentColumn("id"),
|
|
|
|
"lifetime" => $entityLogConfig["lifetime"] ?? 90,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($entityLogConfig["update"]) && $entityLogConfig["update"] === true) {
|
|
|
|
$queries[] = $sql->createTrigger("${table}_trg_update")
|
|
|
|
->after()->update($table)
|
|
|
|
->exec(new CreateProcedure($sql, "UpdateEntityLog"), [
|
|
|
|
"tableName" => new CurrentTable(),
|
|
|
|
"entityId" => new CurrentColumn("id"),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($entityLogConfig["delete"]) && $entityLogConfig["delete"] === true) {
|
|
|
|
$queries[] = $sql->createTrigger("${table}_trg_delete")
|
|
|
|
->after()->delete($table)
|
|
|
|
->exec(new CreateProcedure($sql, "DeleteEntityLog"), [
|
|
|
|
"tableName" => new CurrentTable(),
|
|
|
|
"entityId" => new CurrentColumn("id"),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return $queries;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTableQuery(SQL $sql): CreateTable {
|
|
|
|
$query = $sql->createTable($this->tableName)
|
2022-06-17 20:53:35 +02:00
|
|
|
->onlyIfNotExists()
|
|
|
|
->addSerial("id")
|
|
|
|
->primaryKey("id");
|
|
|
|
|
|
|
|
foreach ($this->columns as $column) {
|
|
|
|
$query->addColumn($column);
|
|
|
|
}
|
|
|
|
|
2022-06-20 19:52:31 +02:00
|
|
|
foreach ($this->constraints as $constraint) {
|
2022-06-17 20:53:35 +02:00
|
|
|
$query->addConstraint($constraint);
|
|
|
|
}
|
|
|
|
|
2022-06-17 22:58:42 +02:00
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
2022-11-20 17:13:53 +01:00
|
|
|
private function prepareRow(DatabaseEntity $entity, string $action, ?array $columns = null): bool|array {
|
2022-06-20 19:52:31 +02:00
|
|
|
$row = [];
|
|
|
|
foreach ($this->columns as $propertyName => $column) {
|
|
|
|
if ($columns && !in_array($column->getName(), $columns)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$property = $this->properties[$propertyName];
|
|
|
|
$property->setAccessible(true);
|
|
|
|
if ($property->isInitialized($entity)) {
|
|
|
|
$value = $property->getValue($entity);
|
|
|
|
if (isset($this->relations[$propertyName])) {
|
2022-11-19 01:15:34 +01:00
|
|
|
$value = $value?->getId();
|
2022-06-20 19:52:31 +02:00
|
|
|
}
|
|
|
|
} else if (!$this->columns[$propertyName]->notNull()) {
|
|
|
|
$value = null;
|
|
|
|
} else {
|
2022-08-20 22:17:17 +02:00
|
|
|
$defaultValue = self::getAttribute($property, DefaultValue::class);
|
|
|
|
if ($defaultValue) {
|
|
|
|
$value = $defaultValue->getValue();
|
|
|
|
} else if ($action !== "update") {
|
2022-06-20 19:52:31 +02:00
|
|
|
$this->logger->error("Cannot $action entity: property '$propertyName' was not initialized yet.");
|
2022-06-17 23:29:24 +02:00
|
|
|
return false;
|
2022-06-20 19:52:31 +02:00
|
|
|
} else {
|
|
|
|
continue;
|
2022-06-17 23:29:24 +02:00
|
|
|
}
|
2022-06-17 20:53:35 +02:00
|
|
|
}
|
|
|
|
|
2022-06-20 19:52:31 +02:00
|
|
|
$row[$column->getName()] = $value;
|
|
|
|
}
|
|
|
|
|
2022-08-20 22:17:17 +02:00
|
|
|
return $row;
|
|
|
|
}
|
|
|
|
|
2022-11-20 17:13:53 +01:00
|
|
|
public function update(DatabaseEntity $entity, ?array $columns = null, bool $saveNM = false) {
|
2022-08-20 22:17:17 +02:00
|
|
|
$row = $this->prepareRow($entity, "update", $columns);
|
|
|
|
if ($row === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-06-20 19:52:31 +02:00
|
|
|
$entity->preInsert($row);
|
2022-08-20 22:17:17 +02:00
|
|
|
$query = $this->sql->update($this->tableName)
|
|
|
|
->where(new Compare($this->tableName . ".id", $entity->getId()));
|
2022-06-20 19:52:31 +02:00
|
|
|
|
2022-08-20 22:17:17 +02:00
|
|
|
foreach ($row as $columnName => $value) {
|
|
|
|
$query->set($columnName, $value);
|
|
|
|
}
|
2022-06-20 19:52:31 +02:00
|
|
|
|
2022-11-20 17:13:53 +01:00
|
|
|
$res = $query->execute();
|
|
|
|
if ($res && $saveNM) {
|
|
|
|
$res = $this->updateNM($entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $res;
|
2022-08-20 22:17:17 +02:00
|
|
|
}
|
2022-06-17 20:53:35 +02:00
|
|
|
|
2022-11-20 17:13:53 +01:00
|
|
|
public function insert(DatabaseEntity $entity): bool|int {
|
2022-08-20 22:17:17 +02:00
|
|
|
$row = $this->prepareRow($entity, "insert");
|
|
|
|
if ($row === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-06-17 23:29:24 +02:00
|
|
|
|
2022-08-20 22:17:17 +02:00
|
|
|
$entity->preInsert($row);
|
|
|
|
|
|
|
|
// insert with id?
|
|
|
|
$entityId = $entity->getId();
|
|
|
|
if ($entityId !== null) {
|
|
|
|
$row["id"] = $entityId;
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = $this->sql->insert($this->tableName, array_keys($row))
|
|
|
|
->addRow(...array_values($row));
|
2022-06-17 20:53:35 +02:00
|
|
|
|
2022-08-20 22:17:17 +02:00
|
|
|
// return id if its auto-generated
|
|
|
|
if ($entityId === null) {
|
|
|
|
$query->returning("id");
|
|
|
|
}
|
|
|
|
|
|
|
|
$res = $query->execute();
|
|
|
|
if ($res !== false) {
|
|
|
|
return $this->sql->getLastInsertId();
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-20 17:13:53 +01:00
|
|
|
public function insertOrUpdate(DatabaseEntity $entity, ?array $columns = null, bool $saveNM = false) {
|
2022-08-20 22:17:17 +02:00
|
|
|
$id = $entity->getId();
|
|
|
|
if ($id === null) {
|
|
|
|
return $this->insert($entity);
|
|
|
|
} else {
|
2022-11-20 17:13:53 +01:00
|
|
|
return $this->update($entity, $columns, $saveNM);
|
2022-06-17 20:53:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-17 22:58:42 +02:00
|
|
|
public function delete(int $id) {
|
2022-06-20 19:52:31 +02:00
|
|
|
return $this->sql
|
|
|
|
->delete($this->tableName)
|
|
|
|
->where(new Compare($this->tableName . ".id", $id))
|
|
|
|
->execute();
|
2022-06-17 22:58:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function raiseError(string $message) {
|
|
|
|
$this->logger->error($message);
|
|
|
|
throw new Exception($message);
|
2022-06-17 20:53:35 +02:00
|
|
|
}
|
2022-11-19 01:15:34 +01:00
|
|
|
|
|
|
|
public function getSQL(): SQL {
|
|
|
|
return $this->sql;
|
|
|
|
}
|
2022-11-20 17:13:53 +01:00
|
|
|
|
|
|
|
public function getInsertQuery(DatabaseEntity|array $entities): ?Insert {
|
|
|
|
|
|
|
|
if (empty($entities)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$firstEntity = (is_array($entities) ? current($entities) : $entities);
|
|
|
|
$firstRow = $this->prepareRow($firstEntity, "insert");
|
|
|
|
|
|
|
|
$statement = $this->sql->insert($this->tableName, array_keys($firstRow))
|
|
|
|
->addRow(...array_values($firstRow));
|
|
|
|
|
|
|
|
if (is_array($entities)) {
|
|
|
|
foreach ($entities as $entity) {
|
|
|
|
if ($entity === $firstEntity) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$row = $this->prepareRow($entity, "insert");
|
|
|
|
$statement->addRow(...array_values($row));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $statement;
|
|
|
|
}
|
2022-06-17 20:53:35 +02:00
|
|
|
}
|