adodb-error.inc.php 8.66 KB
Newer Older
Penny Leach's avatar
Penny Leach committed
1
<?php
2
/**
3
 * @version   v5.20.14  06-Jan-2019
4
5
 * @copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
 * @copyright (c) 2014      Damien Regad, Mark Newnham and the ADOdb community
6
7
8
 * Released under both BSD license and Lesser GPL library license.
 * Whenever there is any discrepancy between the two licenses,
 * the BSD license will take precedence.
Penny Leach's avatar
Penny Leach committed
9
10
 *
 * Set tabs to 4 for best viewing.
11
 *
Penny Leach's avatar
Penny Leach committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 * The following code is adapted from the PEAR DB error handling code.
 * Portions (c)1997-2002 The PHP Group.
 */


if (!defined("DB_ERROR")) define("DB_ERROR",-1);

if (!defined("DB_ERROR_SYNTAX")) {
	define("DB_ERROR_SYNTAX",              -2);
	define("DB_ERROR_CONSTRAINT",          -3);
	define("DB_ERROR_NOT_FOUND",           -4);
	define("DB_ERROR_ALREADY_EXISTS",      -5);
	define("DB_ERROR_UNSUPPORTED",         -6);
	define("DB_ERROR_MISMATCH",            -7);
	define("DB_ERROR_INVALID",             -8);
	define("DB_ERROR_NOT_CAPABLE",         -9);
	define("DB_ERROR_TRUNCATED",          -10);
	define("DB_ERROR_INVALID_NUMBER",     -11);
	define("DB_ERROR_INVALID_DATE",       -12);
	define("DB_ERROR_DIVZERO",            -13);
	define("DB_ERROR_NODBSELECTED",       -14);
	define("DB_ERROR_CANNOT_CREATE",      -15);
	define("DB_ERROR_CANNOT_DELETE",      -16);
	define("DB_ERROR_CANNOT_DROP",        -17);
	define("DB_ERROR_NOSUCHTABLE",        -18);
	define("DB_ERROR_NOSUCHFIELD",        -19);
	define("DB_ERROR_NEED_MORE_DATA",     -20);
	define("DB_ERROR_NOT_LOCKED",         -21);
	define("DB_ERROR_VALUE_COUNT_ON_ROW", -22);
	define("DB_ERROR_INVALID_DSN",        -23);
	define("DB_ERROR_CONNECT_FAILED",     -24);
	define("DB_ERROR_EXTENSION_NOT_FOUND",-25);
	define("DB_ERROR_NOSUCHDB",           -25);
	define("DB_ERROR_ACCESS_VIOLATION",   -26);
46
47
48
	define("DB_ERROR_DEADLOCK",           -27);
	define("DB_ERROR_STATEMENT_TIMEOUT",  -28);
	define("DB_ERROR_SERIALIZATION_FAILURE", -29);
Penny Leach's avatar
Penny Leach committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
}

function adodb_errormsg($value)
{
global $ADODB_LANG,$ADODB_LANG_ARRAY;

	if (empty($ADODB_LANG)) $ADODB_LANG = 'en';
	if (isset($ADODB_LANG_ARRAY['LANG']) && $ADODB_LANG_ARRAY['LANG'] == $ADODB_LANG) ;
	else {
		include_once(ADODB_DIR."/lang/adodb-$ADODB_LANG.inc.php");
    }
	return isset($ADODB_LANG_ARRAY[$value]) ? $ADODB_LANG_ARRAY[$value] : $ADODB_LANG_ARRAY[DB_ERROR];
}

function adodb_error($provider,$dbType,$errno)
{
	//var_dump($errno);
	if (is_numeric($errno) && $errno == 0) return 0;
67
	switch($provider) {
Penny Leach's avatar
Penny Leach committed
68
	case 'mysql': $map = adodb_error_mysql(); break;
69

Penny Leach's avatar
Penny Leach committed
70
71
	case 'oracle':
	case 'oci8': $map = adodb_error_oci8(); break;
72

Penny Leach's avatar
Penny Leach committed
73
	case 'ibase': $map = adodb_error_ibase(); break;
74

Penny Leach's avatar
Penny Leach committed
75
	case 'odbc': $map = adodb_error_odbc(); break;
76

Penny Leach's avatar
Penny Leach committed
77
78
	case 'mssql':
	case 'sybase': $map = adodb_error_mssql(); break;
79

Penny Leach's avatar
Penny Leach committed
80
	case 'informix': $map = adodb_error_ifx(); break;
81

Penny Leach's avatar
Penny Leach committed
82
	case 'postgres': return adodb_error_pg($errno); break;
83

Penny Leach's avatar
Penny Leach committed
84
85
86
	case 'sqlite': return $map = adodb_error_sqlite(); break;
	default:
		return DB_ERROR;
87
	}
Penny Leach's avatar
Penny Leach committed
88
89
90
91
92
93
94
95
96
97
98
	//print_r($map);
	//var_dump($errno);
	if (isset($map[$errno])) return $map[$errno];
	return DB_ERROR;
}

