2016-10-06 02:08:43 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace JKingWeb\NewsSync\Db;
|
2017-03-02 19:19:16 +00:00
|
|
|
use JKingWeb\NewsSync\Db\DriverSQLite3 as Driver;
|
2016-10-06 02:08:43 +00:00
|
|
|
|
|
|
|
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 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() {
|
|
|
|
$this->st->close();
|
|
|
|
unset($this->st);
|
|
|
|
}
|
|
|
|
|
|
|
|
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++) {
|
2017-03-02 19:19:16 +00:00
|
|
|
// find the right SQLite binding type for the value/specified type
|
|
|
|
$type = null;
|
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)) {
|
|
|
|
$type = $this->translateType($this->types[$a]);
|
2017-03-02 04:12:42 +00:00
|
|
|
} else {
|
2017-03-02 19:19:16 +00:00
|
|
|
$type = \SQLITE3_TEXT;
|
2017-03-02 04:12:42 +00:00
|
|
|
}
|
2017-03-02 19:19:16 +00:00
|
|
|
// cast values if necessary
|
|
|
|
switch($this->types[$a]) {
|
2017-03-02 04:12:42 +00:00
|
|
|
case "null":
|
2017-03-02 19:19:16 +00:00
|
|
|
$value = null; break;
|
2017-02-16 20:29:42 +00:00
|
|
|
case "integer":
|
2017-03-02 19:19:16 +00:00
|
|
|
$value = (int) $values[$a]; break;
|
2017-02-16 20:29:42 +00:00
|
|
|
case "float":
|
2017-03-02 19:19:16 +00:00
|
|
|
$value = (float) $values[$a]; break;
|
2017-02-16 20:29:42 +00:00
|
|
|
case "date":
|
2017-03-02 19:19:16 +00:00
|
|
|
$value = Driver::formatDate($values[$a], Driver::TS_DATE); break;
|
2017-02-16 20:29:42 +00:00
|
|
|
case "time":
|
2017-03-02 19:19:16 +00:00
|
|
|
$value = Driver::formatDate($values[$a], Driver::TS_TIME); break;
|
2017-02-16 20:29:42 +00:00
|
|
|
case "datetime":
|
2017-03-02 19:19:16 +00:00
|
|
|
$value = Driver::formatDate($values[$a], Driver::TS_BOTH); break;
|
2017-02-16 20:29:42 +00:00
|
|
|
case "binary":
|
2017-03-02 19:19:16 +00:00
|
|
|
$value = (string) $values[$a]; break;
|
2017-02-16 20:29:42 +00:00
|
|
|
case "text":
|
2017-03-02 19:19:16 +00:00
|
|
|
$value = $values[$a]; break;
|
2017-02-16 20:29:42 +00:00
|
|
|
case "boolean":
|
2017-03-02 19:19:16 +00:00
|
|
|
$value = (bool) $values[$a]; break;
|
2017-02-16 20:29:42 +00:00
|
|
|
default:
|
2017-03-02 19:19:16 +00:00
|
|
|
throw new Exception("paramTypeUnknown", $type);
|
|
|
|
}
|
|
|
|
if($type===null) {
|
|
|
|
$this->st->bindParam($a+1, $value);
|
|
|
|
} else {
|
|
|
|
$this->st->bindParam($a+1, $value, $type);
|
2017-02-16 20:29:42 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-02 19:19:16 +00:00
|
|
|
return new ResultSQLite3($this->st->execute(), $this->db->changes(), $this);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function rebind(...$bindings): bool {
|
|
|
|
return $this->rebindArray($bindings);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function translateType(string $type) {
|
|
|
|
switch($type) {
|
|
|
|
case "null":
|
|
|
|
return \SQLITE3_NULL;
|
|
|
|
case "integer":
|
|
|
|
return \SQLITE3_INTEGER;
|
|
|
|
case "float":
|
|
|
|
return \SQLITE3_FLOAT;
|
|
|
|
case "date":
|
|
|
|
case "time":
|
|
|
|
case "datetime":
|
|
|
|
return \SQLITE3_TEXT;
|
|
|
|
case "binary":
|
|
|
|
return \SQLITE3_BLOB;
|
|
|
|
case "text":
|
|
|
|
return \SQLITE3_TEXT;
|
|
|
|
case "boolean":
|
|
|
|
return \SQLITE3_INTEGER;
|
|
|
|
default:
|
|
|
|
throw new Db\Exception("paramTypeUnknown", $binding);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function rebindArray(array $bindings): bool {
|
|
|
|
$this->types = [];
|
|
|
|
foreach($bindings as $binding) {
|
|
|
|
$binding = trim(strtolower($binding));
|
|
|
|
if(!array_key_exists($binding, self::TYPES)) throw new Db\Exception("paramTypeInvalid", $binding);
|
|
|
|
$this->types[] = self::TYPES[$binding];
|
|
|
|
}
|
|
|
|
return true;
|
2017-02-16 20:29:42 +00:00
|
|
|
}
|
2016-10-06 02:08:43 +00:00
|
|
|
}
|