2016-10-06 02:08:43 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
2017-03-07 23:01:13 +00:00
|
|
|
namespace JKingWeb\NewsSync\Db\SQLite3;
|
|
|
|
use JKingWeb\NewsSync\Db\Exception;
|
2017-03-08 14:55:16 +00:00
|
|
|
use JKingWeb\NewsSync\Db\ExceptionInput;
|
|
|
|
use JKingWeb\NewsSync\Db\ExceptionTimeout;
|
2016-10-06 02:08:43 +00:00
|
|
|
|
2017-03-07 23:01:13 +00:00
|
|
|
class Statement extends \JKingWeb\NewsSync\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 = [
|
|
|
|
"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-02-16 20:29:42 +00:00
|
|
|
protected $db;
|
|
|
|
protected $st;
|
|
|
|
protected $types;
|
2016-10-06 02:08:43 +00:00
|
|
|
|
2017-03-02 19:19:16 +00:00
|
|
|
public function __construct(\SQLite3 $db, \SQLite3Stmt $st, array $bindings = []) {
|
2017-02-16 20:29:42 +00:00
|
|
|
$this->db = $db;
|
|
|
|
$this->st = $st;
|
2017-03-02 04:12:42 +00:00
|
|
|
$this->rebindArray($bindings);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __destruct() {
|
2017-03-03 18:20:26 +00:00
|
|
|
try {$this->st->close();} catch(\Throwable $e) {}
|
2017-03-02 04:12:42 +00:00
|
|
|
unset($this->st);
|
|
|
|
}
|
|
|
|
|
2017-03-02 23:42:19 +00:00
|
|
|
public static function dateFormat(int $part = self::TS_BOTH): string {
|
|
|
|
return ([
|
|
|
|
self::TS_TIME => 'h:i:sP',
|
|
|
|
self::TS_DATE => 'Y-m-d',
|
|
|
|
self::TS_BOTH => 'Y-m-d h:i:sP',
|
|
|
|
])[$part];
|
2017-03-02 04:12:42 +00:00
|
|
|
}
|
|
|
|
|
2017-03-07 23:01:13 +00:00
|
|
|
public function runArray(array $values = null): \JKingWeb\NewsSync\Db\Result {
|
2017-03-02 04:12:42 +00:00
|
|
|
$this->st->clear();
|
|
|
|
$l = sizeof($values);
|
|
|
|
for($a = 0; $a < $l; $a++) {
|
2017-03-02 19:19:16 +00:00
|
|
|
// find the right SQLite binding type for the value/specified type
|
2017-03-02 04:12:42 +00:00
|
|
|
if($values[$a]===null) {
|
|
|
|
$type = \SQLITE3_NULL;
|
2017-03-02 19:19:16 +00:00
|
|
|
} else if(array_key_exists($a,$this->types)) {
|
2017-03-02 23:42:19 +00:00
|
|
|
if(!array_key_exists($this->types[$a], self::BINDINGS)) throw new Exception("paramTypeUnknown", $this->types[$a]);
|
|
|
|
$type = self::BINDINGS[$this->types[$a]];
|
2017-03-02 04:12:42 +00:00
|
|
|
} else {
|
2017-03-03 18:20:26 +00:00
|
|
|
throw new Exception("paramTypeMissing", $a+1);
|
2017-03-02 04:12:42 +00:00
|
|
|
}
|
2017-03-02 23:42:19 +00:00
|
|
|
// cast value if necessary
|
2017-03-03 03:43:59 +00:00
|
|
|
$values[$a] = $this->cast($values[$a], $this->types[$a]);
|
2017-03-02 23:42:19 +00:00
|
|
|
// re-adjust for null casts
|
2017-03-03 03:43:59 +00:00
|
|
|
if($values[$a]===null) $type = \SQLITE3_NULL;
|
2017-03-02 23:42:19 +00:00
|
|
|
// perform binding
|
2017-03-03 18:20:26 +00:00
|
|
|
$this->st->bindValue($a+1, $values[$a], $type);
|
2017-02-16 20:29:42 +00:00
|
|
|
}
|
2017-03-08 14:55:16 +00:00
|
|
|
try {
|
|
|
|
$r = $this->st->execute();
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
list($excClass, $excMsg, $excData) = $this->exceptionBuild();
|
|
|
|
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
|
|
|
}
|
2016-10-06 02:08:43 +00:00
|
|
|
}
|