Browse Source
No testing has been performed yet, but changes are extensive enough to warrant a commit. Of particular note: - SQL states are enumerated in a separate trait to reduce duplication - PDOStatement is now an abstract class to avoid duplication of engine-specific error handling - Error handling has been cleaned up somewhatmicrosub
J. King
5 years ago
23 changed files with 353 additions and 122 deletions
@ -0,0 +1,30 @@ |
|||||
|
<?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\MySQL; |
||||
|
|
||||
|
use JKingWeb\Arsse\Db\Exception; |
||||
|
use JKingWeb\Arsse\Db\ExceptionInput; |
||||
|
use JKingWeb\Arsse\Db\ExceptionTimeout; |
||||
|
|
||||
|
trait ExceptionBuilder { |
||||
|
protected function buildException(): array { |
||||
|
return self::buildEngineException($this->db->errno, $this->db->error); |
||||
|
} |
||||
|
|
||||
|
public static function buildEngineException($code, string $msg): array { |
||||
|
switch ($code) { |
||||
|
case 1205: |
||||
|
return [ExceptionTimeout::class, 'general', $msg]; |
||||
|
case 1364: |
||||
|
return [ExceptionInput::class, "constraintViolation", $msg]; |
||||
|
case 1366: |
||||
|
return [ExceptionInput::class, 'engineTypeViolation', $msg]; |
||||
|
default: |
||||
|
return [Exception::class, 'engineErrorGeneral', $msg]; |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,51 @@ |
|||||
|
<?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\MySQL; |
||||
|
|
||||
|
use JKingWeb\Arsse\Db\Exception; |
||||
|
|
||||
|
class Result extends \JKingWeb\Arsse\Db\AbstractResult { |
||||
|
protected $st; |
||||
|
protected $set; |
||||
|
protected $cur = null; |
||||
|
protected $rows = 0; |
||||
|
protected $id = 0; |
||||
|
|
||||
|
// actual public methods |
||||
|
|
||||
|
public function changes(): int { |
||||
|
return $this->rows; |
||||
|
} |
||||
|
|
||||
|
public function lastId(): int { |
||||
|
return $this->id; |
||||
|
} |
||||
|
|
||||
|
// constructor/destructor |
||||
|
|
||||
|
public function __construct(\mysqli_result $result, array $changes = [0,0], Statement $statement = null) { |
||||
|
$this->st = $statement; //keeps the statement from being destroyed, invalidating the result set |
||||
|
$this->set = $result; |
||||
|
$this->rows = $changes[0]; |
||||
|
$this->id = $changes[1]; |
||||
|
} |
||||
|
|
||||
|
public function __destruct() { |
||||
|
try { |
||||
|
$this->set->free(); |
||||
|
} catch (\Throwable $e) { // @codeCoverageIgnore |
||||
|
} |
||||
|
unset($this->set); |
||||
|
} |
||||
|
|
||||
|
// PHP iterator methods |
||||
|
|
||||
|
public function valid() { |
||||
|
$this->cur = $this->set->fetch_assoc(); |
||||
|
return ($this->cur !== null); |
||||
|
} |
||||
|
} |
@ -0,0 +1,107 @@ |
|||||
|
<?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\MySQL; |
||||
|
|
||||
|
use JKingWeb\Arsse\Db\Exception; |
||||
|
use JKingWeb\Arsse\Db\ExceptionInput; |
||||
|
use JKingWeb\Arsse\Db\ExceptionTimeout; |
||||
|
|
||||
|
class Statement extends \JKingWeb\Arsse\Db\AbstractStatement { |
||||
|
use ExceptionBuilder; |
||||
|
|
||||
|
const BINDINGS = [ |
||||
|
"integer" => "i", |
||||
|
"float" => "d", |
||||
|
"datetime" => "s", |
||||
|
"binary" => "b", |
||||
|
"string" => "s", |
||||
|
"boolean" => "i", |
||||
|
]; |
||||
|
|
||||
|
protected $db; |
||||
|
protected $st; |
||||
|
protected $query; |
||||
|
protected $packetSize; |
||||
|
|
||||
|
protected $values; |
||||
|
protected $types; |
||||
|
|
||||
|
public function __construct(\mysqli $db, string $query, array $bindings = [], int $packetSize) { |
||||
|
$this->db = $db; |
||||
|
$this->query = $query; |
||||
|
$this->packetSize = $packetSize; |
||||
|
$this->retypeArray($bindings); |
||||
|
} |
||||
|
|
||||
|
protected function prepare(string $query): bool { |
||||
|
$this->st = $this->db->prepare($query); |
||||
|
if (!$this->st) { |
||||
|
list($excClass, $excMsg, $excData) = $this->buildEngineException($this->db->errno, $this->db->error); |
||||
|
throw new $excClass($excMsg, $excData); |
||||
|
} |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
public function __destruct() { |
||||
|
try { |
||||
|
$this->st->close(); |
||||
|
} catch (\Throwable $e) { // @codeCoverageIgnore |
||||
|
} |
||||
|
unset($this->st); |
||||
|
} |
||||
|
|
||||
|
public function runArray(array $values = []): \JKingWeb\Arsse\Db\Result { |
||||
|
$this->st->reset(); |
||||
|
// prepare values and them all at once |
||||
|
$this->bindValues($values); |
||||
|
$this->st->bind_params($this->types, ...$this->values); |
||||
|
// execute the statement |
||||
|
$this->st->execute(); |
||||
|
// clear normalized values |
||||
|
$this->types = ""; |
||||
|
$this->values = []; |
||||
|
// check for errors |
||||
|
if ($this->st->sqlstate != "00000") { |
||||
|
if ($this->st->sqlstate == "HY000") { |
||||
|
list($excClass, $excMsg, $excData) = $this->buildEngineException($this->st->errno, $this->st->error); |
||||
|
} else { |
||||
|
list($excClass, $excMsg, $excData) = $this->buildStandardException($this->st->sqlstate, $this->st->error); |
||||
|
} |
||||
|
throw new $excClass($excMsg, $excData); |
||||
|
} |
||||
|
// create a result-set instance |
||||
|
$r = $this->st->get_result(); |
||||
|
$changes = $this->st->affected_rows; |
||||
|
$lastId = $this->st->insert_id; |
||||
|
return new Result($r, [$changes, $lastId], $this); |
||||
|
} |
||||
|
|
||||
|
protected function bindValue($value, string $type, int $position): bool { |
||||
|
// this is a bit of a hack: we collect values (and MySQL bind types) here so that we can take |
||||
|
// advantage of the work done by bindValues() even though MySQL requires everything to be bound |
||||
|
// all at once; we also packetize large values here if necessary |
||||
|
if (is_string($value) && strlen($value) > $this->packetSize) { |
||||
|
$this->values[] = null; |
||||
|
$this->st->send_long_data($position - 1, $value); |
||||
|
} else { |
||||
|
$this->values[] = $value; |
||||
|
$this->types .= self::BINDINGS[$type]; |
||||
|
} |
||||
|
return true; |
||||
|
} |
||||
|
public static function mungeQuery(string $query, array $types, ...$extraData): string { |
||||
|
$query = explode("?", $query); |
||||
|
$out = ""; |
||||
|
for ($b = 1; $b < sizeof($query); $b++) { |
||||
|
$a = $b - 1; |
||||
|
$mark = (($types[$a] ?? "") == "datetime") ? "cast(? as datetime(0))" : "?"; |
||||
|
$out .= $query[$a].$mark; |
||||
|
} |
||||
|
$out .= array_pop($query); |
||||
|
return $out; |
||||
|
} |
||||
|
} |
@ -0,0 +1,31 @@ |
|||||
|
<?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\Db\Exception; |
||||
|
use JKingWeb\Arsse\Db\ExceptionInput; |
||||
|
use JKingWeb\Arsse\Db\ExceptionTimeout; |
||||
|
|
||||
|
trait SQLState { |
||||
|
protected static function buildStandardException(string $code, string $msg): array { |
||||
|
switch ($code) { |
||||
|
case "22007": |
||||
|
case "22P02": |
||||
|
case "42804": |
||||
|
return [ExceptionInput::class, 'engineTypeViolation', $msg]; |
||||
|
case "23000": |
||||
|
case "23502": |
||||
|
case "23505": |
||||
|
return [ExceptionInput::class, "constraintViolation", $msg]; |
||||
|
case "55P03": |
||||
|
case "57014": |
||||
|
return [ExceptionTimeout::class, 'general', $msg]; |
||||
|
default: |
||||
|
return [Exception::class, "engineErrorGeneral", "SQLSTATE $code: $msg"]; // @codeCoverageIgnore |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,12 @@ |
|||||
|
<?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\SQLite3; |
||||
|
|
||||
|
class PDOStatement extends \JKingWeb\Arsse\Db\PDOStatement { |
||||
|
use ExceptionBuilder; |
||||
|
use \JKingWeb\Arsse\Db\PDOError; |
||||
|
} |
Loading…
Reference in new issue