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

63 lines
1.8 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
use JKingWeb\Arsse\Db\Exception;
use JKingWeb\Arsse\Db\ExceptionInput;
use JKingWeb\Arsse\Db\ExceptionTimeout;
2017-03-28 04:12:12 +00:00
class Statement extends \JKingWeb\Arsse\Db\AbstractStatement {
use ExceptionBuilder;
const SQLITE_BUSY = 5;
const SQLITE_CONSTRAINT = 19;
const SQLITE_MISMATCH = 20;
const BINDINGS = [
"integer" => \SQLITE3_INTEGER,
"float" => \SQLITE3_FLOAT,
"datetime" => \SQLITE3_TEXT,
"binary" => \SQLITE3_BLOB,
"string" => \SQLITE3_TEXT,
"boolean" => \SQLITE3_INTEGER,
];
2017-02-16 20:29:42 +00:00
protected $db;
protected $st;
public function __construct(\SQLite3 $db, \SQLite3Stmt $st, array $bindings = []) {
2017-02-16 20:29:42 +00:00
$this->db = $db;
$this->st = $st;
$this->retypeArray($bindings);
}
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) {
list($excClass, $excMsg, $excData) = $this->exceptionBuild();
throw new $excClass($excMsg, $excData);
}
$changes = $this->db->changes();
$lastId = $this->db->lastInsertRowID();
return new Result($r, [$changes, $lastId], $this);
}
protected function bindValue($value, string $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
}