v2.4.4: DatabaseEntity migration and bugfixes

This commit is contained in:
2024-05-21 12:32:44 +02:00
parent b96d0d053c
commit 037f0fae91
14 changed files with 249 additions and 47 deletions

View File

@@ -6,7 +6,7 @@ use Core\Driver\SQL\Column\Column;
use Core\Driver\SQL\MySQL;
use Core\Driver\SQL\PostgreSQL;
use Core\Driver\SQL\SQL;
use Core\External\PHPMailer\Exception;
use Exception;
class DateAdd extends Expression {

View File

@@ -6,7 +6,7 @@ use Core\Driver\SQL\Column\Column;
use Core\Driver\SQL\MySQL;
use Core\Driver\SQL\PostgreSQL;
use Core\Driver\SQL\SQL;
use Core\External\PHPMailer\Exception;
use Exception;
class DateSub extends Expression {

View File

@@ -375,7 +375,10 @@ class MySQL extends SQL {
list ($name, $alias) = $parts;
return "`$name` $alias";
} else {
return "`$table`";
$parts = explode(".", $table);
return implode(".", array_map(function ($n) {
return "`$n`";
}, $parts));
}
}
}
@@ -484,6 +487,27 @@ class MySQL extends SQL {
return $res && $res[0]["count"] > 0;
}
public function listTables(): ?array {
$tableSchema = $this->connectionData->getProperty("database");
$res = $this->select("TABLE_NAME")
->from("information_schema.TABLES")
->where(new Compare("TABLE_SCHEMA", $tableSchema, "=", true))
->where(new CondLike(new Column("TABLE_TYPE"), "BASE TABLE"))
->execute();
if ($res !== false) {
$tableNames = [];
foreach ($res as $row) {
$tableNames[] = $row["TABLE_NAME"];
}
return $tableNames;
}
return null;
}
}
class RowIteratorMySQL extends RowIterator {

View File

@@ -334,7 +334,10 @@ class PostgreSQL extends SQL {
list ($name, $alias) = $parts;
return "\"$name\" $alias";
} else {
return "\"$table\"";
$parts = explode(".", $table);
return implode(".", array_map(function ($n) {
return "\"$n\"";
}, $parts));
}
}
}
@@ -463,6 +466,28 @@ class PostgreSQL extends SQL {
return $res && $res[0]["count"] > 0;
}
public function listTables(): ?array {
$tableSchema = $this->connectionData->getProperty("database");
$res = $this->select("tablename")
->from("pg_tables")
->where(new Compare("schemaname", $tableSchema))
->execute();
if ($res !== false) {
$tableNames = [];
foreach ($res as $row) {
$tableNames[] = $row["tablename"];
}
return $tableNames;
}
return null;
}
}
class RowIteratorPostgreSQL extends RowIterator {

View File

@@ -2,7 +2,10 @@
namespace Core\Driver\SQL\Query;
use Core\Driver\SQL\MySQL;
use Core\Driver\SQL\PostgreSQL;
use Core\Driver\SQL\SQL;
use Exception;
class CreateTrigger extends Query {
@@ -11,6 +14,9 @@ class CreateTrigger extends Query {
private string $event;
private string $tableName;
private array $parameters;
private bool $ifNotExist;
private ?CreateProcedure $procedure;
public function __construct(SQL $sql, string $triggerName) {
@@ -21,6 +27,7 @@ class CreateTrigger extends Query {
$this->event = "";
$this->parameters = [];
$this->procedure = null;
$this->ifNotExist = false;
}
public function before(): CreateTrigger {
@@ -28,6 +35,11 @@ class CreateTrigger extends Query {
return $this;
}
public function onlyIfNotExist(): CreateTrigger {
$this->ifNotExist = true;
return $this;
}
public function after(): CreateTrigger {
$this->time = "AFTER";
return $this;
@@ -70,7 +82,20 @@ class CreateTrigger extends Query {
$tableName = $this->sql->tableName($this->getTable());
$params = array();
$query = "CREATE TRIGGER $name $time $event ON $tableName FOR EACH ROW ";
if ($this->sql instanceof MySQL) {
$query = "CREATE TRIGGER";
if ($this->ifNotExist) {
$query .= " IF NOT EXISTS";
}
} else if ($this->sql instanceof PostgreSQL) {
$ifNotExists = $this->ifNotExist ? " OR REPLACE" : "";
$query = "CREATE$ifNotExists TRIGGER";
} else {
throw new Exception("CreateTrigger Not implemented for driver type: " . get_class($this->sql));
}
$query .= " $name $time $event ON $tableName FOR EACH ROW ";
$triggerBody = $this->sql->createTriggerBody($this, $this->parameters);
if ($triggerBody === null) {
return null;

View File

@@ -130,6 +130,8 @@ abstract class SQL {
// Schema
public abstract function tableExists(string $tableName): bool;
public abstract function listTables(): ?array;
/**
* @param Query $query
* @param int $fetchType