crud.h 6.3 KB

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