xref: /PHP-5.4/ext/mysqli/php_mysqli_structs.h (revision 9137acc7)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 5                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2014 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   | Authors: Georg Richter <georg@php.net>                               |
16   |          Andrey Hristov <andrey@php.net>                             |
17   |          Ulf Wendel <uw@php.net>                                     |
18   +----------------------------------------------------------------------+
19 
20   $Id$
21 */
22 
23 #ifndef PHP_MYSQLI_STRUCTS_H
24 #define PHP_MYSQLI_STRUCTS_H
25 
26 /* A little hack to prevent build break, when mysql is used together with
27  * c-client, which also defines LIST.
28  */
29 #ifdef LIST
30 #undef LIST
31 #endif
32 
33 #ifndef TRUE
34 #define TRUE 1
35 #endif
36 
37 #ifndef FALSE
38 #define FALSE 0
39 #endif
40 
41 #ifdef MYSQLI_USE_MYSQLND
42 #include "ext/mysqlnd/mysqlnd.h"
43 #include "mysqli_mysqlnd.h"
44 #else
45 
46 /*
47   The libmysql headers (a PITA) also define it and there will be an warning.
48   Undef it and later we might need to define it again.
49 */
50 #ifdef HAVE_MBRLEN
51 #undef HAVE_MBRLEN
52 #define WE_HAD_MBRLEN
53 #endif
54 #ifdef HAVE_MBSTATE_T
55 #undef HAVE_MBSTATE_T
56 #define WE_HAD_MBSTATE_T
57 #endif
58 
59 #if defined(ulong) && !defined(HAVE_ULONG)
60 #define HAVE_ULONG
61 #endif
62 
63 #include <my_global.h>
64 
65 #if !defined(HAVE_MBRLEN) && defined(WE_HAD_MBRLEN)
66 #define HAVE_MBRLEN 1
67 #endif
68 
69 #if !defined(HAVE_MBSTATE_T) && defined(WE_HAD_MBSTATE_T)
70 #define HAVE_MBSTATE_T 1
71 #endif
72 
73 /*
74   We need more than mysql.h because we need CHARSET_INFO in one place.
75   This order has been borrowed from the ODBC driver. Nothing can be removed
76   from the list of headers :(
77 */
78 
79 #include <my_sys.h>
80 #include <mysql.h>
81 #include <errmsg.h>
82 #include <my_list.h>
83 #include <m_string.h>
84 #include <mysqld_error.h>
85 #include <my_list.h>
86 #include <m_ctype.h>
87 #include "mysqli_libmysql.h"
88 #endif /* MYSQLI_USE_MYSQLND */
89 
90 
91 #define MYSQLI_VERSION_ID		101009
92 
93 enum mysqli_status {
94 	MYSQLI_STATUS_UNKNOWN=0,
95 	MYSQLI_STATUS_CLEARED,
96 	MYSQLI_STATUS_INITIALIZED,
97 	MYSQLI_STATUS_VALID
98 };
99 
100 typedef struct {
101 	char		*val;
102 	ulong		buflen;
103 	ulong		output_len;
104 	ulong		type;
105 } VAR_BUFFER;
106 
107 typedef struct {
108 	unsigned int	var_cnt;
109 	VAR_BUFFER		*buf;
110 	zval			**vars;
111 	char			*is_null;
112 } BIND_BUFFER;
113 
114 typedef struct {
115 	MYSQL_STMT	*stmt;
116 	BIND_BUFFER	param;
117 	BIND_BUFFER	result;
118 	char		*query;
119 #ifndef MYSQLI_USE_MYSQLND
120 	/* used to manage refcount with libmysql (already implement in mysqlnd) */
121 	zend_object_handle link_handle;
122 #endif
123 } MY_STMT;
124 
125 typedef struct {
126 	MYSQL			*mysql;
127 	char			*hash_key;
128 	zval			*li_read;
129 	php_stream		*li_stream;
130 	unsigned int 	multi_query;
131 	zend_bool		persistent;
132 #if defined(MYSQLI_USE_MYSQLND)
133 	int				async_result_fetch_type;
134 #endif
135 } MY_MYSQL;
136 
137 typedef struct {
138 	int			mode;
139 	int			socket;
140 	FILE		*fp;
141 } PROFILER;
142 
143 typedef struct {
144 	void				*ptr;		/* resource: (mysql, result, stmt)   */
145 	void				*info;		/* additional buffer				 */
146 	enum mysqli_status	status;		/* object status */
147 } MYSQLI_RESOURCE;
148 
149 typedef struct _mysqli_object {
150 	zend_object 		zo;
151 	void 				*ptr;
152 	HashTable 			*prop_handler;
153 } mysqli_object; /* extends zend_object */
154 
155 typedef struct st_mysqli_warning MYSQLI_WARNING;
156 
157 struct st_mysqli_warning {
158 	zval	reason;
159 	zval	sqlstate;
160 	int		errorno;
161    	MYSQLI_WARNING	*next;
162 };
163 
164 typedef struct _mysqli_property_entry {
165 	const char *pname;
166 	size_t pname_length;
167 	int (*r_func)(mysqli_object *obj, zval **retval TSRMLS_DC);
168 	int (*w_func)(mysqli_object *obj, zval *value TSRMLS_DC);
169 } mysqli_property_entry;
170 
171 #if !defined(MYSQLI_USE_MYSQLND)
172 typedef struct {
173 	char	error_msg[LOCAL_INFILE_ERROR_LEN];
174   	void	*userdata;
175 } mysqli_local_infile;
176 #endif
177 
178 typedef struct {
179 	zend_ptr_stack free_links;
180 } mysqli_plist_entry;
181 
182 #ifdef PHP_WIN32
183 #define PHP_MYSQLI_API __declspec(dllexport)
184 #define MYSQLI_LLU_SPEC "%I64u"
185 #define MYSQLI_LL_SPEC "%I64d"
186 #ifndef L64
187 #define L64(x) x##i64
188 #endif
189 typedef __int64 my_longlong;
190 #else
191 # if defined(__GNUC__) && __GNUC__ >= 4
192 #  define PHP_MYSQLI_API __attribute__ ((visibility("default")))
193 # else
194 #  define PHP_MYSQLI_API
195 # endif
196 /* we need this for PRIu64 and PRId64 */
197 #include <inttypes.h>
198 #define MYSQLI_LLU_SPEC "%" PRIu64
199 #define MYSQLI_LL_SPEC "%" PRId64
200 #ifndef L64
201 #define L64(x) x##LL
202 #endif
203 typedef int64_t my_longlong;
204 #endif
205 
206 #ifdef ZTS
207 #include "TSRM.h"
208 #endif
209 
210 extern zend_class_entry *mysqli_link_class_entry;
211 extern zend_class_entry *mysqli_stmt_class_entry;
212 extern zend_class_entry *mysqli_result_class_entry;
213 extern zend_class_entry *mysqli_driver_class_entry;
214 extern zend_class_entry *mysqli_warning_class_entry;
215 extern zend_class_entry *mysqli_exception_class_entry;
216 extern int php_le_pmysqli(void);
217 extern void php_mysqli_dtor_p_elements(void *data);
218 
219 extern void php_mysqli_close(MY_MYSQL * mysql, int close_type, int resource_status TSRMLS_DC);
220 
221 extern zend_object_iterator_funcs php_mysqli_result_iterator_funcs;
222 extern zend_object_iterator *php_mysqli_result_get_iterator(zend_class_entry *ce, zval *object, int by_ref TSRMLS_DC);
223 
224 extern void php_mysqli_fetch_into_hash_aux(zval *return_value, MYSQL_RES * result, long fetchtype TSRMLS_DC);
225 
226 #ifdef HAVE_SPL
227 extern PHPAPI zend_class_entry *spl_ce_RuntimeException;
228 #endif
229 
230 #define MYSQLI_DISABLE_MQ if (mysql->multi_query) { \
231 	mysql_set_server_option(mysql->mysql, MYSQL_OPTION_MULTI_STATEMENTS_OFF); \
232 	mysql->multi_query = 0; \
233 }
234 
235 #define MYSQLI_ENABLE_MQ if (!mysql->multi_query) { \
236 	mysql_set_server_option(mysql->mysql, MYSQL_OPTION_MULTI_STATEMENTS_ON); \
237 	mysql->multi_query = 1; \
238 }
239 
240 #define REGISTER_MYSQLI_CLASS_ENTRY(name, mysqli_entry, class_functions) { \
241 	zend_class_entry ce; \
242 	INIT_CLASS_ENTRY(ce, name,class_functions); \
243 	ce.create_object = mysqli_objects_new; \
244 	mysqli_entry = zend_register_internal_class(&ce TSRMLS_CC); \
245 } \
246 
247 #define MYSQLI_REGISTER_RESOURCE_EX(__ptr, __zval)  \
248 	((mysqli_object *) zend_object_store_get_object(__zval TSRMLS_CC))->ptr = __ptr;
249 
250 #define MYSQLI_RETURN_RESOURCE(__ptr, __ce) \
251 	Z_TYPE_P(return_value) = IS_OBJECT; \
252 	(return_value)->value.obj = mysqli_objects_new(__ce TSRMLS_CC); \
253 	MYSQLI_REGISTER_RESOURCE_EX(__ptr, return_value)
254 
255 #define MYSQLI_REGISTER_RESOURCE(__ptr, __ce) \
256 {\
257 	zval *object = getThis();\
258 	if (!object || !instanceof_function(Z_OBJCE_P(object), mysqli_link_class_entry TSRMLS_CC)) {\
259 		object = return_value;\
260 		Z_TYPE_P(object) = IS_OBJECT;\
261 		(object)->value.obj = mysqli_objects_new(__ce TSRMLS_CC);\
262 	}\
263 	MYSQLI_REGISTER_RESOURCE_EX(__ptr, object)\
264 }
265 
266 #define MYSQLI_FETCH_RESOURCE(__ptr, __type, __id, __name, __check) \
267 { \
268 	MYSQLI_RESOURCE *my_res; \
269 	mysqli_object *intern = (mysqli_object *)zend_object_store_get_object(*(__id) TSRMLS_CC);\
270 	if (!(my_res = (MYSQLI_RESOURCE *)intern->ptr)) {\
271   		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't fetch %s", intern->zo.ce->name);\
272   		RETURN_NULL();\
273   	}\
274 	__ptr = (__type)my_res->ptr; \
275 	if (__check && my_res->status < __check) { \
276 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid object or resource %s\n", intern->zo.ce->name); \
277 		RETURN_NULL();\
278 	}\
279 }
280 
281 #define MYSQLI_FETCH_RESOURCE_BY_OBJ(__ptr, __type, __obj, __name, __check) \
282 { \
283 	MYSQLI_RESOURCE *my_res; \
284 	if (!(my_res = (MYSQLI_RESOURCE *)(__obj->ptr))) {\
285   		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't fetch %s", intern->zo.ce->name);\
286   		return;\
287   	}\
288 	__ptr = (__type)my_res->ptr; \
289 	if (__check && my_res->status < __check) { \
290 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid object or resource %s\n", intern->zo.ce->name); \
291 		return;\
292 	}\
293 }
294 
295 
296 #define MYSQLI_FETCH_RESOURCE_CONN(__ptr, __id, __check) \
297 { \
298 	MYSQLI_FETCH_RESOURCE((__ptr), MY_MYSQL *, (__id), "mysqli_link", (__check)); \
299 	if (!(__ptr)->mysql) { \
300 		mysqli_object *intern = (mysqli_object *)zend_object_store_get_object(*(__id) TSRMLS_CC);\
301 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid object or resource %s\n", intern->zo.ce->name); \
302 		RETURN_NULL();\
303 	} \
304 }
305 
306 #define MYSQLI_FETCH_RESOURCE_STMT(__ptr, __id, __check) \
307 { \
308 	MYSQLI_FETCH_RESOURCE((__ptr), MY_STMT *, (__id), "mysqli_stmt", (__check)); \
309 	if (!(__ptr)->stmt) { \
310 		mysqli_object *intern = (mysqli_object *)zend_object_store_get_object(*(__id) TSRMLS_CC);\
311 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid object or resource %s\n", intern->zo.ce->name); \
312 		RETURN_NULL();\
313 	} \
314 }
315 
316 
317 #define MYSQLI_SET_STATUS(__id, __value) \
318 { \
319 	mysqli_object *intern = (mysqli_object *)zend_object_store_get_object(*(__id) TSRMLS_CC);\
320 	((MYSQLI_RESOURCE *)intern->ptr)->status = __value; \
321 } \
322 
323 #define MYSQLI_CLEAR_RESOURCE(__id) \
324 { \
325 	mysqli_object *intern = (mysqli_object *)zend_object_store_get_object(*(__id) TSRMLS_CC);\
326 	efree(intern->ptr); \
327 	intern->ptr = NULL; \
328 }
329 
330 
331 ZEND_BEGIN_MODULE_GLOBALS(mysqli)
332 	long			default_link;
333 	long			num_links;
334 	long			max_links;
335 	long 			num_active_persistent;
336 	long 			num_inactive_persistent;
337 	long			max_persistent;
338 	long			allow_persistent;
339 	unsigned long	default_port;
340 	char			*default_host;
341 	char			*default_user;
342 	char			*default_socket;
343 	char			*default_pw;
344 	long			reconnect;
345 	long			allow_local_infile;
346 	long			strict;
347 	long			error_no;
348 	char			*error_msg;
349 	long			report_mode;
350 	HashTable		*report_ht;
351 	unsigned long	multi_query;
352 	unsigned long	embedded;
353 ZEND_END_MODULE_GLOBALS(mysqli)
354 
355 
356 #ifdef ZTS
357 #define MyG(v) TSRMG(mysqli_globals_id, zend_mysqli_globals *, v)
358 #else
359 #define MyG(v) (mysqli_globals.v)
360 #endif
361 
362 #define my_estrdup(x) (x) ? estrdup(x) : NULL
363 #define my_efree(x) if (x) efree(x)
364 
365 ZEND_EXTERN_MODULE_GLOBALS(mysqli)
366 
367 #endif	/* PHP_MYSQLI_STRUCTS.H */
368 
369 
370 /*
371  * Local variables:
372  * tab-width: 4
373  * c-basic-offset: 4
374  * indent-tabs-mode: t
375  * End:
376  * vim600: noet sw=4 ts=4 fdm=marker
377  * vim<600: noet sw=4 ts=4
378  */
379