DBALException.php 8.24 KB
Newer Older
1
<?php
Benjamin Morel's avatar
Benjamin Morel committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the MIT license. For more information, see
 * <http://www.doctrine-project.org>.
 */
19 20 21

namespace Doctrine\DBAL;

22
use Doctrine\DBAL\Driver\DriverException;
23 24
use Doctrine\DBAL\Driver\ExceptionConverterDriver;

25 26
class DBALException extends \Exception
{
Benjamin Morel's avatar
Benjamin Morel committed
27 28 29 30 31
    /**
     * @param string $method
     *
     * @return \Doctrine\DBAL\DBALException
     */
32 33
    public static function notSupported($method)
    {
34
        return new self("Operation '$method' is not supported by platform.");
35
    }
36

Benjamin Morel's avatar
Benjamin Morel committed
37 38 39
    /**
     * @return \Doctrine\DBAL\DBALException
     */
40 41
    public static function invalidPlatformSpecified()
    {
42 43 44 45 46
        return new self(
            "Invalid 'platform' option specified, need to give an instance of ".
            "\Doctrine\DBAL\Platforms\AbstractPlatform.");
    }

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    /**
     * Returns a new instance for an invalid specified platform version.
     *
     * @param string $version        The invalid platform version given.
     * @param string $expectedFormat The expected platform version format.
     *
     * @return DBALException
     */
    public static function invalidPlatformVersionSpecified($version, $expectedFormat)
    {
        return new self(
            sprintf(
                'Invalid platform version "%s" specified. ' .
                'The platform version has to be specified in the format: "%s".',
                $version,
                $expectedFormat
            )
        );
    }

Benjamin Morel's avatar
Benjamin Morel committed
67 68 69
    /**
     * @return \Doctrine\DBAL\DBALException
     */
70 71 72 73 74 75 76 77
    public static function invalidPdoInstance()
    {
        return new self(
            "The 'pdo' option was used in DriverManager::getConnection() but no ".
            "instance of PDO was given."
        );
    }

Benjamin Morel's avatar
Benjamin Morel committed
78
    /**
79
     * @param string|null $url The URL that was provided in the connection parameters (if any).
80
     *
Benjamin Morel's avatar
Benjamin Morel committed
81 82
     * @return \Doctrine\DBAL\DBALException
     */
83
    public static function driverRequired($url = null)
84
    {
85 86 87 88 89 90 91 92 93 94
        if ($url) {
            return new self(
                sprintf(
                    "The options 'driver' or 'driverClass' are mandatory if a connection URL without scheme " .
                    "is given to DriverManager::getConnection(). Given URL: %s",
                    $url
                )
            );
        }

95 96 97 98
        return new self("The options 'driver' or 'driverClass' are mandatory if no PDO ".
            "instance is given to DriverManager::getConnection().");
    }

Benjamin Morel's avatar
Benjamin Morel committed
99 100 101 102 103 104
    /**
     * @param string $unknownDriverName
     * @param array  $knownDrivers
     *
     * @return \Doctrine\DBAL\DBALException
     */
105 106 107 108
    public static function unknownDriver($unknownDriverName, array $knownDrivers)
    {
        return new self("The given 'driver' ".$unknownDriverName." is unknown, ".
            "Doctrine currently supports only the following drivers: ".implode(", ", $knownDrivers));
109
    }
110

Benjamin Morel's avatar
Benjamin Morel committed
111
    /**
112
     * @param \Doctrine\DBAL\Driver     $driver
Benjamin Morel's avatar
Benjamin Morel committed
113 114 115 116 117 118
     * @param \Exception $driverEx
     * @param string     $sql
     * @param array      $params
     *
     * @return \Doctrine\DBAL\DBALException
     */
119
    public static function driverExceptionDuringQuery(Driver $driver, \Exception $driverEx, $sql, array $params = array())
120 121 122
    {
        $msg = "An exception occurred while executing '".$sql."'";
        if ($params) {
123
            $msg .= " with params " . self::formatParameters($params);
124 125 126
        }
        $msg .= ":\n\n".$driverEx->getMessage();

127 128 129
        if ($driver instanceof ExceptionConverterDriver && $driverEx instanceof DriverException) {
            return $driver->convertException($msg, $driverEx);
        }
130

131
        return new self($msg, 0, $driverEx);
132 133
    }

134 135 136 137 138 139 140 141 142 143
    /**
     * @param \Doctrine\DBAL\Driver     $driver
     * @param \Exception $driverEx
     *
     * @return \Doctrine\DBAL\DBALException
     */
    public static function driverException(Driver $driver, \Exception $driverEx)
    {
        $msg = "An exception occured in driver: " . $driverEx->getMessage();

144 145
        if ($driver instanceof ExceptionConverterDriver && $driverEx instanceof DriverException) {
            return $driver->convertException($msg, $driverEx);
146
        }
147 148

        return new self($msg, 0, $driverEx);
149 150
    }

151 152 153 154 155 156 157 158 159 160
    /**
     * Returns a human-readable representation of an array of parameters.
     * This properly handles binary data by returning a hex representation.
     *
     * @param array $params
     *
     * @return string
     */
    private static function formatParameters(array $params)
    {
Steve Müller's avatar
Steve Müller committed
161
        return '[' . implode(', ', array_map(function ($param) {
162 163 164 165 166 167 168 169 170 171 172
            $json = @json_encode($param);

            if (! is_string($json) || $json == 'null' && is_string($param)) {
                // JSON encoding failed, this is not a UTF-8 string.
                return '"\x' . implode('\x', str_split(bin2hex($param), 2)) . '"';
            }

            return $json;
        }, $params)) . ']';
    }

Benjamin Morel's avatar
Benjamin Morel committed
173 174 175 176 177
    /**
     * @param string $wrapperClass
     *
     * @return \Doctrine\DBAL\DBALException
     */
178 179 180 181 182 183
    public static function invalidWrapperClass($wrapperClass)
    {
        return new self("The given 'wrapperClass' ".$wrapperClass." has to be a ".
            "subtype of \Doctrine\DBAL\Connection.");
    }

Benjamin Morel's avatar
Benjamin Morel committed
184 185 186 187 188
    /**
     * @param string $driverClass
     *
     * @return \Doctrine\DBAL\DBALException
     */
189 190 191 192 193
    public static function invalidDriverClass($driverClass)
    {
        return new self("The given 'driverClass' ".$driverClass." has to implement the ".
            "\Doctrine\DBAL\Driver interface.");
    }
194 195 196

