Skip to content

Commit

Permalink
fixing code
Browse files Browse the repository at this point in the history
  • Loading branch information
bxel07 committed Mar 25, 2024
1 parent 4c6798e commit d5db7c5
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 61 deletions.
5 changes: 4 additions & 1 deletion src/Contract/QueryBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ public function subQueryCondition
string $connectOperator = 'AND'
): static;


public function innerJoin(string $table, string $condition): static;
public function leftJoin(string $table, string $condition): static;
public function rightJoin(string $table, string $condition): static;
public function crossJoin(string $table): static;
/**
* @throws Exception
*/
Expand Down
1 change: 0 additions & 1 deletion src/QueryBuilder/Migration/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public function init(array $config): PDO

public function isDatabaseExists(): bool
{

$stmt = $this->pdo->prepare("SELECT SCHEMA_NAME FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = :db");
$stmt->execute([':db' => $this->config['dbname']]);
return (bool)$stmt->fetch(PDO::FETCH_ASSOC);
Expand Down
18 changes: 15 additions & 3 deletions src/QueryBuilder/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use PDO;
use PDOStatement;
use stdClass;
use Swoole\Coroutine;
use Swoole\Coroutine\Channel;
use Xel\DB\Contract\QueryBuilderInterface;
use Xel\DB\Contract\QueryBuilderResultInterface;
use Xel\DB\XgenConnector;
Expand All @@ -17,11 +19,12 @@ class QueryBuilder implements QueryBuilderInterface, QueryBuilderResultInterface
*/
protected array $binding = [];

protected Channel $chan;

use QueryBuilderExecutor;

public function __construct
(
// private readonly QueryBuilderExecutor $builderExecutor
protected XgenConnector $connector,
protected bool $mode
){}
Expand Down Expand Up @@ -319,6 +322,17 @@ public function get(): array
return $this->executor()->fetchAll(PDO::FETCH_ASSOC);
}

public function getAsync():?array
{
$can = new Channel(1);
Coroutine::create(function () use($can){
$data = $this->execute($this->getQuery(), $this->getBinding());
$result = $data->fetchAll(PDO::FETCH_ASSOC);
$can->push($result);
});
return $can->pop();
}

/**
* @throws Exception
*/
Expand All @@ -332,8 +346,6 @@ public function toJson(bool $prettyPrint = false): false|string
$result = $this->executor()->fetchAll(PDO::FETCH_ASSOC);
return json_encode($result);
}


