| 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 | 2145 |  |     public static function driverExceptionDuringQuery(Driver $driver, \Exception $driverEx, $sql, array $params = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 2145 |  |         $msg = "An exception occurred while executing '".$sql."'"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 2145 |  |         if ($params) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 175 |  |             $msg .= " with params " . self::formatParameters($params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 2145 |  |         $msg .= ":\n\n".$driverEx->getMessage(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 2145 |  |         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 | 48 |  |     public static function driverException(Driver $driver, \Exception $driverEx) | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 48 |  |         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 | 2193 |  |     private static function wrapException(Driver $driver, \Exception $driverEx, $msg) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 2193 |  |         if ($driverEx instanceof Exception\DriverException) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 16 |  |             return $driverEx; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 2177 |  |         if ($driver instanceof ExceptionConverterDriver && $driverEx instanceof Driver\DriverException) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1887 |  |             return $driver->convertException($msg, $driverEx); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 290 |  |         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 | 175 |  |     private static function formatParameters(array $params) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         return '[' . implode(', ', array_map(function ($param) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 175 |  |             if (is_resource($param)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 16 |  |                 return (string) $param; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |              | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 159 |  |             $json = @json_encode($param); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 159 |  |             if (! is_string($json) || $json == 'null' && is_string($param)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 // JSON encoding failed, this is not a UTF-8 string. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 16 |  |                 return '"\x' . implode('\x', str_split(bin2hex($param), 2)) . '"'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 159 |  |             return $json; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 175 |  |         }, $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.