table_crud.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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_TABLE_CRUD_H
  31. #define MYSQLX_TABLE_CRUD_H
  32. /**
  33. @file
  34. Crud operations on tables.
  35. Classes declared here represent CRUD operations on a table. They are
  36. analogous to collection CRUD operation classes defined in collection_crud.h.
  37. The following classes for table CRUD operations are defined:
  38. - TableInsert
  39. - TableDelete
  40. - TableSelect
  41. - TableUpdate
  42. CRUD operation objects can be created directly, or assigned from
  43. result of DevAPI methods that create such operations:
  44. ~~~~~~
  45. TableInsert insert_op(table);
  46. TableSelect select_op = table.select(...).orderBy(...);
  47. ~~~~~~
  48. CRUD operation objects have methods which can modify the operation
  49. before it gets executed. For example `TableInsert::values()`
  50. appends a row to the list of rows that should be inserted into a table
  51. by the given TableInsert operation. These methods can be chained
  52. as allowed by the fluent API grammar.
  53. */
  54. #include "common.h"
  55. #include "result.h"
  56. #include "executable.h"
  57. #include "crud.h"
  58. namespace mysqlx {
  59. class Table;
  60. // ---------------------------------------------------------------------------
  61. class TableInsert;
  62. namespace internal {
  63. struct Table_insert_base
  64. : public Executable<Result, TableInsert>
  65. {};
  66. }
  67. /**
  68. An operation which inserts rows into a table.
  69. This class defines methods that specify the rows to be inserted into
  70. the table.
  71. @todo Check that every row passed to .values() call has
  72. the same number of values. The column count should match
  73. the one in insert(c1,...) call. For insert() without column
  74. list, it should match the number of columns in the table.
  75. @ingroup devapi_op
  76. */
  77. class TableInsert
  78. : public internal::Table_insert_base
  79. , internal::Table_insert_detail
  80. {
  81. protected:
  82. template <class... Cols>
  83. TableInsert(Table &table, const Cols&... cols)
  84. : TableInsert(table)
  85. {
  86. add_columns(get_impl(), cols...);
  87. }
  88. public:
  89. // Create operation which inserts rows into given table.
  90. TableInsert(Table &table)
  91. {
  92. try {
  93. reset(internal::Crud_factory::mk_insert(table));
  94. }
  95. CATCH_AND_WRAP
  96. }
  97. TableInsert(const internal::Table_insert_base &other)
  98. {
  99. internal::Table_insert_base::operator=(other);
  100. }
  101. TableInsert(internal::Table_insert_base &&other)
  102. {
  103. internal::Table_insert_base::operator=(std::move(other));
  104. }
  105. /// Add the given row to the list of rows to be inserted.
  106. virtual TableInsert& values(const Row &row)
  107. {
  108. try {
  109. add_rows(get_impl(), row);
  110. return *this;
  111. }
  112. CATCH_AND_WRAP
  113. }
  114. /**
  115. Add a single row consisting of the specified values to the list of
  116. rows to be inserted.
  117. */
  118. template<typename... Types>
  119. TableInsert& values(Types... rest)
  120. {
  121. try {
  122. add_values(get_impl(), rest...);
  123. return *this;
  124. }
  125. CATCH_AND_WRAP
  126. }
  127. /**
  128. Add rows from a container such as vector or list.
  129. */
  130. template<typename Container>
  131. TableInsert& rows(const Container &cont)
  132. {
  133. try {
  134. add_rows(get_impl(), cont);
  135. return *this;
  136. }
  137. CATCH_AND_WRAP
  138. }
  139. /**
  140. Add rows from a range given by two iterators.
  141. */
  142. template<typename It>
  143. TableInsert& rows(const It &begin, const It &end)
  144. {
  145. try {
  146. add_rows(get_impl(), begin, end);
  147. return *this;
  148. }
  149. CATCH_AND_WRAP
  150. }
  151. /**
  152. Add the given list of rows.
  153. */
  154. template<typename... Types>
  155. TableInsert& rows(const Row &first, Types... rest)
  156. {
  157. try {
  158. add_rows(get_impl(), first, rest...);
  159. return *this;
  160. }
  161. CATCH_AND_WRAP
  162. }
  163. protected:
  164. using Table_insert_detail::Impl;
  165. Impl* get_impl()
  166. {
  167. return static_cast<Impl*>(internal::Table_insert_base::get_impl());
  168. }
  169. ///@cond IGNORED
  170. friend Table;
  171. ///@endcond
  172. };
  173. // ---------------------------------------------------------------------------
  174. class TableSelect;
  175. namespace internal {
  176. class Op_view_create_alter;
  177. struct Table_select_cmd
  178. : public Executable<RowResult, TableSelect>
  179. {};
  180. struct Table_select_base
  181. : public Group_by < Having < Order_by < Limit < Offset< Bind_parameters<
  182. Set_lock< Table_select_cmd, common::Table_select_if >
  183. > > > > > >
  184. {};
  185. }
  186. /**
  187. An operation which selects rows from a table.
  188. The class defines various methods, such as `where()`, to specify which rows
  189. should be returned and in which order.
  190. For each row the operation can return all fields from the
  191. row or a set of values defined by projection expressions
  192. specified when the operation was created.
  193. @ingroup devapi_op
  194. */
  195. class TableSelect
  196. : public internal::Table_select_base
  197. , internal::Table_select_detail
  198. {
  199. using Operation = Table_select_base;
  200. public:
  201. TableSelect(Table &table)
  202. {
  203. try{
  204. reset(internal::Crud_factory::mk_select(table));
  205. }
  206. CATCH_AND_WRAP
  207. }
  208. template <typename...PROJ>
  209. TableSelect(Table &table, const PROJ&... proj)
  210. : TableSelect(table)
  211. {
  212. try {
  213. add_proj(get_impl(), proj...);
  214. }
  215. CATCH_AND_WRAP
  216. }
  217. TableSelect(const internal::Table_select_cmd &other)
  218. {
  219. internal::Table_select_cmd::operator=(other);
  220. }
  221. TableSelect(internal::Table_select_cmd &&other)
  222. {
  223. internal::Table_select_cmd::operator=(std::move(other));
  224. }
  225. /**
  226. Specify row selection criteria.
  227. The criteria is specified as a Boolean expression string.
  228. */
  229. Operation& where(const string& expr)
  230. {
  231. try {
  232. get_impl()->set_where(expr);
  233. return *this;
  234. }
  235. CATCH_AND_WRAP
  236. }
  237. protected:
  238. using Impl = common::Table_select_if;
  239. Impl* get_impl()
  240. {
  241. return static_cast<Impl*>(internal::Table_select_base::get_impl());
  242. }
  243. ///@cond IGNORED
  244. friend Table;
  245. friend internal::Op_view_create_alter;
  246. ///@endcond
  247. };
  248. // ---------------------------------------------------------------------------
  249. class TableUpdate;
  250. namespace internal {
  251. struct Table_update_cmd
  252. : public Executable<Result, TableUpdate>
  253. {};
  254. struct Table_update_base
  255. : public Order_by< Limit< Bind_parameters< Table_update_cmd > > >
  256. {};
  257. }
  258. /**
  259. An operation which updates rows stored in a table.
  260. Methods of this clas specify modifications to be applied to each row as well
  261. as the set of rows that should be modified.
  262. @ingroup devapi_op
  263. */
  264. class TableUpdate
  265. : public internal::Table_update_base
  266. {
  267. using Operation = internal::Table_update_base;
  268. TableUpdate(Table& table)
  269. {
  270. try{
  271. reset(internal::Crud_factory::mk_update(table));
  272. }
  273. CATCH_AND_WRAP
  274. }
  275. public:
  276. TableUpdate(Table &table, const string &expr)
  277. : TableUpdate(table)
  278. {
  279. where(expr);
  280. }
  281. TableUpdate(const internal::Table_update_cmd &other)
  282. {
  283. internal::Table_update_cmd::operator=(other);
  284. }
  285. TableUpdate(internal::Table_update_cmd &&other)
  286. {
  287. internal::Table_update_cmd::operator=(std::move(other));
  288. }
  289. /**
  290. Set the given field in a row to the given value.
  291. The value can be either a direct literal or an expression given
  292. as `expr(<string>)`, to be evaluated in the server.
  293. */
  294. TableUpdate& set(const string& field, const Value &val)
  295. {
  296. try {
  297. get_impl()->add_set(field, (const common::Value&)val);
  298. return *this;
  299. }
  300. CATCH_AND_WRAP
  301. }
  302. /**
  303. Specify selection criteria for rows that should be updated.
  304. */
  305. Operation& where(const string& expr)
  306. {
  307. try {
  308. get_impl()->set_where(expr);
  309. return *this;
  310. }
  311. CATCH_AND_WRAP
  312. }
  313. protected:
  314. using Impl = common::Table_update_if;
  315. Impl* get_impl()
  316. {
  317. return static_cast<Impl*>(internal::Table_update_base::get_impl());
  318. }
  319. ///@cond IGNORED
  320. friend Table;
  321. ///@endcond
  322. };
  323. // ---------------------------------------------------------------------------
  324. class TableRemove;
  325. namespace internal {
  326. struct Table_remove_cmd
  327. : public Executable<Result, TableRemove>
  328. {};
  329. struct Table_remove_base
  330. : Order_by< Limit< Bind_parameters< Table_remove_cmd > > >
  331. {};
  332. }
  333. /**
  334. An operation which removes rows from a table.
  335. The class defines methods to specify which rows should be removed.
  336. @ingroup devapi_op
  337. */
  338. class TableRemove
  339. : public internal::Table_remove_base
  340. {
  341. using Operation = internal::Table_remove_base;
  342. TableRemove(Table& table)
  343. {
  344. try {
  345. reset(internal::Crud_factory::mk_remove(table));
  346. }
  347. CATCH_AND_WRAP
  348. }
  349. public:
  350. TableRemove(Table &table, const string &expr)
  351. : TableRemove(table)
  352. {
  353. where(expr);
  354. }
  355. TableRemove(const internal::Table_remove_cmd &other)
  356. {
  357. internal::Table_remove_cmd::operator=(other);
  358. }
  359. TableRemove(internal::Table_remove_cmd &&other)
  360. {
  361. internal::Table_remove_cmd::operator=(std::move(other));
  362. }
  363. /**
  364. Specify selection criteria for rows to be removed.
  365. */
  366. Operation& where(const string &expr)
  367. {
  368. try {
  369. get_impl()->set_where(expr);
  370. return *this;
  371. }
  372. CATCH_AND_WRAP
  373. }
  374. protected:
  375. using Impl = common::Table_remove_if;
  376. Impl* get_impl()
  377. {
  378. return static_cast<Impl*>(internal::Table_remove_base::get_impl());
  379. }
  380. ///@cond IGNORED
  381. friend Table;
  382. ///@endcond
  383. };
  384. } // mysqlx
  385. #endif