web-base/Core/Driver/SQL/Query/CreateTrigger.class.php

107 lines
2.8 KiB
PHP
Raw Permalink Normal View History

2021-04-08 18:29:47 +02:00
<?php
2022-11-18 18:06:46 +01:00
namespace Core\Driver\SQL\Query;
2021-04-08 18:29:47 +02:00
use Core\Driver\SQL\MySQL;
use Core\Driver\SQL\PostgreSQL;
2022-11-18 18:06:46 +01:00
use Core\Driver\SQL\SQL;
use Exception;
2021-04-08 18:29:47 +02:00
class CreateTrigger extends Query {
private string $name;
private string $time;
private string $event;
private string $tableName;
2022-02-20 16:53:26 +01:00
private array $parameters;
private bool $ifNotExist;
2021-04-08 18:29:47 +02:00
private ?CreateProcedure $procedure;
public function __construct(SQL $sql, string $triggerName) {
parent::__construct($sql);
$this->name = $triggerName;
$this->time = "AFTER";
$this->tableName = "";
$this->event = "";
2022-02-20 16:53:26 +01:00
$this->parameters = [];
2021-04-08 18:29:47 +02:00
$this->procedure = null;
$this->ifNotExist = false;
2021-04-08 18:29:47 +02:00
}
public function before(): CreateTrigger {
$this->time = "BEFORE";
return $this;
}
public function onlyIfNotExist(): CreateTrigger {
$this->ifNotExist = true;
return $this;
}
2021-04-08 18:29:47 +02:00
public function after(): CreateTrigger {
$this->time = "AFTER";
return $this;
}
public function update(string $table): CreateTrigger {
$this->tableName = $table;
$this->event = "UPDATE";
return $this;
}
public function insert(string $table): CreateTrigger {
$this->tableName = $table;
$this->event = "INSERT";
return $this;
}
public function delete(string $table): CreateTrigger {
$this->tableName = $table;
$this->event = "DELETE";
return $this;
}
2022-02-20 16:53:26 +01:00
public function exec(CreateProcedure $procedure, array $parameters = []): CreateTrigger {
2021-04-08 18:29:47 +02:00
$this->procedure = $procedure;
2022-02-20 16:53:26 +01:00
$this->parameters = $parameters;
2021-04-08 18:29:47 +02:00
return $this;
}
public function getName(): string { return $this->name; }
public function getTime(): string { return $this->time; }
public function getEvent(): string { return $this->event; }
public function getTable(): string { return $this->tableName; }
public function getProcedure(): CreateProcedure { return $this->procedure; }
2021-04-09 16:05:36 +02:00
public function build(array &$params): ?string {
2021-04-08 18:29:47 +02:00
$name = $this->sql->tableName($this->getName());
$time = $this->getTime();
$event = $this->getEvent();
$tableName = $this->sql->tableName($this->getTable());
$params = array();
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 ";
2022-02-20 16:53:26 +01:00
$triggerBody = $this->sql->createTriggerBody($this, $this->parameters);
2021-04-08 18:29:47 +02:00
if ($triggerBody === null) {
return null;
}
$query .= $triggerBody;
return $query;
}
}