op_if.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * Copyright (c) 2015, 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_COMMON_OP_IF_H
  31. #define MYSQLX_COMMON_OP_IF_H
  32. /*
  33. This file defines a hierarchy of abstract interfaces for objects that
  34. represent database operations. The base interface is the Executable_if for
  35. any operation that can be executed. Other interfaces in the hierarchy allow
  36. specifying more details of the operation.
  37. Note: Header op_impl.h defines implementations of these interfaces used
  38. in the connector.
  39. */
  40. #include "../common_constants.h"
  41. #include <string>
  42. namespace mysqlx {
  43. namespace common {
  44. class Value;
  45. #define LOCK_MODE(X,N) X = N,
  46. enum Lock_mode
  47. {
  48. LOCK_MODE_LIST(LOCK_MODE)
  49. };
  50. #define LOCK_CONTENTION(X,N) X = N,
  51. enum class Lock_contention
  52. {
  53. LOCK_CONTENTION_LIST(LOCK_CONTENTION)
  54. };
  55. class Result_init;
  56. /*
  57. Abstract interface for internal implementations of an executable object.
  58. The execute() method returns a Result_init object which can be
  59. used to construct a result instance.
  60. Implementation of an executable object holds a description of the operation
  61. that should be executed. Executable objects can be copied (for example
  62. by copy assignment operation) and in this case a new copy of the current
  63. description of the operation should be created by clone() method. After
  64. cloning, the 2 executable implementations can be modified and executed
  65. independently.
  66. See various Op_xxx classes defined for example in operation.h to see examples
  67. of executable object implementations. Note that these Op_xxx classes do
  68. not directly inherit from Executable_if. Instead they use a whole hierarchy
  69. of implementation classes based on Executable_if. But in the end, each
  70. implementation of an executable object defines the execute() method that
  71. executes given operation using all the information collected using other
  72. methods of the implementation class.
  73. */
  74. struct Executable_if
  75. {
  76. /*
  77. Execute the operation and return reference to object which implements
  78. Result_init interface. Such object is then used to construct a result
  79. instance.
  80. */
  81. virtual Result_init& execute() = 0;
  82. virtual Executable_if *clone() const = 0;
  83. virtual ~Executable_if() {}
  84. };
  85. /*
  86. The XXX_if classes defined below form a hierarchy of interfaces, based
  87. on Executable_if, for internal implementations of various crud operations.
  88. The concrete implementations, like Op_collection_find defined in
  89. operation.h, implements one of the top interfaces in this hierarchy but
  90. the hierarchy allows casting the implementation down to the layer
  91. implementing particular aspect of the operation. For example
  92. Limit_if interface allows setting limit and offset for returned/affected
  93. rows/documents, which is common for different CRUD operations.
  94. */
  95. struct Bind_if : public Executable_if
  96. {
  97. using string = std::wstring;
  98. using Value = mysqlx::common::Value;
  99. // Add value for named parameter
  100. virtual void add_param(const string&, const Value&) = 0;
  101. // Add value for positional parameter
  102. virtual void add_param(Value) = 0;
  103. virtual void clear_params() = 0;
  104. };
  105. struct Limit_if : public Bind_if
  106. {
  107. virtual void set_offset(unsigned) = 0;
  108. virtual void clear_offset() = 0;
  109. virtual void set_limit(unsigned) = 0;
  110. virtual void clear_limit() = 0;
  111. };
  112. struct Sort_if : public Limit_if
  113. {
  114. using string = std::wstring;
  115. enum direction_t { ASC, DESC };
  116. virtual void add_sort(const string &expr, direction_t dir) = 0;
  117. virtual void add_sort(const string&) = 0;
  118. virtual void clear_sort() = 0;
  119. };
  120. struct Having_if : public Sort_if
  121. {
  122. using string = std::wstring;
  123. virtual void set_having(const string&) = 0;
  124. virtual void clear_having() = 0;
  125. };
  126. struct Group_by_if : public Having_if
  127. {
  128. using string = std::wstring;
  129. virtual void add_group_by(const string&) = 0;
  130. virtual void clear_group_by() = 0;
  131. };
  132. struct Proj_if : public Group_by_if
  133. {
  134. using string = std::wstring;
  135. /*
  136. Add projection specification for a table query. It is an expression with
  137. optional "AS <alias>" suffix.
  138. */
  139. virtual void add_proj(const string&) = 0;
  140. /*
  141. Set projection for a document query. It is a JSON-like string but document
  142. field values are interpreted as expressions.
  143. */
  144. virtual void set_proj(const string&) = 0;
  145. virtual void clear_proj() = 0;
  146. };
  147. template <class Base>
  148. struct Select_if : public Base
  149. {
  150. using string = std::wstring;
  151. using Lock_mode = mysqlx::common::Lock_mode;
  152. // Set expression to select rows/documents.
  153. virtual void set_where(const string&) = 0;
  154. // Define lock mode for rows/documents returned by the query.
  155. virtual void set_lock_mode(Lock_mode, Lock_contention) = 0;
  156. virtual void clear_lock_mode() = 0;
  157. };
  158. // --------------------------------------------------------------------------
  159. struct Collection_find_if : public Select_if<Proj_if>
  160. {};
  161. /*
  162. Interface to internal implementations of CRUD add operation.
  163. */
  164. struct Collection_add_if : public Executable_if
  165. {
  166. /*
  167. Note: Current implementation only supports sending
  168. documents in form of UTF8 JSON strings.
  169. */
  170. virtual void add_json(const std::string&) = 0;
  171. virtual void clear_docs() = 0;
  172. };
  173. struct Collection_remove_if : public Select_if<Sort_if>
  174. {};
  175. /*
  176. Interface to internal implementations of CRUD modify operation.
  177. Methods `add_operation` are used to pass to the implementation object
  178. the modifications requested by the user.
  179. */
  180. struct Collection_modify_if : public Select_if<Sort_if>
  181. {
  182. using string = std::wstring;
  183. using Value = mysqlx::common::Value;
  184. enum Operation
  185. {
  186. SET,
  187. UNSET,
  188. ARRAY_INSERT,
  189. ARRAY_APPEND,
  190. ARRAY_DELETE,
  191. MERGE_PATCH
  192. };
  193. virtual void add_operation(Operation, const string&, const Value&) = 0;
  194. virtual void add_operation(Operation, const string&) = 0;
  195. virtual void clear_modifications() = 0;
  196. };
  197. // --------------------------------------------------------------------------
  198. /*
  199. Interface to be implemented by internal implementations of
  200. table insert operation.
  201. */
  202. template <class Row_impl>
  203. struct Table_insert_if : public Executable_if
  204. {
  205. using string = std::wstring;
  206. /*
  207. Pass to the implementation names of columns specified by
  208. the user. Columns are passed one-by-one in the order in
  209. which they were specified.
  210. */
  211. virtual void add_column(const string&) = 0;
  212. virtual void clear_columns() = 0;
  213. /*
  214. Pass to the implementation a row that should be inserted
  215. into the table. Several rows can be passed.
  216. TODO: use move semantics instead
  217. */
  218. virtual void add_row(const Row_impl&) = 0;
  219. virtual void clear_rows() = 0;
  220. };
  221. /*
  222. Interface to be implemented by internal implementations
  223. of table CRUD select operation.
  224. Method `add_where` is used to report selection criteria
  225. to the implementation.
  226. */
  227. struct Table_select_if : public Select_if<Proj_if>
  228. {};
  229. /*
  230. Interface to be implemented by internal implementations
  231. of table CRUD remove operation.
  232. Selection criteria which selects rows to be removed is
  233. passed to the implementation using `set_where` method.
  234. Note: setting where condition to empty string removes it.
  235. */
  236. struct Table_remove_if : public Select_if<Sort_if>
  237. {};
  238. /*
  239. Interface to be implemented by internal implementations of
  240. table CRUD update operation. Such update operation sets values
  241. of fields in a row. Name of the column that should be set and
  242. expression defining new value are reported to the implementation
  243. using method `add_set`.
  244. */
  245. struct Table_update_if : public Table_remove_if
  246. {
  247. using string = std::wstring;
  248. using Value = mysqlx::common::Value;
  249. virtual void add_set(const string&, const Value&) = 0;
  250. virtual void clear_modifications() = 0;
  251. };
  252. } // internal
  253. } // mysqlx
  254. #endif