2017-12-19 22:15:05 +00:00
|
|
|
<?php
|
|
|
|
/** @license MIT
|
|
|
|
* Copyright 2017 J. King, Dustin Wilson et al.
|
|
|
|
* See LICENSE and AUTHORS files for details */
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace JKingWeb\Arsse\Db;
|
|
|
|
|
2019-01-11 00:01:32 +00:00
|
|
|
abstract class PDOStatement extends AbstractStatement {
|
2017-12-19 22:15:05 +00:00
|
|
|
use PDOError;
|
|
|
|
|
2020-03-01 23:32:01 +00:00
|
|
|
protected const BINDINGS = [
|
2019-03-01 17:17:33 +00:00
|
|
|
self::T_INTEGER => \PDO::PARAM_INT,
|
|
|
|
self::T_FLOAT => \PDO::PARAM_STR,
|
|
|
|
self::T_DATETIME => \PDO::PARAM_STR,
|
|
|
|
self::T_BINARY => \PDO::PARAM_LOB,
|
|
|
|
self::T_STRING => \PDO::PARAM_STR,
|
|
|
|
self::T_BOOLEAN => \PDO::PARAM_INT, // FIXME: using \PDO::PARAM_BOOL leads to incompatibilities with versions of SQLite bundled prior to PHP 7.3
|
2017-12-19 22:15:05 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
protected $st;
|
|
|
|
protected $db;
|
2018-12-21 17:35:10 +00:00
|
|
|
protected $query;
|
2017-12-19 22:15:05 +00:00
|
|
|
|
2018-12-21 17:35:10 +00:00
|
|
|
public function __construct(\PDO $db, string $query, array $bindings = []) {
|
2017-12-19 22:15:05 +00:00
|
|
|
$this->db = $db;
|
2018-12-21 17:35:10 +00:00
|
|
|
$this->query = $query;
|
2017-12-30 22:04:21 +00:00
|
|
|
$this->retypeArray($bindings);
|
2017-12-19 22:15:05 +00:00
|
|
|
}
|
|
|
|
|
2018-12-21 17:35:10 +00:00
|
|
|
protected function prepare(string $query): bool {
|
|
|
|
try {
|
2018-12-21 22:51:49 +00:00
|
|
|
// PDO statements aren't usually evaluated at creation, and so should not fail
|
2018-12-21 17:35:10 +00:00
|
|
|
$this->st = $this->db->prepare($query);
|
|
|
|
return true;
|
2018-12-21 22:51:49 +00:00
|
|
|
} catch (\PDOException $e) { // @codeCoverageIgnore
|
2020-03-01 20:16:50 +00:00
|
|
|
[$excClass, $excMsg, $excData] = $this->buildPDOException(); // @codeCoverageIgnore
|
2018-12-21 22:51:49 +00:00
|
|
|
throw new $excClass($excMsg, $excData); // @codeCoverageIgnore
|
2018-12-21 17:35:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-19 22:15:05 +00:00
|
|
|
public function __destruct() {
|
2018-11-29 18:45:37 +00:00
|
|
|
unset($this->st, $this->db);
|
2017-12-19 22:15:05 +00:00
|
|
|
}
|
|
|
|
|
2018-11-29 18:45:37 +00:00
|
|
|
public function runArray(array $values = []): Result {
|
2017-12-19 22:15:05 +00:00
|
|
|
$this->st->closeCursor();
|
|
|
|
$this->bindValues($values);
|
|
|
|
try {
|
|
|
|
$this->st->execute();
|
|
|
|
} catch (\PDOException $e) {
|
2020-03-01 20:16:50 +00:00
|
|
|
[$excClass, $excMsg, $excData] = $this->buildPDOException(true);
|
2017-12-19 22:15:05 +00:00
|
|
|
throw new $excClass($excMsg, $excData);
|
|
|
|
}
|
2018-12-06 22:46:00 +00:00
|
|
|
return new PDOResult($this->db, $this->st);
|
2017-12-19 22:15:05 +00:00
|
|
|
}
|
|
|
|
|
2019-03-01 17:17:33 +00:00
|
|
|
protected function bindValue($value, int $type, int $position): bool {
|
2017-12-30 23:50:56 +00:00
|
|
|
return $this->st->bindValue($position, $value, is_null($value) ? \PDO::PARAM_NULL : self::BINDINGS[$type]);
|
2017-12-19 22:15:05 +00:00
|
|
|
}
|
|
|
|
}
|