public function orderBy(string $column, string $direction = 'DESC'): static
{
$this->query .= " ORDER BY $column $direction";
Expand Down
96 changes: 65 additions & 31 deletions src/QueryBuilder/QueryBuilderExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,26 @@
use PDOException;
use PDOStatement;
use Xel\DB\QueryBuilder\Exception\QueryBuilderException;
use Xel\DB\XgenConnector;

trait QueryBuilderExecutor
{
// public function __construct(private XgenConnector $connector, private bool $mode)
// {}

/**
* @throws Exception
*/
private function getConnection(): false|PDO
{
return $this->mode ? $this->connector->getPoolConnection() : $this->connector->getPersistentConnection() ;
return $this->connector->getPoolConnection();
}

/**
* @throws Exception
*/
public function getPersistenceConnection():false|PDO
{
return $this->connector->getPersistentConnection() ;
}


/**
* @param PDO $pdo
* @return void
Expand All @@ -40,40 +45,69 @@ private function closeConnection(PDO $pdo): void
*/
public function execute(string $query, array $binding = []): false|string|PDOStatement
{
$conn = $this->getConnection();
if ($conn === false){
throw new Exception('Failed to get valid database connection', 500);
}

try {
$conn->setAttribute(PDO::ATTR_AUTOCOMMIT, false);
$conn->beginTransaction();
$stmt = $conn->prepare($query);
foreach ($binding as $item => $value) {
$paramType = is_int($value) ? PDO::PARAM_INT : (is_bool($value) ? PDO::PARAM_BOOL : PDO::PARAM_STR);
$stmt->bindValue($item, $value, $paramType);
if($this->mode){
$conn = $this->getConnection();
if ($conn === false){
throw new Exception('Failed to get valid database connection', 500);
}

$stmt->execute();
try {
$conn->setAttribute(PDO::ATTR_AUTOCOMMIT, false);
$conn->beginTransaction();
$stmt = $conn->prepare($query);
foreach ($binding as $item => $value) {
$paramType = is_int($value) ? PDO::PARAM_INT : (is_bool($value) ? PDO::PARAM_BOOL : PDO::PARAM_STR);
$stmt->bindValue($item, $value, $paramType);
}
$stmt->execute();
// commit
$conn->commit();

// commit
$conn->commit();
// Check if the query was a non-SELECT operation
$isNonSelect = strtoupper(substr(trim($query), 0, 6)) !== 'SELECT';

// Check if the query was a non-SELECT operation
$isNonSelect = strtoupper(substr(trim($query), 0, 6)) !== 'SELECT';
// Return the statement if it's a non-SELECT operation
if ($isNonSelect) {
return true; // Or any indication of success
}

// Return the statement if it's a non-SELECT operation
if ($isNonSelect) {
return true; // Or any indication of success
return $stmt;
}catch (PDOException $e){
$conn->rollBack();
throw new QueryBuilderException($e->getMessage());
} finally {
$this->closeConnection($conn);
}
}else{
$conn = $this->getPersistenceConnection();
if ($conn === false){
throw new Exception('Failed to get valid database connection', 500);
}

return $stmt;
}catch (PDOException $e){
$conn->rollBack();
throw new QueryBuilderException($e->getMessage());
} finally {
$this->closeConnection($conn);
try {
$conn->setAttribute(PDO::ATTR_AUTOCOMMIT, false);
$conn->beginTransaction();
$stmt = $conn->prepare($query);
foreach ($binding as $item => $value) {
$paramType = is_int($value) ? PDO::PARAM_INT : (is_bool($value) ? PDO::PARAM_BOOL : PDO::PARAM_STR);
$stmt->bindValue($item, $value, $paramType);
}
$stmt->execute();
// commit
$conn->commit();
// Check if the query was a non-SELECT operation
$isNonSelect = strtoupper(substr(trim($query), 0, 6)) !== 'SELECT';
// Return the statement if it's a non-SELECT operation
if ($isNonSelect) {
return true; // Or any indication of success
}
return $stmt;
}catch (PDOException $e){
$conn->rollBack();
throw new QueryBuilderException($e->getMessage());
}
}


}
}
39 changes: 14 additions & 25 deletions src/XgenConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class XgenConnector
{
private ?Channel $channel;
private Channel $channel;
private ?PDO $persistence = null;
/**
* @param array<string|int, mixed> $config
Expand Down Expand Up @@ -37,7 +37,6 @@ public function initializeConnections(): void
});
}
}else{
$this->channel = null;
$conn = $this->createConnections();
$this->persistence = $conn;
}
Expand All @@ -48,33 +47,23 @@ public function initializeConnections(): void
*/
private function createConnections(): PDO
{

$dsn = "{$this->config['driver']}:host={$this->config['host']};dbname={$this->config['dbname']};charset={$this->config['charset']}";
if ($this->poolMode){
try {
return new PDO(
$dsn,
$this->config['username'],
$this->config['password'],
$this->config['options']
);
} catch (PDOException $e){
throw new Exception("Failed to create database connection: " . $e->getMessage(), $e->getCode(), $e);
}
}else{
try {
$pdo = new PDO(
$dsn,
$this->config['username'],
$this->config['password'],
$this->config['options']
);
try {
$pdo = new PDO(
$dsn,
$this->config['username'],
$this->config['password'],
$this->config['options']
);

if (!$this->poolMode) {
$pdo->setAttribute(PDO::ATTR_PERSISTENT, true);
return $pdo;
} catch (PDOException $e){
throw new Exception("Failed to create database connection: " . $e->getMessage(), $e->getCode(), $e);
}
return $pdo;
} catch (PDOException $e) {
throw new Exception("Failed to create database connection: " . $e->getMessage(), $e->getCode(), $e);
}

}

/**
Expand Down

0 comments on commit d5db7c5

Please sign in to comment.