Documentation Home
X DevAPI User Guide for MySQL Shell in JavaScript Mode
Download this Manual
PDF (US Ltr) - 1.2Mb
PDF (A4) - 1.2Mb


2.6 Dynamic SQL

A quoting function exists to escape SQL names and identifiers. Session.quoteName() escapes the identifier given in accordance to the settings of the current connection.

Note

The quoting function must not be used to escape values. Use the value binding syntax of Session.sql() instead; see Section 2.4, “Using SQL with Session” for some examples.

function createTestTable(session, name) {

  // use escape function to quote names/identifier
  quoted_name = session.quoteName(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.getCurrentSchema().getTable(name);
}

var mysqlx = require('mysqlx');

var session = mysqlx.getSession('user:password@localhost:33060/test');

var default_schema = session.getDefaultSchema().name;
session.setCurrentSchema(default_schema);

// Creates some tables
var table1 = createTestTable(session, 'test1');
var table2 = createTestTable(session, 'test2');

Code that uses X DevAPI does not need to escape identifiers. This is true for working with collections and for working with relational tables.