crud.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright (c) 2015, 2019, 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_CRUD_H
  31. #define MYSQLX_DETAIL_CRUD_H
  32. /**
  33. @file
  34. Details for public API classes representing CRUD operations.
  35. */
  36. #include "../common.h"
  37. #include "../executable.h"
  38. namespace mysqlx {
  39. MYSQLX_ABI_BEGIN(2,0)
  40. namespace internal {
  41. struct PUBLIC_API Bind_detail
  42. {
  43. protected:
  44. using Impl = common::Bind_if;
  45. using Args_prc = Args_processor<Bind_detail, Impl*>;
  46. static void process_one(Impl *impl, const Value &val)
  47. {
  48. impl->add_param((const common::Value&)val);
  49. }
  50. template <typename... T>
  51. static void add_params(Impl *impl, T&&... vals)
  52. {
  53. Args_prc::process_args(impl, std::forward<T>(vals)...);
  54. }
  55. friend Args_prc;
  56. };
  57. struct PUBLIC_API Sort_detail
  58. {
  59. protected:
  60. using Impl = common::Sort_if;
  61. using Args_prc = Args_processor<Sort_detail, Impl*>;
  62. static void process_one(Impl *impl, const string &ord_spec)
  63. {
  64. impl->add_sort(ord_spec);
  65. }
  66. template <typename... T>
  67. static void add_sort(Impl *impl, T... args)
  68. {
  69. Args_prc::process_args(impl, args...);
  70. }
  71. friend Args_prc;
  72. };
  73. struct PUBLIC_API Group_by_detail
  74. {
  75. protected:
  76. using Impl = common::Group_by_if;
  77. using Args_prc = Args_processor<Group_by_detail, Impl*>;
  78. static void process_one(Impl *impl, const string &spec)
  79. {
  80. impl->add_group_by(spec);
  81. }
  82. template <typename... T>
  83. static void do_group_by(Impl *impl, T... args)
  84. {
  85. Args_prc::process_args(impl, args...);
  86. }
  87. friend Args_prc;
  88. };
  89. struct PUBLIC_API Proj_detail
  90. {
  91. protected:
  92. using Impl = common::Proj_if;
  93. using Args_prc = Args_processor<Proj_detail, Impl*>;
  94. static void process_one(Impl *impl, const string &spec)
  95. {
  96. impl->add_proj(spec);
  97. }
  98. template <typename... T>
  99. static void add_proj(Impl *impl, T... proj_spec)
  100. {
  101. Args_prc::process_args(impl, proj_spec...);
  102. }
  103. friend Args_prc;
  104. };
  105. struct PUBLIC_API Collection_add_detail
  106. {
  107. protected:
  108. using Impl = common::Collection_add_if;
  109. using Args_prc = Args_processor<Collection_add_detail, Impl*>;
  110. static void process_one(Impl *impl, const string &json)
  111. {
  112. impl->add_json(json);
  113. }
  114. static void process_one(Impl *impl, const DbDoc &doc)
  115. {
  116. // TODO: Do it better when we support sending structured
  117. // document descriptions to the server.
  118. std::ostringstream buf;
  119. buf << doc;
  120. // Note: utf8 conversion using mysqlx::string.
  121. impl->add_json(mysqlx::string(buf.str()));
  122. }
  123. template <typename... T>
  124. static void do_add(Impl *impl, T... args)
  125. {
  126. Args_prc::process_args(impl, args...);
  127. }
  128. friend Args_prc;
  129. };
  130. struct PUBLIC_API Collection_find_detail
  131. {
  132. protected:
  133. using Impl = common::Proj_if;
  134. using Args_prc = Args_processor<Collection_find_detail, Impl*>;
  135. static void process_one(Impl *impl, const string &proj)
  136. {
  137. impl->add_proj(proj);
  138. }
  139. static void do_fields(Impl *impl, const Expression &proj)
  140. {
  141. impl->set_proj(proj.get<string>());
  142. }
  143. /*
  144. Note: If e is an expression (of type Expression) then only
  145. .fields(e) is valid - the multi-argument variant .fields(e,...)
  146. should be disabled.
  147. */
  148. template <
  149. typename T, typename... R,
  150. typename std::enable_if<!std::is_same<T, Expression>::value>::type* = nullptr
  151. >
  152. static void do_fields(Impl *impl, T first, R... rest)
  153. {
  154. Args_prc::process_args(impl, first, rest...);
  155. }
  156. friend Args_prc;
  157. };
  158. struct PUBLIC_API Table_insert_detail
  159. {
  160. protected:
  161. using Row_impl = internal::Row_detail::Impl;
  162. using Impl = common::Table_insert_if<Row_impl>;
  163. /*
  164. Helper methods which pass column/row information to the
  165. internal implementation object.
  166. */
  167. struct Add_column
  168. {
  169. static void process_one(Impl *impl, const string &col)
  170. {
  171. impl->add_column(col);
  172. }
  173. };
  174. struct Add_value
  175. {
  176. using Impl = std::pair<Row, unsigned>;
  177. static void process_one(Impl *impl, const mysqlx::Value &val)
  178. {
  179. impl->first.set((impl->second)++, val);
  180. }
  181. };
  182. struct Add_row
  183. {
  184. static void process_one(Impl *impl, const Row &row)
  185. {
  186. impl->add_row(*row.m_impl);
  187. }
  188. };
  189. template <typename... T>
  190. static void add_columns(Impl *impl, T... args)
  191. {
  192. Args_processor<Add_column, Impl*>::process_args(impl, args...);
  193. }
  194. template <typename... T>
  195. static void add_rows(Impl *impl, T... args)
  196. {
  197. Args_processor<Add_row, Impl*>::process_args(impl, args...);
  198. }
  199. template <typename... T>
  200. static void add_values(Impl *impl, T... args)
  201. {
  202. Add_value::Impl row{ {}, 0 };
  203. Args_processor<Add_value>::process_args(&row, args...);
  204. Add_row::process_one(impl, row.first);
  205. }
  206. friend Args_processor<Add_column, Impl*>;
  207. friend Args_processor<Add_row, Impl*>;
  208. friend Args_processor<Add_value, Impl*>;
  209. };
  210. using Table_select_detail = Proj_detail;
  211. } // internal
  212. MYSQLX_ABI_END(2,0)
  213. } // mysqlx
  214. #endif