php_mysqli_structs.h 7.9 KB

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