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