weekly release 3.11.8+
[moodle.git] / lib / adodb / adodb-error.inc.php
blobb5a352c60c224f6cb41803a35e95c4dfaed19038
1 <?php
2 /**
3 * @version v5.21.0 2021-02-27
4 * @copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
5 * @copyright (c) 2014 Damien Regad, Mark Newnham and the ADOdb community
6 * Released under both BSD license and Lesser GPL library license.
7 * Whenever there is any discrepancy between the two licenses,
8 * the BSD license will take precedence.
10 * Set tabs to 4 for best viewing.
12 * The following code is adapted from the PEAR DB error handling code.
13 * Portions (c)1997-2002 The PHP Group.
17 if (!defined("DB_ERROR")) define("DB_ERROR",-1);
19 if (!defined("DB_ERROR_SYNTAX")) {
20 define("DB_ERROR_SYNTAX", -2);
21 define("DB_ERROR_CONSTRAINT", -3);
22 define("DB_ERROR_NOT_FOUND", -4);
23 define("DB_ERROR_ALREADY_EXISTS", -5);
24 define("DB_ERROR_UNSUPPORTED", -6);
25 define("DB_ERROR_MISMATCH", -7);
26 define("DB_ERROR_INVALID", -8);
27 define("DB_ERROR_NOT_CAPABLE", -9);
28 define("DB_ERROR_TRUNCATED", -10);
29 define("DB_ERROR_INVALID_NUMBER", -11);
30 define("DB_ERROR_INVALID_DATE", -12);
31 define("DB_ERROR_DIVZERO", -13);
32 define("DB_ERROR_NODBSELECTED", -14);
33 define("DB_ERROR_CANNOT_CREATE", -15);
34 define("DB_ERROR_CANNOT_DELETE", -16);
35 define("DB_ERROR_CANNOT_DROP", -17);
36 define("DB_ERROR_NOSUCHTABLE", -18);
37 define("DB_ERROR_NOSUCHFIELD", -19);
38 define("DB_ERROR_NEED_MORE_DATA", -20);
39 define("DB_ERROR_NOT_LOCKED", -21);
40 define("DB_ERROR_VALUE_COUNT_ON_ROW", -22);
41 define("DB_ERROR_INVALID_DSN", -23);
42 define("DB_ERROR_CONNECT_FAILED", -24);
43 define("DB_ERROR_EXTENSION_NOT_FOUND",-25);
44 define("DB_ERROR_NOSUCHDB", -25);
45 define("DB_ERROR_ACCESS_VIOLATION", -26);
46 define("DB_ERROR_DEADLOCK", -27);
47 define("DB_ERROR_STATEMENT_TIMEOUT", -28);
48 define("DB_ERROR_SERIALIZATION_FAILURE", -29);
51 function adodb_errormsg($value)
53 global $ADODB_LANG,$ADODB_LANG_ARRAY;
55 if (empty($ADODB_LANG)) $ADODB_LANG = 'en';
56 if (isset($ADODB_LANG_ARRAY['LANG']) && $ADODB_LANG_ARRAY['LANG'] == $ADODB_LANG) ;
57 else {
58 include_once(ADODB_DIR."/lang/adodb-$ADODB_LANG.inc.php");
60 return isset($ADODB_LANG_ARRAY[$value]) ? $ADODB_LANG_ARRAY[$value] : $ADODB_LANG_ARRAY[DB_ERROR];
63 function adodb_error($provider,$dbType,$errno)
65 //var_dump($errno);
66 if (is_numeric($errno) && $errno == 0) return 0;
67 switch($provider) {
68 case 'mysql': $map = adodb_error_mysql(); break;
70 case 'oracle':
71 case 'oci8': $map = adodb_error_oci8(); break;
73 // As discussed in https://github.com/ADOdb/ADOdb/issues/201#issuecomment-188154980
74 // firebird uses the ibase error handler for now. This may change if and
75 // when the PHP driver is updated to use the new SQLSTATE error codes
76 case 'firebird':
77 case 'ibase': $map = adodb_error_ibase(); break;
79 case 'odbc': $map = adodb_error_odbc(); break;
81 case 'mssql':
82 case 'sybase': $map = adodb_error_mssql(); break;
84 case 'informix': $map = adodb_error_ifx(); break;
86 case 'postgres': return adodb_error_pg($errno); break;
88 case 'sqlite': return $map = adodb_error_sqlite(); break;
89 default:
90 return DB_ERROR;
92 //print_r($map);
93 //var_dump($errno);
94 if (isset($map[$errno])) return $map[$errno];
95 return DB_ERROR;
98 //**************************************************************************************
100 function adodb_error_pg($errormsg)
102 if (is_numeric($errormsg)) return (integer) $errormsg;
103 // Postgres has no lock-wait timeout. The best we could do would be to set a statement timeout.
104 static $error_regexps = array(
105 '(Table does not exist\.|Relation [\"\'].*[\"\'] does not exist|sequence does not exist|class ".+" not found)$' => DB_ERROR_NOSUCHTABLE,
106 'Relation [\"\'].*[\"\'] already exists|Cannot insert a duplicate key into (a )?unique index.*|duplicate key.*violates unique constraint' => DB_ERROR_ALREADY_EXISTS,
107 'database ".+" does not exist$' => DB_ERROR_NOSUCHDB,
108 '(divide|division) by zero$' => DB_ERROR_DIVZERO,
109 'pg_atoi: error in .*: can\'t parse ' => DB_ERROR_INVALID_NUMBER,
110 'ttribute [\"\'].*[\"\'] not found|Relation [\"\'].*[\"\'] does not have attribute [\"\'].*[\"\']' => DB_ERROR_NOSUCHFIELD,
111 '(parser: parse|syntax) error at or near \"' => DB_ERROR_SYNTAX,
112 'referential integrity violation' => DB_ERROR_CONSTRAINT,
113 'deadlock detected$' => DB_ERROR_DEADLOCK,
114 'canceling statement due to statement timeout$' => DB_ERROR_STATEMENT_TIMEOUT,
115 'could not serialize access due to' => DB_ERROR_SERIALIZATION_FAILURE
117 reset($error_regexps);
118 foreach ($error_regexps as $regexp => $code) {
119 if (preg_match("/$regexp/mi", $errormsg)) {
120 return $code;
123 // Fall back to DB_ERROR if there was no mapping.
124 return DB_ERROR;
127 function adodb_error_odbc()
129 static $MAP = array(
130 '01004' => DB_ERROR_TRUNCATED,
131 '07001' => DB_ERROR_MISMATCH,
132 '21S01' => DB_ERROR_MISMATCH,
133 '21S02' => DB_ERROR_MISMATCH,
134 '22003' => DB_ERROR_INVALID_NUMBER,
135 '22008' => DB_ERROR_INVALID_DATE,
136 '22012' => DB_ERROR_DIVZERO,
137 '23000' => DB_ERROR_CONSTRAINT,
138 '24000' => DB_ERROR_INVALID,
139 '34000' => DB_ERROR_INVALID,
140 '37000' => DB_ERROR_SYNTAX,
141 '42000' => DB_ERROR_SYNTAX,
142 'IM001' => DB_ERROR_UNSUPPORTED,
143 'S0000' => DB_ERROR_NOSUCHTABLE,
144 'S0001' => DB_ERROR_NOT_FOUND,
145 'S0002' => DB_ERROR_NOSUCHTABLE,
146 'S0011' => DB_ERROR_ALREADY_EXISTS,
147 'S0012' => DB_ERROR_NOT_FOUND,
148 'S0021' => DB_ERROR_ALREADY_EXISTS,
149 'S0022' => DB_ERROR_NOT_FOUND,
150 'S1000' => DB_ERROR_NOSUCHTABLE,
151 'S1009' => DB_ERROR_INVALID,
152 'S1090' => DB_ERROR_INVALID,
153 'S1C00' => DB_ERROR_NOT_CAPABLE
155 return $MAP;
158 function adodb_error_ibase()
160 static $MAP = array(
161 -104 => DB_ERROR_SYNTAX,
162 -150 => DB_ERROR_ACCESS_VIOLATION,
163 -151 => DB_ERROR_ACCESS_VIOLATION,
164 -155 => DB_ERROR_NOSUCHTABLE,
165 -157 => DB_ERROR_NOSUCHFIELD,
166 -158 => DB_ERROR_VALUE_COUNT_ON_ROW,
167 -170 => DB_ERROR_MISMATCH,
168 -171 => DB_ERROR_MISMATCH,
169 -172 => DB_ERROR_INVALID,
170 -204 => DB_ERROR_INVALID,
171 -205 => DB_ERROR_NOSUCHFIELD,
172 -206 => DB_ERROR_NOSUCHFIELD,
173 -208 => DB_ERROR_INVALID,
174 -219 => DB_ERROR_NOSUCHTABLE,
175 -297 => DB_ERROR_CONSTRAINT,
176 -530 => DB_ERROR_CONSTRAINT,
177 -803 => DB_ERROR_CONSTRAINT,
178 -551 => DB_ERROR_ACCESS_VIOLATION,
179 -552 => DB_ERROR_ACCESS_VIOLATION,
180 -922 => DB_ERROR_NOSUCHDB,
181 -923 => DB_ERROR_CONNECT_FAILED,
182 -924 => DB_ERROR_CONNECT_FAILED
185 return $MAP;
188 function adodb_error_ifx()
190 static $MAP = array(
191 '-201' => DB_ERROR_SYNTAX,
192 '-206' => DB_ERROR_NOSUCHTABLE,
193 '-217' => DB_ERROR_NOSUCHFIELD,
194 '-329' => DB_ERROR_NODBSELECTED,
195 '-1204' => DB_ERROR_INVALID_DATE,
196 '-1205' => DB_ERROR_INVALID_DATE,
197 '-1206' => DB_ERROR_INVALID_DATE,
198 '-1209' => DB_ERROR_INVALID_DATE,
199 '-1210' => DB_ERROR_INVALID_DATE,
200 '-1212' => DB_ERROR_INVALID_DATE
203 return $MAP;
206 function adodb_error_oci8()
208 static $MAP = array(
209 1 => DB_ERROR_ALREADY_EXISTS,
210 900 => DB_ERROR_SYNTAX,
211 904 => DB_ERROR_NOSUCHFIELD,
212 923 => DB_ERROR_SYNTAX,
213 942 => DB_ERROR_NOSUCHTABLE,
214 955 => DB_ERROR_ALREADY_EXISTS,
215 1476 => DB_ERROR_DIVZERO,
216 1722 => DB_ERROR_INVALID_NUMBER,
217 2289 => DB_ERROR_NOSUCHTABLE,
218 2291 => DB_ERROR_CONSTRAINT,
219 2449 => DB_ERROR_CONSTRAINT
222 return $MAP;
225 function adodb_error_mssql()
227 static $MAP = array(
228 208 => DB_ERROR_NOSUCHTABLE,
229 2601 => DB_ERROR_ALREADY_EXISTS
232 return $MAP;
235 function adodb_error_sqlite()
237 static $MAP = array(
238 1 => DB_ERROR_SYNTAX
241 return $MAP;
244 function adodb_error_mysql()
246 static $MAP = array(
247 1004 => DB_ERROR_CANNOT_CREATE,
248 1005 => DB_ERROR_CANNOT_CREATE,
249 1006 => DB_ERROR_CANNOT_CREATE,
250 1007 => DB_ERROR_ALREADY_EXISTS,
251 1008 => DB_ERROR_CANNOT_DROP,
252 1045 => DB_ERROR_ACCESS_VIOLATION,
253 1046 => DB_ERROR_NODBSELECTED,
254 1049 => DB_ERROR_NOSUCHDB,
255 1050 => DB_ERROR_ALREADY_EXISTS,
256 1051 => DB_ERROR_NOSUCHTABLE,
257 1054 => DB_ERROR_NOSUCHFIELD,
258 1062 => DB_ERROR_ALREADY_EXISTS,
259 1064 => DB_ERROR_SYNTAX,
260 1100 => DB_ERROR_NOT_LOCKED,
261 1136 => DB_ERROR_VALUE_COUNT_ON_ROW,
262 1146 => DB_ERROR_NOSUCHTABLE,
263 1048 => DB_ERROR_CONSTRAINT,
264 2002 => DB_ERROR_CONNECT_FAILED,
265 2005 => DB_ERROR_CONNECT_FAILED
268 return $MAP;