Documentation Home
X DevAPI User Guide
Download this Manual
PDF (US Ltr) - 1.0Mb
PDF (A4) - 1.0Mb
EPUB - 0.7Mb
HTML Download (TGZ) - 0.6Mb
HTML Download (Zip) - 0.7Mb


X DevAPI User Guide  /  ...  /  Setting the Current Schema

Beta Draft: 2017-03-17

2.5.2 Setting the Current Schema

A newly opened session has no schema set. There is no concept of an implicitly set default schema when a session is established. A default schema can be defined, but a session's current schema is not set to a default before being explicitly selected.

An explicit schema selection happens as part of using the CRUD functions of the X DevAPI. For example, Table and Collection objects are created by Schema objects. Schema objects, in turn, are created by Session objects. To insert rows into a table, call session.getSchema().getTable().insert(). To add a document to a collection from the default schema, call session.getDefaultSchema().getCollection().add().

The sql() function is a method of the NodeSession class but not the Schema class. Upon creation, the NodeSession class has no schema selected by default. Therefore, the sql() function does not know which schema to execute the SQL statement against. Use the Session class setCurrentSchema() function to set or change the current schema.

MySQL Shell JavaScript Code

var mysqlx = require('mysqlx');

// Direct connect with no client side default schema defined
var mySession = mysqlx.getNodeSession('mike:s3cr3t!@localhost');
mySession.setCurrentSchema("test");

MySQL Shell Python Code

import mysqlx

# Direct connect with no client side default schema defined
mySession = mysqlx.get_node_session('mike:s3cr3t!@localhost')
mySession.set_current_schema("test")

Node.js JavaScript Code


C# Code

// Direct connect with no client side default schema defined
var mySession = MySqlX.GetNodeSession("server=localhost;port=33060;user=mike;password=s3cr3t!;");
mySession.GetSchema("test");
	
var session = MySqlX.GetNodeSession("server=localhost;port=33060;user=mike;password=s3cr3t!;");

var default_schema = session.GetSchema("test");

// print the current schema name
Console.WriteLine(session.Schema.Name);


private Table CreateTestTable(NodeSession session, string name)

// use escape function to quote names/identifier
string quoted_name = "`" + name + "`";

session.SQL("DROP TABLE IF EXISTS " + quoted_name).Execute();

var create = "CREATE TABLE ";
create += quoted_name;
create += " (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT)";

session.SQL(create).Execute();

return session.Schema.GetTable(name);
}
	

Java Code

import com.mysql.cj.api.xdevapi.*;
import com.mysql.cj.xdevapi.*;

// Schema 'test' set in connection URL
NodeSession mySession = new XSessionFactory().getNodeSession("mysqlx://localhost:33060/test?user=mike&password=s3cr3t!"); 

C++ Code

#include <mysql_devapi.h>

/*
  Currently Connector/C++ does not support .setCurrentSchema() method.
  One can specify default schema in a connection string.
*/

string url = "mysqlx://localhost:33060/test?user=mike&password=s3cr3t!"
NodeSession mySession(url);

C Code



User Comments
Sign Up Login You must be logged in to post a comment.