- Notifications
You must be signed in to change notification settings - Fork 7.8k
/
Copy pathpdo.c
314 lines (258 loc) · 7.8 KB
/
pdo.c
1
2
3
4
5
6
7
8
9
10
11
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Wez Furlong <wez@php.net> |
| Marcus Boerger <helly@php.net> |
| Sterling Hughes <sterling@php.net> |
+----------------------------------------------------------------------+
*/
#ifdefHAVE_CONFIG_H
#include<config.h>
#endif
#include<ctype.h>
#include"php.h"
#include"php_ini.h"
#include"ext/standard/info.h"
#include"php_pdo.h"
#include"php_pdo_driver.h"
#include"php_pdo_int.h"
#include"zend_exceptions.h"
#include"ext/spl/spl_exceptions.h"
#include"pdo_arginfo.h"
zend_class_entry*pdo_dbh_ce, *pdo_dbstmt_ce, *pdo_row_ce;
/* for exceptional circumstances */
zend_class_entry*pdo_exception_ce;
/* True global resources - no need for thread safety here */
/* the registry of PDO drivers */
HashTablepdo_driver_hash;
/* the registry of PDO driver specific class entries */
HashTablepdo_driver_specific_ce_hash;
/* we use persistent resources for the driver connection stuff */
staticintle_ppdo;
intphp_pdo_list_entry(void) /* {{{ */
{
returnle_ppdo;
}
/* }}} */
PDO_APIzend_class_entry*php_pdo_get_dbh_ce(void) /* {{{ */
{
returnpdo_dbh_ce;
}
/* }}} */
PDO_APIzend_class_entry*php_pdo_get_exception(void) /* {{{ */
{
returnpdo_exception_ce;
}
/* }}} */
/* {{{ Return array of available PDO drivers */
PHP_FUNCTION(pdo_drivers)
{
pdo_driver_t*pdriver;
ZEND_PARSE_PARAMETERS_NONE();
array_init(return_value);
ZEND_HASH_MAP_FOREACH_PTR(&pdo_driver_hash, pdriver) {
add_next_index_stringl(return_value, pdriver->driver_name, pdriver->driver_name_len);
} ZEND_HASH_FOREACH_END();
}
/* }}} */
/* {{{ pdo_functions[] */
staticconstzend_module_deppdo_deps[] = {
ZEND_MOD_REQUIRED("spl")
ZEND_MOD_END
};
/* }}} */
/* {{{ pdo_module_entry */
zend_module_entrypdo_module_entry= {
STANDARD_MODULE_HEADER_EX, NULL,
pdo_deps,
"PDO",
ext_functions,
PHP_MINIT(pdo),
PHP_MSHUTDOWN(pdo),
NULL,
NULL,
PHP_MINFO(pdo),
PHP_PDO_VERSION,
STANDARD_MODULE_PROPERTIES
};
/* }}} */
/* TODO: visit persistent handles: for each persistent statement handle,
* remove bound parameter associations */
#ifdefCOMPILE_DL_PDO
ZEND_GET_MODULE(pdo)
#endif
PDO_APIzend_resultphp_pdo_register_driver(constpdo_driver_t*driver) /* {{{ */
{
if (driver->api_version!=PDO_DRIVER_API) {
zend_error_noreturn(E_ERROR, "PDO: driver %s requires PDO API version "ZEND_ULONG_FMT"; this is PDO version %d",
driver->driver_name, driver->api_version, PDO_DRIVER_API);
returnFAILURE;
}
if (!zend_hash_str_exists(&module_registry, "pdo", sizeof("pdo") -1)) {
zend_error_noreturn(E_ERROR, "The PDO extension must be loaded first in order to load PDO drivers");
returnFAILURE; /* NOTREACHED */
}
returnzend_hash_str_add_ptr(&pdo_driver_hash, driver->driver_name, driver->driver_name_len, (void*)driver) !=NULL ? SUCCESS : FAILURE;
}
/* }}} */
PDO_APIvoidphp_pdo_unregister_driver(constpdo_driver_t*driver) /* {{{ */
{
if (!zend_hash_str_exists(&module_registry, "pdo", sizeof("pdo") -1)) {
return;
}
zend_hash_str_del(&pdo_driver_specific_ce_hash, driver->driver_name, driver->driver_name_len);
zend_hash_str_del(&pdo_driver_hash, driver->driver_name, driver->driver_name_len);
}
/* }}} */
PDO_APIzend_resultphp_pdo_register_driver_specific_ce(constpdo_driver_t*driver, zend_class_entry*ce) /* {{{ */
{
if (!zend_hash_str_exists(&module_registry, "pdo", sizeof("pdo") -1)) {
zend_error_noreturn(E_ERROR, "The PDO extension must be loaded first in order to load PDO drivers");
returnFAILURE; /* NOTREACHED */
}
returnzend_hash_str_add_ptr(&pdo_driver_specific_ce_hash, driver->driver_name,
driver->driver_name_len, (void*)ce) !=NULL ? SUCCESS : FAILURE;
}
pdo_driver_t*pdo_find_driver(constchar*name, intnamelen) /* {{{ */
{
returnzend_hash_str_find_ptr(&pdo_driver_hash, name, namelen);
}
/* }}} */
PDO_APIintphp_pdo_parse_data_source(constchar*data_source, zend_ulongdata_source_len, structpdo_data_src_parser*parsed, intnparams) /* {{{ */
{
zend_ulongi;
intj;
intvalstart=-1;
intsemi=-1;
intoptstart=0;
intnlen;
intn_matches=0;
intn_semicolumns=0;
i=0;
while (i<data_source_len) {
/* looking for NAME= */
if (data_source[i] =='\0') {
break;
}
if (data_source[i] !='=') {
++i;
continue;
}
valstart=++i;
/* now we're looking for VALUE; or just VALUE<NUL> */
semi=-1;
n_semicolumns=0;
while (i<data_source_len) {
if (data_source[i] =='\0') {
semi=i++;
break;
}
if (data_source[i] ==';') {
if ((i+1 >= data_source_len) ||data_source[i+1] !=';') {
semi=i++;
break;
} else {
n_semicolumns++;
i+=2;
continue;
}
}
++i;
}
if (semi==-1) {
semi=i;
}
/* find the entry in the array */
nlen=valstart-optstart-1;
for (j=0; j<nparams; j++) {
if (0==strncmp(data_source+optstart, parsed[j].optname, nlen) &&parsed[j].optname[nlen] =='\0') {
/* got a match */
if (parsed[j].freeme) {
efree(parsed[j].optval);
}
if (n_semicolumns==0) {
parsed[j].optval=estrndup(data_source+valstart, semi-valstart-n_semicolumns);
} else {
intvlen=semi-valstart;
constchar*orig_val=data_source+valstart;
char*new_val= (char*) emalloc(vlen-n_semicolumns+1);
parsed[j].optval=new_val;
while (vlen&&*orig_val) {
*new_val=*orig_val;
new_val++;
if (*orig_val==';') {
orig_val+=2;
vlen-=2;
} else {
orig_val++;
vlen--;
}
}
*new_val='\0';
}
parsed[j].freeme=1;
++n_matches;
break;
}
}
while (i<data_source_len&&isspace(data_source[i])) {
i++;
}
optstart=i;
}
returnn_matches;
}
/* }}} */
/* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(pdo)
{
pdo_sqlstate_init_error_table();
zend_hash_init(&pdo_driver_hash, 0, NULL, NULL, 1);
zend_hash_init(&pdo_driver_specific_ce_hash, 0, NULL, NULL, 1);
le_ppdo=zend_register_list_destructors_ex(NULL, php_pdo_pdbh_dtor,
"PDO persistent database", module_number);
pdo_exception_ce=register_class_PDOException(spl_ce_RuntimeException);
pdo_dbh_init(module_number);
pdo_stmt_init();
returnSUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION */
PHP_MSHUTDOWN_FUNCTION(pdo)
{
zend_hash_destroy(&pdo_driver_hash);
zend_hash_destroy(&pdo_driver_specific_ce_hash);
pdo_sqlstate_fini_error_table();
returnSUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(pdo)
{
char*drivers=NULL, *ldrivers=estrdup("");
pdo_driver_t*pdriver;
php_info_print_table_start();
php_info_print_table_row(2, "PDO support", "enabled");
ZEND_HASH_MAP_FOREACH_PTR(&pdo_driver_hash, pdriver) {
spprintf(&drivers, 0, "%s, %s", ldrivers, pdriver->driver_name);
efree(ldrivers);
ldrivers=drivers;
} ZEND_HASH_FOREACH_END();
php_info_print_table_row(2, "PDO drivers", drivers ? drivers+2 : "");
if (drivers) {
efree(drivers);
} else {
efree(ldrivers);
}
php_info_print_table_end();
}
/* }}} */