xref: /PHP-7.1/ext/mysqli/php_mysqli_structs.h (revision ccd4716e)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2018 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 #define MYSQLI_DISABLE_MQ if (mysql->multi_query) { \
220 	mysql_set_server_option(mysql->mysql, MYSQL_OPTION_MULTI_STATEMENTS_OFF); \
221 	mysql->multi_query = 0; \
222 }
223 
224 #define MYSQLI_ENABLE_MQ if (!mysql->multi_query) { \
225 	mysql_set_server_option(mysql->mysql, MYSQL_OPTION_MULTI_STATEMENTS_ON); \
226 	mysql->multi_query = 1; \
227 }
228 
229 #define REGISTER_MYSQLI_CLASS_ENTRY(name, mysqli_entry, class_functions) { \
230 	zend_class_entry ce; \
231 	INIT_CLASS_ENTRY(ce, name,class_functions); \
232 	ce.create_object = mysqli_objects_new; \
233 	mysqli_entry = zend_register_internal_class(&ce); \
234 } \
235 
236 #define MYSQLI_REGISTER_RESOURCE_EX(__ptr, __zval)  \
237 	(Z_MYSQLI_P(__zval))->ptr = __ptr;
238 
239 #define MYSQLI_RETURN_RESOURCE(__ptr, __ce) \
240 	RETVAL_OBJ(mysqli_objects_new(__ce)); \
241 	MYSQLI_REGISTER_RESOURCE_EX(__ptr, return_value)
242 
243 #define MYSQLI_REGISTER_RESOURCE(__ptr, __ce) \
244 {\
245 	zval *object = getThis(); \
246 	if (!object || !instanceof_function(Z_OBJCE_P(object), mysqli_link_class_entry)) { \
247 		object = return_value; \
248 		ZVAL_OBJ(object, mysqli_objects_new(__ce)); \
249 	} \
250 	MYSQLI_REGISTER_RESOURCE_EX(__ptr, object)\
251 }
252 
253 #define MYSQLI_FETCH_RESOURCE(__ptr, __type, __id, __name, __check) \
254 { \
255 	MYSQLI_RESOURCE *my_res; \
256 	mysqli_object *intern = Z_MYSQLI_P(__id); \
257 	if (!(my_res = (MYSQLI_RESOURCE *)intern->ptr)) {\
258   		php_error_docref(NULL, E_WARNING, "Couldn't fetch %s", ZSTR_VAL(intern->zo.ce->name));\
259   		RETURN_NULL();\
260   	}\
261 	__ptr = (__type)my_res->ptr; \
262 	if (__check && my_res->status < __check) { \
263 		php_error_docref(NULL, E_WARNING, "invalid object or resource %s\n", ZSTR_VAL(intern->zo.ce->name)); \
264 		RETURN_NULL();\
265 	}\
266 }
267 
268 #define MYSQLI_FETCH_RESOURCE_BY_OBJ(__ptr, __type, __obj, __name, __check) \
269 { \
270 	MYSQLI_RESOURCE *my_res; \
271 	if (!(my_res = (MYSQLI_RESOURCE *)(__obj->ptr))) {\
272   		php_error_docref(NULL, E_WARNING, "Couldn't fetch %s", ZSTR_VAL(intern->zo.ce->name));\
273   		return;\
274   	}\
275 	__ptr = (__type)my_res->ptr; \
276 	if (__check && my_res->status < __check) { \
277 		php_error_docref(NULL, E_WARNING, "invalid object or resource %s\n", ZSTR_VAL(intern->zo.ce->name)); \
278 		return;\
279 	}\
280 }
281 
282 #define MYSQLI_FETCH_RESOURCE_CONN(__ptr, __id, __check) \
283 { \
284 	MYSQLI_FETCH_RESOURCE((__ptr), MY_MYSQL *, (__id), "mysqli_link", (__check)); \
285 	if (!(__ptr)->mysql) { \
286 		mysqli_object *intern = Z_MYSQLI_P(__id); \
287 		php_error_docref(NULL, E_WARNING, "invalid object or resource %s\n", ZSTR_VAL(intern->zo.ce->name)); \
288 		RETURN_NULL(); \
289 	} \
290 }
291 
292 #define MYSQLI_FETCH_RESOURCE_STMT(__ptr, __id, __check) \
293 { \
294 	MYSQLI_FETCH_RESOURCE((__ptr), MY_STMT *, (__id), "mysqli_stmt", (__check)); \
295 	if (!(__ptr)->stmt) { \
296 		mysqli_object *intern = Z_MYSQLI_P(__id); \
297 		php_error_docref(NULL, E_WARNING, "invalid object or resource %s\n", ZSTR_VAL(intern->zo.ce->name)); \
298 		RETURN_NULL();\
299 	} \
300 }
301 
302 #define MYSQLI_SET_STATUS(__id, __value) \
303 { \
304 	mysqli_object *intern = Z_MYSQLI_P(__id); \
305 	((MYSQLI_RESOURCE *)intern->ptr)->status = __value; \
306 } \
307 
308 #define MYSQLI_CLEAR_RESOURCE(__id) \
309 { \
310 	mysqli_object *intern = Z_MYSQLI_P(__id); \
311 	efree(intern->ptr); \
312 	intern->ptr = NULL; \
313 }
314 
315 
316 ZEND_BEGIN_MODULE_GLOBALS(mysqli)
317 	zend_long			default_link;
318 	zend_long			num_links;
319 	zend_long			max_links;
320 	zend_long 			num_active_persistent;
321 	zend_long 			num_inactive_persistent;
322 	zend_long			max_persistent;
323 	zend_long			allow_persistent;
324 	zend_ulong	default_port;
325 	char			*default_host;
326 	char			*default_user;
327 	char			*default_socket;
328 	char			*default_pw;
329 	zend_long			reconnect;
330 	zend_long			allow_local_infile;
331 	zend_long			strict;
332 	zend_long			error_no;
333 	char			*error_msg;
334 	zend_long			report_mode;
335 	HashTable		*report_ht;
336 	zend_ulong	multi_query;
337 	zend_ulong	embedded;
338 	zend_bool 		rollback_on_cached_plink;
339 ZEND_END_MODULE_GLOBALS(mysqli)
340 
341 #define MyG(v) ZEND_MODULE_GLOBALS_ACCESSOR(mysqli, v)
342 
343 #if defined(ZTS) && defined(COMPILE_DL_MYSQLI)
344 ZEND_TSRMLS_CACHE_EXTERN()
345 #endif
346 
347 #define my_estrdup(x) (x) ? estrdup(x) : NULL
348 #define my_efree(x) if (x) efree(x)
349 
350 ZEND_EXTERN_MODULE_GLOBALS(mysqli)
351 
352 #endif	/* PHP_MYSQLI_STRUCTS.H */
353 
354 
355 /*
356  * Local variables:
357  * tab-width: 4
358  * c-basic-offset: 4
359  * indent-tabs-mode: t
360  * End:
361  * vim600: noet sw=4 ts=4 fdm=marker
362  * vim<600: noet sw=4 ts=4
363  */
364