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.");
289 friend CollectionOptions;
291 friend mysqlx::internal::Schema_detail;
299class CollectionOptions
303#define COLLECTION_OPTIONS_ENUM(x,y) x=y,
312 COLLECTION_OPTIONS_OPTION(COLLECTION_OPTIONS_ENUM)
320 std::bitset<LAST> used;
334 CollectionOptions(
const std::string& options)
335 : CollectionOptions(DbDoc(options))
378 CollectionOptions(DbDoc options)
380 for(
auto el : options)
382 if(el ==
"reuseExisting")
385 _set(REUSE, options[
"reuseExisting"].get<bool>());
386 }
catch (
const Error& e)
388 std::string err(
"Wrong value for reuseExisting option: ");
390 throw Error(err.c_str());
393 else if(el ==
"validation")
395 _set(VALIDATION, CollectionValidation(options[
"validation"].get<DbDoc>()));
398 std::string err(
"Unexpected collection option ");
400 throw Error(err.c_str());
405 CollectionOptions(CollectionValidation validation)
407 set(VALIDATION, validation);
446 template<
typename... Rest>
447 CollectionOptions(Option opt, Rest&&... rest)
449 set(opt, std::forward<Rest>(rest)...);
453 template<
typename... Rest>
454 CollectionOptions(CollectionValidation::Option opt, Rest&&... rest)
456 set(opt, std::forward<Rest>(rest)...);
464 template<
typename... Rest>
465 void set(Rest&&... rest)
467 Data tmp_data(m_data);
469 _set(std::forward<Rest>(rest)...);
471 m_data = std::move(tmp_data);
482 template<
typename T,
typename... Rest>
483 void _set(Option opt, T&& v, Rest&&... rest)
485#define COLLECTION_OPTIONS_SET(x,y) case x:\
486 do_set<x>(std::forward<T>(v)); break;
490 COLLECTION_OPTIONS_OPTION(COLLECTION_OPTIONS_SET)
491 case LAST: throw_error(
"Invalid option."); ;
break;
494 _set(std::forward<Rest>(rest)...);
498 template<
typename T,
typename... Rest>
499 void _set(CollectionValidation::Option opt, T&& v, Rest&&... rest)
501 m_data.validation._set(opt, std::forward<T>(v));
502 _set(std::forward<Rest>(rest)...);
507 template<CollectionOptions::Option O,
typename T>
510 throw_error(
"Invalid option value type.");
515 friend mysqlx::internal::Schema_detail;
558 :
protected internal::Schema_detail
568 Schema(Session &sess,
const string &name);
615 internal::Session_detail::Name_src list(*m_sess, m_name);
616 list.iterator_start();
617 return list.iterator_next();
643 template<
typename... Rest>
655 template<
typename... Rest>
672 Collection
getCollection(
const string &name,
bool check_exists =
false);
690 Table
getTable(
const string &name,
bool check_exists =
false);
702 return Collection_src(*
this,
"%");
717 return Name_src(*
this, COLLECTION,
"%");
735 return Table_src(*
this,
"%");
753 return Name_src(*
this, TABLE,
"%");
791 Schema_detail::drop_collection(name);
801 friend internal::Schema_detail::Name_src;
802 template <
class Base>
friend class internal::Sch_object;
817template <
class Base = Db_obj_base>
825 Sch_object(
const Schema &sch,
const string &name);
835 const string& getName()
const
846 return m_schema.getSession();
853 const Schema& getSchema()
const
860 std::shared_ptr<common::Session_impl> get_session();
862 Schema_detail& get_schema()
910 :
protected internal::Sch_object<internal::Collection_detail>
914 Collection(
const Schema &sch,
const string &name)
916 : Sch_object(sch, name)
921 using Sch_object::getName;
923 using Sch_object::getSchema;
925 bool existsInDatabase()
const
928 Schema::StringList list(m_schema, Schema::COLLECTION, m_name);
929 return list.begin() != list.end();
959 CollectionFind
find()
962 return CollectionFind(*
this);
982 CollectionFind
find(
const string &cond)
985 return CollectionFind(*
this, cond);
1004 template <
typename... Types>
1005 CollectionAdd
add(Types... args)
1009 CollectionAdd
add(*
this);
1033 CollectionRemove
remove(
const string &cond)
1036 return CollectionRemove(*
this, cond);
1062 return CollectionModify(*
this,
expr);
1074 DbDoc
getOne(
const string &
id)
1076 return find(
"_id = :id").bind(
"id",
id).execute().fetchOne();
1088 return remove(
"_id = :id").bind(
"id",
id).execute();
1107 Result
replaceOne(
const string &
id, Value &&document)
1111 Collection_detail::add_or_replace_one(
id, std::move(document),
true);
1135 Collection_detail::add_or_replace_one(
id, std::move(document),
false);
1153 void createIndex(
const string &name,
const string &idx_spec)
1156 Collection_detail::index_create(name, idx_spec);
1172 Collection_detail::index_drop(name);
1178 friend CollectionFind;
1179 friend CollectionAdd;
1180 friend CollectionRemove;
1181 friend CollectionModify;
1184 friend internal::Crud_factory;
1193void CollectionValidation::do_set<CollectionValidation::SCHEMA>(DbDoc schema)
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;
1203void CollectionValidation::do_set<CollectionValidation::SCHEMA>(
const char* schema)
1205 do_set<CollectionValidation::SCHEMA>(DbDoc(schema));
1210void CollectionValidation::do_set<CollectionValidation::SCHEMA>(std::string schema)
1212 do_set<CollectionValidation::SCHEMA>(DbDoc(schema));
1218void CollectionValidation::do_set<CollectionValidation::LEVEL>(Level level)
1220 if(m_data.used.test(CollectionValidation::LEVEL))
1221 throw_error(
"Validation level already set.");
1222 m_data.used.set(CollectionValidation::LEVEL);
1224#define SCHEMA_VALIDATION_CASE(x,y) case Level::x: m_data.validation_level = #x; break;
1227 COLLECTION_VALIDATION_LEVEL(SCHEMA_VALIDATION_CASE)
1233void CollectionValidation::do_set<CollectionValidation::LEVEL>(std::string level)
1235 if(m_data.used.test(CollectionValidation::LEVEL))
1236 throw_error(
"Validation level already set.");
1237 m_data.used.set(CollectionValidation::LEVEL);
1239 m_data.validation_level = level;
1245void CollectionOptions::do_set<CollectionOptions::REUSE>(
bool reuse)
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;
1255void CollectionOptions::do_set<CollectionOptions::VALIDATION>(CollectionValidation validation)
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.");
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);
1266 m_data.validation = validation;
1271Collection Schema::createCollection(
const string &name)
1274 Schema_detail::create_collection(
1284Collection Schema::createCollection(
const string &name,
bool reuse)
1287 Schema_detail::create_collection(
1290 CollectionOptions::REUSE, reuse)
1299template<
typename... Rest>
1301Collection Schema::createCollection(
const string &name, Rest&&... rest)
1304 Schema_detail::create_collection(name,
CollectionOptions(std::forward<Rest>(rest)...));
1311template <
typename... Opt>
1313void Schema::modifyCollection(
const string &name, Opt&&... options)
1316 Schema_detail::modify_collection(name,
CollectionOptions(std::forward<Opt>(options)...));
1324Collection Schema::getCollection(
const string &name,
bool check_exists)
1327 if (check_exists && !coll.existsInDatabase())
1328 throw_error(
"Collection does not exist");
1376 :
protected internal::Sch_object<>
1381 : Sch_object(sch, name)
1384 Table(
const Schema &sch,
const string &name,
bool is_view)
1385 : Sch_object(sch, name)
1387 m_type = is_view ? VIEW : TABLE;
1391 using Sch_object::getName;
1393 using Sch_object::getSchema;
1396 bool existsInDatabase()
const;
1415 RowResult cnt = select(
"count(*)").execute();
1448 return TableInsert(*
this);
1474 template <
class... T>
1475 TableInsert
insert(
const T&... t)
1478 return TableInsert(*
this, t...);
1503 template<
typename ...PROJ>
1504 TableSelect
select(
const PROJ&...proj)
1507 return TableSelect(*
this, proj...);
1528 return TableRemove(*
this);
1550 return TableUpdate(*
this);
1557 enum { TABLE, VIEW, UNKNOWN } m_type = UNKNOWN;
1565 friend internal::Crud_factory;
1571bool Table::existsInDatabase()
const
1579 Schema::TableList list(m_schema, m_name);
1580 auto it = list.begin();
1582 if (!(it != list.end()))
1585 const_cast<Table*
>(
this)->m_type = (*it).isView() ? VIEW : TABLE;
1600 if (UNKNOWN == m_type)
1601 if (!existsInDatabase())
1602 throw_error(
"Table does not exist");
1603 return VIEW == m_type;
1610Table Schema::getTable(
const string &name,
bool check_exists)
1612 Table tbl(*
this, name);
1613 if (check_exists && !tbl.existsInDatabase())
1614 throw_error(
"Table does not exist");
1620Table Schema::getCollectionAsTable(
const string &name,
bool check_exists)
1622 if (check_exists && !getCollection(name).existsInDatabase())
1623 throw_error(
"Collection does not exist");
1624 return { *
this, name };
1629uint64_t Collection::count()
1631 return m_schema.getCollectionAsTable(m_name).count();
1635using SqlStatement = internal::SQL_statement;
1673 :
private internal::Session_detail
1684 : Session_detail(settings)
1717 template<
typename...T>
1724 Session(Session &&other)
1726 : internal::Session_detail(std::move(other))
1740 Schema
createSchema(
const string &name,
bool reuse =
false)
1743 Session_detail::create_schema(name, reuse);
1744 return Schema(*
this, name);
1762 Schema
getSchema(
const string &name,
bool check_exists =
false)
1764 Schema sch(*
this, name);
1765 if (check_exists && !sch.existsInDatabase())
1766 throw_error(
"Schema does not exist");
1786 return Session_detail::get_default_schema_name();
1801 return Schema_src(*
this,
"%");
1817 Session_detail::drop_schema(name);
1835 SqlStatement
sql(
const string &query)
1838 return SqlStatement(
this, query);
1852 Session_detail::start_transaction();
1867 Session_detail::commit();
1883 Session_detail::rollback();
1900 if (savepoint.empty())
1901 throw_error(
"Invalid empty save point name");
1902 Session_detail::rollback(savepoint);
1923 if (savepoint.empty())
1924 throw_error(
"Invalid empty save point name");
1925 return Session_detail::savepoint_set(savepoint);
1946 return Session_detail::savepoint_set();
1963 if (savepoint.empty())
1964 throw_error(
"Invalid empty save point name");
1965 Session_detail::savepoint_remove(savepoint);
1981 Session_detail::close();
1988 using internal::Session_detail::m_impl;
1999 friend internal::Session_detail;
2000 friend internal::Crud_factory;
2001 friend internal::Result_detail;
2002 template <
class Base>
friend class internal::Sch_object;
2042class Client :
public internal::Client_detail
2046 Client(ClientSettings settings)
2048 : Client_detail(settings)
2052 Client(SessionSettings &settings)
2054 : Client_detail(settings)
2058 template<
typename...T>
2060 : Client(ClientSettings(options...))
2077Session::Session(Client &client)
2079 : internal::Session_detail(client.get_session_pool())
2091 return Session(p...);
2103 return Client(p...);
2113 : Schema_detail(sess.m_impl, name)
2118template <
class Base>
2120internal::Sch_object<Base>::Sch_object(
const Schema &sch,
const string &name)
2126template <
class Base>
2128std::shared_ptr<common::Session_impl>
2129internal::Sch_object<Base>::get_session()
2131 assert(m_schema.m_sess);
2132 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: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.