Skip to content

create event builder #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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",
Expand Down
118 changes: 118 additions & 0 deletions src/QueryBuilder/Clauses/Events/EventCreate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

namespace Saraf\QB\QueryBuilder\Clauses\Events;


use React\Promise\Promise;
use React\Promise\PromiseInterface;
use Saraf\QB\QueryBuilder\Core\DBFactory;
use Saraf\QB\QueryBuilder\Core\EQuery;
use Saraf\QB\QueryBuilder\Core\Query;
use Saraf\QB\QueryBuilder\Exceptions\DBFactoryException;
use Saraf\QB\QueryBuilder\Exceptions\EventCreateException;
use function React\Async\await;

class EventCreate
{
protected string|Query|EQuery $taskQuery;

protected ?string $name = null;
protected ?SchedulerModel $periods = null;

protected ?SchedulerModel $starts = null;
protected ?SchedulerModel $ends = null;

protected ?string $query = null;

public function __construct(protected ?DBFactory $factory = null)
{
}

public function setEventName(string $eventName): static
{
$this->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()
]);
});
}
}

}
53 changes: 53 additions & 0 deletions src/QueryBuilder/Clauses/Events/EventDrop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Saraf\QB\QueryBuilder\Clauses\Events;

use React\Promise\Promise;
use React\Promise\PromiseInterface;
use Saraf\QB\QueryBuilder\Core\DBFactory;
use Saraf\QB\QueryBuilder\Exceptions\DBFactoryException;
use Saraf\QB\QueryBuilder\Exceptions\EventDropException;

class EventDrop
{
protected ?string $eventName = null;

protected string $query;

public function __construct(protected DBFactory $factory)
{
}

public function setEventName(?string $eventName): void
{
$this->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()
]);
});
}
}

}
35 changes: 35 additions & 0 deletions src/QueryBuilder/Clauses/Events/SchedulerModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Saraf\QB\QueryBuilder\Clauses\Events;

class SchedulerModel
{
protected int $amount;
protected string $periods;

public function __construct(int $amount, string $periods)
{
$this->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;
}
}
10 changes: 10 additions & 0 deletions src/QueryBuilder/Clauses/Events/TimePeriods.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Saraf\QB\QueryBuilder\Clauses\Events;

class TimePeriods
{
public const SECOND = "second";
public const HOUR = "hour";
public const MINUTE = "minute";
}
7 changes: 7 additions & 0 deletions src/QueryBuilder/Exceptions/EventCreateException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Saraf\QB\QueryBuilder\Exceptions;

class EventCreateException extends \Exception
{
}
8 changes: 8 additions & 0 deletions src/QueryBuilder/Exceptions/EventDropException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Saraf\QB\QueryBuilder\Exceptions;

class EventDropException extends \Exception
{

}
11 changes: 11 additions & 0 deletions src/QueryBuilder/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Saraf\QB\QueryBuilder;

use Saraf\QB\QueryBuilder\Clauses\Delete;
use Saraf\QB\QueryBuilder\Clauses\Events\EventCreate;
use Saraf\QB\QueryBuilder\Clauses\Events\EventDrop;
use Saraf\QB\QueryBuilder\Clauses\Insert;
use Saraf\QB\QueryBuilder\Clauses\InsertUpdate;
use Saraf\QB\QueryBuilder\Clauses\MultiInsertUpdate;
Expand All @@ -16,6 +18,15 @@ public function __construct(protected DBFactory|null $factory = null)
{
}

public function eventCreate(): EventCreate
{
return new EventCreate($this->factory);
}

public function eventDrop(){
return new EventDrop($this->factory);
}

public function select(): Select
{
return new Select($this->factory);
Expand Down