web-base/core/Configuration/Patch/log.class.php

96 lines
2.9 KiB
PHP
Raw Normal View History

2021-04-08 18:29:47 +02:00
<?php
namespace Configuration\Patch;
use Configuration\DatabaseScript;
use Driver\SQL\Column\IntColumn;
use Driver\SQL\Condition\Compare;
2022-02-20 16:53:26 +01:00
use Driver\SQL\Query\CreateProcedure;
2021-04-08 18:29:47 +02:00
use Driver\SQL\SQL;
use Driver\SQL\Type\CurrentColumn;
use Driver\SQL\Type\CurrentTable;
use Driver\SQL\Type\Trigger;
class log extends DatabaseScript {
2022-02-20 16:53:26 +01:00
public static function createTableLog(SQL $sql, string $table, int $lifetime = 90): array {
return [
$sql->createTrigger("${table}_trg_insert")
->after()->insert($table)
->exec(new CreateProcedure($sql, "InsertEntityLog"), [
"tableName" => new CurrentTable(),
"entityId" => new CurrentColumn("uid"),
"lifetime" => $lifetime,
]),
$sql->createTrigger("${table}_trg_update")
->after()->update($table)
->exec(new CreateProcedure($sql, "UpdateEntityLog"), [
"tableName" => new CurrentTable(),
"entityId" => new CurrentColumn("uid"),
]),
$sql->createTrigger("${table}_trg_delete")
->after()->delete($table)
->exec(new CreateProcedure($sql, "DeleteEntityLog"), [
"tableName" => new CurrentTable(),
"entityId" => new CurrentColumn("uid"),
])
];
}
2021-04-08 18:29:47 +02:00
public static function createQueries(SQL $sql): array {
$queries = array();
$queries[] = $sql->createTable("EntityLog")
->addInt("entityId")
->addString("tableName")
->addDateTime("modified", false, $sql->now())
->addInt("lifetime", false, 90);
$insertProcedure = $sql->createProcedure("InsertEntityLog")
->param(new CurrentTable())
->param(new IntColumn("uid"))
2022-02-20 16:53:26 +01:00
->param(new IntColumn("lifetime", false, 90))
2021-04-08 18:29:47 +02:00
->returns(new Trigger())
->exec(array(
2022-02-20 16:53:26 +01:00
$sql->insert("EntityLog", ["entityId", "tableName", "lifetime"])
->addRow(new CurrentColumn("uid"), new CurrentTable(), new CurrentColumn("lifetime"))
2021-04-08 18:29:47 +02:00
));
$updateProcedure = $sql->createProcedure("UpdateEntityLog")
->param(new CurrentTable())
->param(new IntColumn("uid"))
->returns(new Trigger())
->exec(array(
$sql->update("EntityLog")
->set("modified", $sql->now())
2021-04-08 19:08:05 +02:00
->where(new Compare("entityId", new CurrentColumn("uid")))
->where(new Compare("tableName", new CurrentTable()))
));
$deleteProcedure = $sql->createProcedure("DeleteEntityLog")
->param(new CurrentTable())
->param(new IntColumn("uid"))
->returns(new Trigger())
->exec(array(
$sql->delete("EntityLog")
->where(new Compare("entityId", new CurrentColumn("uid")))
->where(new Compare("tableName", new CurrentTable()))
2021-04-08 18:29:47 +02:00
));
$queries[] = $insertProcedure;
$queries[] = $updateProcedure;
2021-04-08 19:08:05 +02:00
$queries[] = $deleteProcedure;
2021-04-08 18:29:47 +02:00
$tables = ["ContactRequest"];
foreach ($tables as $table) {
2022-02-20 16:53:26 +01:00
$queries = array_merge($queries, self::createTableLog($sql, $table));
2021-04-08 18:29:47 +02:00
}
return $queries;
}
}