//**************************************************************************************

function adodb_error_pg($errormsg)
{
	if (is_numeric($errormsg)) return (integer) $errormsg;
99
	// Postgres has no lock-wait timeout.  The best we could do would be to set a statement timeout.
100
101
102
103
104
105
106
107
108
109
110
111
112
	static $error_regexps = array(
			'(Table does not exist\.|Relation [\"\'].*[\"\'] does not exist|sequence does not exist|class ".+" not found)$' => DB_ERROR_NOSUCHTABLE,
			'Relation [\"\'].*[\"\'] already exists|Cannot insert a duplicate key into (a )?unique index.*|duplicate key.*violates unique constraint'     => DB_ERROR_ALREADY_EXISTS,
			'database ".+" does not exist$'       => DB_ERROR_NOSUCHDB,
			'(divide|division) by zero$'          => DB_ERROR_DIVZERO,
			'pg_atoi: error in .*: can\'t parse ' => DB_ERROR_INVALID_NUMBER,
			'ttribute [\"\'].*[\"\'] not found|Relation [\"\'].*[\"\'] does not have attribute [\"\'].*[\"\']' => DB_ERROR_NOSUCHFIELD,
			'(parser: parse|syntax) error at or near \"'   => DB_ERROR_SYNTAX,
			'referential integrity violation'     => DB_ERROR_CONSTRAINT,
			'deadlock detected$'                  => DB_ERROR_DEADLOCK,
			'canceling statement due to statement timeout$' => DB_ERROR_STATEMENT_TIMEOUT,
			'could not serialize access due to'   => DB_ERROR_SERIALIZATION_FAILURE
		);
Penny Leach's avatar
Penny Leach committed
113
	reset($error_regexps);
114
	foreach ($error_regexps as $regexp => $code) {
115
116
117
118
119
120
		if (preg_match("/$regexp/mi", $errormsg)) {
			return $code;
		}
	}
	// Fall back to DB_ERROR if there was no mapping.
	return DB_ERROR;
Penny Leach's avatar
Penny Leach committed
121
}
122

Penny Leach's avatar
Penny Leach committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
function adodb_error_odbc()
{
static $MAP = array(
            '01004' => DB_ERROR_TRUNCATED,
            '07001' => DB_ERROR_MISMATCH,
            '21S01' => DB_ERROR_MISMATCH,
            '21S02' => DB_ERROR_MISMATCH,
            '22003' => DB_ERROR_INVALID_NUMBER,
            '22008' => DB_ERROR_INVALID_DATE,
            '22012' => DB_ERROR_DIVZERO,
            '23000' => DB_ERROR_CONSTRAINT,
            '24000' => DB_ERROR_INVALID,
            '34000' => DB_ERROR_INVALID,
            '37000' => DB_ERROR_SYNTAX,
            '42000' => DB_ERROR_SYNTAX,
            'IM001' => DB_ERROR_UNSUPPORTED,
            'S0000' => DB_ERROR_NOSUCHTABLE,
            'S0001' => DB_ERROR_NOT_FOUND,
            'S0002' => DB_ERROR_NOSUCHTABLE,
            'S0011' => DB_ERROR_ALREADY_EXISTS,
            'S0012' => DB_ERROR_NOT_FOUND,
            'S0021' => DB_ERROR_ALREADY_EXISTS,
            'S0022' => DB_ERROR_NOT_FOUND,
			'S1000' => DB_ERROR_NOSUCHTABLE,
            'S1009' => DB_ERROR_INVALID,
            'S1090' => DB_ERROR_INVALID,
            'S1C00' => DB_ERROR_NOT_CAPABLE
        );
		return $MAP;
}

