php_mysqli_structs.h 9.5 KB

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