2017-12-19 22:15:05 +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;
|
|
|
|
|
2018-12-20 23:06:28 +00:00
|
|
|
use JKingWeb\Arsse\Db\SQLite3\Driver as SQLite3;
|
|
|
|
|
2017-12-19 22:15:05 +00:00
|
|
|
trait PDOError {
|
2018-11-29 18:45:37 +00:00
|
|
|
public function exceptionBuild(bool $statementError = null): array {
|
|
|
|
if ($statementError ?? ($this instanceof Statement)) {
|
2017-12-19 22:15:05 +00:00
|
|
|
$err = $this->st->errorInfo();
|
|
|
|
} else {
|
|
|
|
$err = $this->db->errorInfo();
|
|
|
|
}
|
|
|
|
switch ($err[0]) {
|
2018-12-20 23:06:28 +00:00
|
|
|
case "22007":
|
2018-11-20 21:32:18 +00:00
|
|
|
case "22P02":
|
2018-11-29 18:45:37 +00:00
|
|
|
case "42804":
|
2018-11-20 21:32:18 +00:00
|
|
|
return [ExceptionInput::class, 'engineTypeViolation', $err[2]];
|
2017-12-19 22:15:05 +00:00
|
|
|
case "23000":
|
2018-11-20 21:32:18 +00:00
|
|
|
case "23502":
|
2018-12-05 14:05:43 +00:00
|
|
|
case "23505":
|
2017-12-19 22:15:05 +00:00
|
|
|
return [ExceptionInput::class, "constraintViolation", $err[2]];
|
2018-11-22 18:30:13 +00:00
|
|
|
case "55P03":
|
|
|
|
case "57014":
|
|
|
|
return [ExceptionTimeout::class, 'general', $err[2]];
|
2017-12-19 22:15:05 +00:00
|
|
|
case "HY000":
|
|
|
|
// engine-specific errors
|
|
|
|
switch ($this->db->getAttribute(\PDO::ATTR_DRIVER_NAME)) {
|
|
|
|
case "sqlite":
|
|
|
|
switch ($err[1]) {
|
2018-12-20 23:06:28 +00:00
|
|
|
case SQLite3::SQLITE_BUSY:
|
2017-12-19 22:15:05 +00:00
|
|
|
return [ExceptionTimeout::class, 'general', $err[2]];
|
2018-12-20 23:06:28 +00:00
|
|
|
case SQLite3::SQLITE_MISMATCH:
|
2017-12-19 22:15:05 +00:00
|
|
|
return [ExceptionInput::class, 'engineTypeViolation', $err[2]];
|
|
|
|
}
|
2018-12-20 23:06:28 +00:00
|
|
|
break;
|
|
|
|
case "mysql":
|
|
|
|
switch ($err[1]) {
|
|
|
|
case 1205:
|
|
|
|
return [ExceptionTimeout::class, 'general', $err[2]];
|
|
|
|
case 1364:
|
|
|
|
return [ExceptionInput::class, "constraintViolation", $err[2]];
|
|
|
|
}
|
|
|
|
break;
|
2017-12-19 22:15:05 +00:00
|
|
|
}
|
2018-12-20 23:06:28 +00:00
|
|
|
return [Exception::class, "engineErrorGeneral", $err[0]."/".$err[1].": ".$err[2]]; // @codeCoverageIgnore
|
2017-12-19 22:15:05 +00:00
|
|
|
default:
|
|
|
|
return [Exception::class, "engineErrorGeneral", $err[0].": ".$err[2]]; // @codeCoverageIgnore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getError(): string {
|
|
|
|
return (string) $this->db->errorInfo()[2];
|
|
|
|
}
|
2017-12-22 16:51:58 +00:00
|
|
|
}
|