|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Doctrine\DBAL; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Exception; |
|
6
|
|
|
use Doctrine\DBAL\Driver; |
|
7
|
|
|
use Doctrine\DBAL\Driver\ExceptionConverterDriver; |
|
8
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
|
9
|
|
|
use function array_map; |
|
10
|
|
|
use function bin2hex; |
|
11
|
|
|
use function implode; |
|
12
|
|
|
use function is_resource; |
|
13
|
|
|
use function is_string; |
|
14
|
|
|
use function json_encode; |
|
15
|
|
|
use function sprintf; |
|
16
|
|
|
use function str_split; |
|
17
|
|
|
|
|
18
|
|
|
class DBALException extends \Exception |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param mixed $invalidPlatform |
|
23
|
|
|
*/ |
|
24
|
|
|
public static function invalidPlatformType($invalidPlatform) : self |
|
25
|
|
|
{ |
|
26
|
|
|
if (\is_object($invalidPlatform)) { |
|
27
|
|
|
return new self( |
|
28
|
|
|
sprintf( |
|
29
|
|
|
"Option 'platform' must be a subtype of '%s', instance of '%s' given", |
|
30
|
|
|
AbstractPlatform::class, |
|
31
|
|
|
\get_class($invalidPlatform) |
|
32
|
|
|
) |
|
33
|
|
|
); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
return new self( |
|
37
|
|
|
sprintf( |
|
38
|
|
|
"Option 'platform' must be an object and subtype of '%s'. Got '%s'", |
|
39
|
|
|
AbstractPlatform::class, |
|
40
|
|
|
\gettype($invalidPlatform) |
|
41
|
|
|
) |
|
42
|
|
|
); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string|null $url The URL that was provided in the connection parameters (if any). |
|
47
|
|
|
* |
|
48
|
|
|
* @return \Doctrine\DBAL\DBALException |
|
49
|
|
|
*/ |
|
50
|
|
|
public static function driverRequired($url = null) |
|
51
|
|
|
{ |
|
52
|
|
|
if ($url) { |
|
53
|
|
|
return new self( |
|
54
|
|
|
sprintf( |
|
55
|
|
|
"The options 'driver' or 'driverClass' are mandatory if a connection URL without scheme " . |
|
56
|
|
|
"is given to DriverManager::getConnection(). Given URL: %s", |
|
57
|
|
|
$url |
|
58
|
|
|
) |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return new self("The options 'driver' or 'driverClass' are mandatory if no PDO ". |
|
63
|
|
|
"instance is given to DriverManager::getConnection()."); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string $unknownDriverName |
|
68
|
|
|
* @param array $knownDrivers |
|
69
|
|
|
* |
|
70
|
|
|
* @return \Doctrine\DBAL\DBALException |
|
71
|
|
|
*/ |
|
72
|
|
|
public static function unknownDriver($unknownDriverName, array $knownDrivers) |
|
73
|
|
|
{ |
|
74
|
|
|
return new self("The given 'driver' ".$unknownDriverName." is unknown, ". |
|
75
|
|
|
"Doctrine currently supports only the following drivers: ".implode(", ", $knownDrivers)); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param \Doctrine\DBAL\Driver $driver |
|
80
|
|
|
* @param \Exception $driverEx |
|
81
|
|
|
* @param string $sql |
|
82
|
|
|
* @param array $params |
|
83
|
|
|
* |
|
84
|
|
|
* @return \Doctrine\DBAL\DBALException |
|
85
|
|
|
*/ |
|
86
|
|
|
public static function driverExceptionDuringQuery(Driver $driver, \Exception $driverEx, $sql, array $params = []) |
|
87
|
|
|
{ |
|
88
|
|
|
$msg = "An exception occurred while executing '".$sql."'"; |
|
89
|
|
|
if ($params) { |
|
|
|
|
|
|
90
|
|
|
$msg .= " with params " . self::formatParameters($params); |
|
91
|
|
|
} |
|
92
|
|
|
$msg .= ":\n\n".$driverEx->getMessage(); |
|
93
|
|
|
|
|
94
|
|
|
return static::wrapException($driver, $driverEx, $msg); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param \Doctrine\DBAL\Driver $driver |
|
99
|
|
|
* @param \Exception $driverEx |
|
100
|
|
|
* |
|
101
|
|
|
* @return \Doctrine\DBAL\DBALException |
|
102
|
|
|
*/ |
|
103
|
|
|
public static function driverException(Driver $driver, \Exception $driverEx) |
|
104
|
|
|
{ |
|
105
|
|
|
return static::wrapException($driver, $driverEx, "An exception occurred in driver: " . $driverEx->getMessage()); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param \Doctrine\DBAL\Driver $driver |
|
110
|
|
|
* @param \Exception $driverEx |
|
111
|
|
|
* |
|
112
|
|
|
* @return \Doctrine\DBAL\DBALException |
|
113
|
|
|
*/ |
|
114
|
|
|
private static function wrapException(Driver $driver, \Exception $driverEx, $msg) |
|
115
|
|
|
{ |
|
116
|
|
|
if ($driverEx instanceof Exception\DriverException) { |
|
117
|
|
|
return $driverEx; |
|
118
|
|
|
} |
|
119
|
|
|
if ($driver instanceof ExceptionConverterDriver && $driverEx instanceof Driver\DriverException) { |
|
120
|
|
|
return $driver->convertException($msg, $driverEx); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return new self($msg, 0, $driverEx); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Returns a human-readable representation of an array of parameters. |
|
128
|
|
|
* This properly handles binary data by returning a hex representation. |
|
129
|
|
|
* |
|
130
|
|
|
* @param array $params |
|
131
|
|
|
* |
|
132
|
|
|
* @return string |
|
133
|
|
|
*/ |
|
134
|
|
|
private static function formatParameters(array $params) |
|
135
|
|
|
{ |
|
136
|
|
|
return '[' . implode(', ', array_map(function ($param) { |
|
137
|
|
|
if (is_resource($param)) { |
|
138
|
|
|
return (string) $param; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$json = @json_encode($param); |
|
142
|
|
|
|
|
143
|
|
|
if (! is_string($json) || $json == 'null' && is_string($param)) { |
|
|
|
|
|
|
144
|
|
|
// JSON encoding failed, this is not a UTF-8 string. |
|
145
|
|
|
return '"\x' . implode('\x', str_split(bin2hex($param), 2)) . '"'; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return $json; |
|
149
|
|
|
}, $params)) . ']'; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.