2018-12-14 00:47:51 +00:00
|
|
|
<?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\PostgreSQL;
|
|
|
|
|
|
|
|
class Statement extends \JKingWeb\Arsse\Db\AbstractStatement {
|
|
|
|
use Dispatch;
|
|
|
|
|
2020-03-01 23:32:01 +00:00
|
|
|
protected const BINDINGS = [
|
2019-03-01 17:17:33 +00:00
|
|
|
self::T_INTEGER => "bigint",
|
|
|
|
self::T_FLOAT => "decimal",
|
|
|
|
self::T_DATETIME => "timestamp(0) without time zone",
|
|
|
|
self::T_BINARY => "bytea",
|
|
|
|
self::T_STRING => "text",
|
|
|
|
self::T_BOOLEAN => "smallint", // FIXME: using boolean leads to incompatibilities with versions of SQLite bundled prior to PHP 7.3
|
2018-12-14 00:47:51 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
protected $db;
|
|
|
|
protected $in = [];
|
2018-12-21 17:35:10 +00:00
|
|
|
protected $query;
|
2018-12-14 00:47:51 +00:00
|
|
|
protected $qMunged;
|
|
|
|
protected $bindings;
|
|
|
|
|
|
|
|
public function __construct($db, string $query, array $bindings = []) {
|
2019-01-23 21:34:54 +00:00
|
|
|
$this->db = $db;
|
2018-12-21 17:35:10 +00:00
|
|
|
$this->query = $query;
|
2018-12-14 00:47:51 +00:00
|
|
|
$this->retypeArray($bindings);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function runArray(array $values = []): \JKingWeb\Arsse\Db\Result {
|
|
|
|
$this->in = [];
|
|
|
|
$this->bindValues($values);
|
|
|
|
$r = $this->dispatchQuery($this->qMunged, $this->in);
|
2019-01-11 00:01:32 +00:00
|
|
|
$this->in = [];
|
2018-12-14 00:47:51 +00:00
|
|
|
if (is_resource($r)) {
|
|
|
|
return new Result($this->db, $r);
|
|
|
|
} else {
|
2020-03-01 20:16:50 +00:00
|
|
|
[$excClass, $excMsg, $excData] = $r;
|
2018-12-14 00:47:51 +00:00
|
|
|
throw new $excClass($excMsg, $excData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-01 17:17:33 +00:00
|
|
|
protected function bindValue($value, int $type, int $position): bool {
|
2018-12-14 00:47:51 +00:00
|
|
|
$this->in[] = $value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-21 17:35:10 +00:00
|
|
|
public static function mungeQuery(string $q, array $types, ...$extraData): string {
|
|
|
|
$mungeParamMarkers = (bool) ($extraData[0] ?? true);
|
2018-12-14 00:47:51 +00:00
|
|
|
$q = explode("?", $q);
|
|
|
|
$out = "";
|
|
|
|
for ($b = 1; $b < sizeof($q); $b++) {
|
|
|
|
$a = $b - 1;
|
|
|
|
$mark = $mungeParamMarkers ? "\$$b" : "?";
|
2019-03-01 17:17:33 +00:00
|
|
|
$type = isset($types[$a]) ? "::".self::BINDINGS[$types[$a] % self::T_NOT_NULL] : "";
|
2018-12-14 00:47:51 +00:00
|
|
|
$out .= $q[$a].$mark.$type;
|
|
|
|
}
|
|
|
|
$out .= array_pop($q);
|
|
|
|
return $out;
|
|
|
|
}
|
2018-12-21 17:35:10 +00:00
|
|
|
|
|
|
|
protected function prepare(string $query): bool {
|
|
|
|
$this->qMunged = $query;
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-14 00:47:51 +00:00
|
|
|
}
|