MySQL Connector/C++
MySQL connector library for C and C++ applications
xdevapi.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015, 2020, Oracle and/or its affiliates.
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
31#ifndef MYSQL_DEVAPI_H
32#define MYSQL_DEVAPI_H
33
34#ifndef __cplusplus
35#error This header can be only used with C++ code
36#endif
37
81/*
82 X DevAPI public classes are declared in this and other headers included from
83 devapi/ folder. The main public API classes, such as Session below, contain
84 declarations of public interface methods. Any obscure details of the public
85 API, which must be defined in the public header, are factored out
86 to Session_detail class from which the main Session class inherits.
87 Among other things, Session_detail declares the implementation class for
88 Session. This implementation class is opaque and its details are not defined
89 in the public headers - only in the implementation part. Definitions of
90 XXX_detail classes can be found in devapi/detail/ sub-folder.
91*/
92
93#include "devapi/common.h"
94#include "devapi/result.h"
96#include "devapi/table_crud.h"
97#include "devapi/settings.h"
98#include "devapi/detail/session.h"
99
100namespace mysqlx {
101MYSQLX_ABI_BEGIN(2,0)
102
103class Session;
104
105namespace internal {
106
107template <class Base> class Sch_object;
108
109} // internal
110
111
113
114class CollectionOptions;
115
122{
123public:
124
125#define COLLECTION_VALIDATION_ENUM(x,y) x=y,
126
127
134 enum Level
135 {
136 COLLECTION_VALIDATION_LEVEL(COLLECTION_VALIDATION_ENUM)
137 };
138
139
145 enum Option
147 COLLECTION_VALIDATION_OPTION(COLLECTION_VALIDATION_ENUM)
148 LAST
149 };
150
151private:
152
153 struct Data
154 {
155 std::string validation_level;
156 DbDoc validation_schema;
157 std::bitset<LAST> used;
158 };
159
160public:
161
163 {}
164
165 CollectionValidation(const char* json_doc)
166 : CollectionValidation(DbDoc(json_doc))
167 {}
168
205 CollectionValidation(DbDoc doc)
206 {
207 for(auto el : doc)
208 {
209 if(el == "level")
210 {
211 try {
212 _set(LEVEL, doc[el].get<std::string>());
213 } catch (const Error& e)
214 {
215 std::string err("Unexpected level type: ");
216 err+=e.what();
217 throw Error(err.c_str());
218 }
219 }
220 else if(el == "schema")
221 {
222 _set(SCHEMA,doc[el].get<DbDoc>());
223 }
224 else {
225 std::string err("Unexpected schema validation field ");
226 err+=el;
227 throw Error(err.c_str());
228 }
229 }
230 }
231
237 template<typename... Rest>
238 CollectionValidation(Option opt, Rest&&... rest)
239 {
240 set(opt, std::forward<Rest>(rest)...);
241 }
242
247 template<typename... Rest>
248 void set(Rest&&... options)
249 {
250 Data tmp_data(m_data);
251 try {
252 _set(std::forward<Rest>(options)...);
253 } catch (...) {
254 m_data = tmp_data;
255 throw;
256 }
257 }
258
259protected:
260
262 // Note: Doxygen gets confused here and renders docs incorrectly.
263 template<typename T,typename... Rest>
264 void _set(Option opt, T&& v, Rest&&... options)
265 {
266#define SCHEMA_VALIDATION_SET(x,y) case CollectionValidation::x:\
267 do_set<CollectionValidation::x>(std::forward<T>(v)); break;
268
269 switch (opt)
270 {
271 COLLECTION_VALIDATION_OPTION(SCHEMA_VALIDATION_SET)
272 case CollectionValidation::LAST: throw_error("Invalid option."); ; break;
273 }
274
275 _set(std::forward<Rest>(options)...);
276 }
278
279 void _set() {}
280
281 template<CollectionValidation::Option, typename T>
282 void do_set(T)
283 {
284 throw_error("Invalid option value type.");
285 }
286
287 Data m_data;
288
290 friend CollectionOptions;
291 friend Schema;
292 friend mysqlx::internal::Schema_detail;
294};
295
296
301class CollectionOptions
302{
303 public:
304
305#define COLLECTION_OPTIONS_ENUM(x,y) x=y,
306
313 {
314 COLLECTION_OPTIONS_OPTION(COLLECTION_OPTIONS_ENUM)
315 LAST
316 };
317
318private:
319
320 struct Data{
321 CollectionValidation validation;
322 std::bitset<LAST> used;
323 bool reuse = false;
324 };
325
326 public:
327
328
330 {}
331
332 CollectionOptions(const char* options)
333 : CollectionOptions(DbDoc(options))
334 {}
335
336 CollectionOptions(const std::string& options)
337 : CollectionOptions(DbDoc(options))
338 {}
339
380 CollectionOptions(DbDoc options)
381 {
382 for(auto el : options)
383 {
384 if(el == "reuseExisting")
385 {
386 try {
387 _set(REUSE, options["reuseExisting"].get<bool>());
388 } catch (const Error& e)
389 {
390 std::string err("Wrong value for reuseExisting option: ");
391 err+=e.what();
392 throw Error(err.c_str());
393 }
394 }
395 else if(el == "validation")
396 {
397 _set(VALIDATION, CollectionValidation(options["validation"].get<DbDoc>()));
398 }
399 else {
400 std::string err("Unexpected collection option ");
401 err+=el;
402 throw Error(err.c_str());
403 }
404 }
405 }
406
407 CollectionOptions(CollectionValidation validation)
408 {
409 set(VALIDATION, validation);
410 }
411
448 template<typename... Rest>
449 CollectionOptions(Option opt, Rest&&... rest)
450 {
451 set(opt, std::forward<Rest>(rest)...);
452 }
453
454
455 template<typename... Rest>
456 CollectionOptions(CollectionValidation::Option opt, Rest&&... rest)
457 {
458 set(opt, std::forward<Rest>(rest)...);
459 }
460
461
466 template<typename... Rest>
467 void set(Rest&&... rest)
468 {
469 Data tmp_data(m_data);
470 try {
471 _set(std::forward<Rest>(rest)...);
472 } catch (...) {
473 m_data = std::move(tmp_data);
474 throw;
475 }
476 }
477
478
479
480protected:
481
483 // Note: Doxygen gets confused here and renders docs incorrectly.
484 template<typename T,typename... Rest>
485 void _set(Option opt, T&& v, Rest&&... rest)
486 {
487#define COLLECTION_OPTIONS_SET(x,y) case x:\
488 do_set<x>(std::forward<T>(v)); break;
489
490 switch (opt)
491 {
492 COLLECTION_OPTIONS_OPTION(COLLECTION_OPTIONS_SET)
493 case LAST: throw_error("Invalid option."); ; break;
494 }
495
496 _set(std::forward<Rest>(rest)...);
497 }
499
500 template<typename T,typename... Rest>
501 void _set(CollectionValidation::Option opt, T&& v, Rest&&... rest)
502 {
503 m_data.validation._set(opt, std::forward<T>(v));
504 _set(std::forward<Rest>(rest)...);
505 }
506
507 void _set(){}
508
509 template<CollectionOptions::Option O,typename T>
510 void do_set(T)
511 {
512 throw_error("Invalid option value type.");
513 }
514
515 Data m_data;
516
518 friend mysqlx::internal::Schema_detail;
520};
521
522
561class Schema
562 : protected internal::Schema_detail
563{
564 Session *m_sess;
565
566public:
567
572 Schema(Session &sess, const string &name);
573
580 Schema(Session&);
581
582
587 const string& getName() const
588 {
589 return m_name;
590 }
591
596 Session& getSession()
597 {
598 return *m_sess;
599 }
600
601 const Session& getSession() const
602 {
603 return const_cast<Schema*>(this)->getSession();
604 }
605
612 bool existsInDatabase() const
613 {
614 try {
615 /*
616 Note: We get from server a list of schemata filtered by the name of
617 this schema - if the schema exists the list should not be empty.
618 */
619 internal::Session_detail::Name_src list(*m_sess, m_name);
620 list.iterator_start();
621 return list.iterator_next();
622 }
623 CATCH_AND_WRAP
624 }
625
626
636 Collection createCollection(const string &name);
637 Collection createCollection(const string &name, bool);
638
647 template<typename... Rest>
648 Collection createCollection(const string &name,
649 Rest&&... rest);
650
659 template<typename... Rest>
660 void modifyCollection(const string &name, Rest&&... options);
661
662
676 Collection getCollection(const string &name, bool check_exists = false);
677
694 Table getTable(const string &name, bool check_exists = false);
695
703 CollectionList getCollections()
704 {
705 try {
706 return Collection_src(*this, "%");
707 }
708 CATCH_AND_WRAP
709 }
710
718 StringList getCollectionNames()
719 {
720 try {
721 return Name_src(*this, COLLECTION, "%");
722 }
723 CATCH_AND_WRAP
724 }
725
736 TableList getTables()
737 {
738 try {
739 return Table_src(*this, "%");
740 }
741 CATCH_AND_WRAP
742 }
743
754 StringList getTableNames()
755 {
756 try {
757 return Name_src(*this, TABLE, "%");
758 }
759 CATCH_AND_WRAP
760 }
761
779 Table getCollectionAsTable(const string &name, bool check_exists = true);
780
781
792 void dropCollection(const mysqlx::string& name)
793 {
794 try {
795 Schema_detail::drop_collection(name);
796 }
797 CATCH_AND_WRAP
798 }
799
800
801 friend Collection;
802 friend Table;
803
805 friend internal::Schema_detail::Name_src;
806 template <class Base> friend class internal::Sch_object;
808};
809
810
811/*
812 Database objects that belong to some schema
813 ===========================================
814*/
815
816
817namespace internal {
818
819// Common base for schema objects defining the common API methods.
820
821template <class Base = Db_obj_base>
822class Sch_object
823 : public Base
824{
825protected:
826
827 Schema m_schema;
828
829 Sch_object(const Schema &sch, const string &name);
830
831public:
832
833 using string = mysqlx::string;
834
839 const string& getName() const
840 {
841 return Base::m_name;
842 }
843
848 Session& getSession()
849 {
850 return m_schema.getSession();
851 }
852
857 const Schema& getSchema() const
858 {
859 return m_schema;
860 }
861
862protected:
863
864 std::shared_ptr<common::Session_impl> get_session();
865
866 Schema_detail& get_schema()
867 {
868 return m_schema;
869 }
870};
871
872} // internal
873
874
875
876
877
878
879
913class Collection
914 : protected internal::Sch_object<internal::Collection_detail>
915{
916public:
917
918 Collection(const Schema &sch, const string &name)
919 try
920 : Sch_object(sch, name)
921 {}
922 CATCH_AND_WRAP
923
924
925 using Sch_object::getName;
927 using Sch_object::getSchema;
928
929 bool existsInDatabase() const
930 {
931 try {
932 Schema::StringList list(m_schema, Schema::COLLECTION, m_name);
933 return list.begin() != list.end();
934 }
935 CATCH_AND_WRAP
936 }
937
942 uint64_t count();
943
944 /*
945 CRUD operations on a collection
946 -------------------------------
947 */
948
963 CollectionFind find()
964 {
965 try {
966 return CollectionFind(*this);
967 }
968 CATCH_AND_WRAP;
969 }
970
986 CollectionFind find(const string &cond)
987 {
988 try {
989 return CollectionFind(*this, cond);
990 }
991 CATCH_AND_WRAP;
992 }
993
1008 template <typename... Types>
1009 CollectionAdd add(Types... args)
1010 {
1011 try {
1012
1013 CollectionAdd add(*this);
1014 return add.add(args...);
1015 }
1016 CATCH_AND_WRAP;
1017 }
1018
1037 CollectionRemove remove(const string &cond)
1038 {
1039 try {
1040 return CollectionRemove(*this, cond);
1041 }
1042 CATCH_AND_WRAP;
1043 }
1044
1063 CollectionModify modify(const string &expr)
1064 {
1065 try {
1066 return CollectionModify(*this, expr);
1067 }
1068 CATCH_AND_WRAP;
1069 }
1070
1078 DbDoc getOne(const string &id)
1079 {
1080 return find("_id = :id").bind("id", id).execute().fetchOne();
1081 }
1082
1090 Result removeOne(const string &id)
1091 {
1092 return remove("_id = :id").bind("id", id).execute();
1093 }
1094
1111 Result replaceOne(const string &id, Value &&document)
1112 {
1113 try {
1114 return
1115 Collection_detail::add_or_replace_one(id, std::move(document), true);
1116 }
1117 CATCH_AND_WRAP
1118 }
1119
1135 Result addOrReplaceOne(const string &id, Value &&document)
1136 {
1137 try {
1138 return
1139 Collection_detail::add_or_replace_one(id, std::move(document), false);
1140 }
1141 CATCH_AND_WRAP
1142 }
1143
1157 void createIndex(const string &name, const string &idx_spec)
1158 {
1159 try {
1160 Collection_detail::index_create(name, idx_spec);
1161 }
1162 CATCH_AND_WRAP
1163 }
1164
1173 void dropIndex(const string &name)
1174 {
1175 try {
1176 Collection_detail::index_drop(name);
1177 }
1178 CATCH_AND_WRAP
1179 }
1180
1181
1182 friend CollectionFind;
1183 friend CollectionAdd;
1184 friend CollectionRemove;
1185 friend CollectionModify;
1186
1188 friend internal::Crud_factory;
1190};
1191
1192//------------------------------------------------------------------------------
1193
1194
1195template<>
1196inline
1197void CollectionValidation::do_set<CollectionValidation::SCHEMA>(DbDoc schema)
1198{
1199 if(m_data.used.test(CollectionValidation::SCHEMA))
1200 throw_error("Validation schema already set.");
1201 m_data.used.set(CollectionValidation::SCHEMA);
1202 m_data.validation_schema = schema;
1203}
1204
1205template<>
1206inline
1207void CollectionValidation::do_set<CollectionValidation::SCHEMA>(const char* schema)
1208{
1209 do_set<CollectionValidation::SCHEMA>(DbDoc(schema));
1210}
1211
1212template<>
1213inline
1214void CollectionValidation::do_set<CollectionValidation::SCHEMA>(std::string schema)
1215{
1216 do_set<CollectionValidation::SCHEMA>(DbDoc(schema));
1217}
1218
1220template<>
1221inline
1222void CollectionValidation::do_set<CollectionValidation::LEVEL>(Level level)
1223{
1224 if(m_data.used.test(CollectionValidation::LEVEL))
1225 throw_error("Validation level already set.");
1226 m_data.used.set(CollectionValidation::LEVEL);
1227
1228#define SCHEMA_VALIDATION_CASE(x,y) case Level::x: m_data.validation_level = #x; break;
1229
1230 switch (level) {
1231 COLLECTION_VALIDATION_LEVEL(SCHEMA_VALIDATION_CASE)
1232 }
1233}
1234
1235template<>
1236inline
1237void CollectionValidation::do_set<CollectionValidation::LEVEL>(std::string level)
1238{
1239 if(m_data.used.test(CollectionValidation::LEVEL))
1240 throw_error("Validation level already set.");
1241 m_data.used.set(CollectionValidation::LEVEL);
1242
1243 m_data.validation_level = level;
1244
1245}
1246
1247template<>
1248inline
1249void CollectionOptions::do_set<CollectionOptions::REUSE>(bool reuse)
1250{
1251 if(m_data.used[CollectionOptions::REUSE])
1252 throw_error("Option reuse already set.");
1253 m_data.used.set(CollectionOptions::REUSE);
1254 m_data.reuse = reuse;
1255}
1256
1257template<>
1258inline
1259void CollectionOptions::do_set<CollectionOptions::VALIDATION>(CollectionValidation validation)
1260{
1261 if(m_data.used.test(CollectionOptions::VALIDATION) ||
1262 m_data.validation.m_data.used.test(CollectionValidation::LEVEL) ||
1263 m_data.validation.m_data.used.test(CollectionValidation::SCHEMA))
1264 throw_error("Validation already set.");
1265
1266 m_data.used.set(CollectionOptions::VALIDATION);
1267 m_data.validation.m_data.used.set(CollectionValidation::LEVEL);
1268 m_data.validation.m_data.used.set(CollectionValidation::SCHEMA);
1269
1270 m_data.validation = validation;
1272
1273
1274inline
1275Collection Schema::createCollection(const string &name)
1276{
1277 try {
1278 Schema_detail::create_collection(
1279 name,
1281
1282 return Collection(*this, name);
1283 }
1284 CATCH_AND_WRAP
1285}
1286
1287inline
1288Collection Schema::createCollection(const string &name, bool reuse)
1289{
1290 try {
1291 Schema_detail::create_collection(
1292 name,
1294 CollectionOptions::REUSE, reuse)
1295 );
1296
1297 return Collection(*this, name);
1298 }
1299 CATCH_AND_WRAP
1300}
1302
1303template<typename... Rest>
1304inline
1305Collection Schema::createCollection(const string &name, Rest&&... rest)
1306{
1307 try {
1308 Schema_detail::create_collection(name, CollectionOptions(std::forward<Rest>(rest)...));
1309 return Collection(*this, name);
1310 }
1311 CATCH_AND_WRAP
1312}
1313
1314
1315template <typename... Opt>
1316inline
1317void Schema::modifyCollection(const string &name, Opt&&... options)
1318{
1319 try {
1320 Schema_detail::modify_collection(name, CollectionOptions(std::forward<Opt>(options)...));
1321 }
1322 CATCH_AND_WRAP
1323}
1325
1326
1327inline
1328Collection Schema::getCollection(const string &name, bool check_exists)
1329{
1330 Collection coll(*this, name);
1331 if (check_exists && !coll.existsInDatabase())
1332 throw_error("Collection does not exist");
1333 return coll;
1334}
1335
1336
1337
1338/*
1339 Table object
1340 ============
1341*/
1342
1379class Table
1380 : protected internal::Sch_object<>
1381{
1382public:
1383
1384 Table(const Schema &sch, const string &name)
1385 : Sch_object(sch, name)
1386 {}
1387
1388 Table(const Schema &sch, const string &name, bool is_view)
1389 : Sch_object(sch, name)
1390 {
1391 m_type = is_view ? VIEW : TABLE;
1392 }
1393
1394
1395 using Sch_object::getName;
1397 using Sch_object::getSchema;
1398
1399
1400 bool existsInDatabase() const;
1401
1402
1409 bool isView();
1410
1411
1416 uint64_t count()
1417 {
1418 try {
1419 RowResult cnt = select("count(*)").execute();
1420 return cnt.fetchOne()[0].get<uint64_t>();
1421 }
1422 CATCH_AND_WRAP
1423 }
1424
1425
1426 /*
1427 CRUD operations
1428 ---------------
1429 */
1430
1449 TableInsert insert()
1450 {
1451 try {
1452 return TableInsert(*this);
1453 }
1454 CATCH_AND_WRAP;
1455 }
1456
1478 template <class... T>
1479 TableInsert insert(const T&... t)
1480 {
1481 try {
1482 return TableInsert(*this, t...);
1483 }
1484 CATCH_AND_WRAP;
1485 }
1486
1507 template<typename ...PROJ>
1508 TableSelect select(const PROJ&...proj)
1509 {
1510 try {
1511 return TableSelect(*this, proj...);
1512 }
1513 CATCH_AND_WRAP;
1514 }
1515
1529 TableRemove remove()
1530 {
1531 try {
1532 return TableRemove(*this);
1533 }
1534 CATCH_AND_WRAP;
1535 }
1536
1551 TableUpdate update()
1552 {
1553 try {
1554 return TableUpdate(*this);
1555 }
1556 CATCH_AND_WRAP;
1557 }
1558
1559private:
1560
1561 enum { TABLE, VIEW, UNKNOWN } m_type = UNKNOWN;
1562
1563 friend TableSelect;
1564 friend TableInsert;
1565 friend TableRemove;
1566 friend TableUpdate;
1567
1569 friend internal::Crud_factory;
1571};
1572
1573
1574inline
1575bool Table::existsInDatabase() const
1576{
1577 try {
1578 /*
1579 Note: When checking existence, we also determine if this is a view or
1580 a plain table because this information is fetched from the server when
1581 querying for a list of tables.
1582 */
1583 Schema::TableList list(m_schema, m_name);
1584 auto it = list.begin();
1585
1586 if (!(it != list.end()))
1587 return false;
1588
1589 const_cast<Table*>(this)->m_type = (*it).isView() ? VIEW : TABLE;
1590 return true;
1591 }
1592 CATCH_AND_WRAP
1594
1595
1596inline
1597bool Table::isView()
1598{
1599 try {
1600 /*
1601 If view status was not determined yet, do existence check which
1602 determines it as a side effect.
1603 */
1604 if (UNKNOWN == m_type)
1605 if (!existsInDatabase())
1606 throw_error("Table does not exist");
1607 return VIEW == m_type;
1608 }
1609 CATCH_AND_WRAP
1611
1612
1613inline
1614Table Schema::getTable(const string &name, bool check_exists)
1615{
1616 Table tbl(*this, name);
1617 if (check_exists && !tbl.existsInDatabase())
1618 throw_error("Table does not exist");
1619 return tbl;
1621
1622
1623inline
1624Table Schema::getCollectionAsTable(const string &name, bool check_exists)
1625{
1626 if (check_exists && !getCollection(name).existsInDatabase())
1627 throw_error("Collection does not exist");
1628 return { *this, name };
1630
1631
1632inline
1633uint64_t Collection::count()
1634{
1635 return m_schema.getCollectionAsTable(m_name).count();
1636}
1637
1638
1639using SqlStatement = internal::SQL_statement;
1640
1641
1676class Session
1677 : private internal::Session_detail
1678{
1679public:
1680
1681
1686 Session(SessionSettings settings)
1687 try
1688 : Session_detail(settings)
1689 {}
1690 CATCH_AND_WRAP
1691
1692
1721 template<typename...T>
1722 Session(T...options)
1723 try
1724 : Session(SessionSettings(options...))
1725 {}CATCH_AND_WRAP
1726
1727
1728 Session(Session &&other)
1729 try
1730 : internal::Session_detail(std::move(other))
1731 {}CATCH_AND_WRAP
1732
1733
1734 Session(Client &client);
1735
1744 Schema createSchema(const string &name, bool reuse = false)
1745 {
1746 try {
1747 Session_detail::create_schema(name, reuse);
1748 return Schema(*this, name);
1749 }
1750 CATCH_AND_WRAP
1751 }
1752
1766 Schema getSchema(const string &name, bool check_exists = false)
1767 {
1768 Schema sch(*this, name);
1769 if (check_exists && !sch.existsInDatabase())
1770 throw_error("Schema does not exist");
1771 return sch;
1772 }
1773
1778 Schema getDefaultSchema()
1779 {
1780 return Schema(*this, getDefaultSchemaName());
1781 }
1782
1787 string getDefaultSchemaName()
1788 {
1789 try {
1790 return Session_detail::get_default_schema_name();
1791 }
1792 CATCH_AND_WRAP
1793 }
1794
1802 SchemaList getSchemas()
1803 {
1804 try {
1805 return Schema_src(*this, "%");
1806 }
1807 CATCH_AND_WRAP
1808 }
1809
1810 // TODO: Should we have getSchemaNames() too?
1811
1818 void dropSchema(const string &name)
1819 {
1820 try {
1821 Session_detail::drop_schema(name);
1822 }
1823 CATCH_AND_WRAP
1824 }
1825
1826
1839 SqlStatement sql(const string &query)
1840 {
1841 try {
1842 return SqlStatement(this, query);
1843 }
1844 CATCH_AND_WRAP
1845 }
1846
1853 void startTransaction()
1854 {
1855 try {
1856 Session_detail::start_transaction();
1857 }
1858 CATCH_AND_WRAP
1859 }
1860
1868 void commit()
1869 {
1870 try {
1871 Session_detail::commit();
1872 }
1873 CATCH_AND_WRAP
1874 }
1875
1884 void rollback()
1885 {
1886 try {
1887 Session_detail::rollback();
1888 }
1889 CATCH_AND_WRAP
1890 }
1891
1901 void rollbackTo(const string &savepoint)
1902 {
1903 try {
1904 if (savepoint.empty())
1905 throw_error("Invalid empty save point name");
1906 Session_detail::rollback(savepoint);
1907 }
1908 CATCH_AND_WRAP
1909 }
1910
1911
1924 string setSavepoint(const string &savepoint)
1925 {
1926 try {
1927 if (savepoint.empty())
1928 throw_error("Invalid empty save point name");
1929 return Session_detail::savepoint_set(savepoint);
1930 }
1931 CATCH_AND_WRAP
1932 }
1933
1934
1947 string setSavepoint()
1948 {
1949 try {
1950 return Session_detail::savepoint_set();
1951 }
1952 CATCH_AND_WRAP
1953 }
1954
1955
1964 void releaseSavepoint(const string &savepoint)
1965 {
1966 try {
1967 if (savepoint.empty())
1968 throw_error("Invalid empty save point name");
1969 Session_detail::savepoint_remove(savepoint);
1970 }
1971 CATCH_AND_WRAP
1972 }
1973
1974
1982 void close()
1983 {
1984 try {
1985 Session_detail::close();
1986 }
1987 CATCH_AND_WRAP
1988 }
1989
1990protected:
1991
1992 using internal::Session_detail::m_impl;
1993
1994public:
1995
1996 friend Schema;
1997 friend Collection;
1998 friend Table;
1999 friend Result;
2000 friend RowResult;
2001
2003 friend internal::Session_detail;
2004 friend internal::Crud_factory;
2005 friend internal::Result_detail;
2006 template <class Base> friend class internal::Sch_object;
2008};
2009
2010
2046class Client : public internal::Client_detail
2047{
2048public:
2049
2050 Client(ClientSettings settings)
2051 try
2052 : Client_detail(settings)
2053 {}
2054 CATCH_AND_WRAP
2055
2056 Client(SessionSettings &settings)
2057 try
2058 : Client_detail(settings)
2059 {}
2060 CATCH_AND_WRAP
2061
2062 template<typename...T>
2063 Client(T...options)
2064 : Client(ClientSettings(options...))
2065 {}
2066
2067
2068 Session getSession()
2069 {
2070 return *this;
2071 }
2072
2073};
2074
2075
2076/*
2077 Session
2078*/
2079
2080inline
2081Session::Session(Client &client)
2082try
2083 : internal::Session_detail(client.get_session_pool())
2084{}CATCH_AND_WRAP
2085
2092template<class ...P>
2093Session getSession(P...p)
2094{
2095 return Session(p...);
2096}
2097
2104template<class ...P>
2105Client getClient(P...p)
2106{
2107 return Client(p...);
2108}
2109
2110
2111/*
2112 Schema class implementation
2113*/
2114
2115inline
2116Schema::Schema(Session &sess, const string &name)
2117 : Schema_detail(sess.m_impl, name)
2118 , m_sess(&sess)
2119{}
2120
2121
2122template <class Base>
2123inline
2124internal::Sch_object<Base>::Sch_object(const Schema &sch, const string &name)
2125 : Base(sch.getSession().m_impl, name)
2126 , m_schema(sch)
2127{}
2128
2129
2130template <class Base>
2131inline
2132std::shared_ptr<common::Session_impl>
2133internal::Sch_object<Base>::get_session()
2134{
2135 assert(m_schema.m_sess);
2136 return m_schema.m_sess->m_impl;
2137}
2138
2139
2140MYSQLX_ABI_END(2,0)
2141} // mysqlx
2142
2143#endif
CollectionAdd & add(const It &begin, const It &end)
Add all documents from a range defined by two iterators.
Definition: collection_crud.h:172
The CollectionOptions class defines collection create/modify options.
Definition: xdevapi.h:300
Option
Collection options
Definition: xdevapi.h:311
void set(Rest &&... rest)
Set list of option and value pairs.
Definition: xdevapi.h:464
The CollectionValidation class defines collection schema and level of validation.
Definition: xdevapi.h:122
Level
Collection validation level options.
Definition: xdevapi.h:135
void set(Rest &&... options)
Set list of Option and value pairs.
Definition: xdevapi.h:246
Represents a collection of documents in a schema.
Definition: xdevapi.h:912
void createIndex(const string &name, const string &idx_spec)
Create index on the collection.
Definition: xdevapi.h:1154
void dropIndex(const string &name)
Drop index on the collection.
Definition: xdevapi.h:1170
Result replaceOne(const string &id, Value &&document)
Replace the document with the given id by a new one.
Definition: xdevapi.h:1108
CollectionAdd add(Types... args)
Return an operation which adds documents to the collection.
Definition: xdevapi.h:1006
Result removeOne(const string &id)
Remove the document with the given id.
Definition: xdevapi.h:1087
uint64_t count()
Get the number of documents in the collection.
Definition: xdevapi.h:1629
Result addOrReplaceOne(const string &id, Value &&document)
Add a new document or replace an existing document with the given id.
Definition: xdevapi.h:1132
CollectionModify modify(const string &expr)
Return an operation which modifies documents that satisfy given criteria.
Definition: xdevapi.h:1060
CollectionFind find()
Return an operation which fetches all documents from the collection.
Definition: xdevapi.h:960
DbDoc getOne(const string &id)
Return the document with the given id.
Definition: xdevapi.h:1075
CollectionRemove remove(const string &cond)
Return an operation which removes documents satisfying given criteria.
Definition: xdevapi.h:1034
Represents a collection of key-value pairs where value can be a scalar or another document.
Definition: document.h:83
Result of an operation that returns rows.
Definition: result.h:585
Row fetchOne()
Return the current row and move to the next one in the sequence.
Definition: result.h:633
Value & get(col_count_t pos)
Get reference to row field at position pos.
Definition: row.h:159
Represents a database schema.
Definition: xdevapi.h:560
Table getCollectionAsTable(const string &name, bool check_exists=true)
Return a table corresponding to the given collection.
Definition: xdevapi.h:1620
const string & getName() const
Get schema name.
Definition: xdevapi.h:584
Table getTable(const string &name, bool check_exists=false)
Return an object representing a table or a view with the given name.
Definition: xdevapi.h:1610
Schema(Session &sess, const string &name)
Construct an object representing the named schema.
Definition: xdevapi.h:2112
void dropCollection(const mysqlx::string &name)
Drop the given collection from the schema.
Definition: xdevapi.h:789
bool existsInDatabase() const
Check if this schema exists in the database.
Definition: xdevapi.h:609
Collection getCollection(const string &name, bool check_exists=false)
Return an object representing a collection with the given name.
Definition: xdevapi.h:1324
void modifyCollection(const string &name, Rest &&... options)
Modify a collection in the schema specifying modify options.
CollectionList getCollections()
Get a list of all collections in the schema.
Definition: xdevapi.h:700
Collection createCollection(const string &name)
Create a new collection in the schema.
Definition: xdevapi.h:1271
StringList getCollectionNames()
Get a list of names of all collections in the schema.
Definition: xdevapi.h:715
TableList getTables()
Get a list of all tables and views in the schema.
Definition: xdevapi.h:733
StringList getTableNames()
Get a list of names of all tables and views in the schema.
Definition: xdevapi.h:751
Represents session options to be passed at session creation time.
Definition: settings.h:450
Represents a session which gives access to data stored in a data store.
Definition: xdevapi.h:1674
void rollbackTo(const string &savepoint)
Roll back opened transaction to specified savepoint.
Definition: xdevapi.h:1897
void rollback()
Roll back opened transaction, if any.
Definition: xdevapi.h:1880
Schema getSchema(const string &name, bool check_exists=false)
Return an object representing a schema with the given name.
Definition: xdevapi.h:1762
Session(SessionSettings settings)
Create a session specified by a SessionSettings object.
Definition: xdevapi.h:1682
SqlStatement sql(const string &query)
Return an operation which executes an arbitrary SQL statement.
Definition: xdevapi.h:1835
string getDefaultSchemaName()
Get the name of the default schema specified when the session was created.
Definition: xdevapi.h:1783
void close()
Close this session.
Definition: xdevapi.h:1978
Schema createSchema(const string &name, bool reuse=false)
Create a new schema.
Definition: xdevapi.h:1740
void releaseSavepoint(const string &savepoint)
Releases savepoint previously added by setSavepoint().
Definition: xdevapi.h:1960
void startTransaction()
Start a new transaction.
Definition: xdevapi.h:1849
void commit()
Commit opened transaction, if any.
Definition: xdevapi.h:1864
Schema getDefaultSchema()
Get the default schema specified when the session was created.
Definition: xdevapi.h:1774
SchemaList getSchemas()
Get a list of all database schemas.
Definition: xdevapi.h:1798
string setSavepoint(const string &savepoint)
Sets a named transaction savepoint with a name as identifier.
Definition: xdevapi.h:1920
void dropSchema(const string &name)
Drop the named schema.
Definition: xdevapi.h:1814
Represents a table in a schema.
Definition: xdevapi.h:1377
TableInsert insert()
Return an operation which inserts rows into the full table without restricting the columns.
Definition: xdevapi.h:1445
TableSelect select(const PROJ &...proj)
Return an operation which selects rows from the table.
Definition: xdevapi.h:1504
TableUpdate update()
Return an operation which updates rows in the table.
Definition: xdevapi.h:1547
TableRemove remove()
Return an operation which removes rows from the table.
Definition: xdevapi.h:1525
A wrapper around std::wstring that can perform conversions from/to different character encodings used...
Definition: common.h:114
Declarations for CRUD operations on document collections.
Client getClient(P...p)
Function to get Client object.
Definition: xdevapi.h:2101
internal::Expression expr(std::string &&e)
Function which indicates that a given string should be treated as expression.
Definition: document.h:638
Session getSession(P...p)
Function to get Session object.
Definition: xdevapi.h:2089
Classes used to access query and command execution results.
Crud operations on tables.