xref: /PHP-8.0/ext/mysqli/php_mysqli_structs.h (revision 7c702b72)
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   | Authors: Georg Richter <georg@php.net>                               |
14   |          Andrey Hristov <andrey@php.net>                             |
15   |          Ulf Wendel <uw@php.net>                                     |
16   +----------------------------------------------------------------------+
17 */
18 
19 #ifndef PHP_MYSQLI_STRUCTS_H
20 #define PHP_MYSQLI_STRUCTS_H
21 
22 /* A little hack to prevent build break, when mysql is used together with
23  * c-client, which also defines LIST.
24  */
25 #ifdef LIST
26 #undef LIST
27 #endif
28 
29 #ifndef TRUE
30 #define TRUE 1
31 #endif
32 
33 #ifndef FALSE
34 #define FALSE 0
35 #endif
36 
37 #ifdef MYSQLI_USE_MYSQLND
38 #include "ext/mysqlnd/mysqlnd.h"
39 #include "mysqli_mysqlnd.h"
40 #else
41 
42 #include <mysql.h>
43 #if MYSQL_VERSION_ID >= 80000 &&  MYSQL_VERSION_ID < 100000
44 typedef _Bool		my_bool;
45 #endif
46 #include <errmsg.h>
47 #include <mysqld_error.h>
48 #include "mysqli_libmysql.h"
49 #endif /* MYSQLI_USE_MYSQLND */
50 
51 
52 #define MYSQLI_VERSION_ID		101009
53 
54 enum mysqli_status {
55 	MYSQLI_STATUS_UNKNOWN=0,
56 	MYSQLI_STATUS_INITIALIZED,
57 	MYSQLI_STATUS_VALID
58 };
59 
60 typedef struct {
61 	char		*val;
62 	zend_ulong		buflen;
63 	zend_ulong		output_len;
64 	zend_ulong		type;
65 } VAR_BUFFER;
66 
67 typedef struct {
68 	unsigned int	var_cnt;
69 	VAR_BUFFER		*buf;
70 	zval			*vars;
71 	my_bool			*is_null;
72 } BIND_BUFFER;
73 
74 typedef struct {
75 	MYSQL_STMT	*stmt;
76 	BIND_BUFFER	param;
77 	BIND_BUFFER	result;
78 	char		*query;
79 #ifndef MYSQLI_USE_MYSQLND
80 	/* used to manage refcount with libmysql (already implement in mysqlnd) */
81 	zval		link_handle;
82 #endif
83 } MY_STMT;
84 
85 typedef struct {
86 	MYSQL			*mysql;
87 	zend_string		*hash_key;
88 	zval			li_read;
89 	php_stream		*li_stream;
90 	unsigned int 	multi_query;
91 	zend_bool		persistent;
92 #ifdef MYSQLI_USE_MYSQLND
93 	int				async_result_fetch_type;
94 #endif
95 } MY_MYSQL;
96 
97 typedef struct {
98 	void				*ptr;		/* resource: (mysql, result, stmt)   */
99 	void				*info;		/* additional buffer				 */
100 	enum mysqli_status	status;		/* object status */
101 } MYSQLI_RESOURCE;
102 
103 typedef struct _mysqli_object {
104 	void 				*ptr;
105 	HashTable 			*prop_handler;
106 	zend_object 		zo;
107 } mysqli_object; /* extends zend_object */
108 
php_mysqli_fetch_object(zend_object * obj)109 static inline mysqli_object *php_mysqli_fetch_object(zend_object *obj) {
110 	return (mysqli_object *)((char*)(obj) - XtOffsetOf(mysqli_object, zo));
111 }
112 
113 #define Z_MYSQLI_P(zv) php_mysqli_fetch_object(Z_OBJ_P((zv)))
114 
115 typedef struct st_mysqli_warning MYSQLI_WARNING;
116 
117 struct st_mysqli_warning {
118 	zval	reason;
119 	zval	sqlstate;
120 	int		errorno;
121    	MYSQLI_WARNING	*next;
122 };
123 
124 typedef struct _mysqli_property_entry {
125 	const char *pname;
126 	size_t pname_length;
127 	int (*r_func)(mysqli_object *obj, zval *retval, zend_bool quiet);
128 	int (*w_func)(mysqli_object *obj, zval *value);
129 } mysqli_property_entry;
130 
131 typedef struct {
132 	zend_ptr_stack free_links;
133 } mysqli_plist_entry;
134 
135 #ifdef PHP_WIN32
136 #define PHP_MYSQLI_API __declspec(dllexport)
137 #ifndef L64
138 #define L64(x) x##i64
139 #endif
140 typedef __int64 my_longlong;
141 #else
142 # if defined(__GNUC__) && __GNUC__ >= 4
143 #  define PHP_MYSQLI_API __attribute__ ((visibility("default")))
144 # else
145 #  define PHP_MYSQLI_API
146 # endif
147 #ifndef L64
148 #define L64(x) x##LL
149 #endif
150 typedef int64_t my_longlong;
151 #endif
152 
153 /* we need this for PRIu64 and PRId64 */
154 #include <inttypes.h>
155 #define MYSQLI_LLU_SPEC "%" PRIu64
156 #define MYSQLI_LL_SPEC "%" PRId64
157 
158 #ifdef ZTS
159 #include "TSRM.h"
160 #endif
161 
162 extern zend_class_entry *mysqli_link_class_entry;
163 extern zend_class_entry *mysqli_stmt_class_entry;
164 extern zend_class_entry *mysqli_result_class_entry;
165 extern zend_class_entry *mysqli_driver_class_entry;
166 extern zend_class_entry *mysqli_warning_class_entry;
167 extern zend_class_entry *mysqli_exception_class_entry;
168 extern int php_le_pmysqli(void);
169 extern void php_mysqli_dtor_p_elements(void *data);
170 
171 extern void php_mysqli_close(MY_MYSQL * mysql, int close_type, int resource_status);
172 
173 extern const zend_object_iterator_funcs php_mysqli_result_iterator_funcs;
174 extern zend_object_iterator *php_mysqli_result_get_iterator(zend_class_entry *ce, zval *object, int by_ref);
175 
176 extern void php_mysqli_fetch_into_hash_aux(zval *return_value, MYSQL_RES * result, zend_long fetchtype);
177 
178 #define MYSQLI_DISABLE_MQ if (mysql->multi_query) { \
179 	mysql_set_server_option(mysql->mysql, MYSQL_OPTION_MULTI_STATEMENTS_OFF); \
180 	mysql->multi_query = 0; \
181 }
182 
183 #define MYSQLI_ENABLE_MQ if (!mysql->multi_query) { \
184 	mysql_set_server_option(mysql->mysql, MYSQL_OPTION_MULTI_STATEMENTS_ON); \
185 	mysql->multi_query = 1; \
186 }
187 
188 #define REGISTER_MYSQLI_CLASS_ENTRY(name, mysqli_entry, class_functions) { \
189 	zend_class_entry ce; \
190 	INIT_CLASS_ENTRY(ce, name,class_functions); \
191 	ce.create_object = mysqli_objects_new; \
192 	mysqli_entry = zend_register_internal_class(&ce); \
193 } \
194 
195 #define MYSQLI_REGISTER_RESOURCE_EX(__ptr, __zval)  \
196 	(Z_MYSQLI_P(__zval))->ptr = __ptr;
197 
198 #define MYSQLI_RETURN_RESOURCE(__ptr, __ce) \
199 	RETVAL_OBJ(mysqli_objects_new(__ce)); \
200 	MYSQLI_REGISTER_RESOURCE_EX(__ptr, return_value)
201 
202 #define MYSQLI_REGISTER_RESOURCE(__ptr, __ce) \
203 {\
204 	zval *object = getThis(); \
205 	if (!object || !instanceof_function(Z_OBJCE_P(object), mysqli_link_class_entry)) { \
206 		object = return_value; \
207 		ZVAL_OBJ(object, mysqli_objects_new(__ce)); \
208 	} \
209 	MYSQLI_REGISTER_RESOURCE_EX(__ptr, object)\
210 }
211 
212 #define MYSQLI_FETCH_RESOURCE(__ptr, __type, __id, __name, __check) \
213 { \
214 	MYSQLI_RESOURCE *my_res; \
215 	mysqli_object *intern = Z_MYSQLI_P(__id); \
216 	if (!(my_res = (MYSQLI_RESOURCE *)intern->ptr)) {\
217 		zend_throw_error(NULL, "%s object is already closed", ZSTR_VAL(intern->zo.ce->name));\
218 		RETURN_THROWS();\
219   	}\
220 	__ptr = (__type)my_res->ptr; \
221 	if (my_res->status < __check) { \
222 		zend_throw_error(NULL, "%s object is not fully initialized", ZSTR_VAL(intern->zo.ce->name)); \
223 		RETURN_THROWS();\
224 	}\
225 }
226 
227 #define MYSQLI_FETCH_RESOURCE_BY_OBJ(__ptr, __type, __obj, __name, __check) \
228 { \
229 	MYSQLI_RESOURCE *my_res; \
230 	if (!(my_res = (MYSQLI_RESOURCE *)(__obj->ptr))) {\
231 		zend_throw_error(NULL, "%s object is already closed", ZSTR_VAL(intern->zo.ce->name));\
232 		return;\
233 	}\
234 	__ptr = (__type)my_res->ptr; \
235 	if (my_res->status < __check) { \
236 		zend_throw_error(NULL, "%s object is not fully initialized", ZSTR_VAL(intern->zo.ce->name)); \
237 		return;\
238 	}\
239 }
240 
241 #define MYSQLI_FETCH_RESOURCE_CONN(__ptr, __id, __check) \
242 { \
243 	MYSQLI_FETCH_RESOURCE((__ptr), MY_MYSQL *, (__id), "mysqli_link", (__check)); \
244 	if (!(__ptr)->mysql) { \
245 		zend_throw_error(NULL, "%s object is not fully initialized", ZSTR_VAL(Z_OBJCE_P(__id)->name)); \
246 		RETURN_THROWS(); \
247 	} \
248 }
249 
250 #define MYSQLI_FETCH_RESOURCE_STMT(__ptr, __id, __check) \
251 { \
252 	MYSQLI_FETCH_RESOURCE((__ptr), MY_STMT *, (__id), "mysqli_stmt", (__check)); \
253 	ZEND_ASSERT((__ptr)->stmt && "Missing statement?"); \
254 }
255 
256 #define MYSQLI_SET_STATUS(__id, __value) \
257 { \
258 	mysqli_object *intern = Z_MYSQLI_P(__id); \
259 	((MYSQLI_RESOURCE *)intern->ptr)->status = __value; \
260 } \
261 
262 #define MYSQLI_CLEAR_RESOURCE(__id) \
263 { \
264 	mysqli_object *intern = Z_MYSQLI_P(__id); \
265 	efree(intern->ptr); \
266 	intern->ptr = NULL; \
267 }
268 
269 
270 ZEND_BEGIN_MODULE_GLOBALS(mysqli)
271 	zend_long			default_link;
272 	zend_long			num_links;
273 	zend_long			max_links;
274 	zend_long 			num_active_persistent;
275 	zend_long 			num_inactive_persistent;
276 	zend_long			max_persistent;
277 	zend_long			allow_persistent;
278 	zend_ulong	default_port;
279 	char			*default_host;
280 	char			*default_user;
281 	char			*default_socket;
282 	char			*default_pw;
283 	zend_long			reconnect;
284 	zend_long			allow_local_infile;
285 	zend_long			strict;
286 	zend_long			error_no;
287 	char			*error_msg;
288 	zend_long			report_mode;
289 	HashTable		*report_ht;
290 	zend_ulong	multi_query;
291 	zend_bool 		rollback_on_cached_plink;
292 ZEND_END_MODULE_GLOBALS(mysqli)
293 
294 #define MyG(v) ZEND_MODULE_GLOBALS_ACCESSOR(mysqli, v)
295 
296 #if defined(ZTS) && defined(COMPILE_DL_MYSQLI)
297 ZEND_TSRMLS_CACHE_EXTERN()
298 #endif
299 
300 ZEND_EXTERN_MODULE_GLOBALS(mysqli)
301 
302 #endif	/* PHP_MYSQLI_STRUCTS.H */
303