function adodb_error_ibase()
{
static $MAP = array(
            -104 => DB_ERROR_SYNTAX,
            -150 => DB_ERROR_ACCESS_VIOLATION,
            -151 => DB_ERROR_ACCESS_VIOLATION,
            -155 => DB_ERROR_NOSUCHTABLE,
            -157 => DB_ERROR_NOSUCHFIELD,
            -158 => DB_ERROR_VALUE_COUNT_ON_ROW,
            -170 => DB_ERROR_MISMATCH,
            -171 => DB_ERROR_MISMATCH,
            -172 => DB_ERROR_INVALID,
            -204 => DB_ERROR_INVALID,
            -205 => DB_ERROR_NOSUCHFIELD,
            -206 => DB_ERROR_NOSUCHFIELD,
            -208 => DB_ERROR_INVALID,
            -219 => DB_ERROR_NOSUCHTABLE,
            -297 => DB_ERROR_CONSTRAINT,
            -530 => DB_ERROR_CONSTRAINT,
            -803 => DB_ERROR_CONSTRAINT,
            -551 => DB_ERROR_ACCESS_VIOLATION,
            -552 => DB_ERROR_ACCESS_VIOLATION,
            -922 => DB_ERROR_NOSUCHDB,
            -923 => DB_ERROR_CONNECT_FAILED,
            -924 => DB_ERROR_CONNECT_FAILED
        );
180

Penny Leach's avatar
Penny Leach committed
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
		return $MAP;
}

function adodb_error_ifx()
{
static $MAP = array(
            '-201'    => DB_ERROR_SYNTAX,
            '-206'    => DB_ERROR_NOSUCHTABLE,
            '-217'    => DB_ERROR_NOSUCHFIELD,
            '-329'    => DB_ERROR_NODBSELECTED,
            '-1204'   => DB_ERROR_INVALID_DATE,
            '-1205'   => DB_ERROR_INVALID_DATE,
            '-1206'   => DB_ERROR_INVALID_DATE,
            '-1209'   => DB_ERROR_INVALID_DATE,
            '-1210'   => DB_ERROR_INVALID_DATE,
            '-1212'   => DB_ERROR_INVALID_DATE
       );
198

Penny Leach's avatar
Penny Leach committed
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
	   return $MAP;
}

function adodb_error_oci8()
{
static $MAP = array(
			 1 => DB_ERROR_ALREADY_EXISTS,
            900 => DB_ERROR_SYNTAX,
            904 => DB_ERROR_NOSUCHFIELD,
            923 => DB_ERROR_SYNTAX,
            942 => DB_ERROR_NOSUCHTABLE,
            955 => DB_ERROR_ALREADY_EXISTS,
            1476 => DB_ERROR_DIVZERO,
            1722 => DB_ERROR_INVALID_NUMBER,
            2289 => DB_ERROR_NOSUCHTABLE,
            2291 => DB_ERROR_CONSTRAINT,
            2449 => DB_ERROR_CONSTRAINT
        );
217

Penny Leach's avatar
Penny Leach committed
218
219
220
221
222
223
224
225
226
	return $MAP;
}

function adodb_error_mssql()
{
static $MAP = array(
		  208 => DB_ERROR_NOSUCHTABLE,
          2601 => DB_ERROR_ALREADY_EXISTS
       );
227

Penny Leach's avatar
Penny Leach committed
228
229
230
231
232
233
234
235
	return $MAP;
}

function adodb_error_sqlite()
{
static $MAP = array(
		  1 => DB_ERROR_SYNTAX
       );
236

Penny Leach's avatar
Penny Leach committed
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
	return $MAP;
}

function adodb_error_mysql()
{
static $MAP = array(
           1004 => DB_ERROR_CANNOT_CREATE,
           1005 => DB_ERROR_CANNOT_CREATE,
           1006 => DB_ERROR_CANNOT_CREATE,
           1007 => DB_ERROR_ALREADY_EXISTS,
           1008 => DB_ERROR_CANNOT_DROP,
		   1045 => DB_ERROR_ACCESS_VIOLATION,
           1046 => DB_ERROR_NODBSELECTED,
		   1049 => DB_ERROR_NOSUCHDB,
           1050 => DB_ERROR_ALREADY_EXISTS,
           1051 => DB_ERROR_NOSUCHTABLE,
           1054 => DB_ERROR_NOSUCHFIELD,
           1062 => DB_ERROR_ALREADY_EXISTS,
           1064 => DB_ERROR_SYNTAX,
           1100 => DB_ERROR_NOT_LOCKED,
           1136 => DB_ERROR_VALUE_COUNT_ON_ROW,
           1146 => DB_ERROR_NOSUCHTABLE,
           1048 => DB_ERROR_CONSTRAINT,
		    2002 => DB_ERROR_CONNECT_FAILED,
			2005 => DB_ERROR_CONNECT_FAILED
       );
263

Penny Leach's avatar
Penny Leach committed
264
265
	return $MAP;
}