php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
pdo.c
Go to the documentation of this file.
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 | https://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 | Marcus Boerger <helly@php.net> |
15 | Sterling Hughes <sterling@php.net> |
16 +----------------------------------------------------------------------+
17*/
18
19#ifdef HAVE_CONFIG_H
20#include <config.h>
21#endif
22
23#include <ctype.h>
24#include "php.h"
25#include "php_ini.h"
26#include "ext/standard/info.h"
27#include "php_pdo.h"
28#include "php_pdo_driver.h"
29#include "php_pdo_int.h"
30#include "zend_exceptions.h"
32#include "pdo_arginfo.h"
33
35
36/* for exceptional circumstances */
38
39/* True global resources - no need for thread safety here */
40
41/* the registry of PDO drivers */
43
44/* the registry of PDO driver specific class entries */
46
47/* we use persistent resources for the driver connection stuff */
48static int le_ppdo;
49
50int php_pdo_list_entry(void) /* {{{ */
51{
52 return le_ppdo;
53}
54/* }}} */
55
57{
58 return pdo_dbh_ce;
59}
60/* }}} */
61
66/* }}} */
67
68/* {{{ Return array of available PDO drivers */
81/* }}} */
82
83/* {{{ pdo_functions[] */
84static const zend_module_dep pdo_deps[] = {
87};
88/* }}} */
89
90/* {{{ pdo_module_entry */
93 pdo_deps,
94 "PDO",
95 ext_functions,
96 PHP_MINIT(pdo),
97 PHP_MSHUTDOWN(pdo),
98 NULL,
99 NULL,
100 PHP_MINFO(pdo),
103};
104/* }}} */
105
106/* TODO: visit persistent handles: for each persistent statement handle,
107 * remove bound parameter associations */
108
109#ifdef COMPILE_DL_PDO
111#endif
112
114{
115 if (driver->api_version != PDO_DRIVER_API) {
116 zend_error_noreturn(E_ERROR, "PDO: driver %s requires PDO API version " ZEND_ULONG_FMT "; this is PDO version %d",
117 driver->driver_name, driver->api_version, PDO_DRIVER_API);
118 return FAILURE;
119 }
120 if (!zend_hash_str_exists(&module_registry, "pdo", sizeof("pdo") - 1)) {
121 zend_error_noreturn(E_ERROR, "The PDO extension must be loaded first in order to load PDO drivers");
122 return FAILURE; /* NOTREACHED */
123 }
124
125 return zend_hash_str_add_ptr(&pdo_driver_hash, driver->driver_name, driver->driver_name_len, (void*)driver) != NULL ? SUCCESS : FAILURE;
126}
127/* }}} */
128
129PDO_API void php_pdo_unregister_driver(const pdo_driver_t *driver) /* {{{ */
130{
131 if (!zend_hash_str_exists(&module_registry, "pdo", sizeof("pdo") - 1)) {
132 return;
133 }
134
137}
138/* }}} */
139
141{
142 if (!zend_hash_str_exists(&module_registry, "pdo", sizeof("pdo") - 1)) {
143 zend_error_noreturn(E_ERROR, "The PDO extension must be loaded first in order to load PDO drivers");
144 return FAILURE; /* NOTREACHED */
145 }
146
147 return zend_hash_str_add_ptr(&pdo_driver_specific_ce_hash, driver->driver_name,
148 driver->driver_name_len, (void*)ce) != NULL ? SUCCESS : FAILURE;
149}
150
151pdo_driver_t *pdo_find_driver(const char *name, int namelen) /* {{{ */
152{
153 return zend_hash_str_find_ptr(&pdo_driver_hash, name, namelen);
154}
155/* }}} */
156
157PDO_API int php_pdo_parse_data_source(const char *data_source, zend_ulong data_source_len, struct pdo_data_src_parser *parsed, int nparams) /* {{{ */
158{
159 zend_ulong i;
160 int j;
161 int valstart = -1;
162 int semi = -1;
163 int optstart = 0;
164 int nlen;
165 int n_matches = 0;
166 int n_semicolumns = 0;
167
168 i = 0;
169 while (i < data_source_len) {
170 /* looking for NAME= */
171
172 if (data_source[i] == '\0') {
173 break;
174 }
175
176 if (data_source[i] != '=') {
177 ++i;
178 continue;
179 }
180
181 valstart = ++i;
182
183 /* now we're looking for VALUE; or just VALUE<NUL> */
184 semi = -1;
185 n_semicolumns = 0;
186 while (i < data_source_len) {
187 if (data_source[i] == '\0') {
188 semi = i++;
189 break;
190 }
191 if (data_source[i] == ';') {
192 if ((i + 1 >= data_source_len) || data_source[i+1] != ';') {
193 semi = i++;
194 break;
195 } else {
196 n_semicolumns++;
197 i += 2;
198 continue;
199 }
200 }
201 ++i;
202 }
203
204 if (semi == -1) {
205 semi = i;
206 }
207
208 /* find the entry in the array */
209 nlen = valstart - optstart - 1;
210 for (j = 0; j < nparams; j++) {
211 if (0 == strncmp(data_source + optstart, parsed[j].optname, nlen) && parsed[j].optname[nlen] == '\0') {
212 /* got a match */
213 if (parsed[j].freeme) {
214 efree(parsed[j].optval);
215 }
216
217 if (n_semicolumns == 0) {
218 parsed[j].optval = estrndup(data_source + valstart, semi - valstart - n_semicolumns);
219 } else {
220 int vlen = semi - valstart;
221 const char *orig_val = data_source + valstart;
222 char *new_val = (char *) emalloc(vlen - n_semicolumns + 1);
223
224 parsed[j].optval = new_val;
225
226 while (vlen && *orig_val) {
227 *new_val = *orig_val;
228 new_val++;
229
230 if (*orig_val == ';') {
231 orig_val+=2;
232 vlen-=2;
233 } else {
234 orig_val++;
235 vlen--;
236 }
237 }
238 *new_val = '\0';
239 }
240
241 parsed[j].freeme = 1;
242 ++n_matches;
243 break;
244 }
245 }
246
247 while (i < data_source_len && isspace(data_source[i])) {
248 i++;
249 }
250
251 optstart = i;
252 }
253
254 return n_matches;
255}
256/* }}} */
257
258/* {{{ PHP_MINIT_FUNCTION */
260{
262
265
266 le_ppdo = zend_register_list_destructors_ex(NULL, php_pdo_pdbh_dtor,
267 "PDO persistent database", module_number);
268
269 pdo_exception_ce = register_class_PDOException(spl_ce_RuntimeException);
270
271 pdo_dbh_init(module_number);
273
274 return SUCCESS;
275}
276/* }}} */
277
278/* {{{ PHP_MSHUTDOWN_FUNCTION */
286/* }}} */
287
288/* {{{ PHP_MINFO_FUNCTION */
290{
291 char *drivers = NULL, *ldrivers = estrdup("");
292 pdo_driver_t *pdriver;
293
295 php_info_print_table_row(2, "PDO support", "enabled");
296
298 spprintf(&drivers, 0, "%s, %s", ldrivers, pdriver->driver_name);
299 efree(ldrivers);
300 ldrivers = drivers;
302
303 php_info_print_table_row(2, "PDO drivers", drivers ? drivers + 2 : "");
304
305 if (drivers) {
306 efree(drivers);
307 } else {
308 efree(ldrivers);
309 }
310
312
313}
314/* }}} */
#define NULL
Definition gdcache.h:45
#define SUCCESS
Definition hash_sha3.c:261
again j
pdo_driver_t * pdo_find_driver(const char *name, int namelen)
Definition pdo.c:151
PDO_API int php_pdo_parse_data_source(const char *data_source, zend_ulong data_source_len, struct pdo_data_src_parser *parsed, int nparams)
Definition pdo.c:157
PDO_API void php_pdo_unregister_driver(const pdo_driver_t *driver)
Definition pdo.c:129
zend_class_entry * pdo_row_ce
Definition pdo.c:34
zend_class_entry * pdo_dbstmt_ce
Definition pdo.c:34
PDO_API zend_result php_pdo_register_driver(const pdo_driver_t *driver)
Definition pdo.c:113
PDO_API zend_result php_pdo_register_driver_specific_ce(const pdo_driver_t *driver, zend_class_entry *ce)
Definition pdo.c:140
HashTable pdo_driver_specific_ce_hash
Definition pdo.c:45
HashTable pdo_driver_hash
Definition pdo.c:42
PDO_API zend_class_entry * php_pdo_get_dbh_ce(void)
Definition pdo.c:56
zend_class_entry * pdo_exception_ce
Definition pdo.c:37
zend_class_entry * pdo_dbh_ce
Definition pdo.c:34
zend_module_entry pdo_module_entry
Definition pdo.c:91
PDO_API zend_class_entry * php_pdo_get_exception(void)
Definition pdo.c:62
int php_pdo_list_entry(void)
Definition pdo.c:50
pdo_drivers()
Definition pdo.stub.php:16
void pdo_dbh_init(int module_number)
Definition pdo_dbh.c:1447
php_info_print_table_start()
Definition info.c:1064
php_info_print_table_row(2, "PDO Driver for Firebird", "enabled")
php_info_print_table_end()
Definition info.c:1074
void pdo_sqlstate_init_error_table(void)
void pdo_sqlstate_fini_error_table(void)
void pdo_stmt_init(void)
Definition pdo_stmt.c:2517
#define PHP_FUNCTION
Definition php.h:364
#define PHP_MSHUTDOWN_FUNCTION
Definition php.h:401
#define PHP_MINFO
Definition php.h:396
#define PHP_MINIT_FUNCTION
Definition php.h:400
#define PHP_MSHUTDOWN
Definition php.h:393
#define PHP_MINFO_FUNCTION
Definition php.h:404
#define PHP_MINIT
Definition php.h:392
#define PDO_API
Definition php_pdo.h:42
#define PHP_PDO_VERSION
Definition php_pdo.h:29
#define PDO_DRIVER_API
ptrdiff_t namelen
Definition session.c:1097
PHPAPI zend_class_entry * spl_ce_RuntimeException
#define spprintf
Definition spprintf.h:29
zend_ulong api_version
size_t driver_name_len
const char * driver_name
ZEND_API ZEND_COLD ZEND_NORETURN void zend_error_noreturn(int type, const char *format,...)
Definition zend.c:1703
ZEND_API zend_result add_next_index_stringl(zval *arg, const char *str, size_t length)
Definition zend_API.c:2195
ZEND_API HashTable module_registry
Definition zend_API.c:41
#define ZEND_PARSE_PARAMETERS_NONE()
Definition zend_API.h:1623
#define ZEND_GET_MODULE(name)
Definition zend_API.h:241
#define array_init(arg)
Definition zend_API.h:537
#define estrndup(s, length)
Definition zend_alloc.h:165
#define efree(ptr)
Definition zend_alloc.h:155
#define estrdup(s)
Definition zend_alloc.h:164
#define emalloc(size)
Definition zend_alloc.h:151
strncmp(string $string1, string $string2, int $length)
#define E_ERROR
Definition zend_errors.h:23
ZEND_API void ZEND_FASTCALL zend_hash_destroy(HashTable *ht)
Definition zend_hash.c:1727
ZEND_API zend_result ZEND_FASTCALL zend_hash_str_del(HashTable *ht, const char *str, size_t len)
Definition zend_hash.c:1661
#define zend_hash_init(ht, nSize, pHashFunction, pDestructor, persistent)
Definition zend_hash.h:108
#define ZEND_HASH_MAP_FOREACH_PTR(ht, _ptr)
Definition zend_hash.h:1326
#define ZEND_HASH_FOREACH_END()
Definition zend_hash.h:1086
ZEND_API int zend_register_list_destructors_ex(rsrc_dtor_func_t ld, rsrc_dtor_func_t pld, const char *type_name, int module_number)
Definition zend_list.c:265
#define ZEND_ULONG_FMT
Definition zend_long.h:88
uint32_t zend_ulong
Definition zend_long.h:43
#define ZEND_MOD_END
struct _zend_module_dep zend_module_dep
struct _zend_module_entry zend_module_entry
#define STANDARD_MODULE_PROPERTIES
#define ZEND_MOD_REQUIRED(name)
#define STANDARD_MODULE_HEADER_EX
struct _zend_class_entry zend_class_entry
struct _zend_array HashTable
Definition zend_types.h:386
@ FAILURE
Definition zend_types.h:61
ZEND_RESULT_CODE zend_result
Definition zend_types.h:64
zval * return_value
zend_string * name