The clean & modern RSS server that doesn't give you any crap. https://thearsse.com/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
3.0 KiB

<?php
/** @license MIT
* Copyright 2017 J. King, Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
namespace JKingWeb\Arsse\Db;
use JKingWeb\Arsse\Misc\Date;
abstract class AbstractStatement implements Statement {
protected $types = [];
protected $isNullable = [];
protected $values = ['pre' => [], 'post' => []];
abstract public function runArray(array $values = []): Result;
public function run(...$values): Result {
return $this->runArray($values);
}
public function rebind(...$bindings): bool {
return $this->rebindArray($bindings);
}
public function rebindArray(array $bindings, bool $append = false): bool {
if (!$append) {
$this->types = [];
}
foreach ($bindings as $binding) {
if (is_array($binding)) {
// recursively flatten any arrays, which may be provided for SET or IN() clauses
$this->rebindArray($binding, true);
} else {
$binding = trim(strtolower($binding));
if (strpos($binding, "strict ")===0) {
// "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;
}
if (!array_key_exists($binding, self::TYPES)) {
throw new Exception("paramTypeInvalid", $binding); // @codeCoverageIgnore
}
$this->types[] = self::TYPES[$binding];
}
}
return true;
}
protected function cast($v, string $t, bool $nullable) {
switch ($t) {
case "date":
if (is_null($v) && !$nullable) {
$v = 0;
}
return Date::transform($v, "date");
case "time":
if (is_null($v) && !$nullable) {
$v = 0;
}
return Date::transform($v, "time");
case "datetime":
if (is_null($v) && !$nullable) {
$v = 0;
}
return Date::transform($v, "sql");
case "null":
case "integer":
case "float":
case "binary":
case "string":
case "boolean":
if ($t=="binary") {
$t = "string";
}
if ($v instanceof \DateTimeInterface) {
if ($t=="string") {
return Date::transform($v, "sql");
} else {
$v = $v->getTimestamp();
settype($v, $t);
}
} else {
settype($v, $t);
}
return $v;
default:
throw new Exception("paramTypeUnknown", $type); // @codeCoverageIgnore
}
}
}