DBALException.php 7.36 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 23
use Doctrine\DBAL\Driver\ExceptionConverterDriver;

24 25
class DBALException extends \Exception
{
26
    const ERROR_DUPLICATE_KEY = 1;
27
    const ERROR_UNKNOWN_TABLE = 2;
28
    const ERROR_TABLE_ALREADY_EXISTS = 3;
29
    const ERROR_FOREIGN_KEY_CONSTRAINT = 4;
30
    const ERROR_NOT_NULL = 5;
31
    const ERROR_BAD_FIELD_NAME = 6;
32
    const ERROR_NON_UNIQUE_FIELD_NAME = 7;
33
    const ERROR_SYNTAX = 9;
34 35
    const ERROR_UNABLE_TO_OPEN = 10;
    const ERROR_WRITE_READONLY = 11;
36
    const ERROR_ACCESS_DENIED = 12;
37

Benjamin Morel's avatar
Benjamin Morel committed
38 39 40 41 42
    /**
     * @param string $method
     *
     * @return \Doctrine\DBAL\DBALException
     */
43 44
    public static function notSupported($method)
    {
45
        return new self("Operation '$method' is not supported by platform.");
46
    }
47

Benjamin Morel's avatar
Benjamin Morel committed
48 49 50
    /**
     * @return \Doctrine\DBAL\DBALException
     */
51 52
    public static function invalidPlatformSpecified()
    {
53 54 55 56 57
        return new self(
            "Invalid 'platform' option specified, need to give an instance of ".
            "\Doctrine\DBAL\Platforms\AbstractPlatform.");
    }

Benjamin Morel's avatar
Benjamin Morel committed
58 59 60
    /**
     * @return \Doctrine\DBAL\DBALException
     */
61 62 63 64 65 66 67 68
    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
69 70 71
    /**
     * @return \Doctrine\DBAL\DBALException
     */
72 73 74 75 76 77
    public static function driverRequired()
    {
        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
78 79 80 81 82 83
    /**
     * @param string $unknownDriverName
     * @param array  $knownDrivers
     *
     * @return \Doctrine\DBAL\DBALException
     */
84 85 86 87
    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));
88
    }
89

Benjamin Morel's avatar
Benjamin Morel committed
90
    /**
91
     * @param \Doctrine\DBAL\Driver     $driver
Benjamin Morel's avatar
Benjamin Morel committed
92 93 94 95 96 97
     * @param \Exception $driverEx
     * @param string     $sql
     * @param array      $params
     *
     * @return \Doctrine\DBAL\DBALException
     */
98
    public static function driverExceptionDuringQuery(Driver $driver, \Exception $driverEx, $sql, array $params = array())
99 100 101
    {
        $msg = "An exception occurred while executing '".$sql."'";
        if ($params) {
102
            $msg .= " with params " . self::formatParameters($params);
103 104 105
        }
        $msg .= ":\n\n".$driverEx->getMessage();

106 107 108 109 110
        $code = ($driver instanceof ExceptionConverterDriver)
            ? $driver->convertExceptionCode($driverEx)
            : 0;

        return new self($msg, $code, $driverEx);
111 112
    }

113 114 115 116 117 118 119 120 121 122 123 124 125
    /**
     * @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();

        return new self($msg, $driver->convertExceptionCode($driverEx), $driverEx);
    }

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    /**
     * 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)
    {
        return '[' . implode(', ', array_map(function($param) {
            $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
148 149 150 151 152
    /**
     * @param string $wrapperClass
     *
     * @return \Doctrine\DBAL\DBALException
     */
153 154 155 156 157 158
    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
159 160 161 162 163
    /**
     * @param string $driverClass
     *
     * @return \Doctrine\DBAL\DBALException
     */
164 165 166 167 168
    public static function invalidDriverClass($driverClass)
    {
        return new self("The given 'driverClass' ".$driverClass." has to implement the ".
            "\Doctrine\DBAL\Driver interface.");
    }
169 170 171

    /**
     * @param string $tableName
Benjamin Morel's avatar
Benjamin Morel committed
172 173
     *
     * @return \Doctrine\DBAL\DBALException
174 175 176 177 178 179 180 181
     */
    public static function invalidTableName($tableName)
    {
        return new self("Invalid table name specified: ".$tableName);
    }

    /**
     * @param string $tableName
Benjamin Morel's avatar
Benjamin Morel committed
182 183
     *
     * @return \Doctrine\DBAL\DBALException
184 185 186 187 188 189
     */
    public static function noColumnsSpecifiedForTable($tableName)
    {
        return new self("No columns specified for table ".$tableName);
    }

Benjamin Morel's avatar
Benjamin Morel committed
190 191 192
    /**
     * @return \Doctrine\DBAL\DBALException
     */
193 194 195 196
    public static function limitOffsetInvalid()
    {
        return new self("Invalid Offset in Limit Query, it has to be larger or equal to 0.");
    }
197

Benjamin Morel's avatar
Benjamin Morel committed
198 199 200 201 202
    /**
     * @param string $name
     *
     * @return \Doctrine\DBAL\DBALException
     */
203 204 205 206 207
    public static function typeExists($name)
    {
        return new self('Type '.$name.' already exists.');
    }

Benjamin Morel's avatar
Benjamin Morel committed
208 209 210 211 212
    /**
     * @param string $name
     *
     * @return \Doctrine\DBAL\DBALException
     */
213 214
    public static function unknownColumnType($name)
    {
215 216
        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 ' .
217
            'known types with \Doctrine\DBAL\Types\Type::getTypesMap(). If this error occurs during database ' .
218 219 220
            '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 ' .
221 222
            'have a problem with the cache or forgot some mapping information.'
        );
223 224
    }

Benjamin Morel's avatar
Benjamin Morel committed
225 226 227 228 229
    /**
     * @param string $name
     *
     * @return \Doctrine\DBAL\DBALException
     */
230 231 232 233
    public static function typeNotFound($name)
    {
        return new self('Type to be overwritten '.$name.' does not exist.');
    }
234
}