MySQL Connector/C++
MySQL connector library for C and C++ applications
Public Member Functions | List of all members
Schema Class Reference

Represents a database schema. More...

Public Member Functions

 Schema (Session &sess, const string &name)
 Construct an object representing the named schema.
 
 Schema (Session &)
 Construct an object representing the default schema of the session. More...
 
const stringgetName () const
 Get schema name.
 
SessiongetSession ()
 Get Session object.
 
bool existsInDatabase () const
 Check if this schema exists in the database. More...
 
Collection createCollection (const string &name)
 Create a new collection in the schema. More...
 
template<typename... Rest>
Collection createCollection (const string &name, Rest &&... rest)
 Create a new collection in the schema, optionally specifying creation options. More...
 
template<typename... Rest>
void modifyCollection (const string &name, Rest &&... options)
 Modify a collection in the schema specifying modify options. More...
 
Collection getCollection (const string &name, bool check_exists=false)
 Return an object representing a collection with the given name. More...
 
Table getTable (const string &name, bool check_exists=false)
 Return an object representing a table or a view with the given name. More...
 
CollectionList getCollections ()
 Get a list of all collections in the schema. More...
 
StringList getCollectionNames ()
 Get a list of names of all collections in the schema. More...
 
TableList getTables ()
 Get a list of all tables and views in the schema. More...
 
StringList getTableNames ()
 Get a list of names of all tables and views in the schema. More...
 
Table getCollectionAsTable (const string &name, bool check_exists=true)
 Return a table corresponding to the given collection. More...
 
void dropCollection (const mysqlx::string &name)
 Drop the given collection from the schema. More...
 

Detailed Description

Represents a database schema.

A Schema instance can be obtained from Session::getSchema() method:

Session session;
Schema mySchema;
mySchema= session.getSchema("My Schema");
Schema(Session &sess, const string &name)
Construct an object representing the named schema.
Definition: xdevapi.h:2110

or it can be directly constructed as follows:

Session session;
Schema mySchema(session, "My Schema");

Each Schema instance is tied to a particular session and all the operations on the schema and its objects are performed using that session. If the session is destroyed, an attempt to use a schema of that session yields an error.

When creating a Schema object, by default no checks are made that it actually exists in the database. An operation that is executed on the server and involves such a non-existent schema throws an error.

Note
A Schema object should be used by at most one thread at a time. It is not safe to call its methods by several threads simultaneously. It is responsibility of the user to ensure this using a synchronization mechanism such as mutexes.

Constructor & Destructor Documentation

◆ Schema()

Schema ( Session )

Construct an object representing the default schema of the session.

The default schema is the one specified by session creation options.

Member Function Documentation

◆ existsInDatabase()

bool existsInDatabase ( ) const
inline

Check if this schema exists in the database.

Note
Involves communication with the server.

◆ createCollection() [1/2]

Collection createCollection ( const string name)
inline

Create a new collection in the schema.

Returns the created collection. Set reuse flag to true to return an already existing collection with the same name. Otherwise, an attempt to create a collection which already exists throws an error.

◆ createCollection() [2/2]

Collection createCollection ( const string name,
Rest &&...  rest 
)
inline

Create a new collection in the schema, optionally specifying creation options.

Arguments following name, if any, are used to construct CollectionOptions object. See CollectionOptions for possible ways of specifying the options.

Returns the created collection.

◆ modifyCollection()

void modifyCollection ( const string name,
Rest &&...  options 
)

Modify a collection in the schema specifying modify options.

Arguments following name are used to construct CollectionOptions object. See CollectionOptions for possible ways of specifying the options.

Note
CollectionOptions::REUSE is not allowed and, if used, will throw error.

◆ getCollection()

Collection getCollection ( const string name,
bool  check_exists = false 
)
inline

Return an object representing a collection with the given name.

To check if the collection actually exists in the database set check_existence flag to true. Otherwise the returned object can refer to a non-existing collection. An attempt to use such a non-existing collection in a database operation throws an error.

Note
Checking the existence of a collection involves communication with the server. If check_exists is false, on the other hand, no I/O is involved when creating a Collection object.

◆ getTable()

Table getTable ( const string name,
bool  check_exists = false 
)
inline

Return an object representing a table or a view with the given name.

To check if the table actually exists in the database set check_existence flag to true. Otherwise the returned object can refer to a non-existing table. An attempt to use such a non-existing table in a database operation throws an error.

Note
The returned Table object can represent a plain table or a view. See Table class description.
Checking the existence of a table involves communication with the server. If check_exists is false, on the other hand, no I/O is involved when creating a Table object.

◆ getCollections()

CollectionList getCollections ( )
inline

Get a list of all collections in the schema.

The returned value can be stored in a container that holds Collection objects, such as std::vector<Collection>.

◆ getCollectionNames()

StringList getCollectionNames ( )
inline

Get a list of names of all collections in the schema.

The returned value can be stored in a container that holds strings, such as std::vector<string>.

◆ getTables()

TableList getTables ( )
inline

Get a list of all tables and views in the schema.

The returned value can be stored in a container that holds Table objects, such as std::vector<Table>.

Note
The list also contains views which are represented by Table objects
  • see Table class description.

◆ getTableNames()

StringList getTableNames ( )
inline

Get a list of names of all tables and views in the schema.

The returned value can be stored in a container that holds strings, such as std::vector<string>.

Note
The list also contains names of views as views are represented by Table objects - see Table class description.

◆ getCollectionAsTable()

Table getCollectionAsTable ( const string name,
bool  check_exists = true 
)
inline

Return a table corresponding to the given collection.

The table has two columns: _id and doc. For each document in the collection there is one row in the table with doc filed holding the document as a JSON value and _id field holding document's identifier.

To check if the collection actually exists in the database set check_existence flag to true. Otherwise the returned table can refer to a non-existing collection. An attempt to use such a non-existing collection table throws an error.

Note
Checking the existence of a collection involves communication with the server. If check_exists is false, on the other hand, no I/O is involved when creating the Table object.

◆ dropCollection()

void dropCollection ( const mysqlx::string name)
inline

Drop the given collection from the schema.

This method silently succeeds if a collection with the given name does not exist.

Note
If a table name is passed to the method, it behaves like dropTable().

The documentation for this class was generated from the following file: