123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- var Classes = Object.create(null);
- /**
- * Create a new Connection instance.
- * @param {object|string} config Configuration or connection string for new MySQL connection
- * @return {Connection} A new MySQL connection
- * @public
- */
- exports.createConnection = function createConnection(config) {
- var Connection = loadClass('Connection');
- var ConnectionConfig = loadClass('ConnectionConfig');
- return new Connection({config: new ConnectionConfig(config)});
- };
- /**
- * Create a new Pool instance.
- * @param {object|string} config Configuration or connection string for new MySQL connections
- * @return {Pool} A new MySQL pool
- * @public
- */
- exports.createPool = function createPool(config) {
- var Pool = loadClass('Pool');
- var PoolConfig = loadClass('PoolConfig');
- return new Pool({config: new PoolConfig(config)});
- };
- /**
- * Create a new PoolCluster instance.
- * @param {object} [config] Configuration for pool cluster
- * @return {PoolCluster} New MySQL pool cluster
- * @public
- */
- exports.createPoolCluster = function createPoolCluster(config) {
- var PoolCluster = loadClass('PoolCluster');
- return new PoolCluster(config);
- };
- /**
- * Create a new Query instance.
- * @param {string} sql The SQL for the query
- * @param {array} [values] Any values to insert into placeholders in sql
- * @param {function} [callback] The callback to use when query is complete
- * @return {Query} New query object
- * @public
- */
- exports.createQuery = function createQuery(sql, values, callback) {
- var Connection = loadClass('Connection');
- return Connection.createQuery(sql, values, callback);
- };
- /**
- * Escape a value for SQL.
- * @param {*} value The value to escape
- * @param {boolean} [stringifyObjects=false] Setting if objects should be stringified
- * @param {string} [timeZone=local] Setting for time zone to use for Date conversion
- * @return {string} Escaped string value
- * @public
- */
- exports.escape = function escape(value, stringifyObjects, timeZone) {
- var SqlString = loadClass('SqlString');
- return SqlString.escape(value, stringifyObjects, timeZone);
- };
- /**
- * Escape an identifier for SQL.
- * @param {*} value The value to escape
- * @param {boolean} [forbidQualified=false] Setting to treat '.' as part of identifier
- * @return {string} Escaped string value
- * @public
- */
- exports.escapeId = function escapeId(value, forbidQualified) {
- var SqlString = loadClass('SqlString');
- return SqlString.escapeId(value, forbidQualified);
- };
- /**
- * Format SQL and replacement values into a SQL string.
- * @param {string} sql The SQL for the query
- * @param {array} [values] Any values to insert into placeholders in sql
- * @param {boolean} [stringifyObjects=false] Setting if objects should be stringified
- * @param {string} [timeZone=local] Setting for time zone to use for Date conversion
- * @return {string} Formatted SQL string
- * @public
- */
- exports.format = function format(sql, values, stringifyObjects, timeZone) {
- var SqlString = loadClass('SqlString');
- return SqlString.format(sql, values, stringifyObjects, timeZone);
- };
- /**
- * Wrap raw SQL strings from escape overriding.
- * @param {string} sql The raw SQL
- * @return {object} Wrapped object
- * @public
- */
- exports.raw = function raw(sql) {
- var SqlString = loadClass('SqlString');
- return SqlString.raw(sql);
- };
- /**
- * The type constants.
- * @public
- */
- Object.defineProperty(exports, 'Types', {
- get: loadClass.bind(null, 'Types')
- });
- /**
- * Load the given class.
- * @param {string} className Name of class to default
- * @return {function|object} Class constructor or exports
- * @private
- */
- function loadClass(className) {
- var Class = Classes[className];
- if (Class !== undefined) {
- return Class;
- }
- // This uses a switch for static require analysis
- switch (className) {
- case 'Connection':
- Class = require('./lib/Connection');
- break;
- case 'ConnectionConfig':
- Class = require('./lib/ConnectionConfig');
- break;
- case 'Pool':
- Class = require('./lib/Pool');
- break;
- case 'PoolCluster':
- Class = require('./lib/PoolCluster');
- break;
- case 'PoolConfig':
- Class = require('./lib/PoolConfig');
- break;
- case 'SqlString':
- Class = require('./lib/protocol/SqlString');
- break;
- case 'Types':
- Class = require('./lib/protocol/constants/types');
- break;
- default:
- throw new Error('Cannot find class \'' + className + '\'');
- }
- // Store to prevent invoking require()
- Classes[className] = Class;
- return Class;
- }
|