web-base/Core/API/DatabaseAPI.class.php

112 lines
3.1 KiB
PHP
Raw Normal View History

2023-01-09 20:27:01 +01:00
<?php
namespace Core\API {
abstract class DatabaseAPI extends Request {
}
}
namespace Core\API\Database {
use Core\API\DatabaseAPI;
use Core\API\Parameter\StringType;
2023-01-16 21:47:23 +01:00
use Core\Driver\SQL\Query\Insert;
2023-01-09 20:27:01 +01:00
use Core\Objects\Context;
2023-01-10 22:12:05 +01:00
use Core\Objects\DatabaseEntity\Controller\DatabaseEntity;
2023-01-16 21:47:23 +01:00
use Core\Objects\DatabaseEntity\Group;
2023-01-09 20:27:01 +01:00
class Status extends DatabaseAPI {
public function __construct(Context $context, bool $externalCall = false) {
parent::__construct($context, $externalCall, []);
}
protected function _execute(): bool {
$sql = $this->context->getSQL();
$status = $sql->getStatus();
$this->result["status"] = $status;
return true;
}
2023-01-16 21:47:23 +01:00
public static function getDefaultACL(Insert $insert): void {
2024-03-27 14:12:01 +01:00
$insert->addRow(self::getEndpoint(), [Group::ADMIN], "Allows users to view the database status", true);
2023-01-16 21:47:23 +01:00
}
2023-01-09 20:27:01 +01:00
}
class Migrate extends DatabaseAPI {
public function __construct(Context $context, bool $externalCall = false) {
parent::__construct($context, $externalCall, [
"className" => new StringType("className", 256)
]);
}
protected function _execute(): bool {
$className = $this->getParam("className");
if (!preg_match("/[a-zA-Z0-9]+/", $className)) {
return $this->createError("Invalid class name");
}
$class = null;
foreach (["Site", "Core"] as $baseDir) {
$classPath = "\\$baseDir\\Objects\\DatabaseEntity\\$className";
if (isClass($classPath)) {
$class = new \ReflectionClass($classPath);
2023-01-10 22:12:05 +01:00
if (!$class->isSubclassOf(DatabaseEntity::class)) {
$class = null;
continue;
}
2023-01-09 20:27:01 +01:00
break;
}
}
if ($class === null) {
return $this->createError("Class not found");
}
$sql = $this->context->getSQL();
$handler = call_user_func("$classPath::getHandler", $sql, null, true);
$persistables = array_merge([
$handler->getTableName() => $handler
], $handler->getNMRelations());
foreach ($persistables as $tableName => $persistable) {
// first check if table exists
if (!$sql->tableExists($tableName)) {
$sql->startTransaction();
$success = true;
try {
foreach ($persistable->getCreateQueries($sql) as $query) {
if (!$query->execute()) {
$this->lastError = "Error migrating table: " . $sql->getLastError();
$success = false;
break;
}
}
} catch (\Exception $ex) {
$success = false;
$this->lastError = "Error migrating table: " . $ex->getMessage();
}
if (!$success) {
$sql->rollback();
return false;
} else {
$sql->commit();
}
} else {
// TODO: Alter table ...
}
}
return true;
}
2023-01-16 21:47:23 +01:00
public static function getDefaultACL(Insert $insert): void {
2024-03-27 14:12:01 +01:00
$insert->addRow(self::getEndpoint(), [Group::ADMIN], "Allows users to migrate the database structure", true);
2023-01-16 21:47:23 +01:00
}
2023-01-09 20:27:01 +01:00
}
}