Exception.php 7.1 KB
Newer Older
1
<?php
lsmith's avatar
lsmith committed
2
/*
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *  $Id$
 *
 * 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 LGPL. For more information, see
19
 * <http://www.phpdoctrine.org>.
20 21 22 23 24
 */
Doctrine::autoload('Doctrine_Connection_Exception');
/**
 * Doctrine_Connection_Firebird_Exception
 *
25
 * @package     Doctrine
26
 * @subpackage  Connection
27
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
28
 * @link        www.phpdoctrine.org
29 30 31 32 33 34
 * @since       1.0
 * @version     $Revision$
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 * @author      Lorenzo Alberton <l.alberton@quipo.it> (PEAR MDB2 Interbase driver)
 * @author      Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
 */
lsmith's avatar
lsmith committed
35 36
class Doctrine_Connection_Firebird_Exception extends Doctrine_Connection_Exception
{
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    /**
     * @var array $errorCodeMap         an array that is used for determining portable
     *                                  error code from a native database error code
     */
    protected static $errorCodeMap = array(
                                    -104 => Doctrine::ERR_SYNTAX,
                                    -150 => Doctrine::ERR_ACCESS_VIOLATION,
                                    -151 => Doctrine::ERR_ACCESS_VIOLATION,
                                    -155 => Doctrine::ERR_NOSUCHTABLE,
                                    -157 => Doctrine::ERR_NOSUCHFIELD,
                                    -158 => Doctrine::ERR_VALUE_COUNT_ON_ROW,
                                    -170 => Doctrine::ERR_MISMATCH,
                                    -171 => Doctrine::ERR_MISMATCH,
                                    -172 => Doctrine::ERR_INVALID,
                                    // -204 =>  // Covers too many errors, need to use regex on msg
                                    -205 => Doctrine::ERR_NOSUCHFIELD,
                                    -206 => Doctrine::ERR_NOSUCHFIELD,
                                    -208 => Doctrine::ERR_INVALID,
                                    -219 => Doctrine::ERR_NOSUCHTABLE,
                                    -297 => Doctrine::ERR_CONSTRAINT,
                                    -303 => Doctrine::ERR_INVALID,
                                    -413 => Doctrine::ERR_INVALID_NUMBER,
                                    -530 => Doctrine::ERR_CONSTRAINT,
                                    -551 => Doctrine::ERR_ACCESS_VIOLATION,
                                    -552 => Doctrine::ERR_ACCESS_VIOLATION,
                                    // -607 =>  // Covers too many errors, need to use regex on msg
                                    -625 => Doctrine::ERR_CONSTRAINT_NOT_NULL,
                                    -803 => Doctrine::ERR_CONSTRAINT,
                                    -804 => Doctrine::ERR_VALUE_COUNT_ON_ROW,
                                    -904 => Doctrine::ERR_CONNECT_FAILED,
                                    -922 => Doctrine::ERR_NOSUCHDB,
                                    -923 => Doctrine::ERR_CONNECT_FAILED,
                                    -924 => Doctrine::ERR_CONNECT_FAILED
                                    );
71

72
    /**
lsmith's avatar
lsmith committed
73
     * @var array $errorRegexps         an array that is used for determining portable
74 75 76 77 78 79
     *                                  error code from a native database error message
     */
    protected static $errorRegexps = array(
                                    '/generator .* is not defined/'
                                        => Doctrine::ERR_SYNTAX,  // for compat. w ibase_errcode()
                                    '/table.*(not exist|not found|unknown)/i'
lsmith's avatar
lsmith committed
80
                                        => Doctrine::ERR_NOSUCHTABLE,
81
                                    '/table .* already exists/i'
lsmith's avatar
lsmith committed
82
                                        => Doctrine::ERR_ALREADY_EXISTS,
83 84 85 86 87 88 89 90 91 92 93 94 95 96
                                    '/unsuccessful metadata update .* failed attempt to store duplicate value/i'
                                        => Doctrine::ERR_ALREADY_EXISTS,
                                    '/unsuccessful metadata update .* not found/i'
                                        => Doctrine::ERR_NOT_FOUND,
                                    '/validation error for column .* value "\*\*\* null/i'
                                        => Doctrine::ERR_CONSTRAINT_NOT_NULL,
                                    '/violation of [\w ]+ constraint/i'
                                        => Doctrine::ERR_CONSTRAINT,
                                    '/conversion error from string/i'
                                        => Doctrine::ERR_INVALID_NUMBER,
                                    '/no permission for/i'
                                        => Doctrine::ERR_ACCESS_VIOLATION,
                                    '/arithmetic exception, numeric overflow, or string truncation/i'
                                        => Doctrine::ERR_INVALID,
lsmith's avatar
lsmith committed
97
                                    '/table unknown/i'
98 99
                                        => Doctrine::ERR_NOSUCHTABLE,
                                    );
100

101
    /**
lsmith's avatar
lsmith committed
102 103
     * This method checks if native error code/message can be
     * converted into a portable code and then adds this
104 105 106 107 108 109 110 111
     * portable error code to errorInfo array and returns the modified array
     *
     * the portable error code is added at the end of array
     *
     * @param array $errorInfo      error info array
     * @since 1.0
     * @return array
     */
lsmith's avatar
lsmith committed
112 113
    public function processErrorInfo(array $errorInfo)
    {
114 115 116 117 118 119 120 121 122 123 124
            /**
            // todo: are the following lines needed?
            // memo for the interbase php module hackers: we need something similar
            // to mysql_errno() to retrieve error codes instead of this ugly hack
            if (preg_match('/^([^0-9\-]+)([0-9\-]+)\s+(.*)$/', $native_msg, $m)) {
                $native_code = (int)$m[2];
            } else {
                $native_code = null;
            }
            */

lsmith's avatar
lsmith committed
125
        foreach (self::$errorRegexps as $regexp => $code) {
126 127 128 129 130
            if (preg_match($regexp, $errorInfo[2])) {
                $errorInfo[3] = $code;
                break;
            }
        }
lsmith's avatar
lsmith committed
131
        if (isset(self::$errorCodeMap[$errorInfo[1]])) {
132
            $errorInfo[3] = self::$errorCodeMap[$errorInfo[1]];
lsmith's avatar
lsmith committed
133
        }
134 135
        return $errorInfo;
    }
136
}