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 #if defined(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 #define MYSQLI_LLU_SPEC "%I64u"
131 #define MYSQLI_LL_SPEC "%I64d"
132 #ifndef L64
133 #define L64(x) x##i64
134 #endif
135 typedef __int64 my_longlong;
136 #else
137 # if defined(__GNUC__) && __GNUC__ >= 4
138 # define PHP_MYSQLI_API __attribute__ ((visibility("default")))
139 # else
140 # define PHP_MYSQLI_API
141 # endif
142 /* we need this for PRIu64 and PRId64 */
143 #include <inttypes.h>
144 #define MYSQLI_LLU_SPEC "%" PRIu64
145 #define MYSQLI_LL_SPEC "%" PRId64
146 #ifndef L64
147 #define L64(x) x##LL
148 #endif
149 typedef int64_t my_longlong;
150 #endif
151
152 typedef zend_ulong ulong;
153
154 #ifdef ZTS
155 #include "TSRM.h"
156 #endif
157
158 extern zend_class_entry *mysqli_link_class_entry;
159 extern zend_class_entry *mysqli_stmt_class_entry;
160 extern zend_class_entry *mysqli_result_class_entry;
161 extern zend_class_entry *mysqli_driver_class_entry;
162 extern zend_class_entry *mysqli_warning_class_entry;
163 extern zend_class_entry *mysqli_exception_class_entry;
164 extern int php_le_pmysqli(void);
165 extern void php_mysqli_dtor_p_elements(void *data);
166
167 extern void php_mysqli_close(MY_MYSQL * mysql, int close_type, int resource_status);
168
169 extern const zend_object_iterator_funcs php_mysqli_result_iterator_funcs;
170 extern zend_object_iterator *php_mysqli_result_get_iterator(zend_class_entry *ce, zval *object, int by_ref);
171
172 extern void php_mysqli_fetch_into_hash_aux(zval *return_value, MYSQL_RES * result, zend_long fetchtype);
173
174 #define MYSQLI_DISABLE_MQ if (mysql->multi_query) { \
175 mysql_set_server_option(mysql->mysql, MYSQL_OPTION_MULTI_STATEMENTS_OFF); \
176 mysql->multi_query = 0; \
177 }
178
179 #define MYSQLI_ENABLE_MQ if (!mysql->multi_query) { \
180 mysql_set_server_option(mysql->mysql, MYSQL_OPTION_MULTI_STATEMENTS_ON); \
181 mysql->multi_query = 1; \
182 }
183
184 #define MYSQLI_REGISTER_RESOURCE_EX(__ptr, __zval) \
185 (Z_MYSQLI_P(__zval))->ptr = __ptr;
186
187 #define MYSQLI_RETVAL_RESOURCE(__ptr, __ce) \
188 RETVAL_OBJ(mysqli_objects_new(__ce)); \
189 MYSQLI_REGISTER_RESOURCE_EX(__ptr, return_value)
190
191 #define MYSQLI_REGISTER_RESOURCE(__ptr, __ce) \
192 {\
193 zval *object = getThis(); \
194 if (!object || !instanceof_function(Z_OBJCE_P(object), mysqli_link_class_entry)) { \
195 object = return_value; \
196 ZVAL_OBJ(object, mysqli_objects_new(__ce)); \
197 } \
198 MYSQLI_REGISTER_RESOURCE_EX(__ptr, object)\
199 }
200
201 #define MYSQLI_FETCH_RESOURCE(__ptr, __type, __id, __name, __check) \
202 { \
203 MYSQLI_RESOURCE *my_res; \
204 mysqli_object *intern = Z_MYSQLI_P(__id); \
205 if (!(my_res = (MYSQLI_RESOURCE *)intern->ptr)) {\
206 zend_throw_error(NULL, "%s object is already closed", ZSTR_VAL(intern->zo.ce->name));\
207 RETURN_THROWS();\
208 }\
209 __ptr = (__type)my_res->ptr; \
210 if (my_res->status < __check) { \
211 zend_throw_error(NULL, "%s object is not fully initialized", ZSTR_VAL(intern->zo.ce->name)); \
212 RETURN_THROWS();\
213 }\
214 }
215
216 #define MYSQLI_FETCH_RESOURCE_BY_OBJ(__ptr, __type, __obj, __name, __check) \
217 { \
218 MYSQLI_RESOURCE *my_res; \
219 if (!(my_res = (MYSQLI_RESOURCE *)(__obj->ptr))) {\
220 zend_throw_error(NULL, "%s object is already closed", ZSTR_VAL(intern->zo.ce->name));\
221 return;\
222 }\
223 __ptr = (__type)my_res->ptr; \
224 if (my_res->status < __check) { \
225 zend_throw_error(NULL, "%s object is not fully initialized", ZSTR_VAL(intern->zo.ce->name)); \
226 return;\
227 }\
228 }
229
230 #define MYSQLI_FETCH_RESOURCE_CONN(__ptr, __id, __check) \
231 { \
232 MYSQLI_FETCH_RESOURCE((__ptr), MY_MYSQL *, (__id), "mysqli_link", (__check)); \
233 if (!(__ptr)->mysql) { \
234 zend_throw_error(NULL, "%s object is not fully initialized", ZSTR_VAL(Z_OBJCE_P(__id)->name)); \
235 RETURN_THROWS(); \
236 } \
237 }
238
239 #define MYSQLI_FETCH_RESOURCE_STMT(__ptr, __id, __check) \
240 { \
241 MYSQLI_FETCH_RESOURCE((__ptr), MY_STMT *, (__id), "mysqli_stmt", (__check)); \
242 ZEND_ASSERT((__ptr)->stmt && "Missing statement?"); \
243 }
244
245 #define MYSQLI_SET_STATUS(__id, __value) \
246 { \
247 mysqli_object *intern = Z_MYSQLI_P(__id); \
248 ((MYSQLI_RESOURCE *)intern->ptr)->status = __value; \
249 } \
250
251 #define MYSQLI_CLEAR_RESOURCE(__id) \
252 { \
253 mysqli_object *intern = Z_MYSQLI_P(__id); \
254 efree(intern->ptr); \
255 intern->ptr = NULL; \
256 }
257
258
259 ZEND_BEGIN_MODULE_GLOBALS(mysqli)
260 zend_long num_links;
261 zend_long max_links;
262 zend_long num_active_persistent;
263 zend_long num_inactive_persistent;
264 zend_long max_persistent;
265 zend_long allow_persistent;
266 zend_ulong default_port;
267 char *default_host;
268 char *default_user;
269 char *default_pw;
270 char *default_socket;
271 zend_long reconnect;
272 zend_long allow_local_infile;
273 char *local_infile_directory;
274 zend_long error_no;
275 char *error_msg;
276 zend_long report_mode;
277 bool rollback_on_cached_plink;
278 ZEND_END_MODULE_GLOBALS(mysqli)
279
280 #define MyG(v) ZEND_MODULE_GLOBALS_ACCESSOR(mysqli, v)
281
282 #if defined(ZTS) && defined(COMPILE_DL_MYSQLI)
283 ZEND_TSRMLS_CACHE_EXTERN()
284 #endif
285
286 ZEND_EXTERN_MODULE_GLOBALS(mysqli)
287
288 #endif /* PHP_MYSQLI_STRUCTS.H */
289