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 #ifndef PHP_PDO_DRIVER_H
18 #define PHP_PDO_DRIVER_H
19 
20 #include "php_pdo.h"
21 
22 /* forward declarations */
23 typedef struct _pdo_dbh_t 		 pdo_dbh_t;
24 typedef struct _pdo_dbh_object_t pdo_dbh_object_t;
25 typedef struct _pdo_stmt_t		 pdo_stmt_t;
26 typedef struct _pdo_row_t		 pdo_row_t;
27 struct pdo_bound_param_data;
28 
29 #ifdef PHP_WIN32
30 typedef __int64 pdo_int64_t;
31 typedef unsigned __int64 pdo_uint64_t;
32 #else
33 typedef long long int pdo_int64_t;
34 typedef unsigned long long int pdo_uint64_t;
35 #endif
36 PDO_API char *php_pdo_int64_to_str(pdo_int64_t i64);
37 
38 #ifndef TRUE
39 # define TRUE 1
40 #endif
41 #ifndef FALSE
42 # define FALSE 0
43 #endif
44 
45 #define PDO_DRIVER_API	20170320
46 
47 enum pdo_param_type {
48 	PDO_PARAM_NULL,
49 
50 	/* int as in long (the php native int type).
51 	 * If you mark a column as an int, PDO expects get_col to return
52 	 * a pointer to a long */
53 	PDO_PARAM_INT,
54 
55 	/* get_col ptr should point to start of the string buffer */
56 	PDO_PARAM_STR,
57 
58 	/* get_col: when len is 0 ptr should point to a php_stream *,
59 	 * otherwise it should behave like a string. Indicate a NULL field
60 	 * value by setting the ptr to NULL */
61 	PDO_PARAM_LOB,
62 
63 	/* get_col: will expect the ptr to point to a new PDOStatement object handle,
64 	 * but this isn't wired up yet */
65 	PDO_PARAM_STMT, /* hierarchical result set */
66 
67 	/* get_col ptr should point to a zend_bool */
68 	PDO_PARAM_BOOL,
69 
70 	/* get_col ptr should point to a zval*
71 	   and the driver is responsible for adding correct type information to get_column_meta()
72 	 */
73 	PDO_PARAM_ZVAL,
74 
75 	/* magic flag to denote a parameter as being input/output */
76 	PDO_PARAM_INPUT_OUTPUT = 0x80000000,
77 
78 	/* magic flag to denote a string that uses the national character set
79 	   see section 4.2.1 of SQL-92: http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt
80 	 */
81 	PDO_PARAM_STR_NATL = 0x40000000,
82 
83 	/* magic flag to denote a string that uses the regular character set */
84 	PDO_PARAM_STR_CHAR = 0x20000000,
85 };
86 
87 #define PDO_PARAM_FLAGS			0xFFFF0000
88 
89 #define PDO_PARAM_TYPE(x)		((x) & ~PDO_PARAM_FLAGS)
90 
91 enum pdo_fetch_type {
92 	PDO_FETCH_USE_DEFAULT,
93 	PDO_FETCH_LAZY,
94 	PDO_FETCH_ASSOC,
95 	PDO_FETCH_NUM,
96 	PDO_FETCH_BOTH,
97 	PDO_FETCH_OBJ,
98 	PDO_FETCH_BOUND, /* return true/false only; rely on bound columns */
99 	PDO_FETCH_COLUMN,	/* fetch a numbered column only */
100 	PDO_FETCH_CLASS,	/* create an instance of named class, call ctor and set properties */
101 	PDO_FETCH_INTO,		/* fetch row into an existing object */
102 	PDO_FETCH_FUNC,		/* fetch into function and return its result */
103 	PDO_FETCH_NAMED,    /* like PDO_FETCH_ASSOC, but can handle duplicate names */
104 	PDO_FETCH_KEY_PAIR,	/* fetch into an array where the 1st column is a key and all subsequent columns are values */
105 	PDO_FETCH__MAX /* must be last */
106 };
107 
108 #define PDO_FETCH_FLAGS     0xFFFF0000  /* fetchAll() modes or'd to PDO_FETCH_XYZ */
109 #define PDO_FETCH_GROUP     0x00010000  /* fetch into groups */
110 #define PDO_FETCH_UNIQUE    0x00030000  /* fetch into groups assuming first col is unique */
111 #define PDO_FETCH_CLASSTYPE 0x00040000  /* fetch class gets its class name from 1st column */
112 #define PDO_FETCH_SERIALIZE 0x00080000  /* fetch class instances by calling serialize */
113 #define PDO_FETCH_PROPS_LATE 0x00100000  /* fetch props after calling ctor */
114 
115 /* fetch orientation for scrollable cursors */
116 enum pdo_fetch_orientation {
117 	PDO_FETCH_ORI_NEXT,		/* default: fetch the next available row */
118 	PDO_FETCH_ORI_PRIOR,	/* scroll back to prior row and fetch that */
119 	PDO_FETCH_ORI_FIRST,	/* scroll to the first row and fetch that */
120 	PDO_FETCH_ORI_LAST,		/* scroll to the last row and fetch that */
121 	PDO_FETCH_ORI_ABS,		/* scroll to an absolute numbered row and fetch that */
122 	PDO_FETCH_ORI_REL		/* scroll relative to the current row, and fetch that */
123 };
124 
125 enum pdo_attribute_type {
126 	PDO_ATTR_AUTOCOMMIT,	/* use to turn on or off auto-commit mode */
127 	PDO_ATTR_PREFETCH,		/* configure the prefetch size for drivers that support it. Size is in KB */
128 	PDO_ATTR_TIMEOUT,		/* connection timeout in seconds */
129 	PDO_ATTR_ERRMODE,		/* control how errors are handled */
130 	PDO_ATTR_SERVER_VERSION,	/* database server version */
131 	PDO_ATTR_CLIENT_VERSION,	/* client library version */
132 	PDO_ATTR_SERVER_INFO,		/* server information */
133 	PDO_ATTR_CONNECTION_STATUS,	/* connection status */
134 	PDO_ATTR_CASE,				/* control case folding for portability */
135 	PDO_ATTR_CURSOR_NAME,		/* name a cursor for use in "WHERE CURRENT OF <name>" */
136 	PDO_ATTR_CURSOR,			/* cursor type */
137 	PDO_ATTR_ORACLE_NULLS,		/* convert empty strings to NULL */
138 	PDO_ATTR_PERSISTENT,		/* pconnect style connection */
139 	PDO_ATTR_STATEMENT_CLASS,	/* array(classname, array(ctor_args)) to specify the class of the constructed statement */
140 	PDO_ATTR_FETCH_TABLE_NAMES, /* include table names in the column names, where available */
141 	PDO_ATTR_FETCH_CATALOG_NAMES, /* include the catalog/db name names in the column names, where available */
142 	PDO_ATTR_DRIVER_NAME,		  /* name of the driver (as used in the constructor) */
143 	PDO_ATTR_STRINGIFY_FETCHES,	/* converts integer/float types to strings during fetch */
144 	PDO_ATTR_MAX_COLUMN_LEN,	/* make database calculate maximum length of data found in a column */
145 	PDO_ATTR_DEFAULT_FETCH_MODE, /* Set the default fetch mode */
146 	PDO_ATTR_EMULATE_PREPARES,  /* use query emulation rather than native */
147 	PDO_ATTR_DEFAULT_STR_PARAM, /* set the default string parameter type (see the PDO::PARAM_STR_* magic flags) */
148 
149 	/* this defines the start of the range for driver specific options.
150 	 * Drivers should define their own attribute constants beginning with this
151 	 * value. */
152 	PDO_ATTR_DRIVER_SPECIFIC = 1000
153 };
154 
155 enum pdo_cursor_type {
156 	PDO_CURSOR_FWDONLY,		/* forward only cursor (default) */
157 	PDO_CURSOR_SCROLL		/* scrollable cursor */
158 };
159 
160 /* SQL-92 SQLSTATE error codes.
161 
162 The character string value returned for an SQLSTATE consists of a two-character
163 class value followed by a three-character subclass value. A class value of 01
164 indicates a warning and is accompanied by a return code of
165 SQL_SUCCESS_WITH_INFO.
166 
167 Class values other than '01', except for the class 'IM',
168 indicate an error and are accompanied by a return code of SQL_ERROR. The class
169 'IM' is specific to warnings and errors that derive from the implementation of
170 ODBC itself.
171 
172 The subclass value '000' in any class indicates that there is no
173 subclass for that SQLSTATE. The assignment of class and subclass values is
174 defined by SQL-92.
175 */
176 
177 typedef char pdo_error_type[6]; /* SQLSTATE */
178 
179 
180 #define PDO_ERR_NONE				"00000"
181 
182 enum pdo_error_mode {
183 	PDO_ERRMODE_SILENT,		/* just set error codes */
184 	PDO_ERRMODE_WARNING,	/* raise E_WARNING */
185 	PDO_ERRMODE_EXCEPTION	/* throw exceptions */
186 };
187 
188 enum pdo_case_conversion {
189 	PDO_CASE_NATURAL,
190 	PDO_CASE_UPPER,
191 	PDO_CASE_LOWER
192 };
193 
194 /* oracle interop settings */
195 enum pdo_null_handling {
196 	PDO_NULL_NATURAL = 0,
197 	PDO_NULL_EMPTY_STRING = 1,
198 	PDO_NULL_TO_STRING = 2
199 };
200 
201 /* {{{ utils for reading attributes set as driver_options */
pdo_attr_lval(zval * options,unsigned option_name,zend_long defval)202 static inline zend_long pdo_attr_lval(zval *options, unsigned option_name, zend_long defval)
203 {
204 	zval *v;
205 
206 	if (options && (v = zend_hash_index_find(Z_ARRVAL_P(options), option_name))) {
207 		return zval_get_long(v);
208 	}
209 	return defval;
210 }
pdo_attr_strval(zval * options,unsigned option_name,zend_string * defval)211 static inline zend_string *pdo_attr_strval(zval *options, unsigned option_name, zend_string *defval)
212 {
213 	zval *v;
214 
215 	if (options && (v = zend_hash_index_find(Z_ARRVAL_P(options), option_name))) {
216 		return zval_try_get_string(v);
217 	}
218 	return defval ? zend_string_copy(defval) : NULL;
219 }
220 /* }}} */
221 
222 /* This structure is registered with PDO when a PDO driver extension is
223  * initialized */
224 typedef struct {
225 	const char	*driver_name;
226 	size_t		driver_name_len;
227 	zend_ulong	api_version; /* needs to be compatible with PDO */
228 
229 #define PDO_DRIVER_HEADER(name)	\
230 	#name, sizeof(#name)-1, \
231 	PDO_DRIVER_API
232 
233 	/* create driver specific portion of the database handle and stash it into
234 	 * the dbh.  dbh contains the data source string and flags for this
235 	 * instance.  You MUST respect dbh->is_persistent and pass that flag to
236 	 * pemalloc() for all allocations that are stored in the dbh or your instance
237 	 * data in the db, otherwise you will crash PHP when persistent connections
238 	 * are used.
239 	 */
240 	int (*db_handle_factory)(pdo_dbh_t *dbh, zval *driver_options);
241 
242 } pdo_driver_t;
243 
244 /* {{{ methods for a database handle */
245 
246 /* close or otherwise disconnect the database */
247 typedef int (*pdo_dbh_close_func)(pdo_dbh_t *dbh);
248 
249 /* prepare a statement and stash driver specific portion into stmt */
250 typedef int (*pdo_dbh_prepare_func)(pdo_dbh_t *dbh, const char *sql, size_t sql_len, pdo_stmt_t *stmt, zval *driver_options);
251 
252 /* execute a statement (that does not return a result set) */
253 typedef zend_long (*pdo_dbh_do_func)(pdo_dbh_t *dbh, const char *sql, size_t sql_len);
254 
255 /* quote a string */
256 typedef int (*pdo_dbh_quote_func)(pdo_dbh_t *dbh, const char *unquoted, size_t unquotedlen, char **quoted, size_t *quotedlen, enum pdo_param_type paramtype);
257 
258 /* transaction related */
259 typedef int (*pdo_dbh_txn_func)(pdo_dbh_t *dbh);
260 
261 /* setting of attributes */
262 typedef int (*pdo_dbh_set_attr_func)(pdo_dbh_t *dbh, zend_long attr, zval *val);
263 
264 /* return last insert id.  NULL indicates error condition, otherwise, the return value
265  * MUST be an emalloc'd NULL terminated string. */
266 typedef char *(*pdo_dbh_last_id_func)(pdo_dbh_t *dbh, const char *name, size_t *len);
267 
268 /* fetch error information.  if stmt is not null, fetch information pertaining
269  * to the statement, otherwise fetch global error information.  The driver
270  * should add the following information to the array "info" in this order:
271  * - native error code
272  * - string representation of the error code ... any other optional driver
273  *   specific data ...  */
274 typedef	int (*pdo_dbh_fetch_error_func)(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info);
275 
276 /* fetching of attributes */
277 typedef int (*pdo_dbh_get_attr_func)(pdo_dbh_t *dbh, zend_long attr, zval *val);
278 
279 /* checking/pinging persistent connections; return SUCCESS if the connection
280  * is still alive and ready to be used, FAILURE otherwise.
281  * You may set this handler to NULL, which is equivalent to returning SUCCESS. */
282 typedef int (*pdo_dbh_check_liveness_func)(pdo_dbh_t *dbh);
283 
284 /* called at request end for each persistent dbh; this gives the driver
285  * the opportunity to safely release resources that only have per-request
286  * scope */
287 typedef void (*pdo_dbh_request_shutdown)(pdo_dbh_t *dbh);
288 
289 /* for adding methods to the dbh or stmt objects
290 pointer to a list of driver specific functions. The convention is
291 to prefix the function names using the PDO driver name; this will
292 reduce the chance of collisions with future functionality in the
293 PDO class or in user code (they can extend the PDO object).
294 */
295 enum {
296 	PDO_DBH_DRIVER_METHOD_KIND_DBH = 0,
297 	PDO_DBH_DRIVER_METHOD_KIND_STMT,
298 	PDO_DBH_DRIVER_METHOD_KIND__MAX
299 };
300 
301 typedef const zend_function_entry *(*pdo_dbh_get_driver_methods_func)(pdo_dbh_t *dbh, int kind);
302 
303 struct pdo_dbh_methods {
304 	pdo_dbh_close_func		closer;
305 	pdo_dbh_prepare_func	preparer;
306 	pdo_dbh_do_func			doer;
307 	pdo_dbh_quote_func		quoter;
308 	pdo_dbh_txn_func		begin;
309 	pdo_dbh_txn_func		commit;
310 	pdo_dbh_txn_func		rollback;
311 	pdo_dbh_set_attr_func	set_attribute;
312 	pdo_dbh_last_id_func		last_id;
313 	pdo_dbh_fetch_error_func	fetch_err;
314 	pdo_dbh_get_attr_func   	get_attribute;
315 	pdo_dbh_check_liveness_func	check_liveness;
316 	pdo_dbh_get_driver_methods_func get_driver_methods;
317 	pdo_dbh_request_shutdown	persistent_shutdown;
318 	pdo_dbh_txn_func		in_transaction;
319 };
320 
321 /* }}} */
322 
323 /* {{{ methods for a statement handle */
324 
325 /* free the statement handle */
326 typedef int (*pdo_stmt_dtor_func)(pdo_stmt_t *stmt);
327 
328 /* start the query */
329 typedef int (*pdo_stmt_execute_func)(pdo_stmt_t *stmt);
330 
331 /* causes the next row in the set to be fetched; indicates if there are no
332  * more rows.  The ori and offset params modify which row should be returned,
333  * if the stmt represents a scrollable cursor */
334 typedef int (*pdo_stmt_fetch_func)(pdo_stmt_t *stmt,
335 	enum pdo_fetch_orientation ori, zend_long offset);
336 
337 /* queries information about the type of a column, by index (0 based).
338  * Driver should populate stmt->columns[colno] with appropriate info */
339 typedef int (*pdo_stmt_describe_col_func)(pdo_stmt_t *stmt, int colno);
340 
341 /* retrieves pointer and size of the value for a column.
342  * Note that PDO expects the driver to manage the lifetime of this data;
343  * it will copy the value into a zval on behalf of the script.
344  * If the driver sets caller_frees, ptr should point to emalloc'd memory
345  * and PDO will free it as soon as it is done using it.
346  */
347 typedef int (*pdo_stmt_get_col_data_func)(pdo_stmt_t *stmt, int colno, char **ptr, size_t *len, int *caller_frees);
348 
349 /* hook for bound params */
350 enum pdo_param_event {
351 	PDO_PARAM_EVT_ALLOC,
352 	PDO_PARAM_EVT_FREE,
353 	PDO_PARAM_EVT_EXEC_PRE,
354 	PDO_PARAM_EVT_EXEC_POST,
355 	PDO_PARAM_EVT_FETCH_PRE,
356 	PDO_PARAM_EVT_FETCH_POST,
357 	PDO_PARAM_EVT_NORMALIZE
358 };
359 
360 typedef int (*pdo_stmt_param_hook_func)(pdo_stmt_t *stmt, struct pdo_bound_param_data *param, enum pdo_param_event event_type);
361 
362 /* setting of attributes */
363 typedef int (*pdo_stmt_set_attr_func)(pdo_stmt_t *stmt, zend_long attr, zval *val);
364 
365 /* fetching of attributes */
366 typedef int (*pdo_stmt_get_attr_func)(pdo_stmt_t *stmt, zend_long attr, zval *val);
367 
368 /* retrieves meta data for a numbered column.
369  * Returns SUCCESS/FAILURE.
370  * On SUCCESS, fill in return_value with an array with the following fields.
371  * If a particular field is not supported, then the driver simply does not add it to
372  * the array, so that scripts can use isset() to check for it.
373  *
374  * ### this is just a rough first cut, and subject to change ###
375  *
376  * these are added by PDO itself, based on data from the describe handler:
377  *   name => the column name
378  *   len => the length/size of the column
379  *   precision => precision of the column
380  *   pdo_type => an integer, one of the PDO_PARAM_XXX values
381  *
382  *   scale => the floating point scale
383  *   table => the table for that column
384  *   type => a string representation of the type, mapped to the PHP equivalent type name
385  *   native_type => a string representation of the type, native style, if different from
386  *                  the mapped name.
387  *   flags => an array of flags including zero or more of the following:
388  *            primary_key, not_null, unique_key, multiple_key, unsigned, auto_increment, blob
389  *
390  * Any driver specific data should be returned using a prefixed key or value.
391  * Eg: custom data for the mysql driver would use either
392  *   'mysql:foobar' => 'some data' // to add a new key to the array
393  * or
394  *   'flags' => array('not_null', 'mysql:some_flag'); // to add data to an existing key
395  */
396 typedef int (*pdo_stmt_get_column_meta_func)(pdo_stmt_t *stmt, zend_long colno, zval *return_value);
397 
398 /* advances the statement to the next rowset of the batch.
399  * If it returns 1, PDO will tear down its idea of columns
400  * and meta data.  If it returns 0, PDO will indicate an error
401  * to the caller. */
402 typedef int (*pdo_stmt_next_rowset_func)(pdo_stmt_t *stmt);
403 
404 /* closes the active cursor on a statement, leaving the prepared
405  * statement ready for re-execution.  Useful to explicitly state
406  * that you are done with a given rowset, without having to explicitly
407  * fetch all the rows. */
408 typedef int (*pdo_stmt_cursor_closer_func)(pdo_stmt_t *stmt);
409 
410 struct pdo_stmt_methods {
411 	pdo_stmt_dtor_func			dtor;
412 	pdo_stmt_execute_func		executer;
413 	pdo_stmt_fetch_func			fetcher;
414 	pdo_stmt_describe_col_func	describer;
415 	pdo_stmt_get_col_data_func	get_col;
416 	pdo_stmt_param_hook_func	param_hook;
417 	pdo_stmt_set_attr_func		set_attribute;
418 	pdo_stmt_get_attr_func		get_attribute;
419 	pdo_stmt_get_column_meta_func get_column_meta;
420 	pdo_stmt_next_rowset_func		next_rowset;
421 	pdo_stmt_cursor_closer_func 	cursor_closer;
422 };
423 
424 /* }}} */
425 
426 enum pdo_placeholder_support {
427 	PDO_PLACEHOLDER_NONE=0,
428 	PDO_PLACEHOLDER_NAMED=1,
429 	PDO_PLACEHOLDER_POSITIONAL=2
430 };
431 
432 struct _pdo_dbh_t {
433 	/* driver specific methods */
434 	const struct pdo_dbh_methods *methods;
435 	/* driver specific data */
436 	void *driver_data;
437 
438 	/* credentials */
439 	char *username, *password;
440 
441 	/* if true, then data stored and pointed at by this handle must all be
442 	 * persistently allocated */
443 	unsigned is_persistent:1;
444 
445 	/* if true, driver should act as though a COMMIT were executed between
446 	 * each executed statement; otherwise, COMMIT must be carried out manually
447 	 * */
448 	unsigned auto_commit:1;
449 
450 	/* if true, the handle has been closed and will not function anymore */
451 	unsigned is_closed:1;
452 
453 	/* if true, the driver requires that memory be allocated explicitly for
454 	 * the columns that are returned */
455 	unsigned alloc_own_columns:1;
456 
457 	/* if true, commit or rollBack is allowed to be called */
458 	unsigned in_txn:1;
459 
460 	/* max length a single character can become after correct quoting */
461 	unsigned max_escaped_char_length:3;
462 
463 	/* oracle compat; see enum pdo_null_handling */
464 	unsigned oracle_nulls:2;
465 
466 	/* when set, convert int/floats to strings */
467 	unsigned stringify:1;
468 
469 	/* bitmap for pdo_param_event(s) to skip in dispatch_param_event */
470 	unsigned skip_param_evt:7;
471 
472 	/* the sum of the number of bits here and the bit fields preceding should
473 	 * equal 32 */
474 	unsigned _reserved_flags:14;
475 
476 	/* data source string used to open this handle */
477 	const char *data_source;
478 	size_t data_source_len;
479 
480 	/* the global error code. */
481 	pdo_error_type error_code;
482 
483 	enum pdo_error_mode error_mode;
484 
485 	enum pdo_case_conversion native_case, desired_case;
486 
487 	/* persistent hash key associated with this handle */
488 	const char *persistent_id;
489 	size_t persistent_id_len;
490 	unsigned int refcount;
491 
492 	/* driver specific "class" methods for the dbh and stmt */
493 	HashTable *cls_methods[PDO_DBH_DRIVER_METHOD_KIND__MAX];
494 
495 	pdo_driver_t *driver;
496 
497 	zend_class_entry *def_stmt_ce;
498 
499 	zval def_stmt_ctor_args;
500 
501 	/* when calling PDO::query(), we need to keep the error
502 	 * context from the statement around until we next clear it.
503 	 * This will allow us to report the correct error message
504 	 * when PDO::query() fails */
505 	pdo_stmt_t *query_stmt;
506 	zval query_stmt_zval;
507 
508 	/* defaults for fetches */
509 	enum pdo_fetch_type default_fetch_type;
510 };
511 
512 /* represents a connection to a database */
513 struct _pdo_dbh_object_t {
514 	pdo_dbh_t *inner;
515 	/* these items must appear in this order at the beginning of the
516        struct so that this can be cast as a zend_object.  we need this
517        to allow the extending class to escape all the custom handlers
518 	   that PDO declares.
519     */
520 	zend_object std;
521 };
522 
php_pdo_dbh_fetch_inner(zend_object * obj)523 static inline pdo_dbh_t *php_pdo_dbh_fetch_inner(zend_object *obj) {
524 	return (pdo_dbh_t *)(((pdo_dbh_object_t *)((char*)(obj) - XtOffsetOf(pdo_dbh_object_t, std)))->inner);
525 }
526 
php_pdo_dbh_fetch_object(zend_object * obj)527 static inline pdo_dbh_object_t *php_pdo_dbh_fetch_object(zend_object *obj) {
528 	return (pdo_dbh_object_t *)((char*)(obj) - XtOffsetOf(pdo_dbh_object_t, std));
529 }
530 
531 #define Z_PDO_DBH_P(zv) php_pdo_dbh_fetch_inner(Z_OBJ_P((zv)))
532 #define Z_PDO_OBJECT_P(zv) php_pdo_dbh_fetch_object(Z_OBJ_P((zv)))
533 
534 /* describes a column */
535 struct pdo_column_data {
536 	zend_string *name;
537 	size_t maxlen;
538 	zend_ulong precision;
539 	enum pdo_param_type param_type;
540 };
541 
542 /* describes a bound parameter */
543 struct pdo_bound_param_data {
544 	zval parameter;				/* the variable itself */
545 
546 	zval driver_params;			/* optional parameter(s) for the driver */
547 
548 	zend_long paramno; /* if -1, then it has a name, and we don't know the index *yet* */
549 	zend_string *name;
550 
551 	zend_long max_value_len;	/* as a hint for pre-allocation */
552 
553 	void *driver_data;
554 
555 	pdo_stmt_t *stmt;	/* for convenience in dtor */
556 
557 	enum pdo_param_type param_type; /* desired or suggested variable type */
558 
559 	int is_param;		/* parameter or column ? */
560 };
561 
562 /* represents a prepared statement */
563 struct _pdo_stmt_t {
564 	/* driver specifics */
565 	const struct pdo_stmt_methods *methods;
566 	void *driver_data;
567 
568 	/* if true, we've already successfully executed this statement at least
569 	 * once */
570 	unsigned executed:1;
571 	/* if true, the statement supports placeholders and can implement
572 	 * bindParam() for its prepared statements, if false, PDO should
573 	 * emulate prepare and bind on its behalf */
574 	unsigned supports_placeholders:2;
575 
576 	unsigned _reserved:29;
577 
578 	/* the number of columns in the result set; not valid until after
579 	 * the statement has been executed at least once.  In some cases, might
580 	 * not be valid until fetch (at the driver level) has been called at least once.
581 	 * */
582 	int column_count;
583 	struct pdo_column_data *columns;
584 
585 	/* we want to keep the dbh alive while we live, so we own a reference */
586 	zval database_object_handle;
587 	pdo_dbh_t *dbh;
588 
589 	/* keep track of bound input parameters.  Some drivers support
590 	 * input/output parameters, but you can't rely on that working */
591 	HashTable *bound_params;
592 	/* When rewriting from named to positional, this maps positions to names */
593 	HashTable *bound_param_map;
594 	/* keep track of PHP variables bound to named (or positional) columns
595 	 * in the result set */
596 	HashTable *bound_columns;
597 
598 	/* not always meaningful */
599 	zend_long row_count;
600 
601 	/* used to hold the statement's current query */
602 	char *query_string;
603 	size_t query_stringlen;
604 
605 	/* the copy of the query with expanded binds ONLY for emulated-prepare drivers */
606 	char *active_query_string;
607 	size_t active_query_stringlen;
608 
609 	/* the cursor specific error code. */
610 	pdo_error_type error_code;
611 
612 	/* for lazy fetches, we always return the same lazy object handle.
613 	 * Let's keep it here. */
614 	zval lazy_object_ref;
615 	zend_ulong refcount;
616 
617 	/* defaults for fetches */
618 	enum pdo_fetch_type default_fetch_type;
619 	union {
620 		int column;
621 		struct {
622 			zval ctor_args;            /* freed */
623 			zend_fcall_info fci;
624 			zend_fcall_info_cache fcc;
625 			zval retval;
626 			zend_class_entry *ce;
627 		} cls;
628 		struct {
629 			zval fetch_args;           /* freed */
630 			zend_fcall_info fci;
631 			zend_fcall_info_cache fcc;
632 			zval object;
633 			zval function;
634 			zval *values;              /* freed */
635 		} func;
636 		zval into;
637 	} fetch;
638 
639 	/* used by the query parser for driver specific
640 	 * parameter naming (see pgsql driver for example) */
641 	const char *named_rewrite_template;
642 
643 	/* these items must appear in this order at the beginning of the
644        struct so that this can be cast as a zend_object.  we need this
645        to allow the extending class to escape all the custom handlers
646 	   that PDO declares.
647     */
648 	zend_object std;
649 };
650 
php_pdo_stmt_fetch_object(zend_object * obj)651 static inline pdo_stmt_t *php_pdo_stmt_fetch_object(zend_object *obj) {
652 	return (pdo_stmt_t *)((char*)(obj) - XtOffsetOf(pdo_stmt_t, std));
653 }
654 
655 #define Z_PDO_STMT_P(zv) php_pdo_stmt_fetch_object(Z_OBJ_P((zv)))
656 
657 struct _pdo_row_t {
658 	zend_object std;
659 	pdo_stmt_t *stmt;
660 };
661 
662 /* call this in MINIT to register your PDO driver */
663 PDO_API int php_pdo_register_driver(const pdo_driver_t *driver);
664 /* call this in MSHUTDOWN to unregister your PDO driver */
665 PDO_API void php_pdo_unregister_driver(const pdo_driver_t *driver);
666 
667 /* For the convenience of drivers, this function will parse a data source
668  * string, of the form "name=value; name2=value2" and populate variables
669  * according to the data you pass in and array of pdo_data_src_parser structures */
670 struct pdo_data_src_parser {
671 	const char *optname;
672 	char *optval;
673 	int freeme;
674 };
675 
676 PDO_API int php_pdo_parse_data_source(const char *data_source,
677 		zend_ulong data_source_len, struct pdo_data_src_parser *parsed,
678 		int nparams);
679 
680 PDO_API zend_class_entry *php_pdo_get_dbh_ce(void);
681 PDO_API zend_class_entry *php_pdo_get_exception(void);
682 
683 PDO_API int pdo_parse_params(pdo_stmt_t *stmt, const char *inquery, size_t inquery_len,
684 	char **outquery, size_t *outquery_len);
685 
686 PDO_API void pdo_raise_impl_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt,
687 	const char *sqlstate, const char *supp);
688 
689 PDO_API void php_pdo_dbh_addref(pdo_dbh_t *dbh);
690 PDO_API void php_pdo_dbh_delref(pdo_dbh_t *dbh);
691 
692 PDO_API void php_pdo_free_statement(pdo_stmt_t *stmt);
693 PDO_API void php_pdo_stmt_set_column_count(pdo_stmt_t *stmt, int new_count);
694 
695 PDO_API void pdo_throw_exception(unsigned int driver_errcode, char *driver_errmsg, pdo_error_type *pdo_error);
696 #endif /* PHP_PDO_DRIVER_H */
697