    /**
     * @param string $tableName
Benjamin Morel's avatar
Benjamin Morel committed
197 198
     *
     * @return \Doctrine\DBAL\DBALException
199 200 201 202 203 204 205 206
     */
    public static function invalidTableName($tableName)
    {
        return new self("Invalid table name specified: ".$tableName);
    }

    /**
     * @param string $tableName
Benjamin Morel's avatar
Benjamin Morel committed
207 208
     *
     * @return \Doctrine\DBAL\DBALException
209 210 211 212 213 214
     */
    public static function noColumnsSpecifiedForTable($tableName)
    {
        return new self("No columns specified for table ".$tableName);
    }

Benjamin Morel's avatar
Benjamin Morel committed
215 216 217
    /**
     * @return \Doctrine\DBAL\DBALException
     */
218 219 220 221
    public static function limitOffsetInvalid()
    {
        return new self("Invalid Offset in Limit Query, it has to be larger or equal to 0.");
    }
222

Benjamin Morel's avatar
Benjamin Morel committed
223 224 225 226 227
    /**
     * @param string $name
     *
     * @return \Doctrine\DBAL\DBALException
     */
228 229 230 231 232
    public static function typeExists($name)
    {
        return new self('Type '.$name.' already exists.');
    }

Benjamin Morel's avatar
Benjamin Morel committed
233 234 235 236 237
    /**
     * @param string $name
     *
     * @return \Doctrine\DBAL\DBALException
     */
238 239
    public static function unknownColumnType($name)
    {
240 241
        return new self('Unknown column type "'.$name.'" requested. Any Doctrine type that you use has ' .
            'to be registered with \Doctrine\DBAL\Types\Type::addType(). You can get a list of all the ' .
242
            'known types with \Doctrine\DBAL\Types\Type::getTypesMap(). If this error occurs during database ' .
243 244 245
            'introspection then you might have forgot to register all database types for a Doctrine Type. Use ' .
            'AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement ' .
            'Type#getMappedDatabaseTypes(). If the type name is empty you might ' .
246 247
            'have a problem with the cache or forgot some mapping information.'
        );
248 249
    }

Benjamin Morel's avatar
Benjamin Morel committed
250 251 252 253 254
    /**
     * @param string $name
     *
     * @return \Doctrine\DBAL\DBALException
     */
255 256 257 258
    public static function typeNotFound($name)
    {
        return new self('Type to be overwritten '.$name.' does not exist.');
    }
259
}