1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (https://nette.org)
5: * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6: */
7:
8: namespace Nette\Database;
9:
10:
11: /**
12: * Base class for all errors in the driver or SQL server.
13: */
14: class DriverException extends \PDOException
15: {
16: /** @var string */
17: public $queryString;
18:
19:
20: /**
21: * @return static
22: */
23: public static function from(\PDOException $src)
24: {
25: $e = new static($src->message, null, $src);
26: if (!$src->errorInfo && preg_match('#SQLSTATE\[(.*?)\] \[(.*?)\] (.*)#A', $src->message, $m)) {
27: $m[2] = (int) $m[2];
28: $e->errorInfo = array_slice($m, 1);
29: $e->code = $m[1];
30: } else {
31: $e->errorInfo = $src->errorInfo;
32: $e->code = $src->code;
33: }
34: return $e;
35: }
36:
37:
38: /**
39: * @return int|string|null Driver-specific error code
40: */
41: public function getDriverCode()
42: {
43: return isset($this->errorInfo[1]) ? $this->errorInfo[1] : null;
44: }
45:
46:
47: /**
48: * @return string|null SQLSTATE error code
49: */
50: public function getSqlState()
51: {
52: return isset($this->errorInfo[0]) ? $this->errorInfo[0] : null;
53: }
54:
55:
56: /**
57: * @return string|null SQL command
58: */
59: public function getQueryString()
60: {
61: return $this->queryString;
62: }
63: }
64: