2019-08-21 20:13:00 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Ulmus\Common;
|
|
|
|
|
|
|
|
use PDO, PDOStatement;
|
|
|
|
|
|
|
|
class PdoObject extends PDO {
|
|
|
|
|
|
|
|
public function select(string $sql, array $parameters = []) : PDOStatement
|
|
|
|
{
|
|
|
|
try {
|
2019-12-19 14:54:33 +00:00
|
|
|
if ( false !== ( $statement = $this->prepare($sql) ) ) {
|
2019-08-21 20:13:00 +00:00
|
|
|
$statement = $this->execute($statement, $parameters, true);
|
|
|
|
$statement->setFetchMode(\PDO::FETCH_ASSOC);
|
|
|
|
return $statement;
|
|
|
|
}
|
|
|
|
} catch (\PDOException $e) { throw $e; }
|
|
|
|
}
|
|
|
|
|
2019-12-19 14:54:33 +00:00
|
|
|
public function runQuery(string $sql, array $parameters = []) : PDOStatement
|
|
|
|
{
|
|
|
|
# var_dump($sql, $parameters);die();
|
|
|
|
try {
|
|
|
|
if ( false !== ( $statement = $this->prepare($sql) ) ) {
|
|
|
|
return $this->execute($statement, $parameters, true);
|
|
|
|
}
|
|
|
|
} catch (\PDOException $e) { throw $e; }
|
|
|
|
}
|
|
|
|
|
2019-08-21 20:13:00 +00:00
|
|
|
public function execute(PDOStatement $statement, array $parameters = [], bool $commit = true) : ? PDOStatement
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
if (! $this->inTransaction() ) {
|
|
|
|
$this->beginTransaction();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( empty($parameters) ? $statement->execute() : $statement->execute($parameters) ) {
|
|
|
|
# if ( $commit ) {
|
|
|
|
$this->commit();
|
|
|
|
# }
|
|
|
|
|
|
|
|
return $statement;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new PDOException('Could not begin transaction or given statement is invalid.');
|
|
|
|
}
|
|
|
|
} catch (\PDOException $e) {
|
|
|
|
$this->rollback();
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|