46 lines
1.2 KiB
PHP
46 lines
1.2 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace Ulmus\Common;
|
||
|
|
||
|
use PDO, PDOStatement;
|
||
|
|
||
|
class PdoObject extends PDO {
|
||
|
|
||
|
public function select(string $sql, array $parameters = []) : PDOStatement
|
||
|
{
|
||
|
try {
|
||
|
if ( $statement = $this->prepare($sql) ) {
|
||
|
$statement = $this->execute($statement, $parameters, true);
|
||
|
$statement->setFetchMode(\PDO::FETCH_ASSOC);
|
||
|
return $statement;
|
||
|
}
|
||
|
} catch (\PDOException $e) { throw $e; }
|
||
|
}
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
|
||
|
}
|