Skip to content
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
],
"require": {
"php": ">=8.0",
"react/mysql": "^0.6",
"react/mysql": "^0.7",
"react/async": "^4 || ^3 || ^2"
},
"autoload": {
Expand Down
139 changes: 76 additions & 63 deletions src/QueryBuilder/Core/DBFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use React\EventLoop\LoopInterface;
use React\MySQL\Factory;
use React\Mysql\MysqlClient;
use React\Promise\PromiseInterface;
use React\Stream\ReadableStreamInterface;
use Saraf\QB\QueryBuilder\Exceptions\DBFactoryException;
Expand All @@ -15,7 +16,6 @@ class DBFactory
private array $logs = [];
private const MAX_CONNECTION_COUNT = 1000000000;

protected Factory $factory;
protected array $writeConnections = [];
protected array $readConnections = [];

Expand All @@ -24,31 +24,30 @@ class DBFactory
*/
public function __construct(
protected ?LoopInterface $loop,
protected string $host,
protected string $dbName,
protected string $username,
protected string $password,
protected int $writePort = 6446,
protected int $readPort = 6447,
protected int $writeInstanceCount = 2,
protected int $readInstanceCount = 2,
protected int $timeout = 2,
protected int $idle = 2,
protected string $charset = 'utf8mb4',
protected bool $debugMode = false
)
{
$this->factory = new Factory($loop);
protected string $host,
protected string $dbName,
protected string $username,
protected string $password,
protected int $writePort = 6446,
protected int $readPort = 6447,
protected int $writeInstanceCount = 2,
protected int $readInstanceCount = 2,
protected int $timeout = 2,
protected int $idle = 2,
protected string $charset = 'utf8mb4',
protected bool $debugMode = false,
) {
$this->createConnections();
}

public function getTrace(): array
{
if ($this->debugMode === false)
if ($this->debugMode === false) {
return [
'result' => false,
'error' => 'NOT_IN_DEBUG_MODE'
'error' => 'NOT_IN_DEBUG_MODE',
];
}

$jobs = [];
foreach ($this->writeConnections as $i => $writeConnection) {
Expand All @@ -60,9 +59,9 @@ public function getTrace(): array
}

return [
'result' => true,
'logs' => $this->logs,
'workers' => $jobs
'result' => true,
'logs' => $this->logs,
'workers' => $jobs,
];
}

Expand All @@ -71,45 +70,45 @@ public function getTrace(): array
*/
protected function createConnections(): static
{
if (count($this->readConnections) > 0 || count($this->writeConnections) > 0)
if (count($this->readConnections) > 0 || count($this->writeConnections) > 0) {
throw new DBFactoryException("Connections Already Created");
}

for ($i = 0; $i < $this->writeInstanceCount; ++$i) {
$this->writeConnections[] = new DBWorker(
$this->factory
->createLazyConnection(
sprintf(
"%s:%s@%s:%s/%s?idle=%s&timeout=%s&charset=%s",
$this->username,
urlencode($this->password),
$this->host,
$this->writePort,
$this->dbName,
$this->idle,
$this->timeout,
$this->charset
)
)
new MysqlClient(
sprintf(
"%s:%s@%s:%s/%s?idle=%s&timeout=%s&charset=%s",
$this->username,
urlencode($this->password),
$this->host,
$this->writePort,
$this->dbName,
$this->idle,
$this->timeout,
$this->charset,
),
),
);
}

for ($s = 0; $s < $this->readInstanceCount; ++$s) {
$this->readConnections[] = new DBWorker(
$this->factory
->createLazyConnection(
sprintf(
"%s:%s@%s:%s/%s?idle=%s&timeout=%s",
$this->username,
urlencode($this->password),
$this->host,
$this->readPort,
$this->dbName,
$this->idle,
$this->timeout
)
)
new MysqlClient(
sprintf(
"%s:%s@%s:%s/%s?idle=%s&timeout=%s",
$this->username,
urlencode($this->password),
$this->host,
$this->readPort,
$this->dbName,
$this->idle,
$this->timeout,
),
),
);
}

return $this;
}

Expand All @@ -118,8 +117,9 @@ protected function createConnections(): static
*/
public function getQueryBuilder(): QueryBuilder
{
if (count($this->readConnections) == 0 || count($this->writeConnections) == 0)
if (count($this->readConnections) == 0 || count($this->writeConnections) == 0) {
throw new DBFactoryException("Connections Not Created");
}

return new QueryBuilder($this);
}
Expand All @@ -132,29 +132,35 @@ public function query(string $query): PromiseInterface
$isWrite = true;
if (str_starts_with(strtolower($query), "select")
|| str_starts_with(strtolower($query), "show")
) $isWrite = false;
) {
$isWrite = false;
}

$bestConnections = $this->getBestConnection();

$connection = $isWrite
? $this->writeConnections[$bestConnections['write']]
: $this->readConnections[$bestConnections['read']];

if (!($connection instanceof DBWorker))
if (!($connection instanceof DBWorker)) {
throw new DBFactoryException("Connections Not Instance of Worker / Restart App");
}

if (!$this->debugMode)
if (!$this->debugMode) {
return $connection->query($query);
}

$startTime = QBHelper::getCurrentMicroTime();
return $connection->query($query)

return $connection
->query($query)
->then(function ($result) use ($isWrite, $startTime, $query) {
$endTime = QBHelper::getCurrentMicroTime();
$this->logs[] = [
'query' => $query,
'took' => $endTime - $startTime,
'query' => $query,
'took' => $endTime - $startTime,
'isWrite' => $isWrite,
'status' => $result['result']
'status' => $result['result'],
];

return $result;
Expand All @@ -169,16 +175,19 @@ public function streamQuery(string $query): StreamEventHandler
$isWrite = true;
if (str_starts_with(strtolower($query), "select")
|| str_starts_with(strtolower($query), "show")
) $isWrite = false;
) {
$isWrite = false;
}

$bestConnections = $this->getBestConnection();

$connection = $isWrite
? $this->writeConnections[$bestConnections['write']]
: $this->readConnections[$bestConnections['read']];

if (!($connection instanceof DBWorker))
if (!($connection instanceof DBWorker)) {
throw new DBFactoryException("Connections Not Instance of Worker / Restart App");
}

return $connection->streamQuery($query);
}
Expand All @@ -191,16 +200,19 @@ public function streamQueryRaw(string $query): ReadableStreamInterface
$isWrite = true;
if (str_starts_with(strtolower($query), "select")
|| str_starts_with(strtolower($query), "show")
) $isWrite = false;
) {
$isWrite = false;
}

$bestConnections = $this->getBestConnection();

$connection = $isWrite
? $this->writeConnections[$bestConnections['write']]
: $this->readConnections[$bestConnections['read']];

if (!($connection instanceof DBWorker))
if (!($connection instanceof DBWorker)) {
throw new DBFactoryException("Connections Not Instance of Worker / Restart App");
}

return $connection->streamQueryRaw($query);
}
Expand All @@ -210,8 +222,9 @@ public function streamQueryRaw(string $query): ReadableStreamInterface
*/
private function getBestConnection(): array
{
if (count($this->readConnections) == 0 || count($this->writeConnections) == 0)
if (count($this->readConnections) == 0 || count($this->writeConnections) == 0) {
throw new DBFactoryException("Connections Not Created");
}

// Best Writer
$minWriteJobs = self::MAX_CONNECTION_COUNT;
Expand All @@ -237,7 +250,7 @@ private function getBestConnection(): array

return [
'write' => $minJobsWriterConnection,
'read' => $minJobsReaderConnection
'read' => $minJobsReaderConnection,
];
}

Expand Down