xref: /PHP-8.0/ext/pdo/pdo_sqlstate.c (revision 5d6e923d)
1 /*
2   +----------------------------------------------------------------------+
3   | Copyright (c) The PHP Group                                          |
4   +----------------------------------------------------------------------+
5   | This source file is subject to version 3.01 of the PHP license,      |
6   | that is bundled with this package in the file LICENSE, and is        |
7   | available through the world-wide-web at the following url:           |
8   | http://www.php.net/license/3_01.txt                                  |
9   | If you did not receive a copy of the PHP license and are unable to   |
10   | obtain it through the world-wide-web, please send a note to          |
11   | license@php.net so we can mail you a copy immediately.               |
12   +----------------------------------------------------------------------+
13   | Author: Wez Furlong <wez@php.net>                                    |
14   +----------------------------------------------------------------------+
15 */
16 
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20 
21 #include "php.h"
22 #include "php_ini.h"
23 #include "ext/standard/info.h"
24 #include "php_pdo.h"
25 #include "php_pdo_driver.h"
26 
27 struct pdo_sqlstate_info {
28 	const char state[5];
29 	const char *desc;
30 };
31 
32 static HashTable err_hash;
33 
34 static const struct pdo_sqlstate_info err_initializer[] = {
35 	{ "00000",	"No error" },
36 	{ "01000",	"Warning" },
37 	{ "01001",	"Cursor operation conflict" },
38 	{ "01002",	"Disconnect error" },
39 	{ "01003",	"NULL value eliminated in set function" },
40 	{ "01004",	"String data, right truncated" },
41 	{ "01006",	"Privilege not revoked" },
42 	{ "01007",	"Privilege not granted" },
43 	{ "01008",	"Implicit zero bit padding" },
44 	{ "0100C",	"Dynamic result sets returned" },
45 	{ "01P01",	"Deprecated feature" },
46 	{ "01S00",	"Invalid connection string attribute" },
47 	{ "01S01",	"Error in row" },
48 	{ "01S02",	"Option value changed" },
49 	{ "01S06",	"Attempt to fetch before the result set returned the first rowset" },
50 	{ "01S07",	"Fractional truncation" },
51 	{ "01S08",	"Error saving File DSN" },
52 	{ "01S09",	"Invalid keyword" },
53 	{ "02000",	"No data" },
54 	{ "02001",	"No additional dynamic result sets returned" },
55 	{ "03000",	"Sql statement not yet complete" },
56 	{ "07002",	"COUNT field incorrect" },
57 	{ "07005",	"Prepared statement not a cursor-specification" },
58 	{ "07006",	"Restricted data type attribute violation" },
59 	{ "07009",	"Invalid descriptor index" },
60 	{ "07S01",	"Invalid use of default parameter" },
61 	{ "08000",	"Connection exception" },
62 	{ "08001",	"Client unable to establish connection" },
63 	{ "08002",	"Connection name in use" },
64 	{ "08003",	"Connection does not exist" },
65 	{ "08004",	"Server rejected the connection" },
66 	{ "08006",	"Connection failure" },
67 	{ "08007",	"Connection failure during transaction" },
68 	{ "08S01",	"Communication link failure" },
69 	{ "09000",	"Triggered action exception" },
70 	{ "0A000",	"Feature not supported" },
71 	{ "0B000",	"Invalid transaction initiation" },
72 	{ "0F000",	"Locator exception" },
73 	{ "0F001",	"Invalid locator specification" },
74 	{ "0L000",	"Invalid grantor" },
75 	{ "0LP01",	"Invalid grant operation" },
76 	{ "0P000",	"Invalid role specification" },
77 	{ "21000",	"Cardinality violation" },
78 	{ "21S01",	"Insert value list does not match column list" },
79 	{ "21S02",	"Degree of derived table does not match column list" },
80 	{ "22000",	"Data exception" },
81 	{ "22001",	"String data, right truncated" },
82 	{ "22002",	"Indicator variable required but not supplied" },
83 	{ "22003",	"Numeric value out of range" },
84 	{ "22004",	"Null value not allowed" },
85 	{ "22005",	"Error in assignment" },
86 	{ "22007",	"Invalid datetime format" },
87 	{ "22008",	"Datetime field overflow" },
88 	{ "22009",	"Invalid time zone displacement value" },
89 	{ "2200B",	"Escape character conflict" },
90 	{ "2200C",	"Invalid use of escape character" },
91 	{ "2200D",	"Invalid escape octet" },
92 	{ "2200F",	"Zero length character string" },
93 	{ "2200G",	"Most specific type mismatch" },
94 	{ "22010",	"Invalid indicator parameter value" },
95 	{ "22011",	"Substring error" },
96 	{ "22012",	"Division by zero" },
97 	{ "22015",	"Interval field overflow" },
98 	{ "22018",	"Invalid character value for cast specification" },
99 	{ "22019",	"Invalid escape character" },
100 	{ "2201B",	"Invalid regular expression" },
101 	{ "2201E",	"Invalid argument for logarithm" },
102 	{ "2201F",	"Invalid argument for power function" },
103 	{ "2201G",	"Invalid argument for width bucket function" },
104 	{ "22020",	"Invalid limit value" },
105 	{ "22021",	"Character not in repertoire" },
106 	{ "22022",	"Indicator overflow" },
107 	{ "22023",	"Invalid parameter value" },
108 	{ "22024",	"Unterminated c string" },
109 	{ "22025",	"Invalid escape sequence" },
110 	{ "22026",	"String data, length mismatch" },
111 	{ "22027",	"Trim error" },
112 	{ "2202E",	"Array subscript error" },
113 	{ "22P01",	"Floating point exception" },
114 	{ "22P02",	"Invalid text representation" },
115 	{ "22P03",	"Invalid binary representation" },
116 	{ "22P04",	"Bad copy file format" },
117 	{ "22P05",	"Untranslatable character" },
118 	{ "23000",	"Integrity constraint violation" },
119 	{ "23001",	"Restrict violation" },
120 	{ "23502",	"Not null violation" },
121 	{ "23503",	"Foreign key violation" },
122 	{ "23505",	"Unique violation" },
123 	{ "23514",	"Check violation" },
124 	{ "24000",	"Invalid cursor state" },
125 	{ "25000",	"Invalid transaction state" },
126 	{ "25001",	"Active sql transaction" },
127 	{ "25002",	"Branch transaction already active" },
128 	{ "25003",	"Inappropriate access mode for branch transaction" },
129 	{ "25004",	"Inappropriate isolation level for branch transaction" },
130 	{ "25005",	"No active sql transaction for branch transaction" },
131 	{ "25006",	"Read only sql transaction" },
132 	{ "25007",	"Schema and data statement mixing not supported" },
133 	{ "25008",	"Held cursor requires same isolation level" },
134 	{ "25P01",	"No active sql transaction" },
135 	{ "25P02",	"In failed sql transaction" },
136 	{ "25S01",	"Transaction state" },
137 	{ "25S02",	"Transaction is still active" },
138 	{ "25S03",	"Transaction is rolled back" },
139 	{ "26000",	"Invalid sql statement name" },
140 	{ "27000",	"Triggered data change violation" },
141 	{ "28000",	"Invalid authorization specification" },
142 	{ "2B000",	"Dependent privilege descriptors still exist" },
143 	{ "2BP01",	"Dependent objects still exist" },
144 	{ "2D000",	"Invalid transaction termination" },
145 	{ "2F000",	"Sql routine exception" },
146 	{ "2F002",	"Modifying sql data not permitted" },
147 	{ "2F003",	"Prohibited sql statement attempted" },
148 	{ "2F004",	"Reading sql data not permitted" },
149 	{ "2F005",	"Function executed no return statement" },
150 	{ "34000",	"Invalid cursor name" },
151 	{ "38000",	"External routine exception" },
152 	{ "38001",	"Containing sql not permitted" },
153 	{ "38002",	"Modifying sql data not permitted" },
154 	{ "38003",	"Prohibited sql statement attempted" },
155 	{ "38004",	"Reading sql data not permitted" },
156 	{ "39000",	"External routine invocation exception" },
157 	{ "39001",	"Invalid sqlstate returned" },
158 	{ "39004",	"Null value not allowed" },
159 	{ "39P01",	"Trigger protocol violated" },
160 	{ "39P02",	"Srf protocol violated" },
161 	{ "3B000",	"Savepoint exception" },
162 	{ "3B001",	"Invalid savepoint specification" },
163 	{ "3C000",	"Duplicate cursor name" },
164 	{ "3D000",	"Invalid catalog name" },
165 	{ "3F000",	"Invalid schema name" },
166 	{ "40000",	"Transaction rollback" },
167 	{ "40001",	"Serialization failure" },
168 	{ "40002",	"Transaction integrity constraint violation" },
169 	{ "40003",	"Statement completion unknown" },
170 	{ "40P01",	"Deadlock detected" },
171 	{ "42000",	"Syntax error or access violation" },
172 	{ "42501",	"Insufficient privilege" },
173 	{ "42601",	"Syntax error" },
174 	{ "42602",	"Invalid name" },
175 	{ "42611",	"Invalid column definition" },
176 	{ "42622",	"Name too long" },
177 	{ "42701",	"Duplicate column" },
178 	{ "42702",	"Ambiguous column" },
179 	{ "42703",	"Undefined column" },
180 	{ "42704",	"Undefined object" },
181 	{ "42710",	"Duplicate object" },
182 	{ "42712",	"Duplicate alias" },
183 	{ "42723",	"Duplicate function" },
184 	{ "42725",	"Ambiguous function" },
185 	{ "42803",	"Grouping error" },
186 	{ "42804",	"Datatype mismatch" },
187 	{ "42809",	"Wrong object type" },
188 	{ "42830",	"Invalid foreign key" },
189 	{ "42846",	"Cannot coerce" },
190 	{ "42883",	"Undefined function" },
191 	{ "42939",	"Reserved name" },
192 	{ "42P01",	"Undefined table" },
193 	{ "42P02",	"Undefined parameter" },
194 	{ "42P03",	"Duplicate cursor" },
195 	{ "42P04",	"Duplicate database" },
196 	{ "42P05",	"Duplicate prepared statement" },
197 	{ "42P06",	"Duplicate schema" },
198 	{ "42P07",	"Duplicate table" },
199 	{ "42P08",	"Ambiguous parameter" },
200 	{ "42P09",	"Ambiguous alias" },
201 	{ "42P10",	"Invalid column reference" },
202 	{ "42P11",	"Invalid cursor definition" },
203 	{ "42P12",	"Invalid database definition" },
204 	{ "42P13",	"Invalid function definition" },
205 	{ "42P14",	"Invalid prepared statement definition" },
206 	{ "42P15",	"Invalid schema definition" },
207 	{ "42P16",	"Invalid table definition" },
208 	{ "42P17",	"Invalid object definition" },
209 	{ "42P18",	"Indeterminate datatype" },
210 	{ "42S01",	"Base table or view already exists" },
211 	{ "42S02",	"Base table or view not found" },
212 	{ "42S11",	"Index already exists" },
213 	{ "42S12",	"Index not found" },
214 	{ "42S21",	"Column already exists" },
215 	{ "42S22",	"Column not found" },
216 	{ "44000",	"WITH CHECK OPTION violation" },
217 	{ "53000",	"Insufficient resources" },
218 	{ "53100",	"Disk full" },
219 	{ "53200",	"Out of memory" },
220 	{ "53300",	"Too many connections" },
221 	{ "54000",	"Program limit exceeded" },
222 	{ "54001",	"Statement too complex" },
223 	{ "54011",	"Too many columns" },
224 	{ "54023",	"Too many arguments" },
225 	{ "55000",	"Object not in prerequisite state" },
226 	{ "55006",	"Object in use" },
227 	{ "55P02",	"Cant change runtime param" },
228 	{ "55P03",	"Lock not available" },
229 	{ "57000",	"Operator intervention" },
230 	{ "57014",	"Query canceled" },
231 	{ "57P01",	"Admin shutdown" },
232 	{ "57P02",	"Crash shutdown" },
233 	{ "57P03",	"Cannot connect now" },
234 	{ "58030",	"Io error" },
235 	{ "58P01",	"Undefined file" },
236 	{ "58P02",	"Duplicate file" },
237 	{ "F0000",	"Config file error" },
238 	{ "F0001",	"Lock file exists" },
239 	{ "HY000",	"General error" },
240 	{ "HY001",	"Memory allocation error" },
241 	{ "HY003",	"Invalid application buffer type" },
242 	{ "HY004",	"Invalid SQL data type" },
243 	{ "HY007",	"Associated statement is not prepared" },
244 	{ "HY008",	"Operation canceled" },
245 	{ "HY009",	"Invalid use of null pointer" },
246 	{ "HY010",	"Function sequence error" },
247 	{ "HY011",	"Attribute cannot be set now" },
248 	{ "HY012",	"Invalid transaction operation code" },
249 	{ "HY013",	"Memory management error" },
250 	{ "HY014",	"Limit on the number of handles exceeded" },
251 	{ "HY015",	"No cursor name available" },
252 	{ "HY016",	"Cannot modify an implementation row descriptor" },
253 	{ "HY017",	"Invalid use of an automatically allocated descriptor handle" },
254 	{ "HY018",	"Server declined cancel request" },
255 	{ "HY019",	"Non-character and non-binary data sent in pieces" },
256 	{ "HY020",	"Attempt to concatenate a null value" },
257 	{ "HY021",	"Inconsistent descriptor information" },
258 	{ "HY024",	"Invalid attribute value" },
259 	{ "HY090",	"Invalid string or buffer length" },
260 	{ "HY091",	"Invalid descriptor field identifier" },
261 	{ "HY092",	"Invalid attribute/option identifier" },
262 	{ "HY093",	"Invalid parameter number" },
263 	{ "HY095",	"Function type out of range" },
264 	{ "HY096",	"Invalid information type" },
265 	{ "HY097",	"Column type out of range" },
266 	{ "HY098",	"Scope type out of range" },
267 	{ "HY099",	"Nullable type out of range" },
268 	{ "HY100",	"Uniqueness option type out of range" },
269 	{ "HY101",	"Accuracy option type out of range" },
270 	{ "HY103",	"Invalid retrieval code" },
271 	{ "HY104",	"Invalid precision or scale value" },
272 	{ "HY105",	"Invalid parameter type" },
273 	{ "HY106",	"Fetch type out of range" },
274 	{ "HY107",	"Row value out of range" },
275 	{ "HY109",	"Invalid cursor position" },
276 	{ "HY110",	"Invalid driver completion" },
277 	{ "HY111",	"Invalid bookmark value" },
278 	{ "HYC00",	"Optional feature not implemented" },
279 	{ "HYT00",	"Timeout expired" },
280 	{ "HYT01",	"Connection timeout expired" },
281 	{ "IM001",	"Driver does not support this function" },
282 	{ "IM002",	"Data source name not found and no default driver specified" },
283 	{ "IM003",	"Specified driver could not be loaded" },
284 	{ "IM004",	"Driver's SQLAllocHandle on SQL_HANDLE_ENV failed" },
285 	{ "IM005",	"Driver's SQLAllocHandle on SQL_HANDLE_DBC failed" },
286 	{ "IM006",	"Driver's SQLSetConnectAttr failed" },
287 	{ "IM007",	"No data source or driver specified; dialog prohibited" },
288 	{ "IM008",	"Dialog failed" },
289 	{ "IM009",	"Unable to load translation DLL" },
290 	{ "IM010",	"Data source name too long" },
291 	{ "IM011",	"Driver name too long" },
292 	{ "IM012",	"DRIVER keyword syntax error" },
293 	{ "IM013",	"Trace file error" },
294 	{ "IM014",	"Invalid name of File DSN" },
295 	{ "IM015",	"Corrupt file data source" },
296 	{ "P0000",	"Plpgsql error" },
297 	{ "P0001",	"Raise exception" },
298 	{ "XX000",	"Internal error" },
299 	{ "XX001",	"Data corrupted" },
300 	{ "XX002",	"Index corrupted" }
301 };
302 
pdo_sqlstate_fini_error_table(void)303 void pdo_sqlstate_fini_error_table(void)
304 {
305 	zend_hash_destroy(&err_hash);
306 }
307 
pdo_sqlstate_init_error_table(void)308 int pdo_sqlstate_init_error_table(void)
309 {
310 	size_t i;
311 	const struct pdo_sqlstate_info *info;
312 
313 	zend_hash_init(&err_hash, sizeof(err_initializer)/sizeof(err_initializer[0]), NULL, NULL, 1);
314 
315 	for (i = 0; i < sizeof(err_initializer)/sizeof(err_initializer[0]); i++) {
316 		info = &err_initializer[i];
317 
318 		zend_hash_str_add_ptr(&err_hash, info->state, sizeof(info->state), (void *)info);
319 	}
320 
321 	return SUCCESS;
322 }
323 
pdo_sqlstate_state_to_description(char * state)324 const char *pdo_sqlstate_state_to_description(char *state)
325 {
326 	const struct pdo_sqlstate_info *info;
327 	if ((info = zend_hash_str_find_ptr(&err_hash, state, sizeof(err_initializer[0].state))) != NULL) {
328 		return info->desc;
329 	}
330 	return NULL;
331 }
332