xref: /PHP-7.0/ext/mysqli/php_mysqli_structs.h (revision 478f119a)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2017 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 	zend_ulong		buflen;
103 	zend_ulong		output_len;
104 	zend_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 	zval		link_handle;
122 #endif
123 } MY_STMT;
124 
125 typedef struct {
126 	MYSQL			*mysql;
127 	zend_string		*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 	void				*ptr;		/* resource: (mysql, result, stmt)   */
139 	void				*info;		/* additional buffer				 */
140 	enum mysqli_status	status;		/* object status */
141 } MYSQLI_RESOURCE;
142 
143 typedef struct _mysqli_object {
144 	void 				*ptr;
145 	HashTable 			*prop_handler;
146 	zend_object 		zo;
147 } mysqli_object; /* extends zend_object */
148 
php_mysqli_fetch_object(zend_object * obj)149 static inline mysqli_object *php_mysqli_fetch_object(zend_object *obj) {
150 	return (mysqli_object *)((char*)(obj) - XtOffsetOf(mysqli_object, zo));
151 }
152 
153 #define Z_MYSQLI_P(zv) php_mysqli_fetch_object(Z_OBJ_P((zv)))
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 	zval *(*r_func)(mysqli_object *obj, zval *retval);
168 	int (*w_func)(mysqli_object *obj, zval *value);
169 } mysqli_property_entry;
170 
171 typedef struct {
172 	zend_ptr_stack free_links;
173 } mysqli_plist_entry;
174 
175 #ifdef PHP_WIN32
176 #define PHP_MYSQLI_API __declspec(dllexport)
177 #define MYSQLI_LLU_SPEC "%I64u"
178 #define MYSQLI_LL_SPEC "%I64d"
179 #ifndef L64
180 #define L64(x) x##i64
181 #endif
182 typedef __int64 my_longlong;
183 #else
184 # if defined(__GNUC__) && __GNUC__ >= 4
185 #  define PHP_MYSQLI_API __attribute__ ((visibility("default")))
186 # else
187 #  define PHP_MYSQLI_API
188 # endif
189 /* we need this for PRIu64 and PRId64 */
190 #include <inttypes.h>
191 #define MYSQLI_LLU_SPEC "%" PRIu64
192 #define MYSQLI_LL_SPEC "%" PRId64
193 #ifndef L64
194 #define L64(x) x##LL
195 #endif
196 typedef int64_t my_longlong;
197 #endif
198 
199 #ifdef ZTS
200 #include "TSRM.h"
201 #endif
202 
203 extern zend_class_entry *mysqli_link_class_entry;
204 extern zend_class_entry *mysqli_stmt_class_entry;
205 extern zend_class_entry *mysqli_result_class_entry;
206 extern zend_class_entry *mysqli_driver_class_entry;
207 extern zend_class_entry *mysqli_warning_class_entry;
208 extern zend_class_entry *mysqli_exception_class_entry;
209 extern int php_le_pmysqli(void);
210 extern void php_mysqli_dtor_p_elements(void *data);
211 
212 extern void php_mysqli_close(MY_MYSQL * mysql, int close_type, int resource_status);
213 
214 extern zend_object_iterator_funcs php_mysqli_result_iterator_funcs;
215 extern zend_object_iterator *php_mysqli_result_get_iterator(zend_class_entry *ce, zval *object, int by_ref);
216 
217 extern void php_mysqli_fetch_into_hash_aux(zval *return_value, MYSQL_RES * result, zend_long fetchtype);
218 
219 #ifdef HAVE_SPL
220 extern PHPAPI zend_class_entry *spl_ce_RuntimeException;
221 #endif
222 
223 #define MYSQLI_DISABLE_MQ if (mysql->multi_query) { \
224 	mysql_set_server_option(mysql->mysql, MYSQL_OPTION_MULTI_STATEMENTS_OFF); \
225 	mysql->multi_query = 0; \
226 }
227 
228 #define MYSQLI_ENABLE_MQ if (!mysql->multi_query) { \
229 	mysql_set_server_option(mysql->mysql, MYSQL_OPTION_MULTI_STATEMENTS_ON); \
230 	mysql->multi_query = 1; \
231 }
232 
233 #define REGISTER_MYSQLI_CLASS_ENTRY(name, mysqli_entry, class_functions) { \
234 	zend_class_entry ce; \
235 	INIT_CLASS_ENTRY(ce, name,class_functions); \
236 	ce.create_object = mysqli_objects_new; \
237 	mysqli_entry = zend_register_internal_class(&ce); \
238 } \
239 
240 #define MYSQLI_REGISTER_RESOURCE_EX(__ptr, __zval)  \
241 	(Z_MYSQLI_P(__zval))->ptr = __ptr;
242 
243 #define MYSQLI_RETURN_RESOURCE(__ptr, __ce) \
244 	RETVAL_OBJ(mysqli_objects_new(__ce)); \
245 	MYSQLI_REGISTER_RESOURCE_EX(__ptr, return_value)
246 
247 #define MYSQLI_REGISTER_RESOURCE(__ptr, __ce) \
248 {\
249 	zval *object = getThis(); \
250 	if (!object || !instanceof_function(Z_OBJCE_P(object), mysqli_link_class_entry)) { \
251 		object = return_value; \
252 		ZVAL_OBJ(object, mysqli_objects_new(__ce)); \
253 	} \
254 	MYSQLI_REGISTER_RESOURCE_EX(__ptr, object)\
255 }
256 
257 #define MYSQLI_FETCH_RESOURCE(__ptr, __type, __id, __name, __check) \
258 { \
259 	MYSQLI_RESOURCE *my_res; \
260 	mysqli_object *intern = Z_MYSQLI_P(__id); \
261 	if (!(my_res = (MYSQLI_RESOURCE *)intern->ptr)) {\
262   		php_error_docref(NULL, E_WARNING, "Couldn't fetch %s", ZSTR_VAL(intern->zo.ce->name));\
263   		RETURN_NULL();\
264   	}\
265 	__ptr = (__type)my_res->ptr; \
266 	if (__check && my_res->status < __check) { \
267 		php_error_docref(NULL, E_WARNING, "invalid object or resource %s\n", ZSTR_VAL(intern->zo.ce->name)); \
268 		RETURN_NULL();\
269 	}\
270 }
271 
272 #define MYSQLI_FETCH_RESOURCE_BY_OBJ(__ptr, __type, __obj, __name, __check) \
273 { \
274 	MYSQLI_RESOURCE *my_res; \
275 	if (!(my_res = (MYSQLI_RESOURCE *)(__obj->ptr))) {\
276   		php_error_docref(NULL, E_WARNING, "Couldn't fetch %s", ZSTR_VAL(intern->zo.ce->name));\
277   		return;\
278   	}\
279 	__ptr = (__type)my_res->ptr; \
280 	if (__check && my_res->status < __check) { \
281 		php_error_docref(NULL, E_WARNING, "invalid object or resource %s\n", ZSTR_VAL(intern->zo.ce->name)); \
282 		return;\
283 	}\
284 }
285 
286 #define MYSQLI_FETCH_RESOURCE_CONN(__ptr, __id, __check) \
287 { \
288 	MYSQLI_FETCH_RESOURCE((__ptr), MY_MYSQL *, (__id), "mysqli_link", (__check)); \
289 	if (!(__ptr)->mysql) { \
290 		mysqli_object *intern = Z_MYSQLI_P(__id); \
291 		php_error_docref(NULL, E_WARNING, "invalid object or resource %s\n", ZSTR_VAL(intern->zo.ce->name)); \
292 		RETURN_NULL(); \
293 	} \
294 }
295 
296 #define MYSQLI_FETCH_RESOURCE_STMT(__ptr, __id, __check) \
297 { \
298 	MYSQLI_FETCH_RESOURCE((__ptr), MY_STMT *, (__id), "mysqli_stmt", (__check)); \
299 	if (!(__ptr)->stmt) { \
300 		mysqli_object *intern = Z_MYSQLI_P(__id); \
301 		php_error_docref(NULL, E_WARNING, "invalid object or resource %s\n", ZSTR_VAL(intern->zo.ce->name)); \
302 		RETURN_NULL();\
303 	} \
304 }
305 
306 #define MYSQLI_SET_STATUS(__id, __value) \
307 { \
308 	mysqli_object *intern = Z_MYSQLI_P(__id); \
309 	((MYSQLI_RESOURCE *)intern->ptr)->status = __value; \
310 } \
311 
312 #define MYSQLI_CLEAR_RESOURCE(__id) \
313 { \
314 	mysqli_object *intern = Z_MYSQLI_P(__id); \
315 	efree(intern->ptr); \
316 	intern->ptr = NULL; \
317 }
318 
319 
320 ZEND_BEGIN_MODULE_GLOBALS(mysqli)
321 	zend_long			default_link;
322 	zend_long			num_links;
323 	zend_long			max_links;
324 	zend_long 			num_active_persistent;
325 	zend_long 			num_inactive_persistent;
326 	zend_long			max_persistent;
327 	zend_long			allow_persistent;
328 	zend_ulong	default_port;
329 	char			*default_host;
330 	char			*default_user;
331 	char			*default_socket;
332 	char			*default_pw;
333 	zend_long			reconnect;
334 	zend_long			allow_local_infile;
335 	zend_long			strict;
336 	zend_long			error_no;
337 	char			*error_msg;
338 	zend_long			report_mode;
339 	HashTable		*report_ht;
340 	zend_ulong	multi_query;
341 	zend_ulong	embedded;
342 	zend_bool 		rollback_on_cached_plink;
343 ZEND_END_MODULE_GLOBALS(mysqli)
344 
345 #define MyG(v) ZEND_MODULE_GLOBALS_ACCESSOR(mysqli, v)
346 
347 #if defined(ZTS) && defined(COMPILE_DL_MYSQLI)
348 ZEND_TSRMLS_CACHE_EXTERN()
349 #endif
350 
351 #define my_estrdup(x) (x) ? estrdup(x) : NULL
352 #define my_efree(x) if (x) efree(x)
353 
354 ZEND_EXTERN_MODULE_GLOBALS(mysqli)
355 
356 #endif	/* PHP_MYSQLI_STRUCTS.H */
357 
358 
359 /*
360  * Local variables:
361  * tab-width: 4
362  * c-basic-offset: 4
363  * indent-tabs-mode: t
364  * End:
365  * vim600: noet sw=4 ts=4 fdm=marker
366  * vim<600: noet sw=4 ts=4
367  */
368