From 86d225570b785f63d0888a4fa145c763888499be Mon Sep 17 00:00:00 2001 From: Hasan Parasteh Date: Wed, 25 Oct 2023 19:12:45 +0330 Subject: [PATCH] feat: create event builder --- composer.json | 9 +- .../Clauses/Events/EventCreate.php | 118 ++++++++++++++++++ src/QueryBuilder/Clauses/Events/EventDrop.php | 53 ++++++++ .../Clauses/Events/SchedulerModel.php | 35 ++++++ .../Clauses/Events/TimePeriods.php | 10 ++ .../Exceptions/EventCreateException.php | 7 ++ .../Exceptions/EventDropException.php | 8 ++ src/QueryBuilder/QueryBuilder.php | 11 ++ 8 files changed, 247 insertions(+), 4 deletions(-) create mode 100644 src/QueryBuilder/Clauses/Events/EventCreate.php create mode 100644 src/QueryBuilder/Clauses/Events/EventDrop.php create mode 100644 src/QueryBuilder/Clauses/Events/SchedulerModel.php create mode 100644 src/QueryBuilder/Clauses/Events/TimePeriods.php create mode 100644 src/QueryBuilder/Exceptions/EventCreateException.php create mode 100644 src/QueryBuilder/Exceptions/EventDropException.php diff --git a/composer.json b/composer.json index 3c3ede5..0ab035f 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,8 @@ ], "require": { "php": ">=8.0", - "react/mysql": "^0.5.7" + "react/mysql": "^0.5", + "react/async": "^4 || ^3 || ^2" }, "autoload": { "psr-4": { @@ -23,9 +24,9 @@ }, "require-dev": { "phpunit/phpunit": "^9", - "friendsofphp/php-cs-fixer": "^3.12", - "phpstan/phpstan": "^1.8", - "vlucas/phpdotenv": "^5.5.0" + "friendsofphp/php-cs-fixer": "^3", + "phpstan/phpstan": "^1", + "vlucas/phpdotenv": "^5" }, "scripts": { "test": "./vendor/bin/phpunit tests", diff --git a/src/QueryBuilder/Clauses/Events/EventCreate.php b/src/QueryBuilder/Clauses/Events/EventCreate.php new file mode 100644 index 0000000..7b9a73d --- /dev/null +++ b/src/QueryBuilder/Clauses/Events/EventCreate.php @@ -0,0 +1,118 @@ +name = $eventName; + return $this; + } + + public function setTimePeriod(int $amount, string $periods): static + { + $this->periods = new SchedulerModel($amount, $periods); + return $this; + } + + public function setStartTime(int $amount, string $periods): static + { + $this->starts = new SchedulerModel($amount, $periods); + return $this; + } + + public function setEndTime(int $amount, string $periods): static + { + $this->ends = new SchedulerModel($amount, $periods); + return $this; + } + + public function setTaskQuery(string $taskQuery): static + { + $this->taskQuery = $taskQuery; + return $this; + } + + public function setTaskQueryObject(Query|EQuery $query): static + { + $this->taskQuery = $query; + return $this; + } + + /** + * @throws \Throwable + * @throws \Saraf\QB\QueryBuilder\Exceptions\EventCreateException + */ + public function compile(): static + { + $finalQuery = "create event " . $this->name . " on schedule every "; + if (is_null($this->periods)) + throw new EventCreateException("Time Period Error"); + + $finalQuery .= $this->periods->getAmount() . " " . $this->periods->getPeriods() . " "; + + if (!is_null($this->starts)) + $finalQuery .= "starts CURRENT_TIMESTAMP + interval " . $this->starts->getAmount() . " " . $this->starts->getPeriods() . " "; + + if (!is_null($this->ends)) + $finalQuery .= "ends CURRENT_TIMESTAMP + interval " . $this->ends->getAmount() . " " . $this->ends->getPeriods() . " "; + + $finalQuery .= "do "; + + if ($this->taskQuery instanceof Query) { + $finalQuery .= $this->taskQuery->getQueryAsString(); + } else if ($this->taskQuery instanceof EQuery) { + $result = await($this->taskQuery->getQuery()); + if (!$result['result']) + throw new EventCreateException($result['error']); + $finalQuery .= $result['query']; + } else { + $finalQuery .= $this->taskQuery; + } + + $this->query = $finalQuery; + echo $finalQuery . PHP_EOL; + return $this; + } + + public function commit(): PromiseInterface|Promise + { + try { + return $this->factory + ->query($this->query); + } catch (DBFactoryException $e) { + return new Promise(function (callable $resolve) use ($e) { + $resolve([ + 'result' => false, + 'error' => $e->getMessage() + ]); + }); + } + } + +} \ No newline at end of file diff --git a/src/QueryBuilder/Clauses/Events/EventDrop.php b/src/QueryBuilder/Clauses/Events/EventDrop.php new file mode 100644 index 0000000..4fa00f4 --- /dev/null +++ b/src/QueryBuilder/Clauses/Events/EventDrop.php @@ -0,0 +1,53 @@ +eventName = $eventName; + } + + /** + * @throws \Saraf\QB\QueryBuilder\Exceptions\EventDropException + */ + public function compile(): static + { + if (is_null($this->eventName)) + throw new EventDropException("Event name is null"); + + $this->query = "DROP EVENT " . $this->eventName; + return $this; + } + + public function commit(): PromiseInterface|Promise + { + try { + return $this->factory + ->query($this->query); + } catch (DBFactoryException $e) { + return new Promise(function (callable $resolve) use ($e) { + $resolve([ + 'result' => false, + 'error' => $e->getMessage() + ]); + }); + } + } + +} \ No newline at end of file diff --git a/src/QueryBuilder/Clauses/Events/SchedulerModel.php b/src/QueryBuilder/Clauses/Events/SchedulerModel.php new file mode 100644 index 0000000..8afa0ab --- /dev/null +++ b/src/QueryBuilder/Clauses/Events/SchedulerModel.php @@ -0,0 +1,35 @@ +amount = $amount; + $this->periods = $periods; + } + + public function getAmount(): int + { + return $this->amount; + } + + public function setAmount(int $amount): void + { + $this->amount = $amount; + } + + public function getPeriods(): string + { + return $this->periods; + } + + public function setPeriods(string $periods): void + { + $this->periods = $periods; + } +} \ No newline at end of file diff --git a/src/QueryBuilder/Clauses/Events/TimePeriods.php b/src/QueryBuilder/Clauses/Events/TimePeriods.php new file mode 100644 index 0000000..4a2c19f --- /dev/null +++ b/src/QueryBuilder/Clauses/Events/TimePeriods.php @@ -0,0 +1,10 @@ +factory); + } + + public function eventDrop(){ + return new EventDrop($this->factory); + } + public function select(): Select { return new Select($this->factory); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy