PageRenderTime 38ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/hphp/runtime/ext/async_mysql/ext_async_mysql.h

https://gitlab.com/Manu343726/hhvm
C Header | 335 lines | 224 code | 58 blank | 53 comment | 0 complexity | 8e8b324d366b5c59ad5e1e40e84e3f15 MD5 | raw file
  1. /*
  2. +----------------------------------------------------------------------+
  3. | HipHop for PHP |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2010- Facebook, Inc. (http://www.facebook.com) |
  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. */
  16. #ifndef incl_EXT_ASYNC_MYSQL_H_
  17. #define incl_EXT_ASYNC_MYSQL_H_
  18. #include <algorithm>
  19. #include <memory>
  20. #include <squangle/mysql_client/AsyncMysqlClient.h>
  21. #include <squangle/mysql_client/AsyncConnectionPool.h>
  22. #include <squangle/mysql_client/Row.h>
  23. #include "hphp/runtime/ext/asio/asio_external_thread_event.h"
  24. #include "hphp/runtime/ext/ext_collections.h"
  25. #include "hphp/runtime/ext/extension.h"
  26. #include <folly/Format.h>
  27. using folly::StringPiece;
  28. namespace HPHP {
  29. ///////////////////////////////////////////////////////////////////////////////
  30. namespace am = facebook::common::mysql_client;
  31. extern const int64_t k_NOT_NULL_FLAG;
  32. extern const int64_t k_PRI_KEY_FLAG;
  33. extern const int64_t k_UNIQUE_KEY_FLAG;
  34. extern const int64_t k_MULTIPLE_KEY_FLAG;
  35. extern const int64_t k_UNSIGNED_FLAG;
  36. extern const int64_t k_ZEROFILL_FLAG;
  37. extern const int64_t k_BINARY_FLAG;
  38. extern const int64_t k_AUTO_INCREMENT_FLAG;
  39. extern const int64_t k_ENUM_FLAG;
  40. extern const int64_t k_SET_FLAG;
  41. extern const int64_t k_BLOB_FLAG;
  42. extern const int64_t k_TIMESTAMP_FLAG;
  43. extern const int64_t k_NUM_FLAG;
  44. extern const int64_t k_NO_DEFAULT_VALUE_FLAG;
  45. extern const int64_t k_MYSQL_TYPE_TINY;
  46. extern const int64_t k_MYSQL_TYPE_SHORT;
  47. extern const int64_t k_MYSQL_TYPE_LONG;
  48. extern const int64_t k_MYSQL_TYPE_INT24;
  49. extern const int64_t k_MYSQL_TYPE_LONGLONG;
  50. extern const int64_t k_MYSQL_TYPE_DECIMAL;
  51. extern const int64_t k_MYSQL_TYPE_NEWDECIMAL;
  52. extern const int64_t k_MYSQL_TYPE_FLOAT;
  53. extern const int64_t k_MYSQL_TYPE_DOUBLE;
  54. extern const int64_t k_MYSQL_TYPE_BIT;
  55. extern const int64_t k_MYSQL_TYPE_TIMESTAMP;
  56. extern const int64_t k_MYSQL_TYPE_DATE;
  57. extern const int64_t k_MYSQL_TYPE_TIME;
  58. extern const int64_t k_MYSQL_TYPE_DATETIME;
  59. extern const int64_t k_MYSQL_TYPE_YEAR;
  60. extern const int64_t k_MYSQL_TYPE_STRING;
  61. extern const int64_t k_MYSQL_TYPE_VAR_STRING;
  62. extern const int64_t k_MYSQL_TYPE_BLOB;
  63. extern const int64_t k_MYSQL_TYPE_SET;
  64. extern const int64_t k_MYSQL_TYPE_ENUM;
  65. extern const int64_t k_MYSQL_TYPE_GEOMETRY;
  66. extern const int64_t k_MYSQL_TYPE_NULL;
  67. ///////////////////////////////////////////////////////////////////////////////
  68. // class AsyncMysqlConnectionPool
  69. class AsyncMysqlConnectionPool {
  70. public:
  71. AsyncMysqlConnectionPool& operator=(const AsyncMysqlConnectionPool&) = delete;
  72. std::shared_ptr<am::AsyncConnectionPool> m_async_pool;
  73. static const StaticString s_className;
  74. };
  75. ///////////////////////////////////////////////////////////////////////////////
  76. // class AsyncMysqlConnection
  77. class AsyncMysqlConnection {
  78. public:
  79. AsyncMysqlConnection();
  80. AsyncMysqlConnection& operator=(const AsyncMysqlConnection&) = delete;
  81. void sweep();
  82. void setConnection(std::unique_ptr<am::Connection> conn);
  83. void verifyValidConnection();
  84. static Class* getClass();
  85. static ObjectData* newInstance(std::unique_ptr<am::Connection> conn);
  86. Object query(ObjectData* this_, am::Query query, int64_t timeout_micros = -1);
  87. std::unique_ptr<am::Connection> m_conn;
  88. String m_host;
  89. int m_port;
  90. bool m_closed;
  91. static Class* s_class;
  92. static const StaticString s_className;
  93. };
  94. ///////////////////////////////////////////////////////////////////////////////
  95. // class AsyncMysqlResult
  96. class AsyncMysqlResult {
  97. public:
  98. virtual ~AsyncMysqlResult() {}
  99. int64_t elapsedMicros();
  100. double startTime();
  101. double endTime();
  102. virtual am::Operation* op() = 0;
  103. };
  104. ///////////////////////////////////////////////////////////////////////////////
  105. // class AsyncMysqlErrorResult
  106. class AsyncMysqlErrorResult : public AsyncMysqlResult {
  107. public:
  108. virtual ~AsyncMysqlErrorResult() {}
  109. AsyncMysqlErrorResult& operator=(const AsyncMysqlErrorResult&) = delete;
  110. void create(std::shared_ptr<am::Operation> op);
  111. void sweep();
  112. virtual am::Operation* op();
  113. static Class* getClass();
  114. static ObjectData* newInstance(std::shared_ptr<am::Operation> op);
  115. std::shared_ptr<am::Operation> m_op;
  116. static Class* s_class;
  117. static const StaticString s_className;
  118. };
  119. ///////////////////////////////////////////////////////////////////////////////
  120. // class AsyncMysqlQueryErrorResult
  121. class AsyncMysqlQueryErrorResult {
  122. public:
  123. AsyncMysqlQueryErrorResult();
  124. AsyncMysqlQueryErrorResult& operator=(const AsyncMysqlQueryErrorResult&)
  125. = delete;
  126. void sweep();
  127. void create(std::shared_ptr<am::Operation> op, SmartPtr<c_Vector> results);
  128. static Class* getClass();
  129. static ObjectData* newInstance(std::shared_ptr<am::Operation> op,
  130. SmartPtr<c_Vector> results);
  131. SmartPtr<c_Vector> m_query_results;
  132. static Class* s_class;
  133. static const StaticString s_className;
  134. // extends AsyncMysqlErrorResult
  135. //
  136. // The address of native data is computed using the size of the native data
  137. // as an offset from the object data. To ensure that the offset to the parent
  138. // class remains the same, we simulate inheritance by placing the parent
  139. // class below the object instead of using c++ inheritance.
  140. AsyncMysqlErrorResult m_parent;
  141. };
  142. ///////////////////////////////////////////////////////////////////////////////
  143. // This class is shared across the QueryResult and RowBlocks for faster
  144. // indexing of row columns.
  145. // By using HPHP::String instead of other string types, we are able
  146. // to build maps where we just reuse them so we avoid copying.
  147. // Thus, eliminating repetition of map keys and also gain speed.
  148. class FieldIndex {
  149. public:
  150. explicit FieldIndex(const am::RowFields* row_fields);
  151. size_t getFieldIndex(String field_name) const;
  152. String getFieldString(size_t field_index) const;
  153. private:
  154. smart::vector<String> field_names_;
  155. smart::hash_map<
  156. String,
  157. size_t,
  158. hphp_string_hash,
  159. hphp_string_same> field_name_map_;
  160. };
  161. ///////////////////////////////////////////////////////////////////////////////
  162. // class AsyncMysqlQueryResult
  163. class AsyncMysqlQueryResult : public AsyncMysqlResult {
  164. public:
  165. virtual ~AsyncMysqlQueryResult() {}
  166. AsyncMysqlQueryResult& operator=(const AsyncMysqlQueryResult&) = delete;
  167. void sweep();
  168. void create(std::shared_ptr<am::Operation> op, am::QueryResult query_result);
  169. Object buildRows(bool as_maps, bool typed_values);
  170. virtual am::Operation* op();
  171. static Class* getClass();
  172. static ObjectData* newInstance(std::shared_ptr<am::Operation> op,
  173. am::QueryResult query_result);
  174. // Holding the operation just for operation elapsed time
  175. std::shared_ptr<am::Operation> m_op;
  176. std::unique_ptr<am::QueryResult> m_query_result;
  177. // Created here for buildRows and passed to RowBlocks
  178. std::shared_ptr<FieldIndex> m_field_index;
  179. static Class* s_class;
  180. static const StaticString s_className;
  181. };
  182. ///////////////////////////////////////////////////////////////////////////////
  183. // Async events
  184. class AsyncMysqlConnectEvent final : public AsioExternalThreadEvent {
  185. public:
  186. explicit AsyncMysqlConnectEvent(std::shared_ptr<am::ConnectOperation> op) {
  187. m_op = op;
  188. }
  189. void opFinished() { markAsFinished(); }
  190. protected:
  191. void unserialize(Cell& result) override final;
  192. private:
  193. std::shared_ptr<am::ConnectOperation> m_op;
  194. };
  195. class AsyncMysqlQueryEvent final : public AsioExternalThreadEvent {
  196. public:
  197. AsyncMysqlQueryEvent(ObjectData* conn,
  198. std::shared_ptr<am::QueryOperation> op)
  199. : AsioExternalThreadEvent(conn) {
  200. m_query_op = std::move(op);
  201. }
  202. void opFinished() { markAsFinished(); }
  203. protected:
  204. void unserialize(Cell& result) override final;
  205. private:
  206. std::shared_ptr<am::QueryOperation> m_query_op;
  207. };
  208. class AsyncMysqlMultiQueryEvent final : public AsioExternalThreadEvent {
  209. public:
  210. AsyncMysqlMultiQueryEvent(ObjectData* conn,
  211. std::shared_ptr<am::MultiQueryOperation> op)
  212. : AsioExternalThreadEvent(conn) {
  213. m_multi_op = op;
  214. }
  215. void opFinished() { markAsFinished(); }
  216. protected:
  217. void unserialize(Cell& result) override final;
  218. private:
  219. std::shared_ptr<am::MultiQueryOperation> m_multi_op;
  220. };
  221. ///////////////////////////////////////////////////////////////////////////////
  222. // class AsyncMysqlRowBlock
  223. class AsyncMysqlRowBlock {
  224. public:
  225. AsyncMysqlRowBlock& operator=(const AsyncMysqlRowBlock&) = delete;
  226. void sweep();
  227. size_t getIndexFromVariant(const Variant& field);
  228. template <typename FieldType>
  229. FieldType getFieldAs(int64_t row, const Variant& field);
  230. static Class* getClass();
  231. static ObjectData* newInstance(am::RowBlock* row_block,
  232. std::shared_ptr<FieldIndex> indexer);
  233. std::unique_ptr<am::RowBlock> m_row_block;
  234. std::shared_ptr<FieldIndex> m_field_index;
  235. static Class* s_class;
  236. static const StaticString s_className;
  237. };
  238. ///////////////////////////////////////////////////////////////////////////////
  239. // class AsyncMysqlRowBlockIterator
  240. class AsyncMysqlRowBlockIterator {
  241. public:
  242. AsyncMysqlRowBlockIterator& operator=(const AsyncMysqlRowBlockIterator&) =
  243. delete;
  244. static Class* getClass();
  245. static ObjectData* newInstance(Object row_block, size_t row_number);
  246. Object m_row_block;
  247. size_t m_row_number;
  248. static Class* s_class;
  249. static const StaticString s_className;
  250. };
  251. ///////////////////////////////////////////////////////////////////////////////
  252. // class AsyncMysqlRow
  253. class AsyncMysqlRow {
  254. public:
  255. AsyncMysqlRow& operator=(const AsyncMysqlRow&) = delete;
  256. static Class* getClass();
  257. static ObjectData* newInstance(Object row_block, size_t row_number);
  258. Object m_row_block;
  259. size_t m_row_number;
  260. static Class* s_class;
  261. static const StaticString s_className;
  262. };
  263. ///////////////////////////////////////////////////////////////////////////////
  264. // class AsyncMysqlRowIterator
  265. class AsyncMysqlRowIterator {
  266. public:
  267. AsyncMysqlRowIterator& operator=(const AsyncMysqlRowIterator&) = delete;
  268. static Class* getClass();
  269. static ObjectData* newInstance(Object row, size_t field_number);
  270. Object m_row;
  271. size_t m_field_number;
  272. static Class* s_class;
  273. static const StaticString s_className;
  274. };
  275. ///////////////////////////////////////////////////////////////////////////////
  276. }
  277. #endif // incl_EXT_ASYNC_MYSQL_H_