Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401]

   1  <?php
   2  /**
   3   * Error handling code and constants.
   4   *
   5   * Adapted from the PEAR DB error handling code.
   6   * Portions (c)1997-2002 The PHP Group
   7   *
   8   * This file is part of ADOdb, a Database Abstraction Layer library for PHP.
   9   *
  10   * @package ADOdb
  11   * @link https://adodb.org Project's web site and documentation
  12   * @link https://github.com/ADOdb/ADOdb Source code and issue tracker
  13   *
  14   * The ADOdb Library is dual-licensed, released under both the BSD 3-Clause
  15   * and the GNU Lesser General Public Licence (LGPL) v2.1 or, at your option,
  16   * any later version. This means you can use it in proprietary products.
  17   * See the LICENSE.md file distributed with this source code for details.
  18   * @license BSD-3-Clause
  19   * @license LGPL-2.1-or-later
  20   *
  21   * @copyright 2000-2013 John Lim
  22   * @copyright 2014 Damien Regad, Mark Newnham and the ADOdb community
  23   */
  24  
  25  if (!defined("DB_ERROR")) define("DB_ERROR",-1);
  26  
  27  if (!defined("DB_ERROR_SYNTAX")) {
  28  	 define("DB_ERROR_SYNTAX",              -2);
  29  	 define("DB_ERROR_CONSTRAINT",          -3);
  30  	 define("DB_ERROR_NOT_FOUND",           -4);
  31  	 define("DB_ERROR_ALREADY_EXISTS",      -5);
  32  	 define("DB_ERROR_UNSUPPORTED",         -6);
  33  	 define("DB_ERROR_MISMATCH",            -7);
  34  	 define("DB_ERROR_INVALID",             -8);
  35  	 define("DB_ERROR_NOT_CAPABLE",         -9);
  36  	 define("DB_ERROR_TRUNCATED",          -10);
  37  	 define("DB_ERROR_INVALID_NUMBER",     -11);
  38  	 define("DB_ERROR_INVALID_DATE",       -12);
  39  	 define("DB_ERROR_DIVZERO",            -13);
  40  	 define("DB_ERROR_NODBSELECTED",       -14);
  41  	 define("DB_ERROR_CANNOT_CREATE",      -15);
  42  	 define("DB_ERROR_CANNOT_DELETE",      -16);
  43  	 define("DB_ERROR_CANNOT_DROP",        -17);
  44  	 define("DB_ERROR_NOSUCHTABLE",        -18);
  45  	 define("DB_ERROR_NOSUCHFIELD",        -19);
  46  	 define("DB_ERROR_NEED_MORE_DATA",     -20);
  47  	 define("DB_ERROR_NOT_LOCKED",         -21);
  48  	 define("DB_ERROR_VALUE_COUNT_ON_ROW", -22);
  49  	 define("DB_ERROR_INVALID_DSN",        -23);
  50  	 define("DB_ERROR_CONNECT_FAILED",     -24);
  51  	 define("DB_ERROR_EXTENSION_NOT_FOUND",-25);
  52  	 define("DB_ERROR_NOSUCHDB",           -25);
  53  	 define("DB_ERROR_ACCESS_VIOLATION",   -26);
  54  	 define("DB_ERROR_DEADLOCK",           -27);
  55  	 define("DB_ERROR_STATEMENT_TIMEOUT",  -28);
  56  	 define("DB_ERROR_SERIALIZATION_FAILURE", -29);
  57  }
  58  
  59  function adodb_errormsg($value)
  60  {
  61  global $ADODB_LANG,$ADODB_LANG_ARRAY;
  62  
  63  	 if (empty($ADODB_LANG)) $ADODB_LANG = 'en';
  64  	 if (isset($ADODB_LANG_ARRAY['LANG']) && $ADODB_LANG_ARRAY['LANG'] == $ADODB_LANG) ;
  65  	 else {
  66  	 	 include_once(ADODB_DIR."/lang/adodb-$ADODB_LANG.inc.php");
  67      }
  68  	 return isset($ADODB_LANG_ARRAY[$value]) ? $ADODB_LANG_ARRAY[$value] : $ADODB_LANG_ARRAY[DB_ERROR];
  69  }
  70  
  71  function adodb_error($provider,$dbType,$errno)
  72  {
  73  	 //var_dump($errno);
  74  	 if (is_numeric($errno) && $errno == 0) return 0;
  75  	 switch($provider) {
  76  	 case 'mysql': $map = adodb_error_mysql(); break;
  77  
  78  	 case 'oracle':
  79  	 case 'oci8': $map = adodb_error_oci8(); break;
  80  
  81  	 // As discussed in https://github.com/ADOdb/ADOdb/issues/201#issuecomment-188154980
  82  	 // firebird uses the ibase error handler for now. This may change if and
  83  	 // when the PHP driver is updated to use the new SQLSTATE error codes
  84  	 case 'firebird':
  85  	 case 'ibase': $map = adodb_error_ibase(); break;
  86  
  87  	 case 'odbc': $map = adodb_error_odbc(); break;
  88  
  89  	 case 'mssql':
  90  	 case 'sybase': $map = adodb_error_mssql(); break;
  91  
  92  	 case 'informix': $map = adodb_error_ifx(); break;
  93  
  94  	 case 'postgres': return adodb_error_pg($errno); break;
  95  
  96  	 case 'sqlite': return $map = adodb_error_sqlite(); break;
  97  	 default:
  98  	 	 return DB_ERROR;
  99  	 }
 100  	 //print_r($map);
 101  	 //var_dump($errno);
 102  	 if (isset($map[$errno])) return $map[$errno];
 103  	 return DB_ERROR;
 104  }
 105  
 106  //**************************************************************************************
 107  
 108  function adodb_error_pg($errormsg)
 109  {
 110  	 if (is_numeric($errormsg)) return (integer) $errormsg;
 111  	 // Postgres has no lock-wait timeout.  The best we could do would be to set a statement timeout.
 112  	 static $error_regexps = array(
 113  	 	 	 '(Table does not exist\.|Relation [\"\'].*[\"\'] does not exist|sequence does not exist|class ".+" not found)$' => DB_ERROR_NOSUCHTABLE,
 114  	 	 	 'Relation [\"\'].*[\"\'] already exists|Cannot insert a duplicate key into (a )?unique index.*|duplicate key.*violates unique constraint'     => DB_ERROR_ALREADY_EXISTS,
 115  	 	 	 'database ".+" does not exist$'       => DB_ERROR_NOSUCHDB,
 116  	 	 	 '(divide|division) by zero$'          => DB_ERROR_DIVZERO,
 117  	 	 	 'pg_atoi: error in .*: can\'t parse ' => DB_ERROR_INVALID_NUMBER,
 118  	 	 	 'ttribute [\"\'].*[\"\'] not found|Relation [\"\'].*[\"\'] does not have attribute [\"\'].*[\"\']' => DB_ERROR_NOSUCHFIELD,
 119  	 	 	 '(parser: parse|syntax) error at or near \"'   => DB_ERROR_SYNTAX,
 120  	 	 	 'referential integrity violation'     => DB_ERROR_CONSTRAINT,
 121  	 	 	 'deadlock detected$'                  => DB_ERROR_DEADLOCK,
 122  	 	 	 'canceling statement due to statement timeout$' => DB_ERROR_STATEMENT_TIMEOUT,
 123  	 	 	 'could not serialize access due to'   => DB_ERROR_SERIALIZATION_FAILURE
 124  	 	 );
 125  	 reset($error_regexps);
 126  	 foreach ($error_regexps as $regexp => $code) {
 127  	 	 if (preg_match("/$regexp/mi", $errormsg)) {
 128  	 	 	 return $code;
 129  	 	 }
 130  	 }
 131  	 // Fall back to DB_ERROR if there was no mapping.
 132  	 return DB_ERROR;
 133  }
 134  
 135  function adodb_error_odbc()
 136  {
 137  static $MAP = array(
 138              '01004' => DB_ERROR_TRUNCATED,
 139              '07001' => DB_ERROR_MISMATCH,
 140              '21S01' => DB_ERROR_MISMATCH,
 141              '21S02' => DB_ERROR_MISMATCH,
 142              '22003' => DB_ERROR_INVALID_NUMBER,
 143              '22008' => DB_ERROR_INVALID_DATE,
 144              '22012' => DB_ERROR_DIVZERO,
 145              '23000' => DB_ERROR_CONSTRAINT,
 146              '24000' => DB_ERROR_INVALID,
 147              '34000' => DB_ERROR_INVALID,
 148              '37000' => DB_ERROR_SYNTAX,
 149              '42000' => DB_ERROR_SYNTAX,
 150              'IM001' => DB_ERROR_UNSUPPORTED,
 151              'S0000' => DB_ERROR_NOSUCHTABLE,
 152              'S0001' => DB_ERROR_NOT_FOUND,
 153              'S0002' => DB_ERROR_NOSUCHTABLE,
 154              'S0011' => DB_ERROR_ALREADY_EXISTS,
 155              'S0012' => DB_ERROR_NOT_FOUND,
 156              'S0021' => DB_ERROR_ALREADY_EXISTS,
 157              'S0022' => DB_ERROR_NOT_FOUND,
 158  	 	 	 'S1000' => DB_ERROR_NOSUCHTABLE,
 159              'S1009' => DB_ERROR_INVALID,
 160              'S1090' => DB_ERROR_INVALID,
 161              'S1C00' => DB_ERROR_NOT_CAPABLE
 162          );
 163  	 	 return $MAP;
 164  }
 165  
 166  function adodb_error_ibase()
 167  {
 168  static $MAP = array(
 169              -104 => DB_ERROR_SYNTAX,
 170              -150 => DB_ERROR_ACCESS_VIOLATION,
 171              -151 => DB_ERROR_ACCESS_VIOLATION,
 172              -155 => DB_ERROR_NOSUCHTABLE,
 173              -157 => DB_ERROR_NOSUCHFIELD,
 174              -158 => DB_ERROR_VALUE_COUNT_ON_ROW,
 175              -170 => DB_ERROR_MISMATCH,
 176              -171 => DB_ERROR_MISMATCH,
 177              -172 => DB_ERROR_INVALID,
 178              -204 => DB_ERROR_INVALID,
 179              -205 => DB_ERROR_NOSUCHFIELD,
 180              -206 => DB_ERROR_NOSUCHFIELD,
 181              -208 => DB_ERROR_INVALID,
 182              -219 => DB_ERROR_NOSUCHTABLE,
 183              -297 => DB_ERROR_CONSTRAINT,
 184              -530 => DB_ERROR_CONSTRAINT,
 185              -803 => DB_ERROR_CONSTRAINT,
 186              -551 => DB_ERROR_ACCESS_VIOLATION,
 187              -552 => DB_ERROR_ACCESS_VIOLATION,
 188              -922 => DB_ERROR_NOSUCHDB,
 189              -923 => DB_ERROR_CONNECT_FAILED,
 190              -924 => DB_ERROR_CONNECT_FAILED
 191          );
 192  
 193  	 	 return $MAP;
 194  }
 195  
 196  function adodb_error_ifx()
 197  {
 198  static $MAP = array(
 199              '-201'    => DB_ERROR_SYNTAX,
 200              '-206'    => DB_ERROR_NOSUCHTABLE,
 201              '-217'    => DB_ERROR_NOSUCHFIELD,
 202              '-329'    => DB_ERROR_NODBSELECTED,
 203              '-1204'   => DB_ERROR_INVALID_DATE,
 204              '-1205'   => DB_ERROR_INVALID_DATE,
 205              '-1206'   => DB_ERROR_INVALID_DATE,
 206              '-1209'   => DB_ERROR_INVALID_DATE,
 207              '-1210'   => DB_ERROR_INVALID_DATE,
 208              '-1212'   => DB_ERROR_INVALID_DATE
 209         );
 210  
 211  	    return $MAP;
 212  }
 213  
 214  function adodb_error_oci8()
 215  {
 216  static $MAP = array(
 217  	 	 	  1 => DB_ERROR_ALREADY_EXISTS,
 218              900 => DB_ERROR_SYNTAX,
 219              904 => DB_ERROR_NOSUCHFIELD,
 220              923 => DB_ERROR_SYNTAX,
 221              942 => DB_ERROR_NOSUCHTABLE,
 222              955 => DB_ERROR_ALREADY_EXISTS,
 223              1476 => DB_ERROR_DIVZERO,
 224              1722 => DB_ERROR_INVALID_NUMBER,
 225              2289 => DB_ERROR_NOSUCHTABLE,
 226              2291 => DB_ERROR_CONSTRAINT,
 227              2449 => DB_ERROR_CONSTRAINT
 228          );
 229  
 230  	 return $MAP;
 231  }
 232  
 233  function adodb_error_mssql()
 234  {
 235  static $MAP = array(
 236  	 	   208 => DB_ERROR_NOSUCHTABLE,
 237            2601 => DB_ERROR_ALREADY_EXISTS
 238         );
 239  
 240  	 return $MAP;
 241  }
 242  
 243  function adodb_error_sqlite()
 244  {
 245  static $MAP = array(
 246  	 	   1 => DB_ERROR_SYNTAX
 247         );
 248  
 249  	 return $MAP;
 250  }
 251  
 252  function adodb_error_mysql()
 253  {
 254  static $MAP = array(
 255             1004 => DB_ERROR_CANNOT_CREATE,
 256             1005 => DB_ERROR_CANNOT_CREATE,
 257             1006 => DB_ERROR_CANNOT_CREATE,
 258             1007 => DB_ERROR_ALREADY_EXISTS,
 259             1008 => DB_ERROR_CANNOT_DROP,
 260  	    1045 => DB_ERROR_ACCESS_VIOLATION,
 261             1046 => DB_ERROR_NODBSELECTED,
 262  	    1049 => DB_ERROR_NOSUCHDB,
 263             1050 => DB_ERROR_ALREADY_EXISTS,
 264             1051 => DB_ERROR_NOSUCHTABLE,
 265             1054 => DB_ERROR_NOSUCHFIELD,
 266             1062 => DB_ERROR_ALREADY_EXISTS,
 267             1064 => DB_ERROR_SYNTAX,
 268             1100 => DB_ERROR_NOT_LOCKED,
 269             1136 => DB_ERROR_VALUE_COUNT_ON_ROW,
 270             1146 => DB_ERROR_NOSUCHTABLE,
 271             1048 => DB_ERROR_CONSTRAINT,
 272  	    2002 => DB_ERROR_CONNECT_FAILED,
 273             2005 => DB_ERROR_CONNECT_FAILED
 274         );
 275  
 276  	 return $MAP;
 277  }