2019-08-21 20:13:00 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Ulmus\Common;
|
|
|
|
|
2020-02-06 04:41:57 +00:00
|
|
|
use PDO,
|
|
|
|
PDOStatement;
|
2019-08-21 20:13:00 +00:00
|
|
|
|
|
|
|
class PdoObject extends PDO {
|
|
|
|
|
2020-04-14 13:47:09 +00:00
|
|
|
public function select(string $sql, array $parameters = []): PDOStatement
|
|
|
|
{
|
2019-08-21 20:13:00 +00:00
|
|
|
try {
|
2020-02-06 04:41:57 +00:00
|
|
|
if (false !== ( $statement = $this->prepare($sql) )) {
|
2020-01-29 21:11:16 +00:00
|
|
|
$statement = $this->execute($statement, $parameters, false);
|
2019-08-21 20:13:00 +00:00
|
|
|
$statement->setFetchMode(\PDO::FETCH_ASSOC);
|
2020-02-06 04:41:57 +00:00
|
|
|
|
2019-08-21 20:13:00 +00:00
|
|
|
return $statement;
|
|
|
|
}
|
2020-04-09 17:20:07 +00:00
|
|
|
}
|
|
|
|
catch (\PDOException $e) {
|
|
|
|
throw new \PdoException($e->getMessage() . " `$sql` with data:" . json_encode($parameters));
|
2020-02-06 04:41:57 +00:00
|
|
|
}
|
2019-08-21 20:13:00 +00:00
|
|
|
}
|
|
|
|
|
2020-04-14 13:47:09 +00:00
|
|
|
public function runQuery(string $sql, array $parameters = []): ? PDOStatement
|
|
|
|
{
|
2019-12-19 14:54:33 +00:00
|
|
|
try {
|
2020-02-06 04:41:57 +00:00
|
|
|
if (false !== ( $statement = $this->prepare($sql) )) {
|
2019-12-19 14:54:33 +00:00
|
|
|
return $this->execute($statement, $parameters, true);
|
|
|
|
}
|
2020-04-09 17:20:07 +00:00
|
|
|
}
|
|
|
|
catch (\PDOException $e) {
|
|
|
|
throw new \PdoException($e->getMessage() . " `$sql` with data:" . json_encode($parameters));
|
2020-02-06 04:41:57 +00:00
|
|
|
}
|
2020-02-17 13:23:41 +00:00
|
|
|
|
|
|
|
return null;
|
2019-12-19 14:54:33 +00:00
|
|
|
}
|
2020-02-06 04:41:57 +00:00
|
|
|
|
2020-04-14 13:47:09 +00:00
|
|
|
public function execute(PDOStatement $statement, array $parameters = [], bool $commit = true): ? PDOStatement
|
|
|
|
{
|
2020-02-06 04:41:57 +00:00
|
|
|
try {
|
2020-02-07 21:36:38 +00:00
|
|
|
if ( ! $this->inTransaction() ) {
|
2020-02-06 04:41:57 +00:00
|
|
|
$this->beginTransaction();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($parameters) ? $statement->execute() : $statement->execute($parameters)) {
|
|
|
|
$statement->lastInsertId = $this->lastInsertId();
|
|
|
|
|
2020-04-09 13:50:09 +00:00
|
|
|
if ( $commit ) {
|
2020-02-06 04:41:57 +00:00
|
|
|
$this->commit();
|
2020-01-29 21:11:16 +00:00
|
|
|
}
|
2019-08-21 20:13:00 +00:00
|
|
|
|
2020-02-06 04:41:57 +00:00
|
|
|
return $statement;
|
2020-03-26 20:28:03 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new \PDOException($statement->errorCode() . " - " . json_encode($statement->errorInfo()));
|
2020-02-06 04:41:57 +00:00
|
|
|
}
|
2020-05-20 19:34:50 +00:00
|
|
|
}
|
2020-04-09 17:20:07 +00:00
|
|
|
catch (\PDOException $e) {
|
2020-02-06 04:41:57 +00:00
|
|
|
$this->rollback();
|
2020-03-26 20:28:03 +00:00
|
|
|
|
2020-04-14 13:47:09 +00:00
|
|
|
throw $e;
|
2020-02-06 04:41:57 +00:00
|
|
|
}
|
2020-05-20 19:34:50 +00:00
|
|
|
catch (\Throwable $e) {
|
|
|
|
debogueur($statement, $parameters, $commit);
|
|
|
|
|
|
|
|
throw $e;
|
|
|
|
}
|
2020-02-06 04:41:57 +00:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2019-08-21 20:13:00 +00:00
|
|
|
}
|