|
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 \Doctrine\DBAL\Driver $driver |
|
23
|
|
|
* @param \Exception $driverEx |
|
24
|
|
|
* @param string $sql |
|
25
|
|
|
* @param array $params |
|
26
|
|
|
* |
|
27
|
|
|
* @return \Doctrine\DBAL\DBALException |
|
28
|
|
|
*/ |
|
29
|
2011 |
|
public static function driverExceptionDuringQuery(Driver $driver, \Exception $driverEx, $sql, array $params = []) |
|
30
|
|
|
{ |
|
31
|
2011 |
|
$msg = "An exception occurred while executing '".$sql."'"; |
|
32
|
2011 |
|
if ($params) { |
|
|
|
|
|
|
33
|
162 |
|
$msg .= " with params " . self::formatParameters($params); |
|
34
|
|
|
} |
|
35
|
2011 |
|
$msg .= ":\n\n".$driverEx->getMessage(); |
|
36
|
|
|
|
|
37
|
2011 |
|
return static::wrapException($driver, $driverEx, $msg); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param \Doctrine\DBAL\Driver $driver |
|
42
|
|
|
* @param \Exception $driverEx |
|
43
|
|
|
* |
|
44
|
|
|
* @return \Doctrine\DBAL\DBALException |
|
45
|
|
|
*/ |
|
46
|
44 |
|
public static function driverException(Driver $driver, \Exception $driverEx) |
|
47
|
|
|
{ |
|
48
|
44 |
|
return static::wrapException($driver, $driverEx, "An exception occurred in driver: " . $driverEx->getMessage()); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param \Doctrine\DBAL\Driver $driver |
|
53
|
|
|
* @param \Exception $driverEx |
|
54
|
|
|
* |
|
55
|
|
|
* @return \Doctrine\DBAL\DBALException |
|
56
|
|
|
*/ |
|
57
|
2055 |
|
private static function wrapException(Driver $driver, \Exception $driverEx, $msg) |
|
58
|
|
|
{ |
|
59
|
2055 |
|
if ($driverEx instanceof Exception\DriverException) { |
|
60
|
15 |
|
return $driverEx; |
|
61
|
|
|
} |
|
62
|
2040 |
|
if ($driver instanceof ExceptionConverterDriver && $driverEx instanceof Driver\DriverException) { |
|
63
|
1757 |
|
return $driver->convertException($msg, $driverEx); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
283 |
|
return new self($msg, 0, $driverEx); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Returns a human-readable representation of an array of parameters. |
|
71
|
|
|
* This properly handles binary data by returning a hex representation. |
|
72
|
|
|
* |
|
73
|
|
|
* @param array $params |
|
74
|
|
|
* |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
162 |
|
private static function formatParameters(array $params) |
|
78
|
|
|
{ |
|
79
|
|
|
return '[' . implode(', ', array_map(function ($param) { |
|
80
|
162 |
|
if (is_resource($param)) { |
|
81
|
15 |
|
return (string) $param; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
147 |
|
$json = @json_encode($param); |
|
85
|
|
|
|
|
86
|
147 |
|
if (! is_string($json) || $json == 'null' && is_string($param)) { |
|
|
|
|
|
|
87
|
|
|
// JSON encoding failed, this is not a UTF-8 string. |
|
88
|
15 |
|
return '"\x' . implode('\x', str_split(bin2hex($param), 2)) . '"'; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
147 |
|
return $json; |
|
92
|
162 |
|
}, $params)) . ']'; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
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.