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
289 friend CollectionOptions;
290 friend Schema;
291 friend mysqlx::internal::Schema_detail;
292};
293
294
299class CollectionOptions
300{
301 public:
302
303#define COLLECTION_OPTIONS_ENUM(x,y) x=y,
304
311 {
312 COLLECTION_OPTIONS_OPTION(COLLECTION_OPTIONS_ENUM)
313 LAST
314 };
315
316private:
317
318 struct Data{
319 CollectionValidation validation;
320 std::bitset<LAST> used;
321 bool reuse = false;
322 };
323
324 public:
325
326
328 {}
329
330 CollectionOptions(const char* options)
331 : CollectionOptions(DbDoc(options))
332 {}
333
334 CollectionOptions(const std::string& options)
335 : CollectionOptions(DbDoc(options))
336 {}
337
378 CollectionOptions(DbDoc options)
379 {
380 for(auto el : options)
381 {
382 if(el == "reuseExisting")
383 {
384 try {
385 _set(REUSE, options["reuseExisting"].get<bool>());
386 } catch (const Error& e)
387 {
388 std::string err("Wrong value for reuseExisting option: ");
389 err+=e.what();
390 throw Error(err.c_str());
391 }
392 }
393 else if(el == "validation")
394 {
395 _set(VALIDATION, CollectionValidation(options["validation"].get<DbDoc>()));
396 }
397 else {
398 std::string err("Unexpected collection option ");
399 err+=el;
400 throw Error(err.c_str());
401 }
402 }
403 }
404
405 CollectionOptions(CollectionValidation validation)
406 {
407 set(VALIDATION, validation);
408 }
409
446 template<typename... Rest>
447 CollectionOptions(Option opt, Rest&&... rest)
448 {
449 set(opt, std::forward<Rest>(rest)...);
450 }
451
452
453 template<typename... Rest>
454 CollectionOptions(CollectionValidation::Option opt, Rest&&... rest)
455 {
456 set(opt, std::forward<Rest>(rest)...);
457 }
458
459
464 template<typename... Rest>
465 void set(Rest&&... rest)
466 {
467 Data tmp_data(m_data);
468 try {
469 _set(std::forward<Rest>(rest)...);
470 } catch (...) {
471 m_data = std::move(tmp_data);
472 throw;
473 }
474 }
475
476
477
478protected:
479
481 // Note: Doxygen gets confused here and renders docs incorrectly.
482 template<typename T,typename... Rest>
483 void _set(Option opt, T&& v, Rest&&... rest)
484 {
485#define COLLECTION_OPTIONS_SET(x,y) case x:\
486 do_set<x>(std::forward<T>(v)); break;
487
488 switch (opt)
489 {
490 COLLECTION_OPTIONS_OPTION(COLLECTION_OPTIONS_SET)
491 case LAST: throw_error("Invalid option."); ; break;
492 }
493
494 _set(std::forward<Rest>(rest)...);
495 }
497
498 template<typename T,typename... Rest>
499 void _set(CollectionValidation::Option opt, T&& v, Rest&&... rest)
500 {
501 m_data.validation._set(opt, std::forward<T>(v));
502 _set(std::forward<Rest>(rest)...);
503 }
504
505 void _set(){}
506
507 template<CollectionOptions::Option O,typename T>
508 void do_set(T)
509 {
510 throw_error("Invalid option value type.");
511 }
512
513 Data m_data;
514
515 friend mysqlx::internal::Schema_detail;
516};
517
518
557class Schema
558 : protected internal::Schema_detail
559{
560 Session *m_sess;
561
562public:
563
568 Schema(Session &sess, const string &name);
569
576 Schema(Session&);
577
578
583 const string& getName() const
584 {
585 return m_name;
586 }
587
592 Session& getSession()
593 {
594 return *m_sess;
595 }
596
597 const Session& getSession() const
598 {
599 return const_cast<Schema*>(this)->getSession();
600 }
601
608 bool existsInDatabase() const
609 {
610 try {
611 /*
612 Note: We get from server a list of schemata filtered by the name of
613 this schema - if the schema exists the list should not be empty.
614 */
615 internal::Session_detail::Name_src list(*m_sess, m_name);
616 list.iterator_start();
617 return list.iterator_next();
618 }
619 CATCH_AND_WRAP
620 }
621
622
632 Collection createCollection(const string &name);
633 Collection createCollection(const string &name, bool);
634
643 template<typename... Rest>
644 Collection createCollection(const string &name,
645 Rest&&... rest);
646
655 template<typename... Rest>
656 void modifyCollection(const string &name, Rest&&... options);
657
658
672 Collection getCollection(const string &name, bool check_exists = false);
673
690 Table getTable(const string &name, bool check_exists = false);
691
699 CollectionList getCollections()
700 {
701 try {
702 return Collection_src(*this, "%");
703 }
704 CATCH_AND_WRAP
705 }
706
714 StringList getCollectionNames()
715 {
716 try {
717 return Name_src(*this, COLLECTION, "%");
718 }
719 CATCH_AND_WRAP
720 }
721
732 TableList getTables()
733 {
734 try {
735 return Table_src(*this, "%");
736 }
737 CATCH_AND_WRAP
738 }
739
750 StringList getTableNames()
751 {
752 try {
753 return Name_src(*this, TABLE, "%");
754 }
755 CATCH_AND_WRAP
756 }
757
775 Table getCollectionAsTable(const string &name, bool check_exists = true);
776
777
788 void dropCollection(const mysqlx::string& name)
789 {
790 try {
791 Schema_detail::drop_collection(name);
792 }
793 CATCH_AND_WRAP
794 }
795
796
797 friend Collection;
798 friend Table;
799
801 friend internal::Schema_detail::Name_src;
802 template <class Base> friend class internal::Sch_object;
804};
805
806
807/*
808 Database objects that belong to some schema
809 ===========================================
810*/
811
812
813namespace internal {
814
815// Common base for schema objects defining the common API methods.
816
817template <class Base = Db_obj_base>
818class Sch_object
819 : public Base
820{
821protected:
822
823 Schema m_schema;
824
825 Sch_object(const Schema &sch, const string &name);
826
827public:
828
829 using string = mysqlx::string;
830
835 const string& getName() const
836 {
837 return Base::m_name;
838 }
839
844 Session& getSession()
845 {
846 return m_schema.getSession();
847 }
848
853 const Schema& getSchema() const
854 {
855 return m_schema;
856 }
857
858protected:
859
860 std::shared_ptr<common::Session_impl> get_session();
861
862 Schema_detail& get_schema()
863 {
864 return m_schema;
865 }
866};
867
868} // internal
869
870
871
872
873
874
875
909class Collection
910 : protected internal::Sch_object<internal::Collection_detail>
911{
912public:
913
914 Collection(const Schema &sch, const string &name)
915 try
916 : Sch_object(sch, name)
917 {}
918 CATCH_AND_WRAP
919
920
921 using Sch_object::getName;
923 using Sch_object::getSchema;
924
925 bool existsInDatabase() const
926 {
927 try {
928 Schema::StringList list(m_schema, Schema::COLLECTION, m_name);
929 return list.begin() != list.end();
930 }
931 CATCH_AND_WRAP
932 }
933
938 uint64_t count();
939
940 /*
941 CRUD operations on a collection
942 -------------------------------
943 */
944
959 CollectionFind find()
960 {
961 try {
962 return CollectionFind(*this);
963 }
964 CATCH_AND_WRAP;
965 }
966
982 CollectionFind find(const string &cond)
983 {
984 try {
985 return CollectionFind(*this, cond);
986 }
987 CATCH_AND_WRAP;
988 }
989
1004 template <typename... Types>
1005 CollectionAdd add(Types... args)
1006 {
1007 try {
1008
1009 CollectionAdd add(*this);
1010 return add.add(args...);
1011 }
1012 CATCH_AND_WRAP;
1013 }
1014
1033 CollectionRemove remove(const string &cond)
1034 {
1035 try {
1036 return CollectionRemove(*this, cond);
1037 }
1038 CATCH_AND_WRAP;
1039 }
1040
1059 CollectionModify modify(const string &expr)
1060 {
1061 try {
1062 return CollectionModify(*this, expr);
1063 }
1064 CATCH_AND_WRAP;
1065 }
1066
1074 DbDoc getOne(const string &id)
1075 {
1076 return find("_id = :id").bind("id", id).execute().fetchOne();
1077 }
1078
1086 Result removeOne(const string &id)
1087 {
1088 return remove("_id = :id").bind("id", id).execute();
1089 }
1090
1107 Result replaceOne(const string &id, Value &&document)
1108 {
1109 try {
1110 return
1111 Collection_detail::add_or_replace_one(id, std::move(document), true);
1112 }
1113 CATCH_AND_WRAP
1114 }
1115
1131 Result addOrReplaceOne(const string &id, Value &&document)
1132 {
1133 try {
1134 return
1135 Collection_detail::add_or_replace_one(id, std::move(document), false);
1136 }
1137 CATCH_AND_WRAP
1138 }
1139
1153 void createIndex(const string &name, const string &idx_spec)
1154 {
1155 try {
1156 Collection_detail::index_create(name, idx_spec);
1157 }
1158 CATCH_AND_WRAP
1159 }
1160
1169 void dropIndex(const string &name)
1170 {
1171 try {
1172 Collection_detail::index_drop(name);
1173 }
1174 CATCH_AND_WRAP
1175 }
1176
1177
1178 friend CollectionFind;
1179 friend CollectionAdd;
1180 friend CollectionRemove;
1181 friend CollectionModify;
1182
1184 friend internal::Crud_factory;
1186};
1187
1188//------------------------------------------------------------------------------
1189
1190
1191template<>
1192inline
1193void CollectionValidation::do_set<CollectionValidation::SCHEMA>(DbDoc schema)
1194{
1195 if(m_data.used.test(CollectionValidation::SCHEMA))
1196 throw_error("Validation schema already set.");
1197 m_data.used.set(CollectionValidation::SCHEMA);
1198 m_data.validation_schema = schema;
1199}
1200
1201template<>
1202inline
1203void CollectionValidation::do_set<CollectionValidation::SCHEMA>(const char* schema)
1204{
1205 do_set<CollectionValidation::SCHEMA>(DbDoc(schema));
1206}
1207
1208template<>
1209inline
1210void CollectionValidation::do_set<CollectionValidation::SCHEMA>(std::string schema)
1211{
1212 do_set<CollectionValidation::SCHEMA>(DbDoc(schema));
1213}
1214
1216template<>
1217inline
1218void CollectionValidation::do_set<CollectionValidation::LEVEL>(Level level)
1219{
1220 if(m_data.used.test(CollectionValidation::LEVEL))
1221 throw_error("Validation level already set.");
1222 m_data.used.set(CollectionValidation::LEVEL);
1223
1224#define SCHEMA_VALIDATION_CASE(x,y) case Level::x: m_data.validation_level = #x; break;
1225
1226 switch (level) {
1227 COLLECTION_VALIDATION_LEVEL(SCHEMA_VALIDATION_CASE)
1228 }
1229}
1230
1231template<>
1232inline
1233void CollectionValidation::do_set<CollectionValidation::LEVEL>(std::string level)
1234{
1235 if(m_data.used.test(CollectionValidation::LEVEL))
1236 throw_error("Validation level already set.");
1237 m_data.used.set(CollectionValidation::LEVEL);
1238
1239 m_data.validation_level = level;
1240
1241}
1242
1243template<>
1244inline
1245void CollectionOptions::do_set<CollectionOptions::REUSE>(bool reuse)
1246{
1247 if(m_data.used[CollectionOptions::REUSE])
1248 throw_error("Option reuse already set.");
1249 m_data.used.set(CollectionOptions::REUSE);
1250 m_data.reuse = reuse;
1251}
1252
1253template<>
1254inline
1255void CollectionOptions::do_set<CollectionOptions::VALIDATION>(CollectionValidation validation)
1256{
1257 if(m_data.used.test(CollectionOptions::VALIDATION) ||
1258 m_data.validation.m_data.used.test(CollectionValidation::LEVEL) ||
1259 m_data.validation.m_data.used.test(CollectionValidation::SCHEMA))
1260 throw_error("Validation already set.");
1261
1262 m_data.used.set(CollectionOptions::VALIDATION);
1263 m_data.validation.m_data.used.set(CollectionValidation::LEVEL);
1264 m_data.validation.m_data.used.set(CollectionValidation::SCHEMA);
1265
1266 m_data.validation = validation;
1268
1269
1270inline
1271Collection Schema::createCollection(const string &name)
1272{
1273 try {
1274 Schema_detail::create_collection(
1275 name,
1277
1278 return Collection(*this, name);
1279 }
1280 CATCH_AND_WRAP
1281}
1282
1283inline
1284Collection Schema::createCollection(const string &name, bool reuse)
1285{
1286 try {
1287 Schema_detail::create_collection(
1288 name,
1290 CollectionOptions::REUSE, reuse)
1291 );
1292
1293 return Collection(*this, name);
1294 }
1295 CATCH_AND_WRAP
1296}
1298
1299template<typename... Rest>
1300inline
1301Collection Schema::createCollection(const string &name, Rest&&... rest)
1302{
1303 try {
1304 Schema_detail::create_collection(name, CollectionOptions(std::forward<Rest>(rest)...));
1305 return Collection(*this, name);
1306 }
1307 CATCH_AND_WRAP
1308}
1309
1310
1311template <typename... Opt>
1312inline
1313void Schema::modifyCollection(const string &name, Opt&&... options)
1314{
1315 try {
1316 Schema_detail::modify_collection(name, CollectionOptions(std::forward<Opt>(options)...));
1317 }
1318 CATCH_AND_WRAP
1319}
1321
1322
1323inline
1324Collection Schema::getCollection(const string &name, bool check_exists)
1325{
1326 Collection coll(*this, name);
1327 if (check_exists && !coll.existsInDatabase())
1328 throw_error("Collection does not exist");
1329 return coll;
1330}
1331
1332
1333
1334/*
1335 Table object
1336 ============
1337*/
1338
1375class Table
1376 : protected internal::Sch_object<>
1377{
1378public:
1379
1380 Table(const Schema &sch, const string &name)
1381 : Sch_object(sch, name)
1382 {}
1383
1384 Table(const Schema &sch, const string &name, bool is_view)
1385 : Sch_object(sch, name)
1386 {
1387 m_type = is_view ? VIEW : TABLE;
1388 }
1389
1390
1391 using Sch_object::getName;
1393 using Sch_object::getSchema;
1394
1395
1396 bool existsInDatabase() const;
1397
1398
1405 bool isView();
1406
1407
1412 uint64_t count()
1413 {
1414 try {
1415 RowResult cnt = select("count(*)").execute();
1416 return cnt.fetchOne()[0].get<uint64_t>();
1417 }
1418 CATCH_AND_WRAP
1419 }
1420
1421
1422 /*
1423 CRUD operations
1424 ---------------
1425 */
1426
1445 TableInsert insert()
1446 {
1447 try {
1448 return TableInsert(*this);
1449 }
1450 CATCH_AND_WRAP;
1451 }
1452
1474 template <class... T>
1475 TableInsert insert(const T&... t)
1476 {
1477 try {
1478 return TableInsert(*this, t...);
1479 }
1480 CATCH_AND_WRAP;
1481 }
1482
1503 template<typename ...PROJ>
1504 TableSelect select(const PROJ&...proj)
1505 {
1506 try {
1507 return TableSelect(*this, proj...);
1508 }
1509 CATCH_AND_WRAP;
1510 }
1511
1525 TableRemove remove()
1526 {
1527 try {
1528 return TableRemove(*this);
1529 }
1530 CATCH_AND_WRAP;
1531 }
1532
1547 TableUpdate update()
1548 {
1549 try {
1550 return TableUpdate(*this);
1551 }
1552 CATCH_AND_WRAP;
1553 }
1554
1555private:
1556
1557 enum { TABLE, VIEW, UNKNOWN } m_type = UNKNOWN;
1558
1559 friend TableSelect;
1560 friend TableInsert;
1561 friend TableRemove;
1562 friend TableUpdate;
1563
1565 friend internal::Crud_factory;
1567};
1568
1569
1570inline
1571bool Table::existsInDatabase() const
1572{
1573 try {
1574 /*
1575 Note: When checking existence, we also determine if this is a view or
1576 a plain table because this information is fetched from the server when
1577 querying for a list of tables.
1578 */
1579 Schema::TableList list(m_schema, m_name);
1580 auto it = list.begin();
1581
1582 if (!(it != list.end()))
1583 return false;
1584
1585 const_cast<Table*>(this)->m_type = (*it).isView() ? VIEW : TABLE;
1586 return true;
1587 }
1588 CATCH_AND_WRAP
1590
1591
1592inline
1593bool Table::isView()
1594{
1595 try {
1596 /*
1597 If view status was not determined yet, do existence check which
1598 determines it as a side effect.
1599 */
1600 if (UNKNOWN == m_type)
1601 if (!existsInDatabase())
1602 throw_error("Table does not exist");
1603 return VIEW == m_type;
1604 }
1605 CATCH_AND_WRAP
1607
1608
1609inline
1610Table Schema::getTable(const string &name, bool check_exists)
1611{
1612 Table tbl(*this, name);
1613 if (check_exists && !tbl.existsInDatabase())
1614 throw_error("Table does not exist");
1615 return tbl;
1617
1618
1619inline
1620Table Schema::getCollectionAsTable(const string &name, bool check_exists)
1621{
1622 if (check_exists && !getCollection(name).existsInDatabase())
1623 throw_error("Collection does not exist");
1624 return { *this, name };
1626
1627
1628inline
1629uint64_t Collection::count()
1630{
1631 return m_schema.getCollectionAsTable(m_name).count();
1632}
1633
1634
1635using SqlStatement = internal::SQL_statement;
1636
1637
1672class Session
1673 : private internal::Session_detail
1674{
1675public:
1676
1677
1682 Session(SessionSettings settings)
1683 try
1684 : Session_detail(settings)
1685 {}
1686 CATCH_AND_WRAP
1687
1688
1717 template<typename...T>
1718 Session(T...options)
1719 try
1720 : Session(SessionSettings(options...))
1721 {}CATCH_AND_WRAP
1722
1723
1724 Session(Session &&other)
1725 try
1726 : internal::Session_detail(std::move(other))
1727 {}CATCH_AND_WRAP
1728
1729
1730 Session(Client &client);
1731
1740 Schema createSchema(const string &name, bool reuse = false)
1741 {
1742 try {
1743 Session_detail::create_schema(name, reuse);
1744 return Schema(*this, name);
1745 }
1746 CATCH_AND_WRAP
1747 }
1748
1762 Schema getSchema(const string &name, bool check_exists = false)
1763 {
1764 Schema sch(*this, name);
1765 if (check_exists && !sch.existsInDatabase())
1766 throw_error("Schema does not exist");
1767 return sch;
1768 }
1769
1774 Schema getDefaultSchema()
1775 {
1776 return Schema(*this, getDefaultSchemaName());
1777 }
1778
1783 string getDefaultSchemaName()
1784 {
1785 try {
1786 return Session_detail::get_default_schema_name();
1787 }
1788 CATCH_AND_WRAP
1789 }
1790
1798 SchemaList getSchemas()
1799 {
1800 try {
1801 return Schema_src(*this, "%");
1802 }
1803 CATCH_AND_WRAP
1804 }
1805
1806 // TODO: Should we have getSchemaNames() too?
1807
1814 void dropSchema(const string &name)
1815 {
1816 try {
1817 Session_detail::drop_schema(name);
1818 }
1819 CATCH_AND_WRAP
1820 }
1821
1822
1835 SqlStatement sql(const string &query)
1836 {
1837 try {
1838 return SqlStatement(this, query);
1839 }
1840 CATCH_AND_WRAP
1841 }
1842
1849 void startTransaction()
1850 {
1851 try {
1852 Session_detail::start_transaction();
1853 }
1854 CATCH_AND_WRAP
1855 }
1856
1864 void commit()
1865 {
1866 try {
1867 Session_detail::commit();
1868 }
1869 CATCH_AND_WRAP
1870 }
1871
1880 void rollback()
1881 {
1882 try {
1883 Session_detail::rollback();
1884 }
1885 CATCH_AND_WRAP
1886 }
1887
1897 void rollbackTo(const string &savepoint)
1898 {
1899 try {
1900 if (savepoint.empty())
1901 throw_error("Invalid empty save point name");
1902 Session_detail::rollback(savepoint);
1903 }
1904 CATCH_AND_WRAP
1905 }
1906
1907
1920 string setSavepoint(const string &savepoint)
1921 {
1922 try {
1923 if (savepoint.empty())
1924 throw_error("Invalid empty save point name");
1925 return Session_detail::savepoint_set(savepoint);
1926 }
1927 CATCH_AND_WRAP
1928 }
1929
1930
1943 string setSavepoint()
1944 {
1945 try {
1946 return Session_detail::savepoint_set();
1947 }
1948 CATCH_AND_WRAP
1949 }
1950
1951
1960 void releaseSavepoint(const string &savepoint)
1961 {
1962 try {
1963 if (savepoint.empty())
1964 throw_error("Invalid empty save point name");
1965 Session_detail::savepoint_remove(savepoint);
1966 }
1967 CATCH_AND_WRAP
1968 }
1969
1970
1978 void close()
1979 {
1980 try {
1981 Session_detail::close();
1982 }
1983 CATCH_AND_WRAP
1984 }
1985
1986protected:
1987
1988 using internal::Session_detail::m_impl;
1989
1990public:
1991
1992 friend Schema;
1993 friend Collection;
1994 friend Table;
1995 friend Result;
1996 friend RowResult;
1997
1999 friend internal::Session_detail;
2000 friend internal::Crud_factory;
2001 friend internal::Result_detail;
2002 template <class Base> friend class internal::Sch_object;
2004};
2005
2006
2042class Client : public internal::Client_detail
2043{
2044public:
2045
2046 Client(ClientSettings settings)
2047 try
2048 : Client_detail(settings)
2049 {}
2050 CATCH_AND_WRAP
2051
2052 Client(SessionSettings &settings)
2053 try
2054 : Client_detail(settings)
2055 {}
2056 CATCH_AND_WRAP
2057
2058 template<typename...T>
2059 Client(T...options)
2060 : Client(ClientSettings(options...))
2061 {}
2062
2063
2064 Session getSession()
2065 {
2066 return *this;
2067 }
2068
2069};
2070
2071
2072/*
2073 Session
2074*/
2075
2076inline
2077Session::Session(Client &client)
2078try
2079 : internal::Session_detail(client.get_session_pool())
2080{}CATCH_AND_WRAP
2081
2088template<class ...P>
2089Session getSession(P...p)
2090{
2091 return Session(p...);
2092}
2093
2100template<class ...P>
2101Client getClient(P...p)
2102{
2103 return Client(p...);
2104}
2105
2106
2107/*
2108 Schema class implementation
2109*/
2110
2111inline
2112Schema::Schema(Session &sess, const string &name)
2113 : Schema_detail(sess.m_impl, name)
2114 , m_sess(&sess)
2115{}
2116
2117
2118template <class Base>
2119inline
2120internal::Sch_object<Base>::Sch_object(const Schema &sch, const string &name)
2121 : Base(sch.getSession().m_impl, name)
2122 , m_schema(sch)
2123{}
2124
2125
2126template <class Base>
2127inline
2128std::shared_ptr<common::Session_impl>
2129internal::Sch_object<Base>::get_session()
2130{
2131 assert(m_schema.m_sess);
2132 return m_schema.m_sess->m_impl;
2133}
2134
2135
2136MYSQLX_ABI_END(2,0)
2137} // mysqlx
2138
2139#endif
CollectionAdd & add(const It &begin, const It &end)
Add all documents from a range defined by two iterators.
Definition: collection_crud.h:170
Represents a collection of documents in a schema.
Definition: xdevapi.h:908
void createIndex(const string &name, const string &idx_spec)
Create index on the collection.
Definition: xdevapi.h:1150
void dropIndex(const string &name)
Drop index on the collection.
Definition: xdevapi.h:1166
Result replaceOne(const string &id, Value &&document)
Replace the document with the given id by a new one.
Definition: xdevapi.h:1104
CollectionAdd add(Types... args)
Return an operation which adds documents to the collection.
Definition: xdevapi.h:1002
Result removeOne(const string &id)
Remove the document with the given id.
Definition: xdevapi.h:1083
uint64_t count()
Get the number of documents in the collection.
Definition: xdevapi.h:1625
Result addOrReplaceOne(const string &id, Value &&document)
Add a new document or replace an existing document with the given id.
Definition: xdevapi.h:1128
CollectionModify modify(const string &expr)
Return an operation which modifies documents that satisfy given criteria.
Definition: xdevapi.h:1056
CollectionFind find()
Return an operation which fetches all documents from the collection.
Definition: xdevapi.h:956
DbDoc getOne(const string &id)
Return the document with the given id.
Definition: xdevapi.h:1071
CollectionRemove remove(const string &cond)
Return an operation which removes documents satisfying given criteria.
Definition: xdevapi.h:1030
The CollectionOptions class defines collection create/modify options.
Definition: xdevapi.h:298
Option
Collection options
Definition: xdevapi.h:309
void set(Rest &&... rest)
Set list of option and value pairs.
Definition: xdevapi.h:462
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 key-value pairs where value can be a scalar or another document.
Definition: document.h:83
Value & get(col_count_t pos)
Get reference to row field at position pos.
Definition: row.h:159
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
Represents a database schema.
Definition: xdevapi.h:556
Table getCollectionAsTable(const string &name, bool check_exists=true)
Return a table corresponding to the given collection.
Definition: xdevapi.h:1616
const string & getName() const
Get schema name.
Definition: xdevapi.h:580
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:1606
Schema(Session &sess, const string &name)
Construct an object representing the named schema.
Definition: xdevapi.h:2108
void dropCollection(const mysqlx::string &name)
Drop the given collection from the schema.
Definition: xdevapi.h:785
bool existsInDatabase() const
Check if this schema exists in the database.
Definition: xdevapi.h:605
Collection getCollection(const string &name, bool check_exists=false)
Return an object representing a collection with the given name.
Definition: xdevapi.h:1320
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:696
Collection createCollection(const string &name)
Create a new collection in the schema.
Definition: xdevapi.h:1267
StringList getCollectionNames()
Get a list of names of all collections in the schema.
Definition: xdevapi.h:711
TableList getTables()
Get a list of all tables and views in the schema.
Definition: xdevapi.h:729
StringList getTableNames()
Get a list of names of all tables and views in the schema.
Definition: xdevapi.h:747
Represents a session which gives access to data stored in a data store.
Definition: xdevapi.h:1670
void rollbackTo(const string &savepoint)
Roll back opened transaction to specified savepoint.
Definition: xdevapi.h:1893
void rollback()
Roll back opened transaction, if any.
Definition: xdevapi.h:1876
Schema getSchema(const string &name, bool check_exists=false)
Return an object representing a schema with the given name.
Definition: xdevapi.h:1758
Session(SessionSettings settings)
Create a session specified by a SessionSettings object.
Definition: xdevapi.h:1678
SqlStatement sql(const string &query)
Return an operation which executes an arbitrary SQL statement.
Definition: xdevapi.h:1831
string getDefaultSchemaName()
Get the name of the default schema specified when the session was created.
Definition: xdevapi.h:1779
void close()
Close this session.
Definition: xdevapi.h:1974
Schema createSchema(const string &name, bool reuse=false)
Create a new schema.
Definition: xdevapi.h:1736
void releaseSavepoint(const string &savepoint)
Releases savepoint previously added by setSavepoint().
Definition: xdevapi.h:1956
void startTransaction()
Start a new transaction.
Definition: xdevapi.h:1845
void commit()
Commit opened transaction, if any.
Definition: xdevapi.h:1860
Schema getDefaultSchema()
Get the default schema specified when the session was created.
Definition: xdevapi.h:1770
SchemaList getSchemas()
Get a list of all database schemas.
Definition: xdevapi.h:1794
string setSavepoint(const string &savepoint)
Sets a named transaction savepoint with a name as identifier.
Definition: xdevapi.h:1916
void dropSchema(const string &name)
Drop the named schema.
Definition: xdevapi.h:1810
Represents session options to be passed at session creation time.
Definition: settings.h:451
Represents a table in a schema.
Definition: xdevapi.h:1373
TableInsert insert()
Return an operation which inserts rows into the full table without restricting the columns.
Definition: xdevapi.h:1441
TableSelect select(const PROJ &...proj)
Return an operation which selects rows from the table.
Definition: xdevapi.h:1500
TableUpdate update()
Return an operation which updates rows in the table.
Definition: xdevapi.h:1543
TableRemove remove()
Return an operation which removes rows from the table.
Definition: xdevapi.h:1521
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:2097
internal::Expression expr(std::string &&e)
Function which indicates that a given string should be treated as expression.
Definition: document.h:636
Session getSession(P...p)
Function to get Session object.
Definition: xdevapi.h:2085
Classes used to access query and command execution results.
Crud operations on tables.