35#error This header can be only used with C++ code
93#include "devapi/common.h"
98#include "devapi/detail/session.h"
107template <
class Base>
class Sch_object;
114class CollectionOptions;
125#define COLLECTION_VALIDATION_ENUM(x,y) x=y,
136 COLLECTION_VALIDATION_LEVEL(COLLECTION_VALIDATION_ENUM)
147 COLLECTION_VALIDATION_OPTION(COLLECTION_VALIDATION_ENUM)
155 std::string validation_level;
156 DbDoc validation_schema;
157 std::bitset<LAST> used;
205 CollectionValidation(DbDoc doc)
212 _set(LEVEL, doc[el].get<std::string>());
213 }
catch (
const Error& e)
215 std::string err(
"Unexpected level type: ");
217 throw Error(err.c_str());
220 else if(el ==
"schema")
222 _set(SCHEMA,doc[el].get<DbDoc>());
225 std::string err(
"Unexpected schema validation field ");
227 throw Error(err.c_str());
237 template<
typename... Rest>
238 CollectionValidation(Option opt, Rest&&... rest)
240 set(opt, std::forward<Rest>(rest)...);
247 template<
typename... Rest>
248 void set(Rest&&... options)
250 Data tmp_data(m_data);
252 _set(std::forward<Rest>(options)...);
263 template<
typename T,
typename... Rest>
264 void _set(Option opt, T&& v, Rest&&... options)
266#define SCHEMA_VALIDATION_SET(x,y) case CollectionValidation::x:\
267 do_set<CollectionValidation::x>(std::forward<T>(v)); break;
271 COLLECTION_VALIDATION_OPTION(SCHEMA_VALIDATION_SET)
272 case CollectionValidation::LAST: throw_error(
"Invalid option."); ;
break;
275 _set(std::forward<Rest>(options)...);
281 template<CollectionVal
idation::Option,
typename T>
284 throw_error(
"Invalid option value type.");
290 friend CollectionOptions;
292 friend mysqlx::internal::Schema_detail;
301class CollectionOptions
305#define COLLECTION_OPTIONS_ENUM(x,y) x=y,
314 COLLECTION_OPTIONS_OPTION(COLLECTION_OPTIONS_ENUM)
322 std::bitset<LAST> used;
336 CollectionOptions(
const std::string& options)
337 : CollectionOptions(DbDoc(options))
380 CollectionOptions(DbDoc options)
382 for(
auto el : options)
384 if(el ==
"reuseExisting")
387 _set(REUSE, options[
"reuseExisting"].get<bool>());
388 }
catch (
const Error& e)
390 std::string err(
"Wrong value for reuseExisting option: ");
392 throw Error(err.c_str());
395 else if(el ==
"validation")
397 _set(VALIDATION, CollectionValidation(options[
"validation"].get<DbDoc>()));
400 std::string err(
"Unexpected collection option ");
402 throw Error(err.c_str());
407 CollectionOptions(CollectionValidation validation)
409 set(VALIDATION, validation);
448 template<
typename... Rest>
449 CollectionOptions(Option opt, Rest&&... rest)
451 set(opt, std::forward<Rest>(rest)...);
455 template<
typename... Rest>
456 CollectionOptions(CollectionValidation::Option opt, Rest&&... rest)
458 set(opt, std::forward<Rest>(rest)...);
466 template<
typename... Rest>
467 void set(Rest&&... rest)
469 Data tmp_data(m_data);
471 _set(std::forward<Rest>(rest)...);
473 m_data = std::move(tmp_data);
484 template<
typename T,
typename... Rest>
485 void _set(Option opt, T&& v, Rest&&... rest)
487#define COLLECTION_OPTIONS_SET(x,y) case x:\
488 do_set<x>(std::forward<T>(v)); break;
492 COLLECTION_OPTIONS_OPTION(COLLECTION_OPTIONS_SET)
493 case LAST: throw_error(
"Invalid option."); ;
break;
496 _set(std::forward<Rest>(rest)...);
500 template<
typename T,
typename... Rest>
501 void _set(CollectionValidation::Option opt, T&& v, Rest&&... rest)
503 m_data.validation._set(opt, std::forward<T>(v));
504 _set(std::forward<Rest>(rest)...);
509 template<CollectionOptions::Option O,
typename T>
512 throw_error(
"Invalid option value type.");
518 friend mysqlx::internal::Schema_detail;
562 :
protected internal::Schema_detail
572 Schema(Session &sess,
const string &name);
619 internal::Session_detail::Name_src list(*m_sess, m_name);
620 list.iterator_start();
621 return list.iterator_next();
647 template<
typename... Rest>
659 template<
typename... Rest>
676 Collection
getCollection(
const string &name,
bool check_exists =
false);
694 Table
getTable(
const string &name,
bool check_exists =
false);
706 return Collection_src(*
this,
"%");
721 return Name_src(*
this, COLLECTION,
"%");
739 return Table_src(*
this,
"%");
757 return Name_src(*
this, TABLE,
"%");
795 Schema_detail::drop_collection(name);
805 friend internal::Schema_detail::Name_src;
806 template <
class Base>
friend class internal::Sch_object;
821template <
class Base = Db_obj_base>
829 Sch_object(
const Schema &sch,
const string &name);
839 const string& getName()
const
850 return m_schema.getSession();
857 const Schema& getSchema()
const
864 std::shared_ptr<common::Session_impl> get_session();
866 Schema_detail& get_schema()
914 :
protected internal::Sch_object<internal::Collection_detail>
918 Collection(
const Schema &sch,
const string &name)
920 : Sch_object(sch, name)
925 using Sch_object::getName;
927 using Sch_object::getSchema;
929 bool existsInDatabase()
const
932 Schema::StringList list(m_schema, Schema::COLLECTION, m_name);
933 return list.begin() != list.end();
963 CollectionFind
find()
966 return CollectionFind(*
this);
986 CollectionFind
find(
const string &cond)
989 return CollectionFind(*
this, cond);
1008 template <
typename... Types>
1009 CollectionAdd
add(Types... args)
1013 CollectionAdd
add(*
this);
1037 CollectionRemove
remove(
const string &cond)
1040 return CollectionRemove(*
this, cond);
1066 return CollectionModify(*
this,
expr);
1078 DbDoc
getOne(
const string &
id)
1080 return find(
"_id = :id").bind(
"id",
id).execute().fetchOne();
1092 return remove(
"_id = :id").bind(
"id",
id).execute();
1111 Result
replaceOne(
const string &
id, Value &&document)
1115 Collection_detail::add_or_replace_one(
id, std::move(document),
true);
1139 Collection_detail::add_or_replace_one(
id, std::move(document),
false);
1157 void createIndex(
const string &name,
const string &idx_spec)
1160 Collection_detail::index_create(name, idx_spec);
1174 Collection_detail::index_drop(name);
1180 friend CollectionFind;
1181 friend CollectionAdd;
1182 friend CollectionRemove;
1183 friend CollectionModify;
1186 friend internal::Crud_factory;
1195void CollectionValidation::do_set<CollectionValidation::SCHEMA>(DbDoc schema)
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;
1205void CollectionValidation::do_set<CollectionValidation::SCHEMA>(
const char* schema)
1207 do_set<CollectionValidation::SCHEMA>(DbDoc(schema));
1212void CollectionValidation::do_set<CollectionValidation::SCHEMA>(std::string schema)
1214 do_set<CollectionValidation::SCHEMA>(DbDoc(schema));
1220void CollectionValidation::do_set<CollectionValidation::LEVEL>(Level level)
1222 if(m_data.used.test(CollectionValidation::LEVEL))
1223 throw_error(
"Validation level already set.");
1224 m_data.used.set(CollectionValidation::LEVEL);
1226#define SCHEMA_VALIDATION_CASE(x,y) case Level::x: m_data.validation_level = #x; break;
1229 COLLECTION_VALIDATION_LEVEL(SCHEMA_VALIDATION_CASE)
1235void CollectionValidation::do_set<CollectionValidation::LEVEL>(std::string level)
1237 if(m_data.used.test(CollectionValidation::LEVEL))
1238 throw_error(
"Validation level already set.");
1239 m_data.used.set(CollectionValidation::LEVEL);
1241 m_data.validation_level = level;
1247void CollectionOptions::do_set<CollectionOptions::REUSE>(
bool reuse)
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;
1257void CollectionOptions::do_set<CollectionOptions::VALIDATION>(CollectionValidation validation)
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.");
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);
1268 m_data.validation = validation;
1273Collection Schema::createCollection(
const string &name)
1276 Schema_detail::create_collection(
1286Collection Schema::createCollection(
const string &name,
bool reuse)
1289 Schema_detail::create_collection(
1292 CollectionOptions::REUSE, reuse)
1301template<
typename... Rest>
1303Collection Schema::createCollection(
const string &name, Rest&&... rest)
1306 Schema_detail::create_collection(name,
CollectionOptions(std::forward<Rest>(rest)...));
1313template <
typename... Opt>
1315void Schema::modifyCollection(
const string &name, Opt&&... options)
1318 Schema_detail::modify_collection(name,
CollectionOptions(std::forward<Opt>(options)...));
1326Collection Schema::getCollection(
const string &name,
bool check_exists)
1329 if (check_exists && !coll.existsInDatabase())
1330 throw_error(
"Collection does not exist");
1378 :
protected internal::Sch_object<>
1383 : Sch_object(sch, name)
1386 Table(
const Schema &sch,
const string &name,
bool is_view)
1387 : Sch_object(sch, name)
1389 m_type = is_view ? VIEW : TABLE;
1393 using Sch_object::getName;
1395 using Sch_object::getSchema;
1398 bool existsInDatabase()
const;
1417 RowResult cnt = select(
"count(*)").execute();
1450 return TableInsert(*
this);
1476 template <
class... T>
1477 TableInsert
insert(
const T&... t)
1480 return TableInsert(*
this, t...);
1505 template<
typename ...PROJ>
1506 TableSelect
select(
const PROJ&...proj)
1509 return TableSelect(*
this, proj...);
1530 return TableRemove(*
this);
1552 return TableUpdate(*
this);
1559 enum { TABLE, VIEW, UNKNOWN } m_type = UNKNOWN;
1567 friend internal::Crud_factory;
1573bool Table::existsInDatabase()
const
1581 Schema::TableList list(m_schema, m_name);
1582 auto it = list.begin();
1584 if (!(it != list.end()))
1587 const_cast<Table*
>(
this)->m_type = (*it).isView() ? VIEW : TABLE;
1602 if (UNKNOWN == m_type)
1603 if (!existsInDatabase())
1604 throw_error(
"Table does not exist");
1605 return VIEW == m_type;
1612Table Schema::getTable(
const string &name,
bool check_exists)
1614 Table tbl(*
this, name);
1615 if (check_exists && !tbl.existsInDatabase())
1616 throw_error(
"Table does not exist");
1622Table Schema::getCollectionAsTable(
const string &name,
bool check_exists)
1624 if (check_exists && !getCollection(name).existsInDatabase())
1625 throw_error(
"Collection does not exist");
1626 return { *
this, name };
1631uint64_t Collection::count()
1633 return m_schema.getCollectionAsTable(m_name).count();
1637using SqlStatement = internal::SQL_statement;
1675 :
private internal::Session_detail
1686 : Session_detail(settings)
1719 template<
typename...T>
1726 Session(Session &&other)
1728 : internal::Session_detail(std::move(other))
1742 Schema
createSchema(
const string &name,
bool reuse =
false)
1745 Session_detail::create_schema(name, reuse);
1746 return Schema(*
this, name);
1764 Schema
getSchema(
const string &name,
bool check_exists =
false)
1766 Schema sch(*
this, name);
1767 if (check_exists && !sch.existsInDatabase())
1768 throw_error(
"Schema does not exist");
1788 return Session_detail::get_default_schema_name();
1803 return Schema_src(*
this,
"%");
1819 Session_detail::drop_schema(name);
1837 SqlStatement
sql(
const string &query)
1840 return SqlStatement(
this, query);
1854 Session_detail::start_transaction();
1869 Session_detail::commit();
1885 Session_detail::rollback();
1902 if (savepoint.empty())
1903 throw_error(
"Invalid empty save point name");
1904 Session_detail::rollback(savepoint);
1925 if (savepoint.empty())
1926 throw_error(
"Invalid empty save point name");
1927 return Session_detail::savepoint_set(savepoint);
1948 return Session_detail::savepoint_set();
1965 if (savepoint.empty())
1966 throw_error(
"Invalid empty save point name");
1967 Session_detail::savepoint_remove(savepoint);
1983 Session_detail::close();
1990 using internal::Session_detail::m_impl;
2001 friend internal::Session_detail;
2002 friend internal::Crud_factory;
2003 friend internal::Result_detail;
2004 template <
class Base>
friend class internal::Sch_object;
2044class Client :
public internal::Client_detail
2048 Client(ClientSettings settings)
2050 : Client_detail(settings)
2054 Client(SessionSettings &settings)
2056 : Client_detail(settings)
2060 template<
typename...T>
2062 : Client(ClientSettings(options...))
2079Session::Session(Client &client)
2081 : internal::Session_detail(client.get_session_pool())
2093 return Session(p...);
2105 return Client(p...);
2115 : Schema_detail(sess.m_impl, name)
2120template <
class Base>
2122internal::Sch_object<Base>::Sch_object(
const Schema &sch,
const string &name)
2128template <
class Base>
2130std::shared_ptr<common::Session_impl>
2131internal::Sch_object<Base>::get_session()
2133 assert(m_schema.m_sess);
2134 return m_schema.m_sess->m_impl;
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.