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