MySQL Connector/C++ 9.1.0
MySQL connector library for C and C++ applications
xdevapi.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015, 2024, 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 designed to work with certain software (including
9 * but not limited to OpenSSL) that is licensed under separate terms, as
10 * designated in a particular file or component or in included license
11 * documentation. The authors of MySQL hereby grant you an additional
12 * permission to link the program and your derivative works with the
13 * separately licensed software that they have either included with
14 * the program or referenced in the documentation.
15 *
16 * Without limiting anything contained in the foregoing, this file,
17 * which is part of Connector/C++, is also subject to the
18 * Universal FOSS Exception, version 1.0, a copy of which can be found at
19 * https://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
1171 void dropIndex(const string &name)
1172 {
1173 try {
1174 Collection_detail::index_drop(name);
1175 }
1176 CATCH_AND_WRAP
1177 }
1178
1179
1180 friend CollectionFind;
1181 friend CollectionAdd;
1182 friend CollectionRemove;
1183 friend CollectionModify;
1184
1186 friend internal::Crud_factory;
1188};
1189
1190//------------------------------------------------------------------------------
1191
1192
1193template<>
1194inline
1195void CollectionValidation::do_set<CollectionValidation::SCHEMA>(DbDoc schema)
1196{
1197 if(m_data.used.test(CollectionValidation::SCHEMA))
1198 throw_error("Validation schema already set.");
1199 m_data.used.set(CollectionValidation::SCHEMA);
1200 m_data.validation_schema = schema;
1201}
1202
1203template<>
1204inline
1205void CollectionValidation::do_set<CollectionValidation::SCHEMA>(const char* schema)
1206{
1207 do_set<CollectionValidation::SCHEMA>(DbDoc(schema));
1208}
1209
1210template<>
1211inline
1212void CollectionValidation::do_set<CollectionValidation::SCHEMA>(std::string schema)
1213{
1214 do_set<CollectionValidation::SCHEMA>(DbDoc(schema));
1215}
1216
1218template<>
1219inline
1220void CollectionValidation::do_set<CollectionValidation::LEVEL>(Level level)
1221{
1222 if(m_data.used.test(CollectionValidation::LEVEL))
1223 throw_error("Validation level already set.");
1224 m_data.used.set(CollectionValidation::LEVEL);
1225
1226#define SCHEMA_VALIDATION_CASE(x,y) case Level::x: m_data.validation_level = #x; break;
1227
1228 switch (level) {
1229 COLLECTION_VALIDATION_LEVEL(SCHEMA_VALIDATION_CASE)
1230 }
1231}
1232
1233template<>
1234inline
1235void CollectionValidation::do_set<CollectionValidation::LEVEL>(std::string level)
1236{
1237 if(m_data.used.test(CollectionValidation::LEVEL))
1238 throw_error("Validation level already set.");
1239 m_data.used.set(CollectionValidation::LEVEL);
1240
1241 m_data.validation_level = level;
1242
1243}
1244
1245template<>
1246inline
1247void CollectionOptions::do_set<CollectionOptions::REUSE>(bool reuse)
1248{
1249 if(m_data.used[CollectionOptions::REUSE])
1250 throw_error("Option reuse already set.");
1251 m_data.used.set(CollectionOptions::REUSE);
1252 m_data.reuse = reuse;
1253}
1254
1255template<>
1256inline
1257void CollectionOptions::do_set<CollectionOptions::VALIDATION>(CollectionValidation validation)
1258{
1259 if(m_data.used.test(CollectionOptions::VALIDATION) ||
1260 m_data.validation.m_data.used.test(CollectionValidation::LEVEL) ||
1261 m_data.validation.m_data.used.test(CollectionValidation::SCHEMA))
1262 throw_error("Validation already set.");
1263
1264 m_data.used.set(CollectionOptions::VALIDATION);
1265 m_data.validation.m_data.used.set(CollectionValidation::LEVEL);
1266 m_data.validation.m_data.used.set(CollectionValidation::SCHEMA);
1267
1268 m_data.validation = validation;
1270
1271
1272inline
1273Collection Schema::createCollection(const string &name)
1274{
1275 try {
1276 Schema_detail::create_collection(
1277 name,
1279
1280 return Collection(*this, name);
1281 }
1282 CATCH_AND_WRAP
1283}
1284
1285inline
1286Collection Schema::createCollection(const string &name, bool reuse)
1287{
1288 try {
1289 Schema_detail::create_collection(
1290 name,
1292 CollectionOptions::REUSE, reuse)
1293 );
1294
1295 return Collection(*this, name);
1296 }
1297 CATCH_AND_WRAP
1298}
1300
1301template<typename... Rest>
1302inline
1303Collection Schema::createCollection(const string &name, Rest&&... rest)
1304{
1305 try {
1306 Schema_detail::create_collection(name, CollectionOptions(std::forward<Rest>(rest)...));
1307 return Collection(*this, name);
1308 }
1309 CATCH_AND_WRAP
1310}
1311
1312
1313template <typename... Opt>
1314inline
1315void Schema::modifyCollection(const string &name, Opt&&... options)
1316{
1317 try {
1318 Schema_detail::modify_collection(name, CollectionOptions(std::forward<Opt>(options)...));
1319 }
1320 CATCH_AND_WRAP
1321}
1323
1324
1325inline
1326Collection Schema::getCollection(const string &name, bool check_exists)
1327{
1328 Collection coll(*this, name);
1329 if (check_exists && !coll.existsInDatabase())
1330 throw_error("Collection does not exist");
1331 return coll;
1332}
1333
1334
1335
1336/*
1337 Table object
1338 ============
1339*/
1340
1377class Table
1378 : protected internal::Sch_object<>
1379{
1380public:
1381
1382 Table(const Schema &sch, const string &name)
1383 : Sch_object(sch, name)
1384 {}
1385
1386 Table(const Schema &sch, const string &name, bool is_view)
1387 : Sch_object(sch, name)
1388 {
1389 m_type = is_view ? VIEW : TABLE;
1390 }
1391
1392
1393 using Sch_object::getName;
1395 using Sch_object::getSchema;
1396
1397
1398 bool existsInDatabase() const;
1399
1400
1407 bool isView();
1408
1409
1414 uint64_t count()
1415 {
1416 try {
1417 RowResult cnt = select("count(*)").execute();
1418 return cnt.fetchOne()[0].get<uint64_t>();
1419 }
1420 CATCH_AND_WRAP
1421 }
1422
1423
1424 /*
1425 CRUD operations
1426 ---------------
1427 */
1428
1447 TableInsert insert()
1448 {
1449 try {
1450 return TableInsert(*this);
1451 }
1452 CATCH_AND_WRAP;
1453 }
1454
1476 template <class... T>
1477 TableInsert insert(const T&... t)
1478 {
1479 try {
1480 return TableInsert(*this, t...);
1481 }
1482 CATCH_AND_WRAP;
1483 }
1484
1505 template<typename ...PROJ>
1506 TableSelect select(const PROJ&...proj)
1507 {
1508 try {
1509 return TableSelect(*this, proj...);
1510 }
1511 CATCH_AND_WRAP;
1512 }
1513
1527 TableRemove remove()
1528 {
1529 try {
1530 return TableRemove(*this);
1531 }
1532 CATCH_AND_WRAP;
1533 }
1534
1549 TableUpdate update()
1550 {
1551 try {
1552 return TableUpdate(*this);
1553 }
1554 CATCH_AND_WRAP;
1555 }
1556
1557private:
1558
1559 enum { TABLE, VIEW, UNKNOWN } m_type = UNKNOWN;
1560
1561 friend TableSelect;
1562 friend TableInsert;
1563 friend TableRemove;
1564 friend TableUpdate;
1565
1567 friend internal::Crud_factory;
1569};
1570
1571
1572inline
1573bool Table::existsInDatabase() const
1574{
1575 try {
1576 /*
1577 Note: When checking existence, we also determine if this is a view or
1578 a plain table because this information is fetched from the server when
1579 querying for a list of tables.
1580 */
1581 Schema::TableList list(m_schema, m_name);
1582 auto it = list.begin();
1583
1584 if (!(it != list.end()))
1585 return false;
1586
1587 const_cast<Table*>(this)->m_type = (*it).isView() ? VIEW : TABLE;
1588 return true;
1589 }
1590 CATCH_AND_WRAP
1592
1593
1594inline
1595bool Table::isView()
1596{
1597 try {
1598 /*
1599 If view status was not determined yet, do existence check which
1600 determines it as a side effect.
1601 */
1602 if (UNKNOWN == m_type)
1603 if (!existsInDatabase())
1604 throw_error("Table does not exist");
1605 return VIEW == m_type;
1606 }
1607 CATCH_AND_WRAP
1609
1610
1611inline
1612Table Schema::getTable(const string &name, bool check_exists)
1613{
1614 Table tbl(*this, name);
1615 if (check_exists && !tbl.existsInDatabase())
1616 throw_error("Table does not exist");
1617 return tbl;
1619
1620
1621inline
1622Table Schema::getCollectionAsTable(const string &name, bool check_exists)
1623{
1624 if (check_exists && !getCollection(name).existsInDatabase())
1625 throw_error("Collection does not exist");
1626 return { *this, name };
1628
1629
1630inline
1631uint64_t Collection::count()
1632{
1633 return m_schema.getCollectionAsTable(m_name).count();
1634}
1635
1636
1637using SqlStatement = internal::SQL_statement;
1638
1639
1674class Session
1675 : private internal::Session_detail
1676{
1677public:
1678
1679
1684 Session(SessionSettings settings)
1685 try
1686 : Session_detail(settings)
1687 {}
1688 CATCH_AND_WRAP
1689
1690
1719 template<typename...T>
1720 Session(T...options)
1721 try
1722 : Session(SessionSettings(options...))
1723 {}CATCH_AND_WRAP
1724
1725
1726 Session(Session &&other)
1727 try
1728 : internal::Session_detail(std::move(other))
1729 {}CATCH_AND_WRAP
1730
1731
1732 Session(Client &client);
1733
1742 Schema createSchema(const string &name, bool reuse = false)
1743 {
1744 try {
1745 Session_detail::create_schema(name, reuse);
1746 return Schema(*this, name);
1747 }
1748 CATCH_AND_WRAP
1749 }
1750
1764 Schema getSchema(const string &name, bool check_exists = false)
1765 {
1766 Schema sch(*this, name);
1767 if (check_exists && !sch.existsInDatabase())
1768 throw_error("Schema does not exist");
1769 return sch;
1770 }
1771
1776 Schema getDefaultSchema()
1777 {
1778 return Schema(*this, getDefaultSchemaName());
1779 }
1780
1785 string getDefaultSchemaName()
1786 {
1787 try {
1788 return Session_detail::get_default_schema_name();
1789 }
1790 CATCH_AND_WRAP
1791 }
1792
1800 SchemaList getSchemas()
1801 {
1802 try {
1803 return Schema_src(*this, "%");
1804 }
1805 CATCH_AND_WRAP
1806 }
1807
1808 // TODO: Should we have getSchemaNames() too?
1809
1816 void dropSchema(const string &name)
1817 {
1818 try {
1819 Session_detail::drop_schema(name);
1820 }
1821 CATCH_AND_WRAP
1822 }
1823
1824
1837 SqlStatement sql(const string &query)
1838 {
1839 try {
1840 return SqlStatement(this, query);
1841 }
1842 CATCH_AND_WRAP
1843 }
1844
1851 void startTransaction()
1852 {
1853 try {
1854 Session_detail::start_transaction();
1855 }
1856 CATCH_AND_WRAP
1857 }
1858
1866 void commit()
1867 {
1868 try {
1869 Session_detail::commit();
1870 }
1871 CATCH_AND_WRAP
1872 }
1873
1882 void rollback()
1883 {
1884 try {
1885 Session_detail::rollback();
1886 }
1887 CATCH_AND_WRAP
1888 }
1889
1899 void rollbackTo(const string &savepoint)
1900 {
1901 try {
1902 if (savepoint.empty())
1903 throw_error("Invalid empty save point name");
1904 Session_detail::rollback(savepoint);
1905 }
1906 CATCH_AND_WRAP
1907 }
1908
1909
1922 string setSavepoint(const string &savepoint)
1923 {
1924 try {
1925 if (savepoint.empty())
1926 throw_error("Invalid empty save point name");
1927 return Session_detail::savepoint_set(savepoint);
1928 }
1929 CATCH_AND_WRAP
1930 }
1931
1932
1945 string setSavepoint()
1946 {
1947 try {
1948 return Session_detail::savepoint_set();
1949 }
1950 CATCH_AND_WRAP
1951 }
1952
1953
1962 void releaseSavepoint(const string &savepoint)
1963 {
1964 try {
1965 if (savepoint.empty())
1966 throw_error("Invalid empty save point name");
1967 Session_detail::savepoint_remove(savepoint);
1968 }
1969 CATCH_AND_WRAP
1970 }
1971
1972
1980 void close()
1981 {
1982 try {
1983 Session_detail::close();
1984 }
1985 CATCH_AND_WRAP
1986 }
1987
1988protected:
1989
1990 using internal::Session_detail::m_impl;
1991
1992public:
1993
1994 friend Schema;
1995 friend Collection;
1996 friend Table;
1997 friend Result;
1998 friend RowResult;
1999
2001 friend internal::Session_detail;
2002 friend internal::Crud_factory;
2003 friend internal::Result_detail;
2004 template <class Base> friend class internal::Sch_object;
2006};
2007
2008
2044class Client : public internal::Client_detail
2045{
2046public:
2047
2048 Client(ClientSettings settings)
2049 try
2050 : Client_detail(settings)
2051 {}
2052 CATCH_AND_WRAP
2053
2054 Client(SessionSettings &settings)
2055 try
2056 : Client_detail(settings)
2057 {}
2058 CATCH_AND_WRAP
2059
2060 template<typename...T>
2061 Client(T...options)
2062 : Client(ClientSettings(options...))
2063 {}
2064
2065
2066 Session getSession()
2067 {
2068 return *this;
2069 }
2070
2071};
2072
2073
2074/*
2075 Session
2076*/
2077
2078inline
2079Session::Session(Client &client)
2080try
2081 : internal::Session_detail(client.get_session_pool())
2082{}CATCH_AND_WRAP
2083
2090template<class ...P>
2091Session getSession(P...p)
2092{
2093 return Session(p...);
2094}
2095
2102template<class ...P>
2103Client getClient(P...p)
2104{
2105 return Client(p...);
2106}
2107
2108
2109/*
2110 Schema class implementation
2111*/
2112
2113inline
2114Schema::Schema(Session &sess, const string &name)
2115 : Schema_detail(sess.m_impl, name)
2116 , m_sess(&sess)
2117{}
2118
2119
2120template <class Base>
2121inline
2122internal::Sch_object<Base>::Sch_object(const Schema &sch, const string &name)
2123 : Base(sch.getSession().m_impl, name)
2124 , m_schema(sch)
2125{}
2126
2127
2128template <class Base>
2129inline
2130std::shared_ptr<common::Session_impl>
2131internal::Sch_object<Base>::get_session()
2132{
2133 assert(m_schema.m_sess);
2134 return m_schema.m_sess->m_impl;
2135}
2136
2137
2138MYSQLX_ABI_END(2,0)
2139} // mysqlx
2140
2141#endif
CollectionAdd & add(const It &begin, const It &end)
Add all documents from a range defined by two iterators.
Definition: collection_crud.h:171
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:1168
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:1627
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:1618
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:1608
Schema(Session &sess, const string &name)
Construct an object representing the named schema.
Definition: xdevapi.h:2110
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:1322
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:1269
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:1672
void rollbackTo(const string &savepoint)
Roll back opened transaction to specified savepoint.
Definition: xdevapi.h:1895
void rollback()
Roll back opened transaction, if any.
Definition: xdevapi.h:1878
Schema getSchema(const string &name, bool check_exists=false)
Return an object representing a schema with the given name.
Definition: xdevapi.h:1760
Session(SessionSettings settings)
Create a session specified by a SessionSettings object.
Definition: xdevapi.h:1680
SqlStatement sql(const string &query)
Return an operation which executes an arbitrary SQL statement.
Definition: xdevapi.h:1833
string getDefaultSchemaName()
Get the name of the default schema specified when the session was created.
Definition: xdevapi.h:1781
void close()
Close this session.
Definition: xdevapi.h:1976
Schema createSchema(const string &name, bool reuse=false)
Create a new schema.
Definition: xdevapi.h:1738
void releaseSavepoint(const string &savepoint)
Releases savepoint previously added by setSavepoint().
Definition: xdevapi.h:1958
void startTransaction()
Start a new transaction.
Definition: xdevapi.h:1847
void commit()
Commit opened transaction, if any.
Definition: xdevapi.h:1862
Schema getDefaultSchema()
Get the default schema specified when the session was created.
Definition: xdevapi.h:1772
SchemaList getSchemas()
Get a list of all database schemas.
Definition: xdevapi.h:1796
string setSavepoint(const string &savepoint)
Sets a named transaction savepoint with a name as identifier.
Definition: xdevapi.h:1918
void dropSchema(const string &name)
Drop the named schema.
Definition: xdevapi.h:1812
Represents a table in a schema.
Definition: xdevapi.h:1375
TableInsert insert()
Return an operation which inserts rows into the full table without restricting the columns.
Definition: xdevapi.h:1443
TableSelect select(const PROJ &...proj)
Return an operation which selects rows from the table.
Definition: xdevapi.h:1502
TableUpdate update()
Return an operation which updates rows in the table.
Definition: xdevapi.h:1545
TableRemove remove()
Return an operation which removes rows from the table.
Definition: xdevapi.h:1523
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:2099
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:2087
Classes used to access query and command execution results.
Crud operations on tables.