2017-03-02 23:42:19 +00:00
|
|
|
<?php
|
2017-11-17 01:23:18 +00:00
|
|
|
/** @license MIT
|
|
|
|
* Copyright 2017 J. King, Dustin Wilson et al.
|
|
|
|
* See LICENSE and AUTHORS files for details */
|
|
|
|
|
2017-03-02 23:42:19 +00:00
|
|
|
declare(strict_types=1);
|
2017-03-28 04:12:12 +00:00
|
|
|
namespace JKingWeb\Arsse\Db;
|
2017-08-29 14:50:31 +00:00
|
|
|
|
2017-07-17 11:47:57 +00:00
|
|
|
use JKingWeb\Arsse\Misc\Date;
|
2017-12-30 23:50:56 +00:00
|
|
|
use JKingWeb\Arsse\Misc\ValueInfo;
|
2017-03-02 23:42:19 +00:00
|
|
|
|
|
|
|
abstract class AbstractStatement implements Statement {
|
2017-05-04 23:12:33 +00:00
|
|
|
protected $types = [];
|
|
|
|
protected $isNullable = [];
|
2017-03-02 23:42:19 +00:00
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
abstract public function runArray(array $values = []): Result;
|
2017-12-30 23:50:56 +00:00
|
|
|
abstract protected function bindValue($value, string $type, int $position): bool;
|
2017-03-02 23:42:19 +00:00
|
|
|
|
|
|
|
public function run(...$values): Result {
|
|
|
|
return $this->runArray($values);
|
|
|
|
}
|
|
|
|
|
2017-12-30 22:04:21 +00:00
|
|
|
public function retype(...$bindings): bool {
|
|
|
|
return $this->retypeArray($bindings);
|
2017-03-02 23:42:19 +00:00
|
|
|
}
|
|
|
|
|
2017-12-30 22:04:21 +00:00
|
|
|
public function retypeArray(array $bindings, bool $append = false): bool {
|
2017-08-29 14:50:31 +00:00
|
|
|
if (!$append) {
|
2017-07-21 02:40:09 +00:00
|
|
|
$this->types = [];
|
|
|
|
}
|
2017-08-29 14:50:31 +00:00
|
|
|
foreach ($bindings as $binding) {
|
|
|
|
if (is_array($binding)) {
|
2017-04-06 15:02:47 +00:00
|
|
|
// recursively flatten any arrays, which may be provided for SET or IN() clauses
|
2017-12-30 22:04:21 +00:00
|
|
|
$this->retypeArray($binding, true);
|
2017-04-06 15:02:47 +00:00
|
|
|
} else {
|
|
|
|
$binding = trim(strtolower($binding));
|
2017-08-29 14:50:31 +00:00
|
|
|
if (strpos($binding, "strict ")===0) {
|
2017-05-04 23:12:33 +00:00
|
|
|
// "strict" types' values may never be null; null values will later be cast to the type specified
|
|
|
|
$this->isNullable[] = false;
|
|
|
|
$binding = substr($binding, 7);
|
|
|
|
} else {
|
|
|
|
$this->isNullable[] = true;
|
|
|
|
}
|
2017-08-29 14:50:31 +00:00
|
|
|
if (!array_key_exists($binding, self::TYPES)) {
|
2017-07-22 19:29:12 +00:00
|
|
|
throw new Exception("paramTypeInvalid", $binding); // @codeCoverageIgnore
|
2017-07-21 02:40:09 +00:00
|
|
|
}
|
2017-04-06 15:02:47 +00:00
|
|
|
$this->types[] = self::TYPES[$binding];
|
|
|
|
}
|
2017-03-02 23:42:19 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-04 23:12:33 +00:00
|
|
|
protected function cast($v, string $t, bool $nullable) {
|
2017-08-29 14:50:31 +00:00
|
|
|
switch ($t) {
|
2017-04-07 01:41:21 +00:00
|
|
|
case "datetime":
|
2017-12-30 23:50:56 +00:00
|
|
|
$v = Date::transform($v, "sql");
|
2017-08-29 14:50:31 +00:00
|
|
|
if (is_null($v) && !$nullable) {
|
2017-07-21 02:40:09 +00:00
|
|
|
$v = 0;
|
2017-12-30 23:50:56 +00:00
|
|
|
$v = Date::transform($v, "sql");
|
2017-07-21 02:40:09 +00:00
|
|
|
}
|
2017-12-30 23:50:56 +00:00
|
|
|
return $v;
|
2017-04-07 01:41:21 +00:00
|
|
|
case "integer":
|
2017-12-30 23:50:56 +00:00
|
|
|
return ValueInfo::normalize($v, ValueInfo::T_INT | ($nullable ? ValueInfo::M_NULL : 0), null, "sql");
|
2017-04-07 01:41:21 +00:00
|
|
|
case "float":
|
2017-12-30 23:50:56 +00:00
|
|
|
return ValueInfo::normalize($v, ValueInfo::T_FLOAT | ($nullable ? ValueInfo::M_NULL : 0), null, "sql");
|
2017-04-07 01:41:21 +00:00
|
|
|
case "binary":
|
|
|
|
case "string":
|
2017-12-30 23:50:56 +00:00
|
|
|
return ValueInfo::normalize($v, ValueInfo::T_STRING | ($nullable ? ValueInfo::M_NULL : 0), null, "sql");
|
2017-04-07 01:41:21 +00:00
|
|
|
case "boolean":
|
2017-12-30 23:50:56 +00:00
|
|
|
$v = ValueInfo::normalize($v, ValueInfo::T_BOOL | ($nullable ? ValueInfo::M_NULL : 0), null, "sql");
|
|
|
|
return is_null($v) ? $v : (int) $v;
|
2017-04-07 01:41:21 +00:00
|
|
|
default:
|
2017-07-22 19:29:12 +00:00
|
|
|
throw new Exception("paramTypeUnknown", $type); // @codeCoverageIgnore
|
2017-04-07 01:41:21 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-30 23:50:56 +00:00
|
|
|
|
|
|
|
protected function bindValues(array $values, int $offset = 0): int {
|
|
|
|
$a = $offset;
|
|
|
|
foreach ($values as $value) {
|
|
|
|
if (is_array($value)) {
|
|
|
|
// recursively flatten any arrays, which may be provided for SET or IN() clauses
|
|
|
|
$a += $this->bindValues($value, $a);
|
|
|
|
} elseif (array_key_exists($a, $this->types)) {
|
|
|
|
$value = $this->cast($value, $this->types[$a], $this->isNullable[$a]);
|
|
|
|
$this->bindValue($value, $this->types[$a], $a+1);
|
|
|
|
$a++;
|
|
|
|
} else {
|
|
|
|
throw new Exception("paramTypeMissing", $a+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $a - $offset;
|
|
|
|
}
|
2017-08-29 14:50:31 +00:00
|
|
|
}
|