2016-10-05 22:08:43 -04:00
|
|
|
<?php
|
2017-11-16 20:23:18 -05:00
|
|
|
/** @license MIT
|
|
|
|
* Copyright 2017 J. King, Dustin Wilson et al.
|
|
|
|
* See LICENSE and AUTHORS files for details */
|
|
|
|
|
2016-10-05 22:08:43 -04:00
|
|
|
declare(strict_types=1);
|
2017-03-27 23:12:12 -05:00
|
|
|
namespace JKingWeb\Arsse\Db\SQLite3;
|
2017-08-29 10:50:31 -04:00
|
|
|
|
2017-03-27 23:12:12 -05:00
|
|
|
use JKingWeb\Arsse\Db\Exception;
|
|
|
|
use JKingWeb\Arsse\Db\ExceptionInput;
|
|
|
|
use JKingWeb\Arsse\Db\ExceptionTimeout;
|
2016-10-05 22:08:43 -04:00
|
|
|
|
2017-03-27 23:12:12 -05:00
|
|
|
class Statement extends \JKingWeb\Arsse\Db\AbstractStatement {
|
2017-03-08 09:55:16 -05:00
|
|
|
use ExceptionBuilder;
|
|
|
|
|
|
|
|
const SQLITE_BUSY = 5;
|
|
|
|
const SQLITE_CONSTRAINT = 19;
|
|
|
|
const SQLITE_MISMATCH = 20;
|
2017-03-02 18:42:19 -05:00
|
|
|
const BINDINGS = [
|
2017-04-06 21:41:21 -04:00
|
|
|
"null" => \SQLITE3_NULL,
|
|
|
|
"integer" => \SQLITE3_INTEGER,
|
|
|
|
"float" => \SQLITE3_FLOAT,
|
|
|
|
"date" => \SQLITE3_TEXT,
|
|
|
|
"time" => \SQLITE3_TEXT,
|
|
|
|
"datetime" => \SQLITE3_TEXT,
|
|
|
|
"binary" => \SQLITE3_BLOB,
|
|
|
|
"string" => \SQLITE3_TEXT,
|
|
|
|
"boolean" => \SQLITE3_INTEGER,
|
2017-03-02 18:42:19 -05:00
|
|
|
];
|
2017-04-06 21:41:21 -04:00
|
|
|
|
2017-02-16 14:29:42 -06:00
|
|
|
protected $db;
|
|
|
|
protected $st;
|
2016-10-05 22:08:43 -04:00
|
|
|
|
2017-07-07 11:49:54 -04:00
|
|
|
public function __construct(\SQLite3 $db, \SQLite3Stmt $st, array $bindings = []) {
|
2017-02-16 14:29:42 -06:00
|
|
|
$this->db = $db;
|
|
|
|
$this->st = $st;
|
2017-03-01 23:12:42 -05:00
|
|
|
$this->rebindArray($bindings);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __destruct() {
|
2017-08-29 10:50:31 -04:00
|
|
|
try {
|
|
|
|
$this->st->close();
|
2017-09-05 19:35:14 -04:00
|
|
|
} catch (\Throwable $e) { // @codeCoverageIgnore
|
2017-08-29 10:50:31 -04:00
|
|
|
}
|
2017-03-01 23:12:42 -05:00
|
|
|
unset($this->st);
|
|
|
|
}
|
|
|
|
|
2017-06-04 18:00:18 -04:00
|
|
|
public function runArray(array $values = []): \JKingWeb\Arsse\Db\Result {
|
2017-03-01 23:12:42 -05:00
|
|
|
$this->st->clear();
|
2017-06-04 18:00:18 -04:00
|
|
|
$this->bindValues($values);
|
2017-03-08 09:55:16 -05:00
|
|
|
try {
|
|
|
|
$r = $this->st->execute();
|
2017-08-29 10:50:31 -04:00
|
|
|
} catch (\Exception $e) {
|
2017-03-08 09:55:16 -05:00
|
|
|
list($excClass, $excMsg, $excData) = $this->exceptionBuild();
|
|
|
|
throw new $excClass($excMsg, $excData);
|
|
|
|
}
|
2017-03-09 14:48:42 -05:00
|
|
|
$changes = $this->db->changes();
|
|
|
|
$lastId = $this->db->lastInsertRowID();
|
|
|
|
return new Result($r, [$changes, $lastId], $this);
|
2017-03-02 14:19:16 -05:00
|
|
|
}
|
2017-04-06 11:02:47 -04:00
|
|
|
|
|
|
|
protected function bindValues(array $values, int $offset = 0): int {
|
|
|
|
$a = $offset;
|
2017-08-29 10:50:31 -04:00
|
|
|
foreach ($values as $value) {
|
|
|
|
if (is_array($value)) {
|
2017-04-06 11:02:47 -04:00
|
|
|
// recursively flatten any arrays, which may be provided for SET or IN() clauses
|
|
|
|
$a += $this->bindValues($value, $a);
|
2017-08-29 10:50:31 -04:00
|
|
|
} elseif (array_key_exists($a, $this->types)) {
|
2017-05-04 19:12:33 -04:00
|
|
|
// if the parameter type is something other than the known values, this is an error
|
2017-07-21 11:13:04 -04:00
|
|
|
assert(array_key_exists($this->types[$a], self::BINDINGS), new Exception("paramTypeUnknown", $this->types[$a]));
|
2017-05-04 19:12:33 -04:00
|
|
|
// if the parameter type is null or the value is null (and the type is nullable), just bind null
|
2017-08-29 10:50:31 -04:00
|
|
|
if ($this->types[$a]=="null" || ($this->isNullable[$a] && is_null($value))) {
|
2017-05-04 19:12:33 -04:00
|
|
|
$this->st->bindValue($a+1, null, \SQLITE3_NULL);
|
2017-08-29 10:50:31 -04:00
|
|
|
} else {
|
2017-05-04 19:12:33 -04:00
|
|
|
// otherwise cast the value to the right type and bind the result
|
2017-04-06 11:02:47 -04:00
|
|
|
$type = self::BINDINGS[$this->types[$a]];
|
2017-05-04 19:12:33 -04:00
|
|
|
$value = $this->cast($value, $this->types[$a], $this->isNullable[$a]);
|
|
|
|
// re-adjust for null casts
|
2017-08-29 10:50:31 -04:00
|
|
|
if ($value===null) {
|
2017-07-20 22:40:09 -04:00
|
|
|
$type = \SQLITE3_NULL;
|
|
|
|
}
|
2017-05-04 19:12:33 -04:00
|
|
|
// perform binding
|
|
|
|
$this->st->bindValue($a+1, $value, $type);
|
2017-04-06 11:02:47 -04:00
|
|
|
}
|
|
|
|
$a++;
|
2017-05-04 19:12:33 -04:00
|
|
|
} else {
|
|
|
|
throw new Exception("paramTypeMissing", $a+1);
|
2017-04-06 11:02:47 -04:00
|
|
|
}
|
|
|
|
}
|
2017-04-06 21:24:26 -04:00
|
|
|
return $a - $offset;
|
2017-04-06 11:02:47 -04:00
|
|
|
}
|
2017-08-29 10:50:31 -04:00
|
|
|
}
|