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/SQLite3/Statement.php

71 lines
2.3 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);
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 {
use ExceptionBuilder;
2020-03-01 23:32:01 +00:00
public const SQLITE_BUSY = 5;
public const SQLITE_CONSTRAINT = 19;
public const SQLITE_MISMATCH = 20;
protected const BINDINGS = [
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,
2022-04-25 22:30:13 +00:00
self::T_BOOLEAN => \SQLITE3_INTEGER, // NOTE: Integers are used rather than booleans so that they may be manipulated arithmetically
];
2017-02-16 20:29:42 +00:00
protected $db;
protected $st;
protected $query;
public function __construct(\SQLite3 $db, string $query, array $bindings = []) {
2017-02-16 20:29:42 +00:00
$this->db = $db;
$this->query = $query;
$this->retypeArray($bindings);
}
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
$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
}
}
public function __destruct() {
2017-08-29 14:50:31 +00:00
try {
$this->st->close();
} catch (\Throwable $e) { // @codeCoverageIgnore
2017-08-29 14:50:31 +00:00
}
unset($this->st);
}
public function runArray(array $values = []): \JKingWeb\Arsse\Db\Result {
$this->st->clear();
$this->bindValues($values);
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();
throw new $excClass($excMsg, $excData);
}
$changes = $this->db->changes();
$lastId = $this->db->lastInsertRowID();
return new Result($r, [$changes, $lastId], $this);
}
protected function bindValue($value, int $type, int $position): bool {
return $this->st->bindValue($position, $value, is_null($value) ? \SQLITE3_NULL : self::BINDINGS[$type]);
}
2017-08-29 14:50:31 +00:00
}