xref: /PHP-5.3/ext/mysqli/php_mysqli_structs.h (revision a2045ff3)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 5                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2013 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   | Author: Georg Richter <georg@php.net>                                |
16   +----------------------------------------------------------------------+
17 
18   $Id$
19 */
20 
21 #ifndef PHP_MYSQLI_STRUCTS_H
22 #define PHP_MYSQLI_STRUCTS_H
23 
24 /* A little hack to prevent build break, when mysql is used together with
25  * c-client, which also defines LIST.
26  */
27 #ifdef LIST
28 #undef LIST
29 #endif
30 
31 #ifndef TRUE
32 #define TRUE 1
33 #endif
34 
35 #ifndef FALSE
36 #define FALSE 0
37 #endif
38 
39 #ifdef MYSQLI_USE_MYSQLND
40 #include "ext/mysqlnd/mysqlnd.h"
41 #include "mysqli_mysqlnd.h"
42 #else
43 
44 /*
45   The libmysql headers (a PITA) also define it and there will be an warning.
46   Undef it and later we might need to define it again.
47 */
48 #ifdef HAVE_MBRLEN
49 #undef HAVE_MBRLEN
50 #define WE_HAD_MBRLEN
51 #endif
52 #ifdef HAVE_MBSTATE_T
53 #undef HAVE_MBSTATE_T
54 #define WE_HAD_MBSTATE_T
55 #endif
56 
57 #if defined(ulong) && !defined(HAVE_ULONG)
58 #define HAVE_ULONG
59 #endif
60 
61 #include <my_global.h>
62 
63 #if !defined(HAVE_MBRLEN) && defined(WE_HAD_MBRLEN)
64 #define HAVE_MBRLEN 1
65 #endif
66 
67 #if !defined(HAVE_MBSTATE_T) && defined(WE_HAD_MBSTATE_T)
68 #define HAVE_MBSTATE_T 1
69 #endif
70 
71 /*
72   We need more than mysql.h because we need CHARSET_INFO in one place.
73   This order has been borrowed from the ODBC driver. Nothing can be removed
74   from the list of headers :(
75 */
76 
77 #include <my_sys.h>
78 #include <mysql.h>
79 #include <errmsg.h>
80 #include <my_list.h>
81 #include <m_string.h>
82 #include <mysqld_error.h>
83 #include <my_list.h>
84 #include <m_ctype.h>
85 #include "mysqli_libmysql.h"
86 #endif /* MYSQLI_USE_MYSQLND */
87 
88 
89 #define MYSQLI_VERSION_ID		101009
90 
91 enum mysqli_status {
92 	MYSQLI_STATUS_UNKNOWN=0,
93 	MYSQLI_STATUS_CLEARED,
94 	MYSQLI_STATUS_INITIALIZED,
95 	MYSQLI_STATUS_VALID
96 };
97 
98 typedef struct {
99 	char		*val;
100 	ulong		buflen;
101 	ulong		output_len;
102 	ulong		type;
103 } VAR_BUFFER;
104 
105 typedef struct {
106 	unsigned int	var_cnt;
107 	VAR_BUFFER		*buf;
108 	zval			**vars;
109 	char			*is_null;
110 } BIND_BUFFER;
111 
112 typedef struct {
113 	MYSQL_STMT	*stmt;
114 	BIND_BUFFER	param;
115 	BIND_BUFFER	result;
116 	char		*query;
117 } MY_STMT;
118 
119 typedef struct {
120 	MYSQL			*mysql;
121 	char			*hash_key;
122 	zval			*li_read;
123 	php_stream		*li_stream;
124 	unsigned int 	multi_query;
125 	zend_bool		persistent;
126 #if defined(MYSQLI_USE_MYSQLND)
127 	int				async_result_fetch_type;
128 #endif
129 } MY_MYSQL;
130 
131 typedef struct {
132 	int			mode;
133 	int			socket;
134 	FILE		*fp;
135 } PROFILER;
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 	zend_object 		zo;
145 	void 				*ptr;
146 	HashTable 			*prop_handler;
147 } mysqli_object; /* extends zend_object */
148 
149 typedef struct st_mysqli_warning MYSQLI_WARNING;
150 
151 struct st_mysqli_warning {
152 	zval	reason;
153 	zval	sqlstate;
154 	int		errorno;
155    	MYSQLI_WARNING	*next;
156 };
157 
158 typedef struct _mysqli_property_entry {
159 	const char *pname;
160 	size_t pname_length;
161 	int (*r_func)(mysqli_object *obj, zval **retval TSRMLS_DC);
162 	int (*w_func)(mysqli_object *obj, zval *value TSRMLS_DC);
163 } mysqli_property_entry;
164 
165 #if !defined(MYSQLI_USE_MYSQLND)
166 typedef struct {
167 	char	error_msg[LOCAL_INFILE_ERROR_LEN];
168   	void	*userdata;
169 } mysqli_local_infile;
170 #endif
171 
172 typedef struct {
173 	zend_ptr_stack free_links;
174 } mysqli_plist_entry;
175 
176 #ifdef PHP_WIN32
177 #define PHP_MYSQLI_API __declspec(dllexport)
178 #define MYSQLI_LLU_SPEC "%I64u"
179 #define MYSQLI_LL_SPEC "%I64d"
180 #ifndef L64
181 #define L64(x) x##i64
182 #endif
183 typedef __int64 my_longlong;
184 #else
185 # if defined(__GNUC__) && __GNUC__ >= 4
186 #  define PHP_MYSQLI_API __attribute__ ((visibility("default")))
187 # else
188 #  define PHP_MYSQLI_API
189 # endif
190 /* we need this for PRIu64 and PRId64 */
191 #include <inttypes.h>
192 #define MYSQLI_LLU_SPEC "%" PRIu64
193 #define MYSQLI_LL_SPEC "%" PRId64
194 #ifndef L64
195 #define L64(x) x##LL
196 #endif
197 typedef int64_t my_longlong;
198 #endif
199 
200 #ifdef ZTS
201 #include "TSRM.h"
202 #endif
203 
204 extern zend_class_entry *mysqli_link_class_entry;
205 extern zend_class_entry *mysqli_stmt_class_entry;
206 extern zend_class_entry *mysqli_result_class_entry;
207 extern zend_class_entry *mysqli_driver_class_entry;
208 extern zend_class_entry *mysqli_warning_class_entry;
209 extern zend_class_entry *mysqli_exception_class_entry;
210 
211 #define MYSQLI_REGISTER_RESOURCE_EX(__ptr, __zval)  \
212 	((mysqli_object *) zend_object_store_get_object(__zval TSRMLS_CC))->ptr = __ptr;
213 
214 #define MYSQLI_RETURN_RESOURCE(__ptr, __ce) \
215 	Z_TYPE_P(return_value) = IS_OBJECT; \
216 	(return_value)->value.obj = mysqli_objects_new(__ce TSRMLS_CC); \
217 	MYSQLI_REGISTER_RESOURCE_EX(__ptr, return_value)
218 
219 #define MYSQLI_REGISTER_RESOURCE(__ptr, __ce) \
220 {\
221 	zval *object = getThis();\
222 	if (!object || !instanceof_function(Z_OBJCE_P(object), mysqli_link_class_entry TSRMLS_CC)) {\
223 		object = return_value;\
224 		Z_TYPE_P(object) = IS_OBJECT;\
225 		(object)->value.obj = mysqli_objects_new(__ce TSRMLS_CC);\
226 	}\
227 	MYSQLI_REGISTER_RESOURCE_EX(__ptr, object)\
228 }
229 
230 #define MYSQLI_FETCH_RESOURCE(__ptr, __type, __id, __name, __check) \
231 { \
232 	MYSQLI_RESOURCE *my_res; \
233 	mysqli_object *intern = (mysqli_object *)zend_object_store_get_object(*(__id) TSRMLS_CC);\
234 	if (!(my_res = (MYSQLI_RESOURCE *)intern->ptr)) {\
235   		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't fetch %s", intern->zo.ce->name);\
236   		RETURN_NULL();\
237   	}\
238 	__ptr = (__type)my_res->ptr; \
239 	if (__check && my_res->status < __check) { \
240 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid object or resource %s\n", intern->zo.ce->name); \
241 		RETURN_NULL();\
242 	}\
243 }
244 
245 #define MYSQLI_FETCH_RESOURCE_CONN(__ptr, __id, __check) \
246 { \
247 	MYSQLI_FETCH_RESOURCE((__ptr), MY_MYSQL *, (__id), "mysqli_link", (__check)); \
248 	if (!(__ptr)->mysql) { \
249 		mysqli_object *intern = (mysqli_object *)zend_object_store_get_object(*(__id) TSRMLS_CC);\
250 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid object or resource %s\n", intern->zo.ce->name); \
251 		RETURN_NULL();\
252 	} \
253 }
254 
255 #define MYSQLI_FETCH_RESOURCE_STMT(__ptr, __id, __check) \
256 { \
257 	MYSQLI_FETCH_RESOURCE((__ptr), MY_STMT *, (__id), "mysqli_stmt", (__check)); \
258 	if (!(__ptr)->stmt) { \
259 		mysqli_object *intern = (mysqli_object *)zend_object_store_get_object(*(__id) TSRMLS_CC);\
260 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid object or resource %s\n", intern->zo.ce->name); \
261 		RETURN_NULL();\
262 	} \
263 }
264 
265 
266 #define MYSQLI_SET_STATUS(__id, __value) \
267 { \
268 	mysqli_object *intern = (mysqli_object *)zend_object_store_get_object(*(__id) TSRMLS_CC);\
269 	((MYSQLI_RESOURCE *)intern->ptr)->status = __value; \
270 } \
271 
272 #define MYSQLI_CLEAR_RESOURCE(__id) \
273 { \
274 	mysqli_object *intern = (mysqli_object *)zend_object_store_get_object(*(__id) TSRMLS_CC);\
275 	efree(intern->ptr); \
276 	intern->ptr = NULL; \
277 }
278 
279 
280 ZEND_BEGIN_MODULE_GLOBALS(mysqli)
281 	long			default_link;
282 	long			num_links;
283 	long			max_links;
284 	long 			num_active_persistent;
285 	long 			num_inactive_persistent;
286 	long			max_persistent;
287 	long			allow_persistent;
288 	long			cache_size;
289 	unsigned long	default_port;
290 	char			*default_host;
291 	char			*default_user;
292 	char			*default_socket;
293 	char			*default_pw;
294 	long			reconnect;
295 	long			allow_local_infile;
296 	long			strict;
297 	long			error_no;
298 	char			*error_msg;
299 	long			report_mode;
300 	HashTable		*report_ht;
301 	unsigned long	multi_query;
302 	unsigned long	embedded;
303 ZEND_END_MODULE_GLOBALS(mysqli)
304 
305 
306 #ifdef ZTS
307 #define MyG(v) TSRMG(mysqli_globals_id, zend_mysqli_globals *, v)
308 #else
309 #define MyG(v) (mysqli_globals.v)
310 #endif
311 
312 #define my_estrdup(x) (x) ? estrdup(x) : NULL
313 #define my_efree(x) if (x) efree(x)
314 
315 ZEND_EXTERN_MODULE_GLOBALS(mysqli)
316 
317 #endif	/* PHP_MYSQLI_STRUCTS.H */
318 
319 
320 /*
321  * Local variables:
322  * tab-width: 4
323  * c-basic-offset: 4
324  * indent-tabs-mode: t
325  * End:
326  * vim600: noet sw=4 ts=4 fdm=marker
327  * vim<600: noet sw=4 ts=4
328  */
329