- Still working on Update and Insert statements
This commit is contained in:
1 parent
828daffec8
commit
3dc82b1f0d
11 files changed
+243
-62
No files matched your search
+35
-34
@@ -2,56 +2,57 @@
|
||||
|
||||
namespace Ulmus\Common;
|
||||
|
||||
use PDO, PDOStatement;
|
||||
use PDO,
|
||||
PDOStatement;
|
||||
|
||||
class PdoObject extends PDO {
|
||||
|
||||
public function select(string $sql, array $parameters = []) : PDOStatement
|
||||
{
|
||||
# var_dump($sql, $parameters);
|
||||
# die();
|
||||
public function select(string $sql, array $parameters = []): PDOStatement {
|
||||
try {
|
||||
if ( false !== ( $statement = $this->prepare($sql) ) ) {
|
||||
if (false !== ( $statement = $this->prepare($sql) )) {
|
||||
$statement = $this->execute($statement, $parameters, false);
|
||||
$statement->setFetchMode(\PDO::FETCH_ASSOC);
|
||||
|
||||
|
||||
return $statement;
|
||||
}
|
||||
} catch (\PDOException $e) { throw $e; }
|
||||
} catch (\PDOException $e) {
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
public function runQuery(string $sql, array $parameters = []) : PDOStatement
|
||||
{
|
||||
public function runQuery(string $sql, array $parameters = []): PDOStatement {
|
||||
try {
|
||||
if ( false !== ( $statement = $this->prepare($sql) ) ) {
|
||||
if (false !== ( $statement = $this->prepare($sql) )) {
|
||||
return $this->execute($statement, $parameters, true);
|
||||
}
|
||||
} catch (\PDOException $e) { throw $e; }
|
||||
} 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();
|
||||
|
||||
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)) {
|
||||
$statement->lastInsertId = $this->lastInsertId();
|
||||
|
||||
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 $statement;
|
||||
} else {
|
||||
throw new PDOException('Could not begin transaction or given statement is invalid.');
|
||||
}
|
||||
} catch (\PDOException $e) {
|
||||
$this->rollback();
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user