session.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License, version 2.0, as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is also distributed with certain software (including
  9. * but not limited to OpenSSL) that is licensed under separate terms,
  10. * as designated in a particular file or component or in included license
  11. * documentation. The authors of MySQL hereby grant you an
  12. * additional permission to link the program and your derivative works
  13. * with the separately licensed software that they have included with
  14. * MySQL.
  15. *
  16. * Without limiting anything contained in the foregoing, this file,
  17. * which is part of MySQL Connector/C++, is also subject to the
  18. * Universal FOSS Exception, version 1.0, a copy of which can be found at
  19. * http://oss.oracle.com/licenses/universal-foss-exception.
  20. *
  21. * This program is distributed in the hope that it will be useful, but
  22. * WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  24. * See the GNU General Public License, version 2.0, for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software Foundation, Inc.,
  28. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  29. */
  30. #ifndef MYSQLX_DETAIL_SESSION_H
  31. #define MYSQLX_DETAIL_SESSION_H
  32. #include "../common.h"
  33. #include "../crud.h"
  34. #include <set>
  35. namespace cdk {
  36. class Session;
  37. }
  38. namespace mysqlx {
  39. class Value;
  40. class Session;
  41. class Schema;
  42. class Table;
  43. class Collection;
  44. namespace common {
  45. class Session_impl;
  46. class Session_pool;
  47. using Shared_session_pool = std::shared_ptr<Session_pool>;
  48. class Result_init;
  49. }
  50. namespace internal {
  51. class Schema_detail;
  52. using Client_impl = common::Session_pool;
  53. using Shared_client_impl = std::shared_ptr<Client_impl>;
  54. using Session_impl = common::Session_impl;
  55. using Shared_session_impl = std::shared_ptr<common::Session_impl>;
  56. /*
  57. Base class for database objects. Can't be used alone.
  58. */
  59. class PUBLIC_API Db_obj_base
  60. {
  61. protected:
  62. DLL_WARNINGS_PUSH
  63. Shared_session_impl m_sess;
  64. string m_name;
  65. DLL_WARNINGS_POP
  66. Db_obj_base(const Shared_session_impl& sess, const string& name)
  67. : m_sess(sess), m_name(name)
  68. {}
  69. virtual ~Db_obj_base()
  70. {}
  71. };
  72. class PUBLIC_API Collection_detail
  73. : public Db_obj_base
  74. {
  75. protected:
  76. Collection_detail(const Shared_session_impl &sess, const string &name)
  77. : Db_obj_base(sess, name)
  78. {}
  79. virtual Schema_detail& get_schema() = 0;
  80. Result add_or_replace_one(const string &id, Value&&, bool);
  81. void index_drop(const string &name);
  82. void index_create(const string &name, Value &&spec);
  83. };
  84. // ---------------------------------------------------------------------------
  85. /*
  86. Base class for classes to be used by common::List_source<> which get item
  87. from query results.
  88. It assumes that the first column in the results contains string
  89. data. An instance of this class iterates over the string data in the
  90. result until all rows are consumed.
  91. Derived class must send the query to the server and set m_res member to point
  92. at the result of this query.
  93. */
  94. struct PUBLIC_API Query_src
  95. {
  96. using Value = string;
  97. struct Res_impl;
  98. Res_impl *m_res = nullptr;
  99. const void *m_row = nullptr;
  100. public:
  101. Query_src()
  102. {}
  103. Query_src(Query_src &&other)
  104. : m_res(other.m_res)
  105. {
  106. // Note: only one instance of Query_src owns m_res.
  107. other.m_res = nullptr;
  108. }
  109. Query_src(const Query_src&) = delete;
  110. virtual ~Query_src();
  111. virtual void iterator_start()
  112. {
  113. assert(m_res);
  114. }
  115. bool iterator_next();
  116. string iterator_get();
  117. };
  118. // ---------------------------------------------------------------------------
  119. class PUBLIC_API Schema_detail
  120. : public Db_obj_base
  121. {
  122. protected:
  123. enum Obj_type { COLLECTION, TABLE };
  124. /*
  125. Sources for lists of schema objects and their names.
  126. When constructing a source, a SQL style patter on object names is
  127. given as ctor parameter -- only object matching the pattern are listed.
  128. Name_src accepts a parameter which tells whether names of tables or
  129. collections should be listed.
  130. */
  131. struct PUBLIC_API Name_src
  132. : public Query_src
  133. {
  134. const Schema &m_schema;
  135. Name_src(const Schema&, Obj_type, const string &pattern);
  136. };
  137. struct PUBLIC_API Collection_src
  138. : Name_src
  139. {
  140. using Value = Collection;
  141. Collection_src(const Schema &sch, const string &pattern)
  142. : Name_src(sch, COLLECTION, pattern)
  143. {}
  144. using Name_src::iterator_start;
  145. using Name_src::iterator_next;
  146. Collection iterator_get();
  147. };
  148. struct PUBLIC_API Table_src
  149. : Name_src
  150. {
  151. using Value = Table;
  152. Table_src(const Schema &sch, const string &pattern)
  153. : Name_src(sch, TABLE, pattern)
  154. {}
  155. using Name_src::iterator_start;
  156. using Name_src::iterator_next;
  157. Table iterator_get();
  158. };
  159. Schema_detail(const Shared_session_impl &sess, const string &name)
  160. : Db_obj_base(sess, name)
  161. {}
  162. public:
  163. using CollectionList = List_initializer<List_source<Collection_src>>;
  164. using TableList = List_initializer<List_source<Table_src>>;
  165. using StringList = List_initializer<List_source<Name_src>>;
  166. protected:
  167. void create_collection(const string &name, bool reuse);
  168. void drop_collection(const string &name);
  169. friend Collection_detail;
  170. struct Access;
  171. friend Access;
  172. };
  173. /*
  174. Class representing an SQL statement that can be executed on the server.
  175. */
  176. struct SQL_statement;
  177. using SQL_statement_cmd = Executable<SqlResult, SQL_statement>;
  178. struct SQL_statement
  179. : public Bind_placeholders< SQL_statement_cmd >
  180. {
  181. SQL_statement(Session *sess, const string &query)
  182. {
  183. assert(sess);
  184. try {
  185. reset(internal::Crud_factory::mk_sql(*sess, query));
  186. }
  187. CATCH_AND_WRAP
  188. }
  189. SQL_statement(SQL_statement_cmd &other)
  190. {
  191. SQL_statement_cmd::operator=(other);
  192. }
  193. SQL_statement(SQL_statement_cmd &&other)
  194. {
  195. SQL_statement_cmd::operator=(std::move(other));
  196. }
  197. };
  198. struct Session_detail;
  199. struct PUBLIC_API Client_detail
  200. {
  201. // Disable copy semantics for client class.
  202. Client_detail(const Client_detail&) = delete;
  203. Client_detail& operator=(const Client_detail&) = delete;
  204. Client_detail(common::Settings_impl &settings);
  205. // Client_detail(common::Settings_impl &&settings);
  206. void close();
  207. protected:
  208. Client_detail(Client_detail && other)
  209. {
  210. m_impl = other.m_impl;
  211. other.m_impl.reset();
  212. }
  213. common::Shared_session_pool& get_session_pool();
  214. struct INTERNAL Impl;
  215. DLL_WARNINGS_PUSH
  216. Shared_client_impl m_impl = NULL;
  217. DLL_WARNINGS_POP
  218. friend Session;
  219. };
  220. struct PUBLIC_API Session_detail
  221. {
  222. // Disable copy semantics for session class.
  223. Session_detail(const Session_detail&) = delete;
  224. Session_detail& operator=(const Session_detail&) = delete;
  225. /*
  226. Sources for lists of schemata and schema names. Only schemata matching
  227. the given SQL-style pattern are listed.
  228. */
  229. struct PUBLIC_API Name_src
  230. : public Query_src
  231. {
  232. const Session &m_sess;
  233. Name_src(const Session&, const string &pattern);
  234. };
  235. struct PUBLIC_API Schema_src
  236. : Name_src
  237. {
  238. using Value = Schema;
  239. Schema_src(Session &sess, const string &pattern)
  240. : Name_src(sess, pattern)
  241. {}
  242. Schema_src(Session &sess)
  243. : Schema_src(sess, L"%")
  244. {}
  245. using Name_src::iterator_start;
  246. using Name_src::iterator_next;
  247. Schema iterator_get();
  248. };
  249. public:
  250. using SchemaList = List_initializer<List_source<Schema_src>>;
  251. protected:
  252. Session_detail(Session_detail && other)
  253. {
  254. m_impl = other.m_impl;
  255. other.m_impl.reset();
  256. }
  257. struct INTERNAL Impl;
  258. /*
  259. Note: Session implementation is shared with result objects because it
  260. must exists as long as result implementation exists. This means that
  261. even when session object is deleted, its implementation can still hang
  262. around.
  263. */
  264. DLL_WARNINGS_PUSH
  265. Shared_session_impl m_impl = NULL;
  266. DLL_WARNINGS_POP
  267. Session_detail(common::Settings_impl&);
  268. Session_detail(common::Shared_session_pool&);
  269. virtual ~Session_detail()
  270. {
  271. try {
  272. if (m_impl)
  273. close();
  274. }
  275. catch (...) {}
  276. }
  277. void create_schema(const string &name, bool reuse);
  278. void drop_schema(const string &name);
  279. const std::wstring& get_default_schema_name();
  280. void start_transaction();
  281. void commit();
  282. void rollback(const string &sp = string());
  283. string savepoint_set(const string &sp = string());
  284. void savepoint_remove(const string&);
  285. common::Session_impl& get_impl()
  286. {
  287. if (!m_impl)
  288. THROW("Invalid session");
  289. return *m_impl;
  290. }
  291. INTERNAL cdk::Session& get_cdk_session();
  292. void close();
  293. /*
  294. Do necessary cleanups before sending new command to the server.
  295. */
  296. void prepare_for_cmd();
  297. public:
  298. /// @cond IGNORED
  299. friend Result_detail::Impl;
  300. friend internal::Crud_factory;
  301. /// @endcond
  302. };
  303. } // internal namespace
  304. } // mysqlx namespace
  305. #endif