2016-10-06 02:08:43 +00:00
|
|
|
<?php
|
2017-11-17 01:23:18 +00:00
|
|
|
/** @license MIT
|
|
|
|
* Copyright 2017 J. King, Dustin Wilson et al.
|
|
|
|
* See LICENSE and AUTHORS files for details */
|
|
|
|
|
2016-10-06 02:08:43 +00:00
|
|
|
declare(strict_types=1);
|
2017-03-28 04:12:12 +00:00
|
|
|
namespace JKingWeb\Arsse\Db\SQLite3;
|
2017-08-29 14:50:31 +00:00
|
|
|
|
2017-03-28 04:12:12 +00:00
|
|
|
class Statement extends \JKingWeb\Arsse\Db\AbstractStatement {
|
2017-03-08 14:55:16 +00:00
|
|
|
use ExceptionBuilder;
|
|
|
|
|
|
|
|
const SQLITE_BUSY = 5;
|
|
|
|
const SQLITE_CONSTRAINT = 19;
|
|
|
|
const SQLITE_MISMATCH = 20;
|
2017-03-02 23:42:19 +00:00
|
|
|
const BINDINGS = [
|
2019-03-01 17:17:33 +00:00
|
|
|
self::T_INTEGER => \SQLITE3_INTEGER,
|
|
|
|
self::T_FLOAT => \SQLITE3_FLOAT,
|
|
|
|
self::T_DATETIME => \SQLITE3_TEXT,
|
|
|
|
self::T_BINARY => \SQLITE3_BLOB,
|
|
|
|
self::T_STRING => \SQLITE3_TEXT,
|
|
|
|
self::T_BOOLEAN => \SQLITE3_INTEGER,
|
2017-03-02 23:42:19 +00:00
|
|
|
];
|
2017-04-07 01:41:21 +00:00
|
|
|
|
2017-02-16 20:29:42 +00:00
|
|
|
protected $db;
|
|
|
|
protected $st;
|
2018-12-21 17:35:10 +00:00
|
|
|
protected $query;
|
2016-10-06 02:08:43 +00:00
|
|
|
|
2018-12-21 17:35:10 +00:00
|
|
|
public function __construct(\SQLite3 $db, string $query, array $bindings = []) {
|
2017-02-16 20:29:42 +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-03-02 04:12:42 +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
|
|
|
// statements aren't 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 (\Exception $e) { // @codeCoverageIgnore
|
2020-03-01 20:16:50 +00:00
|
|
|
[$excClass, $excMsg, $excData] = $this->buildException(); // @codeCoverageIgnore
|
2018-12-21 22:51:49 +00:00
|
|
|
throw new $excClass($excMsg, $excData); // @codeCoverageIgnore
|
2018-12-21 17:35:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-02 04:12:42 +00:00
|
|
|
public function __destruct() {
|
2017-08-29 14:50:31 +00:00
|
|
|
try {
|
|
|
|
$this->st->close();
|
2017-09-05 23:35:14 +00:00
|
|
|
} catch (\Throwable $e) { // @codeCoverageIgnore
|
2017-08-29 14:50:31 +00:00
|
|
|
}
|
2017-03-02 04:12:42 +00:00
|
|
|
unset($this->st);
|
|
|
|
}
|
|
|
|
|
2017-06-04 22:00:18 +00:00
|
|
|
public function runArray(array $values = []): \JKingWeb\Arsse\Db\Result {
|
2017-03-02 04:12:42 +00:00
|
|
|
$this->st->clear();
|
2017-06-04 22:00:18 +00:00
|
|
|
$this->bindValues($values);
|
2017-03-08 14:55:16 +00:00
|
|
|
try {
|
|
|
|
$r = $this->st->execute();
|
2017-08-29 14:50:31 +00:00
|
|
|
} catch (\Exception $e) {
|
2020-03-01 20:16:50 +00:00
|
|
|
[$excClass, $excMsg, $excData] = $this->buildException();
|
2017-03-08 14:55:16 +00:00
|
|
|
throw new $excClass($excMsg, $excData);
|
|
|
|
}
|
2017-03-09 19:48:42 +00:00
|
|
|
$changes = $this->db->changes();
|
|
|
|
$lastId = $this->db->lastInsertRowID();
|
|
|
|
return new Result($r, [$changes, $lastId], $this);
|
2017-03-02 19:19:16 +00:00
|
|
|
}
|
2017-04-06 15:02:47 +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) ? \SQLITE3_NULL : self::BINDINGS[$type]);
|
2017-04-06 15:02:47 +00:00
|
|
|
}
|
2017-08-29 14:50:31 +00:00
|
|
|
}
|