1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-22 21:22:40 +00:00
Arsse/lib/Db/PDOError.php
J. King 4a1c23ba45 Munge PostgreSQL queries instead of adding explicit casts
PDO does not adequately inform PostgreSQL of a parameter's type, so type
casts are required. Rather than adding these to each query manually, the
queries are instead processed to add type hints automatically.

Unfortunately the queries are processed rather naively; question-mark
characters in string constants, identifiers, regex patterns, or geometry
operators will break things spectacularly.
2018-11-29 13:45:37 -05:00

51 lines
2 KiB
PHP

<?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;
trait PDOError {
public function exceptionBuild(bool $statementError = null): array {
if ($statementError ?? ($this instanceof Statement)) {
$err = $this->st->errorInfo();
} else {
$err = $this->db->errorInfo();
}
switch ($err[0]) {
case "22P02":
case "42804":
return [ExceptionInput::class, 'engineTypeViolation', $err[2]];
case "23000":
case "23502":
return [ExceptionInput::class, "constraintViolation", $err[2]];
case "55P03":
case "57014":
return [ExceptionTimeout::class, 'general', $err[2]];
case "HY000":
// engine-specific errors
switch ($this->db->getAttribute(\PDO::ATTR_DRIVER_NAME)) {
case "sqlite":
switch ($err[1]) {
case \JKingWeb\Arsse\Db\SQLite3\Driver::SQLITE_BUSY:
return [ExceptionTimeout::class, 'general', $err[2]];
case \JKingWeb\Arsse\Db\SQLite3\Driver::SQLITE_MISMATCH:
return [ExceptionInput::class, 'engineTypeViolation', $err[2]];
default:
return [Exception::class, "engineErrorGeneral", $err[1]." - ".$err[2]];
}
// no break
default:
return [Exception::class, "engineErrorGeneral", $err[2]]; // @codeCoverageIgnore
}
// no break
default:
return [Exception::class, "engineErrorGeneral", $err[0].": ".$err[2]]; // @codeCoverageIgnore
}
}
public function getError(): string {
return (string) $this->db->errorInfo()[2];
}
}