PageRenderTime 34ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/hphp/runtime/ext/ext_mysql.h

https://bitbucket.org/gnanakeethan/hiphop-php
C Header | 378 lines | 255 code | 77 blank | 46 comment | 2 complexity | acd63e698af63a42f035515573a0571b MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. /*
  2. +----------------------------------------------------------------------+
  3. | HipHop for PHP |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2010- Facebook, Inc. (http://www.facebook.com) |
  6. | Copyright (c) 1997-2010 The PHP Group |
  7. +----------------------------------------------------------------------+
  8. | This source file is subject to version 3.01 of the PHP license, |
  9. | that is bundled with this package in the file LICENSE, and is |
  10. | available through the world-wide-web at the following url: |
  11. | http://www.php.net/license/3_01.txt |
  12. | If you did not receive a copy of the PHP license and are unable to |
  13. | obtain it through the world-wide-web, please send a note to |
  14. | license@php.net so we can mail you a copy immediately. |
  15. +----------------------------------------------------------------------+
  16. */
  17. #ifndef incl_HPHP_EXT_MYSQL_H_
  18. #define incl_HPHP_EXT_MYSQL_H_
  19. #include "hphp/runtime/base/base_includes.h"
  20. #include "mysql/mysql.h"
  21. #ifdef PHP_MYSQL_UNIX_SOCK_ADDR
  22. #ifdef MYSQL_UNIX_ADDR
  23. #undef MYSQL_UNIX_ADDR
  24. #endif
  25. #define MYSQL_UNIX_ADDR PHP_MYSQL_UNIX_SOCK_ADDR
  26. #endif
  27. namespace HPHP {
  28. ///////////////////////////////////////////////////////////////////////////////
  29. class MySQL : public SweepableResourceData {
  30. public:
  31. /**
  32. * Operations on a resource object.
  33. */
  34. static MYSQL *GetConn(CVarRef link_identifier, MySQL **rconn = NULL);
  35. static MySQL *Get(CVarRef link_identifier);
  36. static bool CloseConn(CVarRef link_identifier);
  37. /**
  38. * Default settings.
  39. */
  40. static String GetDefaultServer() { return String();}
  41. static int GetDefaultPort();
  42. static String GetDefaultSocket();
  43. static String GetDefaultUsername() { return String();}
  44. static String GetDefaultPassword() { return String();}
  45. static String GetDefaultDatabase() { return String();}
  46. /**
  47. * A connection may be persistent across multiple HTTP requests.
  48. */
  49. static MySQL *GetPersistent(CStrRef host, int port, CStrRef socket,
  50. CStrRef username, CStrRef password,
  51. int client_flags) {
  52. return GetCachedImpl("mysql::persistent_conns", host, port, socket,
  53. username, password, client_flags);
  54. }
  55. static void SetPersistent(CStrRef host, int port, CStrRef socket,
  56. CStrRef username, CStrRef password,
  57. int client_flags, MySQL *conn) {
  58. SetCachedImpl("mysql::persistent_conns", host, port, socket,
  59. username, password, client_flags, conn);
  60. }
  61. /**
  62. * If connection object is not provided, a default connection will be used.
  63. */
  64. static MySQL *GetDefaultConn();
  65. static void SetDefaultConn(MySQL *conn);
  66. private:
  67. static int s_default_port;
  68. static String GetHash(CStrRef host, int port, CStrRef socket,
  69. CStrRef username, CStrRef password, int client_flags);
  70. static MySQL *GetCachedImpl(const char *name, CStrRef host, int port,
  71. CStrRef socket, CStrRef username,
  72. CStrRef password, int client_flags);
  73. static void SetCachedImpl(const char *name, CStrRef host, int port,
  74. CStrRef socket, CStrRef username, CStrRef password,
  75. int client_flags, MySQL *conn);
  76. public:
  77. MySQL(const char *host, int port, const char *username,
  78. const char *password, const char *database);
  79. ~MySQL();
  80. void setLastError(const char *func);
  81. void close();
  82. static StaticString s_class_name;
  83. // overriding ResourceData
  84. virtual CStrRef o_getClassNameHook() const { return s_class_name; }
  85. virtual bool isResource() const { return m_conn != NULL;}
  86. bool connect(CStrRef host, int port, CStrRef socket, CStrRef username,
  87. CStrRef password, CStrRef database, int client_flags,
  88. int connect_timeout);
  89. #ifdef FACEBOOK
  90. bool async_connect(CStrRef host, int port, CStrRef socket, CStrRef username,
  91. CStrRef password, CStrRef database);
  92. #endif
  93. bool reconnect(CStrRef host, int port, CStrRef socket, CStrRef username,
  94. CStrRef password, CStrRef database, int client_flags,
  95. int connect_timeout);
  96. MYSQL *get() { return m_conn;}
  97. private:
  98. MYSQL *m_conn;
  99. public:
  100. std::string m_host;
  101. int m_port;
  102. std::string m_username;
  103. std::string m_password;
  104. std::string m_database;
  105. bool m_last_error_set;
  106. int m_last_errno;
  107. std::string m_last_error;
  108. int m_xaction_count;
  109. bool m_multi_query;
  110. };
  111. ///////////////////////////////////////////////////////////////////////////////
  112. class MySQLFieldInfo {
  113. public:
  114. MySQLFieldInfo()
  115. : name(NULL), table(NULL), def(NULL),
  116. max_length(0), length(0), type(0), flags(0) {}
  117. Variant *name;
  118. Variant *table;
  119. Variant *def;
  120. int64_t max_length;
  121. int64_t length;
  122. int type;
  123. unsigned int flags;
  124. };
  125. class MySQLResult : public SweepableResourceData {
  126. public:
  127. DECLARE_OBJECT_ALLOCATION(MySQLResult);
  128. MySQLResult(MYSQL_RES *res, bool localized = false);
  129. virtual ~MySQLResult();
  130. static StaticString s_class_name;
  131. // overriding ResourceData
  132. virtual CStrRef o_getClassNameHook() const { return s_class_name; }
  133. void close() {
  134. if (m_res) {
  135. mysql_free_result(m_res);
  136. m_res = NULL;
  137. }
  138. }
  139. MYSQL_RES *get() {
  140. return m_res;
  141. }
  142. bool isLocalized() {
  143. return m_localized;
  144. }
  145. void addRow();
  146. void addField(Variant *value);
  147. void setFieldCount(int64_t fields);
  148. void setFieldInfo(int64_t f, MYSQL_FIELD *field);
  149. MySQLFieldInfo *getFieldInfo(int64_t field);
  150. /**
  151. * Gets the field content. Only for localized result.
  152. */
  153. Variant getField(int64_t field) const;
  154. int64_t getFieldCount() const;
  155. int64_t getRowCount() const;
  156. bool seekRow(int64_t row);
  157. bool fetchRow();
  158. bool isRowReady() const {
  159. return m_row_ready;
  160. }
  161. bool seekField(int64_t field);
  162. MySQLFieldInfo *fetchFieldInfo();
  163. void setAsyncConnection(MySQL* conn) {
  164. m_conn = conn;
  165. m_conn->incRefCount();
  166. }
  167. protected:
  168. MYSQL_RES *m_res;
  169. MYSQL_ROW m_current_async_row;
  170. bool m_localized; // whether all the rows have been localized
  171. MySQLFieldInfo *m_fields;
  172. std::list<std::vector<Variant *> > *m_rows;
  173. std::list<std::vector<Variant *> >::const_iterator m_current_row;
  174. int64_t m_current_field;
  175. bool m_row_ready; // set to false after seekRow, true after fetchRow
  176. int64_t m_field_count;
  177. int64_t m_row_count;
  178. MySQL* m_conn; // only set for async for refcounting underlying buffers
  179. };
  180. ///////////////////////////////////////////////////////////////////////////////
  181. // connection functions
  182. Variant f_mysql_connect(CStrRef server = null_string,
  183. CStrRef username = null_string,
  184. CStrRef password = null_string,
  185. bool new_link = false,
  186. int client_flags = 0,
  187. int connect_timeout_ms = -1,
  188. int query_timeout_ms = -1);
  189. Variant f_mysql_pconnect(CStrRef server = null_string,
  190. CStrRef username = null_string,
  191. CStrRef password = null_string,
  192. int client_flags = 0,
  193. int connect_timeout_ms = -1,
  194. int query_timeout_ms = -1);
  195. Variant f_mysql_connect_with_db(CStrRef server = null_string,
  196. CStrRef username = null_string,
  197. CStrRef password = null_string,
  198. CStrRef database = null_string,
  199. bool new_link = false,
  200. int client_flags = 0,
  201. int connect_timeout_ms = -1,
  202. int query_timeout_ms = -1);
  203. Variant f_mysql_pconnect_with_db(CStrRef server = null_string,
  204. CStrRef username = null_string,
  205. CStrRef password = null_string,
  206. CStrRef database = null_string,
  207. int client_flags = 0,
  208. int connect_timeout_ms = -1,
  209. int query_timeout_ms = -1);
  210. Variant f_mysql_async_connect_start(CStrRef server = null_string,
  211. CStrRef username = null_string,
  212. CStrRef password = null_string,
  213. CStrRef database = null_string);
  214. bool f_mysql_async_connect_completed(CVarRef link_identifier);
  215. bool f_mysql_async_query_start(CStrRef query, CVarRef link_identifier);
  216. Variant f_mysql_async_query_result(CVarRef link_identifier);
  217. bool f_mysql_async_query_completed(CVarRef result);
  218. Variant f_mysql_async_fetch_array(CVarRef result, int result_type = 1);
  219. Variant f_mysql_async_wait_actionable(CVarRef items, double timeout);
  220. int64_t f_mysql_async_status(CVarRef link_identifier);
  221. String f_mysql_escape_string(CStrRef unescaped_string);
  222. Variant f_mysql_real_escape_string(CStrRef unescaped_string,
  223. CVarRef link_identifier = uninit_null());
  224. String f_mysql_get_client_info();
  225. Variant f_mysql_set_charset(CStrRef charset,
  226. CVarRef link_identifier = uninit_null());
  227. Variant f_mysql_ping(CVarRef link_identifier = uninit_null());
  228. Variant f_mysql_client_encoding(CVarRef link_identifier = uninit_null());
  229. Variant f_mysql_close(CVarRef link_identifier = uninit_null());
  230. Variant f_mysql_errno(CVarRef link_identifier = uninit_null());
  231. Variant f_mysql_error(CVarRef link_identifier = uninit_null());
  232. Variant f_mysql_warning_count(CVarRef link_identifier = uninit_null());
  233. Variant f_mysql_get_host_info(CVarRef link_identifier = uninit_null());
  234. Variant f_mysql_get_proto_info(CVarRef link_identifier = uninit_null());
  235. Variant f_mysql_get_server_info(CVarRef link_identifier = uninit_null());
  236. Variant f_mysql_info(CVarRef link_identifier = uninit_null());
  237. Variant f_mysql_insert_id(CVarRef link_identifier = uninit_null());
  238. Variant f_mysql_stat(CVarRef link_identifier = uninit_null());
  239. Variant f_mysql_thread_id(CVarRef link_identifier = uninit_null());
  240. Variant f_mysql_create_db(CStrRef db,
  241. CVarRef link_identifier = uninit_null());
  242. Variant f_mysql_select_db(CStrRef db,
  243. CVarRef link_identifier = uninit_null());
  244. Variant f_mysql_drop_db(CStrRef db,
  245. CVarRef link_identifier = uninit_null());
  246. Variant f_mysql_affected_rows(CVarRef link_identifier = uninit_null());
  247. ///////////////////////////////////////////////////////////////////////////////
  248. // query functions
  249. Variant mysql_makevalue(CStrRef data, MYSQL_FIELD *mysql_field);
  250. bool f_mysql_set_timeout(int query_timeout_ms = -1,
  251. CVarRef link_identifier = uninit_null());
  252. Variant f_mysql_query(CStrRef query, CVarRef link_identifier = uninit_null());
  253. Variant f_mysql_multi_query(CStrRef query, CVarRef link_identifier = uninit_null());
  254. bool f_mysql_next_result(CVarRef link_identifier = uninit_null());
  255. bool f_mysql_more_results(CVarRef link_identifier = uninit_null());
  256. Variant f_mysql_fetch_result(CVarRef link_identifier = uninit_null());
  257. Variant f_mysql_unbuffered_query(CStrRef query,
  258. CVarRef link_identifier = uninit_null());
  259. Variant f_mysql_db_query(CStrRef database, CStrRef query,
  260. CVarRef link_identifier = uninit_null());
  261. Variant f_mysql_list_dbs(CVarRef link_identifier = uninit_null());
  262. Variant f_mysql_list_tables(CStrRef database,
  263. CVarRef link_identifier = uninit_null());
  264. Variant f_mysql_list_fields(CStrRef database_name, CStrRef table_name,
  265. CVarRef link_identifier = uninit_null());
  266. Variant f_mysql_list_processes(CVarRef link_identifier = uninit_null());
  267. ///////////////////////////////////////////////////////////////////////////////
  268. // row operations
  269. bool f_mysql_data_seek(CVarRef result, int row);
  270. Variant f_mysql_fetch_row(CVarRef result);
  271. Variant f_mysql_fetch_assoc(CVarRef result);
  272. Variant f_mysql_fetch_array(CVarRef result, int result_type = 3);
  273. Variant f_mysql_fetch_lengths(CVarRef result);
  274. Variant f_mysql_fetch_object(CVarRef result, CStrRef class_name = "stdClass",
  275. CArrRef params = uninit_null());
  276. Variant f_mysql_result(CVarRef result, int row, CVarRef field = null_variant);
  277. ///////////////////////////////////////////////////////////////////////////////
  278. // result functions
  279. Variant f_mysql_db_name(CVarRef result, int row,
  280. CVarRef field = null_variant);
  281. Variant f_mysql_tablename(CVarRef result, int i);
  282. Variant f_mysql_num_fields(CVarRef result);
  283. Variant f_mysql_num_rows(CVarRef result);
  284. Variant f_mysql_free_result(CVarRef result);
  285. ///////////////////////////////////////////////////////////////////////////////
  286. // field info
  287. Variant f_mysql_fetch_field(CVarRef result, int field = -1);
  288. bool f_mysql_field_seek(CVarRef result, int field = 0);
  289. Variant f_mysql_field_name(CVarRef result, int field = 0);
  290. Variant f_mysql_field_table(CVarRef result, int field = 0);
  291. Variant f_mysql_field_len(CVarRef result, int field = 0);
  292. Variant f_mysql_field_type(CVarRef result, int field = 0);
  293. Variant f_mysql_field_flags(CVarRef result, int field = 0);
  294. ///////////////////////////////////////////////////////////////////////////////
  295. extern const int64_t k_ASYNC_OP_INVALID;
  296. extern const int64_t k_ASYNC_OP_UNSET;
  297. extern const int64_t k_ASYNC_OP_CONNECT;
  298. extern const int64_t k_ASYNC_OP_QUERY;
  299. extern const int64_t k_ASYNC_OP_FETCH_ROW;
  300. }
  301. #endif // incl_HPHP_EXT_MYSQL_H_