From eef1bd4882b8a283157ed632e9a598af98c293c9 Mon Sep 17 00:00:00 2001 From: zYne Date: Fri, 10 Nov 2006 22:40:18 +0000 Subject: [PATCH] Portable error code updates --- lib/Doctrine.php | 84 +++++++++----------- lib/Doctrine/Connection/Mssql/Mssql.php | 50 +++++++++++- lib/Doctrine/Connection/Sqlite/Exception.php | 2 +- lib/Doctrine/Db/Sqlite.php | 63 --------------- 4 files changed, 83 insertions(+), 116 deletions(-) delete mode 100644 lib/Doctrine/Db/Sqlite.php diff --git a/lib/Doctrine.php b/lib/Doctrine.php index 75cb07b9d..e0cd2bee8 100644 --- a/lib/Doctrine.php +++ b/lib/Doctrine.php @@ -31,55 +31,43 @@ require_once("Doctrine/Exception.php"); */ final class Doctrine { /** - * ERROR MODE CONSTANTS + * error constants */ - - /** - * NO PRIMARY KEY COLUMN ERROR - * no primary key column found error code - */ - const ERR_NO_PK = 0; - /** - * PRIMARY KEY MISMATCH ERROR - * this error code is used when user uses factory refresh for a - * given Doctrine_Record and the old primary key doesn't match the new one - */ - const ERR_REFRESH = 1; - /** - * FIND ERROR - * this code used when for example Doctrine_Table::find() is called and - * a Data Access Object is not found - */ - const ERR_FIND = 2; - /** - * TABLE NOT FOUND ERROR - * this error code is used when user tries to initialize - * a table and there is no database table for this factory - */ - const ERR_NOSUCH_TABLE = 3; - /** - * NAMING ERROR - * this code is used when user defined Doctrine_Table is badly named - */ - const ERR_NAMING = 5; - /** - * TABLE INSTANCE ERROR - * this code is used when user tries to initialize - * a table that is already initialized - */ - const ERR_TABLE_INSTANCE = 6; - /** - * NO OPEN SESSIONS ERROR - * error code which is used when user tries to get - * current session are there are no sessions open - */ - const ERR_NO_SESSIONS = 7; - /** - * MAPPING ERROR - * if there is something wrong with mapping logic - * this error code is used - */ - const ERR_MAPPING = 8; + const ERR = -1; + const ERR_SYNTAX = -2; + const ERR_CONSTRAINT = -3; + const ERR_NOT_FOUND = -4; + const ERR_ALREADY_EXISTS = -5; + const ERR_UNSUPPORTED = -6; + const ERR_MISMATCH = -7; + const ERR_INVALID = -8; + const ERR_NOT_CAPABLE = -9; + const ERR_TRUNCATED = -10; + const ERR_INVALID_NUMBER = -11; + const ERR_INVALID_DATE = -12; + const ERR_DIVZERO = -13; + const ERR_NODBSELECTED = -14; + const ERR_CANNOT_CREATE = -15; + const ERR_CANNOT_DELETE = -16; + const ERR_CANNOT_DROP = -17; + const ERR_NOSUCHTABLE = -18; + const ERR_NOSUCHFIELD = -19; + const ERR_NEED_MORE_DATA = -20; + const ERR_NOT_LOCKED = -21; + const ERR_VALUE_COUNT_ON_ROW = -22; + const ERR_INVALID_DSN = -23; + const ERR_CONNECT_FAILED = -24; + const ERR_EXTENSION_NOT_FOUND = -25; + const ERR_NOSUCHDB = -26; + const ERR_ACCESS_VIOLATION = -27; + const ERR_CANNOT_REPLACE = -28; + const ERR_CONSTRAINT_NOT_NULL = -29; + const ERR_DEADLOCK = -30; + const ERR_CANNOT_ALTER = -31; + const ERR_MANAGER = -32; + const ERR_MANAGER_PARSE = -33; + const ERR_LOADMODULE = -34; + const ERR_INSUFFICIENT_DATA = -35; /** * ATTRIBUTE CONSTANTS diff --git a/lib/Doctrine/Connection/Mssql/Mssql.php b/lib/Doctrine/Connection/Mssql/Mssql.php index 6af4ce15e..f404f518d 100644 --- a/lib/Doctrine/Connection/Mssql/Mssql.php +++ b/lib/Doctrine/Connection/Mssql/Mssql.php @@ -22,8 +22,50 @@ Doctrine::autoload('Doctrine_Connection_Exception'); /** * Doctrine_Connection_Mssql_Exception * - * @package Doctrine ORM - * @url www.phpdoctrine.com - * @license LGPL + * @package Doctrine + * @url http://www.phpdoctrine.com + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @author Konsta Vesterinen + * @author Lukas Smith (PEAR MDB2 library) + * @since 1.0 + * @version $Id$ */ -class Doctrine_Connection_Mssql_Exception extends Doctrine_Connection_Exception { } +class Doctrine_Connection_Mssql_Exception extends Doctrine_Connection_Exception { + /** + * @var array $errorRegexps an array that is used for determining portable + * error code from a native database error code + */ + protected static $errorCodeMap = array( + 110 => MDB2_ERROR_VALUE_COUNT_ON_ROW, + 155 => MDB2_ERROR_NOSUCHFIELD, + 170 => MDB2_ERROR_SYNTAX, + 207 => MDB2_ERROR_NOSUCHFIELD, + 208 => MDB2_ERROR_NOSUCHTABLE, + 245 => MDB2_ERROR_INVALID_NUMBER, + 515 => MDB2_ERROR_CONSTRAINT_NOT_NULL, + 547 => MDB2_ERROR_CONSTRAINT, + 1913 => MDB2_ERROR_ALREADY_EXISTS, + 2627 => MDB2_ERROR_CONSTRAINT, + 2714 => MDB2_ERROR_ALREADY_EXISTS, + 3701 => MDB2_ERROR_NOSUCHTABLE, + 8134 => MDB2_ERROR_DIVZERO, + ); + /** + * This method checks if native error code/message can be + * converted into a portable code and then adds this + * 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 + */ + public function processErrorInfo(array $errorInfo) { + $code = $errorInfo[1]; + if(isset(self::$errorCodeMap[$code])) + $errorInfo[3] = self::$errorCodeMap[$code]; + + return $errorInfo; + } +} diff --git a/lib/Doctrine/Connection/Sqlite/Exception.php b/lib/Doctrine/Connection/Sqlite/Exception.php index 11b422a70..c96e9bb05 100644 --- a/lib/Doctrine/Connection/Sqlite/Exception.php +++ b/lib/Doctrine/Connection/Sqlite/Exception.php @@ -57,7 +57,7 @@ class Doctrine_Connection_Sqlite_Exception extends Doctrine_Connection_Exception * * the portable error code is added at the end of array * - * @param array $errorInfo + * @param array $errorInfo error info array * @since 1.0 * @return array */ diff --git a/lib/Doctrine/Db/Sqlite.php b/lib/Doctrine/Db/Sqlite.php deleted file mode 100644 index 91be34636..000000000 --- a/lib/Doctrine/Db/Sqlite.php +++ /dev/null @@ -1,63 +0,0 @@ -. - */ -Doctrine::autoload('Doctrine_Db'); -/** - * @package Doctrine - * @url http://www.phpdoctrine.com - * @license http://www.opensource.org/licenses/lgpl-license.php LGPL - * @author Konsta Vesterinen - * @author Lukas Smith (PEAR MDB2 library) - * @version $Id$ - */ -class Doctrine_Db_Sqlite extends Doctrine_Db { - protected static $errorRegexps = array( - '/^no such table:/' => Doctrine_Db::ERR_NOSUCHTABLE, - '/^no such index:/' => Doctrine_Db::ERR_NOT_FOUND, - '/^(table|index) .* already exists$/' => Doctrine_Db::ERR_ALREADY_EXISTS, - '/PRIMARY KEY must be unique/i' => Doctrine_Db::ERR_CONSTRAINT, - '/is not unique/' => Doctrine_Db::ERR_CONSTRAINT, - '/columns .* are not unique/i' => Doctrine_Db::ERR_CONSTRAINT, - '/uniqueness constraint failed/' => Doctrine_Db::ERR_CONSTRAINT, - '/may not be NULL/' => Doctrine_Db::ERR_CONSTRAINT_NOT_NULL, - '/^no such column:/' => Doctrine_Db::ERR_NOSUCHFIELD, - '/column not present in both tables/i' => Doctrine_Db::ERR_NOSUCHFIELD, - '/^near ".*": syntax error$/' => Doctrine_Db::ERR_SYNTAX, - '/[0-9]+ values for [0-9]+ columns/i' => Doctrine_Db::ERR_VALUE_COUNT_ON_ROW, - ); - - /** - * This method is used to collect information about an error - * - * @param integer $error - * @return array - * @access public - */ - public function processErrorInfo(array $errorInfo) { - foreach (self::$errorRegexps as $regexp => $code) { - if (preg_match($regexp, $native_msg)) { - $error = $code; - break; - } - } - - return array($error, $native_code, $native_msg); - } -}