php_mysqli_structs.h 10 KB

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