2016-10-06 02:08:43 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace JKingWeb\NewsSync\Db;
|
|
|
|
|
|
|
|
class StatementSQLite3 implements Statement {
|
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 03:47:51 +00:00
|
|
|
public function __construct($db, $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() {
|
|
|
|
$this->st->close();
|
|
|
|
unset($this->st);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __invoke(...$values) {
|
|
|
|
return $this->runArray($values);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run(...$values): Result {
|
|
|
|
return $this->runArray($values);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function runArray(array $values = null): Result {
|
|
|
|
$this->st->clear();
|
|
|
|
$l = sizeof($values);
|
|
|
|
for($a = 0; $a < $l; $a++) {
|
|
|
|
if($values[$a]===null) {
|
|
|
|
$type = \SQLITE3_NULL;
|
|
|
|
} else {
|
|
|
|
$type = (array_key_exists($a,$this->types)) ? $this->types[$a] : \SQLITE3_TEXT;
|
|
|
|
}
|
|
|
|
$this->st->bindParam($a+1, $values[$a], $type);
|
|
|
|
}
|
|
|
|
return new ResultSQLite3($this->st->execute(), $this->db->changes(), $this);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function rebind(...$bindings): bool {
|
|
|
|
return $this->rebindArray($bindings);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function rebindArray(array $bindings): bool {
|
2017-02-16 20:29:42 +00:00
|
|
|
$this->types = [];
|
|
|
|
foreach($bindings as $binding) {
|
|
|
|
switch(trim(strtolower($binding))) {
|
2017-03-02 04:12:42 +00:00
|
|
|
case "null":
|
|
|
|
case "nil":
|
|
|
|
$this->types[] = \SQLITE3_NULL; break;
|
2017-02-16 20:29:42 +00:00
|
|
|
case "int":
|
|
|
|
case "integer":
|
|
|
|
$this->types[] = \SQLITE3_INTEGER; break;
|
|
|
|
case "float":
|
|
|
|
case "double":
|
|
|
|
case "real":
|
|
|
|
case "numeric":
|
|
|
|
$this->types[] = \SQLITE3_FLOAT; break;
|
|
|
|
case "date":
|
|
|
|
case "time":
|
|
|
|
case "datetime":
|
|
|
|
case "timestamp":
|
|
|
|
$this->types[] = \SQLITE3_TEXT; break;
|
|
|
|
case "blob":
|
|
|
|
case "bin":
|
|
|
|
case "binary":
|
|
|
|
$this->types[] = \SQLITE3_BLOB; break;
|
|
|
|
case "text":
|
|
|
|
case "string":
|
|
|
|
case "str":
|
|
|
|
$this->types[] = \SQLITE3_TEXT; break;
|
|
|
|
case "bool":
|
|
|
|
case "boolean":
|
|
|
|
case "bit":
|
|
|
|
$this->types[] = \SQLITE3_INTEGER; break;
|
|
|
|
default:
|
|
|
|
$this->types[] = \SQLITE3_TEXT; break;
|
|
|
|
}
|
|
|
|
}
|
2017-03-02 04:12:42 +00:00
|
|
|
return true;
|
2017-02-16 20:29:42 +00:00
|
|
|
}
|
2016-10-06 02:08:43 +00:00
|
|
|
}
|