133 lines
3.6 KiB
PHP
133 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace Ulmus\Common;
|
|
|
|
use PDO,
|
|
PDOStatement;
|
|
|
|
class PdoObject extends PDO {
|
|
|
|
public static ? string $dump = null;
|
|
|
|
public bool $executionStatus;
|
|
|
|
public int $rowCount = 0;
|
|
|
|
public mixed $lastInsertId = null;
|
|
|
|
public \Closure $onClose;
|
|
|
|
public function select(string $sql, array $parameters = []): PDOStatement
|
|
{
|
|
static::$dump && call_user_func_array(static::$dump, [ $sql, $parameters ]);
|
|
|
|
try {
|
|
if (false !== ( $statement = $this->prepare($sql) )) {
|
|
$statement->setFetchMode(\PDO::FETCH_ASSOC);
|
|
$this->execute($statement, $parameters, false);
|
|
|
|
return $statement;
|
|
}
|
|
}
|
|
catch (\Throwable $e) {
|
|
throw new \PdoException($e->getMessage() . " `$sql` with data:" . json_encode($parameters));
|
|
}
|
|
}
|
|
|
|
public function __destruct()
|
|
{
|
|
if ($this->onClose ?? null) {
|
|
call_user_func($this->onClose, $this);
|
|
}
|
|
}
|
|
|
|
public function runQuery(string $sql, array $parameters = []): ? static
|
|
{
|
|
static::$dump && call_user_func_array(static::$dump, [ $sql, $parameters ]);
|
|
|
|
try {
|
|
if (false !== ( $statement = $this->prepare($sql) )) {
|
|
return $this->execute($statement, $parameters, true);
|
|
}
|
|
}
|
|
catch (\Throwable $e) {
|
|
throw new \PdoException($e->getMessage() . " `$sql` with data:" . json_encode($parameters));
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public function runInsertQuery(string $sql, array $parameters = []) : ? static
|
|
{
|
|
return $this->runQuery($sql, $parameters);
|
|
}
|
|
|
|
public function runUpdateQuery(string $sql, array $parameters = []) : ? static
|
|
{
|
|
return $this->runQuery($sql, $parameters);
|
|
}
|
|
|
|
public function runDeleteQuery(string $sql, array $parameters = []) : ? static
|
|
{
|
|
return $this->runQuery($sql, $parameters);
|
|
}
|
|
|
|
public function execute(PDOStatement $statement, array $parameters = [], bool $commit = true) : ? static
|
|
{
|
|
$this->executionStatus = false;
|
|
$this->lastInsertId = null;
|
|
|
|
try {
|
|
if ( ! $this->inTransaction() ) {
|
|
$this->beginTransaction();
|
|
}
|
|
|
|
foreach($parameters as $key => $value) {
|
|
switch(strtolower(gettype($value))) {
|
|
case "boolean":
|
|
$statement->bindValue($key, $value, Pdo::PARAM_BOOL);
|
|
break;
|
|
|
|
case "integer":
|
|
$statement->bindValue($key, $value, Pdo::PARAM_INT);
|
|
break;
|
|
|
|
case "null":
|
|
$statement->bindValue($key, $value, Pdo::PARAM_NULL);
|
|
break;
|
|
|
|
case "string":
|
|
default:
|
|
$statement->bindValue($key, (string) $value, Pdo::PARAM_STR);
|
|
}
|
|
}
|
|
|
|
$this->executionStatus = $statement->execute();
|
|
|
|
if ( $this->executionStatus ) {
|
|
$this->lastInsertId = $this->lastInsertId();
|
|
$this->rowCount = $statement->rowCount();
|
|
|
|
if ( $commit ) {
|
|
$this->commit();
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
else {
|
|
throw new \PDOException($statement->errorCode() . " - " . json_encode($statement->errorInfo()));
|
|
}
|
|
}
|
|
catch (\PDOException $e) {
|
|
$this->rollback();
|
|
|
|
throw $e;
|
|
}
|
|
catch (\Throwable $e) {
|
|
throw $e;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|