1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-23 09:02:41 +00:00
Arsse/lib/Db/PDOStatement.php

63 lines
2 KiB
PHP
Raw Normal View History

<?php
/** @license MIT
* Copyright 2017 J. King, Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
2021-04-14 15:17:01 +00:00
namespace JKingWeb\Arsse\Db;
abstract class PDOStatement extends AbstractStatement {
use PDOError;
2020-03-01 23:32:01 +00:00
protected const BINDINGS = [
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,
2022-04-25 22:30:13 +00:00
self::T_BOOLEAN => \PDO::PARAM_INT, // NOTE: Integers are used rather than booleans so that they may be manipulated arithmetically
];
protected $st;
protected $db;
protected $query;
public function __construct(\PDO $db, string $query, array $bindings = []) {
$this->db = $db;
$this->query = $query;
$this->retypeArray($bindings);
}
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
$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
}
}
public function __destruct() {
unset($this->st, $this->db);
}
public function runArray(array $values = []): Result {
$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);
throw new $excClass($excMsg, $excData);
}
2018-12-06 22:46:00 +00:00
return new PDOResult($this->db, $this->st);
}
protected function bindValue($value, int $type, int $position): bool {
return $this->st->bindValue($position, $value, is_null($value) ? \PDO::PARAM_NULL : self::BINDINGS[$type]);
}
}