2020-02-08 22:23:33 +00:00
|
|
|
// Copyright IBM Corp. 2012,2019. All Rights Reserved.
|
2016-05-03 23:52:03 +00:00
|
|
|
// Node module: loopback-connector-mysql
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2016-08-10 18:41:03 +00:00
|
|
|
'use strict';
|
2020-08-28 06:43:09 +00:00
|
|
|
const g = require('strong-globalize')();
|
2016-07-19 20:52:46 +00:00
|
|
|
|
2013-10-03 21:50:38 +00:00
|
|
|
/*!
|
2012-12-14 14:01:44 +00:00
|
|
|
* Module dependencies
|
|
|
|
*/
|
2023-06-18 18:04:31 +00:00
|
|
|
const mysql = require('mysql2');
|
2013-06-17 21:25:57 +00:00
|
|
|
|
2020-08-28 06:43:09 +00:00
|
|
|
const SqlConnector = require('loopback-connector').SqlConnector;
|
|
|
|
const ParameterizedSQL = SqlConnector.ParameterizedSQL;
|
|
|
|
const EnumFactory = require('./enumFactory').EnumFactory;
|
2012-12-14 14:01:44 +00:00
|
|
|
|
2020-08-28 06:43:09 +00:00
|
|
|
const debug = require('debug')('loopback:connector:mysql');
|
|
|
|
const setHttpCode = require('./set-http-code');
|
2014-04-08 05:16:26 +00:00
|
|
|
|
2013-10-03 21:50:38 +00:00
|
|
|
/**
|
|
|
|
* @module loopback-connector-mysql
|
|
|
|
*
|
|
|
|
* Initialize the MySQL connector against the given data source
|
|
|
|
*
|
|
|
|
* @param {DataSource} dataSource The loopback-datasource-juggler dataSource
|
|
|
|
* @param {Function} [callback] The callback function
|
|
|
|
*/
|
2013-07-23 19:47:02 +00:00
|
|
|
exports.initialize = function initializeDataSource(dataSource, callback) {
|
2015-05-13 17:17:15 +00:00
|
|
|
dataSource.driver = mysql; // Provide access to the native driver
|
|
|
|
dataSource.connector = new MySQL(dataSource.settings);
|
|
|
|
dataSource.connector.dataSource = dataSource;
|
|
|
|
|
|
|
|
defineMySQLTypes(dataSource);
|
|
|
|
|
|
|
|
dataSource.EnumFactory = EnumFactory; // factory for Enums. Note that currently Enums can not be registered.
|
|
|
|
|
2016-08-10 18:41:03 +00:00
|
|
|
if (callback) {
|
|
|
|
if (dataSource.settings.lazyConnect) {
|
2016-05-09 02:37:19 +00:00
|
|
|
process.nextTick(function() {
|
|
|
|
callback();
|
|
|
|
});
|
2016-08-10 18:41:03 +00:00
|
|
|
} else {
|
2016-05-09 02:37:19 +00:00
|
|
|
dataSource.connector.connect(callback);
|
|
|
|
}
|
|
|
|
}
|
2015-05-13 17:17:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.MySQL = MySQL;
|
|
|
|
|
|
|
|
function defineMySQLTypes(dataSource) {
|
2020-08-28 06:43:09 +00:00
|
|
|
const modelBuilder = dataSource.modelBuilder;
|
|
|
|
const defineType = modelBuilder.defineValueType ?
|
2015-05-13 17:17:15 +00:00
|
|
|
// loopback-datasource-juggler 2.x
|
|
|
|
modelBuilder.defineValueType.bind(modelBuilder) :
|
|
|
|
// loopback-datasource-juggler 1.x
|
|
|
|
modelBuilder.constructor.registerType.bind(modelBuilder.constructor);
|
|
|
|
|
|
|
|
// The Point type is inherited from jugglingdb mysql adapter.
|
|
|
|
// LoopBack uses GeoPoint instead.
|
|
|
|
// The Point type can be removed at some point in the future.
|
|
|
|
defineType(function Point() {
|
|
|
|
});
|
|
|
|
}
|
2014-02-13 00:57:06 +00:00
|
|
|
|
2015-05-13 17:17:15 +00:00
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* Constructor for MySQL connector
|
|
|
|
* @param {Object} client The node-mysql client object
|
|
|
|
*/
|
|
|
|
function MySQL(settings) {
|
|
|
|
SqlConnector.call(this, 'mysql', settings);
|
2016-05-09 02:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
require('util').inherits(MySQL, SqlConnector);
|
2014-02-13 00:57:06 +00:00
|
|
|
|
2022-12-11 16:39:10 +00:00
|
|
|
MySQL.prototype.multiInsertSupported = true;
|
|
|
|
|
2016-05-09 02:37:19 +00:00
|
|
|
MySQL.prototype.connect = function(callback) {
|
2020-08-28 06:43:09 +00:00
|
|
|
const self = this;
|
|
|
|
const options = generateOptions(this.settings);
|
|
|
|
const s = self.settings || {};
|
2016-05-09 02:37:19 +00:00
|
|
|
|
|
|
|
if (this.client) {
|
|
|
|
if (callback) {
|
|
|
|
process.nextTick(function() {
|
|
|
|
callback(null, self.client);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.client = mysql.createPool(options);
|
|
|
|
this.client.getConnection(function(err, connection) {
|
2020-08-28 06:43:09 +00:00
|
|
|
const conn = connection;
|
2016-05-09 02:37:19 +00:00
|
|
|
if (!err) {
|
|
|
|
if (self.debug) {
|
|
|
|
debug('MySQL connection is established: ' + self.settings || {});
|
2016-08-10 18:41:03 +00:00
|
|
|
}
|
2016-05-09 02:37:19 +00:00
|
|
|
connection.release();
|
|
|
|
} else {
|
|
|
|
if (self.debug || !callback) {
|
|
|
|
console.error('MySQL connection is failed: ' + self.settings || {}, err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
callback && callback(err, conn);
|
|
|
|
});
|
|
|
|
}
|
2016-08-10 18:41:03 +00:00
|
|
|
};
|
2016-05-09 02:37:19 +00:00
|
|
|
|
2016-08-10 18:41:03 +00:00
|
|
|
function generateOptions(settings) {
|
2020-08-28 06:43:09 +00:00
|
|
|
const s = settings || {};
|
2024-01-14 06:28:55 +00:00
|
|
|
const generatorSpecificOptions = [
|
|
|
|
'name',
|
|
|
|
'connector',
|
|
|
|
'sharedData',
|
|
|
|
'forwardErrorToEnvironment',
|
|
|
|
'skipLocalCache',
|
|
|
|
'_',
|
|
|
|
'c',
|
|
|
|
'y',
|
|
|
|
'initialGenerator',
|
|
|
|
'resolved',
|
|
|
|
'namespace',
|
|
|
|
'skip-cache',
|
|
|
|
'skip-install',
|
|
|
|
'force-install',
|
|
|
|
'ask-answered',
|
|
|
|
'config',
|
|
|
|
'yes',
|
|
|
|
'url',
|
|
|
|
'engine',
|
|
|
|
'collation',
|
|
|
|
];
|
2014-02-13 00:57:06 +00:00
|
|
|
if (s.collation) {
|
2015-05-13 17:17:15 +00:00
|
|
|
// Charset should be first 'chunk' of collation.
|
|
|
|
s.charset = s.collation.substr(0, s.collation.indexOf('_'));
|
2014-02-13 00:57:06 +00:00
|
|
|
} else {
|
|
|
|
s.collation = 'utf8_general_ci';
|
|
|
|
s.charset = 'utf8';
|
|
|
|
}
|
|
|
|
|
|
|
|
s.supportBigNumbers = (s.supportBigNumbers || false);
|
|
|
|
s.timezone = (s.timezone || 'local');
|
|
|
|
|
2014-11-21 05:58:02 +00:00
|
|
|
if (isNaN(s.connectionLimit)) {
|
2014-02-13 00:57:06 +00:00
|
|
|
s.connectionLimit = 10;
|
|
|
|
}
|
|
|
|
|
2020-08-28 06:43:09 +00:00
|
|
|
let options;
|
2016-03-24 17:20:56 +00:00
|
|
|
if (s.url) {
|
|
|
|
// use url to override other settings if url provided
|
|
|
|
options = s.url;
|
|
|
|
} else {
|
|
|
|
options = {
|
|
|
|
host: s.host || s.hostname || 'localhost',
|
|
|
|
port: s.port || 3306,
|
|
|
|
user: s.username || s.user,
|
|
|
|
password: s.password,
|
|
|
|
timezone: s.timezone,
|
|
|
|
socketPath: s.socketPath,
|
|
|
|
charset: s.collation.toUpperCase(), // Correct by docs despite seeming odd.
|
|
|
|
supportBigNumbers: s.supportBigNumbers,
|
2016-08-10 18:41:03 +00:00
|
|
|
connectionLimit: s.connectionLimit,
|
2016-03-24 17:20:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Don't configure the DB if the pool can be used for multiple DBs
|
|
|
|
if (!s.createDatabase) {
|
|
|
|
options.database = s.database;
|
2014-08-19 23:36:33 +00:00
|
|
|
}
|
2016-03-24 17:20:56 +00:00
|
|
|
|
|
|
|
// Take other options for mysql driver
|
2021-07-08 01:39:50 +00:00
|
|
|
// See https://github.com/loopbackio/loopback-connector-mysql/issues/46
|
2020-08-28 06:43:09 +00:00
|
|
|
for (const p in s) {
|
2024-01-14 06:28:55 +00:00
|
|
|
if (
|
|
|
|
(p === 'database' && s.createDatabase) ||
|
|
|
|
generatorSpecificOptions.includes(p)
|
|
|
|
) {
|
2016-03-24 17:20:56 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (options[p] === undefined) {
|
|
|
|
options[p] = s[p];
|
|
|
|
}
|
2014-08-15 23:27:12 +00:00
|
|
|
}
|
2017-04-25 22:10:40 +00:00
|
|
|
// Legacy UTC Date Processing fallback - SHOULD BE TRANSITIONAL
|
|
|
|
if (s.legacyUtcDateProcessing === undefined) {
|
|
|
|
s.legacyUtcDateProcessing = true;
|
|
|
|
}
|
|
|
|
if (s.legacyUtcDateProcessing) {
|
|
|
|
options.timezone = 'Z';
|
|
|
|
}
|
2014-08-15 23:27:12 +00:00
|
|
|
}
|
2016-05-09 02:37:19 +00:00
|
|
|
return options;
|
2012-12-14 14:01:44 +00:00
|
|
|
}
|
2013-10-03 21:50:38 +00:00
|
|
|
/**
|
|
|
|
* Execute the sql statement
|
|
|
|
*
|
|
|
|
* @param {String} sql The SQL statement
|
|
|
|
* @param {Function} [callback] The callback after the SQL statement is executed
|
|
|
|
*/
|
2015-05-18 19:02:25 +00:00
|
|
|
MySQL.prototype.executeSQL = function(sql, params, options, callback) {
|
2020-08-28 06:43:09 +00:00
|
|
|
const self = this;
|
|
|
|
const client = this.client;
|
|
|
|
const debugEnabled = debug.enabled;
|
|
|
|
const db = this.settings.database;
|
2015-05-13 17:17:15 +00:00
|
|
|
if (typeof callback !== 'function') {
|
2016-07-19 20:52:46 +00:00
|
|
|
throw new Error(g.f('{{callback}} should be a function'));
|
2015-05-13 17:17:15 +00:00
|
|
|
}
|
2014-04-08 05:16:26 +00:00
|
|
|
if (debugEnabled) {
|
2015-05-13 17:17:15 +00:00
|
|
|
debug('SQL: %s, params: %j', sql, params);
|
2014-02-13 00:57:06 +00:00
|
|
|
}
|
|
|
|
|
2020-08-28 06:43:09 +00:00
|
|
|
const transaction = options.transaction;
|
2015-05-18 19:02:25 +00:00
|
|
|
|
|
|
|
function handleResponse(connection, err, result) {
|
|
|
|
if (!transaction) {
|
|
|
|
connection.release();
|
|
|
|
}
|
2017-05-30 18:18:40 +00:00
|
|
|
if (err) {
|
|
|
|
err = setHttpCode(err);
|
|
|
|
}
|
2014-02-13 00:57:06 +00:00
|
|
|
callback && callback(err, result);
|
|
|
|
}
|
|
|
|
|
2015-05-18 19:02:25 +00:00
|
|
|
function runQuery(connection, release) {
|
|
|
|
connection.query(sql, params, function(err, data) {
|
2014-04-08 05:16:26 +00:00
|
|
|
if (debugEnabled) {
|
2014-02-13 00:57:06 +00:00
|
|
|
if (err) {
|
2015-05-13 17:17:15 +00:00
|
|
|
debug('Error: %j', err);
|
2014-02-13 00:57:06 +00:00
|
|
|
}
|
2014-04-08 05:16:26 +00:00
|
|
|
debug('Data: ', data);
|
|
|
|
}
|
2015-05-18 19:02:25 +00:00
|
|
|
handleResponse(connection, err, data);
|
2014-02-13 00:57:06 +00:00
|
|
|
});
|
|
|
|
}
|
2013-11-27 16:42:40 +00:00
|
|
|
|
2015-05-18 19:02:25 +00:00
|
|
|
function executeWithConnection(err, connection) {
|
2014-02-13 00:57:06 +00:00
|
|
|
if (err) {
|
2015-05-13 17:17:15 +00:00
|
|
|
return callback && callback(err);
|
2013-11-27 17:08:03 +00:00
|
|
|
}
|
2014-02-13 00:57:06 +00:00
|
|
|
if (self.settings.createDatabase) {
|
|
|
|
// Call USE db ...
|
2015-05-18 19:02:25 +00:00
|
|
|
connection.query('USE ??', [db], function(err) {
|
2013-11-27 01:40:31 +00:00
|
|
|
if (err) {
|
2014-02-13 00:57:06 +00:00
|
|
|
if (err && err.message.match(/(^|: )unknown database/i)) {
|
2020-08-28 06:43:09 +00:00
|
|
|
const charset = self.settings.charset;
|
|
|
|
const collation = self.settings.collation;
|
|
|
|
const q = 'CREATE DATABASE ?? CHARACTER SET ?? COLLATE ??';
|
2015-05-18 19:02:25 +00:00
|
|
|
connection.query(q, [db, charset, collation], function(err) {
|
2014-02-13 00:57:06 +00:00
|
|
|
if (!err) {
|
2015-05-18 19:02:25 +00:00
|
|
|
connection.query('USE ??', [db], function(err) {
|
2014-02-13 00:57:06 +00:00
|
|
|
runQuery(connection);
|
|
|
|
});
|
|
|
|
} else {
|
2015-05-18 19:02:25 +00:00
|
|
|
handleResponse(connection, err);
|
2014-02-13 00:57:06 +00:00
|
|
|
}
|
2013-11-27 16:42:40 +00:00
|
|
|
});
|
2014-02-13 00:57:06 +00:00
|
|
|
return;
|
|
|
|
} else {
|
2015-05-18 19:02:25 +00:00
|
|
|
handleResponse(connection, err);
|
2014-02-13 00:57:06 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-11-27 16:42:40 +00:00
|
|
|
}
|
2014-02-13 00:57:06 +00:00
|
|
|
runQuery(connection);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Bypass USE db
|
|
|
|
runQuery(connection);
|
|
|
|
}
|
2015-05-18 19:02:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (transaction && transaction.connection &&
|
|
|
|
transaction.connector === this) {
|
|
|
|
if (debugEnabled) {
|
|
|
|
debug('Execute SQL within a transaction');
|
|
|
|
}
|
|
|
|
executeWithConnection(null, transaction.connection);
|
|
|
|
} else {
|
|
|
|
client.getConnection(executeWithConnection);
|
|
|
|
}
|
2012-12-14 14:01:44 +00:00
|
|
|
};
|
|
|
|
|
2015-12-04 19:55:13 +00:00
|
|
|
MySQL.prototype._modifyOrCreate = function(model, data, options, fields, cb) {
|
2020-08-28 06:43:09 +00:00
|
|
|
const sql = new ParameterizedSQL('INSERT INTO ' + this.tableEscaped(model));
|
|
|
|
const columnValues = fields.columnValues;
|
|
|
|
const fieldNames = fields.names;
|
2016-08-10 18:41:03 +00:00
|
|
|
if (fieldNames.length) {
|
|
|
|
sql.merge('(' + fieldNames.join(',') + ')', '');
|
2020-08-28 06:43:09 +00:00
|
|
|
const values = ParameterizedSQL.join(columnValues, ',');
|
2016-08-10 18:41:03 +00:00
|
|
|
values.sql = 'VALUES(' + values.sql + ')';
|
|
|
|
sql.merge(values);
|
|
|
|
} else {
|
|
|
|
sql.merge(this.buildInsertDefaultValues(model, data, options));
|
|
|
|
}
|
2012-12-14 14:01:44 +00:00
|
|
|
|
2016-08-10 18:41:03 +00:00
|
|
|
sql.merge('ON DUPLICATE KEY UPDATE');
|
2020-08-28 06:43:09 +00:00
|
|
|
const setValues = [];
|
|
|
|
for (let i = 0, n = fields.names.length; i < n; i++) {
|
2016-08-10 18:41:03 +00:00
|
|
|
if (!fields.properties[i].id) {
|
|
|
|
setValues.push(new ParameterizedSQL(fields.names[i] + '=' +
|
2021-08-23 14:11:19 +00:00
|
|
|
columnValues[i].sql, columnValues[i].params));
|
2015-04-01 23:25:23 +00:00
|
|
|
}
|
2016-08-10 18:41:03 +00:00
|
|
|
}
|
2012-12-14 14:01:44 +00:00
|
|
|
|
2016-08-10 18:41:03 +00:00
|
|
|
sql.merge(ParameterizedSQL.join(setValues, ','));
|
2015-05-13 17:17:15 +00:00
|
|
|
|
2016-08-10 18:41:03 +00:00
|
|
|
this.execute(sql.sql, sql.params, options, function(err, info) {
|
|
|
|
if (!err && info && info.insertId) {
|
|
|
|
data.id = info.insertId;
|
|
|
|
}
|
2020-08-28 06:43:09 +00:00
|
|
|
const meta = {};
|
2017-07-19 04:06:21 +00:00
|
|
|
// When using the INSERT ... ON DUPLICATE KEY UPDATE statement,
|
|
|
|
// the returned value is as follows:
|
|
|
|
// 1 for each successful INSERT.
|
|
|
|
// 2 for each successful UPDATE.
|
|
|
|
// 1 also for UPDATE with same values, so we cannot accurately
|
|
|
|
// report if we have a new instance.
|
|
|
|
meta.isNewInstance = undefined;
|
2016-08-10 18:41:03 +00:00
|
|
|
cb(err, data, meta);
|
|
|
|
});
|
|
|
|
};
|
2016-05-09 02:37:19 +00:00
|
|
|
|
2015-12-04 19:55:13 +00:00
|
|
|
/**
|
|
|
|
* Replace if the model instance exists with the same id or create a new instance
|
|
|
|
*
|
|
|
|
* @param {String} model The model name
|
|
|
|
* @param {Object} data The model instance data
|
|
|
|
* @param {Object} options The options
|
|
|
|
* @param {Function} [cb] The callback function
|
|
|
|
*/
|
|
|
|
MySQL.prototype.replaceOrCreate = function(model, data, options, cb) {
|
2020-08-28 06:43:09 +00:00
|
|
|
const fields = this.buildReplaceFields(model, data);
|
2015-12-04 19:55:13 +00:00
|
|
|
this._modifyOrCreate(model, data, options, fields, cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update if the model instance exists with the same id or create a new instance
|
|
|
|
*
|
|
|
|
* @param {String} model The model name
|
|
|
|
* @param {Object} data The model instance data
|
|
|
|
* @param {Object} options The options
|
|
|
|
* @param {Function} [cb] The callback function
|
|
|
|
*/
|
|
|
|
MySQL.prototype.save =
|
2021-08-23 14:11:19 +00:00
|
|
|
MySQL.prototype.updateOrCreate = function(model, data, options, cb) {
|
|
|
|
const fields = this.buildFields(model, data);
|
|
|
|
this._modifyOrCreate(model, data, options, fields, cb);
|
|
|
|
};
|
2012-12-14 14:01:44 +00:00
|
|
|
|
2015-05-13 17:17:15 +00:00
|
|
|
MySQL.prototype.getInsertedId = function(model, info) {
|
2020-08-28 06:43:09 +00:00
|
|
|
const insertedId = info && typeof info.insertId === 'number' ?
|
2015-05-13 17:17:15 +00:00
|
|
|
info.insertId : undefined;
|
|
|
|
return insertedId;
|
|
|
|
};
|
|
|
|
|
2022-12-11 16:39:10 +00:00
|
|
|
MySQL.prototype.getInsertedIds = function(model, info) {
|
|
|
|
let insertedIds = [];
|
|
|
|
const idProp = this.getDataSource(model).idProperty(model);
|
|
|
|
if (info && info.affectedRows > 0) {
|
|
|
|
insertedIds = new Array(info.affectedRows);
|
|
|
|
for (let i = 0; i < info.affectedRows; i++) {
|
|
|
|
insertedIds[i] = idProp.generated && typeof idProp.type() === 'number' &&
|
|
|
|
typeof info.insertId === 'number' && info.insertId > 0 ?
|
|
|
|
info.insertId + i : undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return insertedIds;
|
|
|
|
};
|
|
|
|
|
2013-10-03 21:50:38 +00:00
|
|
|
/*!
|
2015-05-13 17:17:15 +00:00
|
|
|
* Convert property name/value to an escaped DB column value
|
|
|
|
* @param {Object} prop Property descriptor
|
|
|
|
* @param {*} val Property value
|
|
|
|
* @returns {*} The escaped value of DB column
|
2013-10-03 21:50:38 +00:00
|
|
|
*/
|
2015-05-13 17:17:15 +00:00
|
|
|
MySQL.prototype.toColumnValue = function(prop, val) {
|
2017-06-05 20:32:12 +00:00
|
|
|
if (val === undefined && this.isNullable(prop)) {
|
2015-05-13 17:17:15 +00:00
|
|
|
return null;
|
2014-02-13 00:57:06 +00:00
|
|
|
}
|
2017-06-05 20:32:12 +00:00
|
|
|
if (val === null) {
|
|
|
|
if (this.isNullable(prop)) {
|
|
|
|
return val;
|
2023-06-18 18:04:31 +00:00
|
|
|
} else if (prop.type === Date) {
|
|
|
|
// MySQL has disallowed comparison of date types with strings.
|
|
|
|
// https://bugs.mysql.com/bug.php?id=95466
|
|
|
|
// https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-16.html
|
|
|
|
return new Date();
|
2017-06-05 20:32:12 +00:00
|
|
|
} else {
|
|
|
|
try {
|
2020-08-28 06:43:09 +00:00
|
|
|
const castNull = prop.type(val);
|
2017-06-05 20:32:12 +00:00
|
|
|
if (prop.type === Object) {
|
|
|
|
return JSON.stringify(castNull);
|
|
|
|
}
|
|
|
|
return castNull;
|
|
|
|
} catch (err) {
|
2017-07-24 16:48:26 +00:00
|
|
|
// if we can't coerce null to a certain type,
|
|
|
|
// we just return it
|
2017-06-05 20:32:12 +00:00
|
|
|
return 'null';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-30 22:15:27 +00:00
|
|
|
if (!prop) {
|
2015-05-13 17:17:15 +00:00
|
|
|
return val;
|
2014-05-30 22:15:27 +00:00
|
|
|
}
|
2015-03-05 06:55:33 +00:00
|
|
|
if (prop.type === String) {
|
2015-05-13 17:17:15 +00:00
|
|
|
return String(val);
|
2015-03-05 06:55:33 +00:00
|
|
|
}
|
2014-05-30 22:15:27 +00:00
|
|
|
if (prop.type === Number) {
|
2015-02-21 00:15:15 +00:00
|
|
|
if (isNaN(val)) {
|
2015-05-13 17:17:15 +00:00
|
|
|
// FIXME: [rfeng] Should fail fast?
|
|
|
|
return val;
|
2015-02-21 00:15:15 +00:00
|
|
|
}
|
2015-05-13 17:17:15 +00:00
|
|
|
return val;
|
2014-05-30 22:15:27 +00:00
|
|
|
}
|
|
|
|
if (prop.type === Date) {
|
2014-02-13 00:57:06 +00:00
|
|
|
if (!val.toUTCString) {
|
|
|
|
val = new Date(val);
|
2013-10-07 21:46:05 +00:00
|
|
|
}
|
2017-04-25 22:10:40 +00:00
|
|
|
return val;
|
2014-02-13 00:57:06 +00:00
|
|
|
}
|
2017-04-27 18:30:00 +00:00
|
|
|
if (prop.type.name === 'DateString') {
|
|
|
|
return val.when;
|
|
|
|
}
|
2014-05-30 22:15:27 +00:00
|
|
|
if (prop.type === Boolean) {
|
2015-05-13 17:17:15 +00:00
|
|
|
return !!val;
|
2014-05-30 22:15:27 +00:00
|
|
|
}
|
2014-02-13 00:57:06 +00:00
|
|
|
if (prop.type.name === 'GeoPoint') {
|
2015-05-13 17:17:15 +00:00
|
|
|
return new ParameterizedSQL({
|
|
|
|
sql: 'Point(?,?)',
|
2017-05-29 19:11:21 +00:00
|
|
|
params: [val.lng, val.lat],
|
2015-05-13 17:17:15 +00:00
|
|
|
});
|
2014-02-13 00:57:06 +00:00
|
|
|
}
|
2016-12-15 17:22:04 +00:00
|
|
|
if (prop.type === Buffer) {
|
|
|
|
return val;
|
|
|
|
}
|
2014-05-25 16:46:55 +00:00
|
|
|
if (prop.type === Object) {
|
2015-03-05 06:55:33 +00:00
|
|
|
return this._serializeObject(val);
|
2014-05-25 16:46:55 +00:00
|
|
|
}
|
|
|
|
if (typeof prop.type === 'function') {
|
2015-03-05 06:55:33 +00:00
|
|
|
return this._serializeObject(val);
|
2014-05-25 16:46:55 +00:00
|
|
|
}
|
2015-03-05 06:55:33 +00:00
|
|
|
return this._serializeObject(val);
|
|
|
|
};
|
|
|
|
|
|
|
|
MySQL.prototype._serializeObject = function(obj) {
|
2020-08-28 06:43:09 +00:00
|
|
|
let val;
|
2015-03-05 06:55:33 +00:00
|
|
|
if (obj && typeof obj.toJSON === 'function') {
|
|
|
|
obj = obj.toJSON();
|
|
|
|
}
|
|
|
|
if (typeof obj !== 'string') {
|
|
|
|
val = JSON.stringify(obj);
|
|
|
|
} else {
|
|
|
|
val = obj;
|
|
|
|
}
|
2015-05-13 17:17:15 +00:00
|
|
|
return val;
|
2012-12-14 14:01:44 +00:00
|
|
|
};
|
|
|
|
|
2013-10-03 21:50:38 +00:00
|
|
|
/*!
|
2015-05-13 17:17:15 +00:00
|
|
|
* Convert the data from database column to model property
|
|
|
|
* @param {object} Model property descriptor
|
|
|
|
* @param {*) val Column value
|
|
|
|
* @returns {*} Model property value
|
2013-10-03 21:50:38 +00:00
|
|
|
*/
|
2015-05-13 17:17:15 +00:00
|
|
|
MySQL.prototype.fromColumnValue = function(prop, val) {
|
|
|
|
if (val == null) {
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
if (prop) {
|
|
|
|
switch (prop.type.name) {
|
|
|
|
case 'Number':
|
|
|
|
val = Number(val);
|
|
|
|
break;
|
|
|
|
case 'String':
|
|
|
|
val = String(val);
|
|
|
|
break;
|
|
|
|
case 'Date':
|
2017-04-27 18:30:00 +00:00
|
|
|
case 'DateString':
|
2016-07-26 11:39:52 +00:00
|
|
|
// MySQL allows, unless NO_ZERO_DATE is set, dummy date/time entries
|
|
|
|
// new Date() will return Invalid Date for those, so we need to handle
|
|
|
|
// those separate.
|
2017-05-03 21:45:02 +00:00
|
|
|
if (!val || /^0{4}(-00){2}( (00:){2}0{2}(\.0{1,6}){0,1}){0,1}$/.test(val)) {
|
2016-07-26 11:39:52 +00:00
|
|
|
val = null;
|
|
|
|
}
|
2015-05-13 17:17:15 +00:00
|
|
|
break;
|
|
|
|
case 'Boolean':
|
2019-04-19 14:31:23 +00:00
|
|
|
// BIT(1) case: <Buffer 01> for true and <Buffer 00> for false
|
|
|
|
// CHAR(1) case: '1' for true and '0' for false
|
|
|
|
// TINYINT(1) case: 1 for true and 0 for false
|
|
|
|
val = Buffer.isBuffer(val) && val.length === 1 ? Boolean(val[0]) : Boolean(parseInt(val));
|
2015-05-13 17:17:15 +00:00
|
|
|
break;
|
|
|
|
case 'GeoPoint':
|
|
|
|
case 'Point':
|
|
|
|
val = {
|
2017-05-29 19:11:21 +00:00
|
|
|
lng: val.x,
|
|
|
|
lat: val.y,
|
2015-05-13 17:17:15 +00:00
|
|
|
};
|
|
|
|
break;
|
2019-06-28 14:54:45 +00:00
|
|
|
case 'ObjectID':
|
|
|
|
val = new prop.type(val);
|
|
|
|
break;
|
|
|
|
case 'Buffer':
|
|
|
|
val = prop.type(val);
|
|
|
|
break;
|
2015-05-13 17:17:15 +00:00
|
|
|
case 'List':
|
|
|
|
case 'Array':
|
|
|
|
case 'Object':
|
|
|
|
case 'JSON':
|
|
|
|
if (typeof val === 'string') {
|
|
|
|
val = JSON.parse(val);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2013-10-22 18:25:17 +00:00
|
|
|
}
|
2014-02-13 00:57:06 +00:00
|
|
|
}
|
2015-05-13 17:17:15 +00:00
|
|
|
return val;
|
2012-12-14 14:01:44 +00:00
|
|
|
};
|
|
|
|
|
2015-05-13 17:17:15 +00:00
|
|
|
/**
|
|
|
|
* Escape an identifier such as the column name
|
|
|
|
* @param {string} name A database identifier
|
|
|
|
* @returns {string} The escaped database identifier
|
|
|
|
*/
|
2015-05-18 19:02:25 +00:00
|
|
|
MySQL.prototype.escapeName = function(name) {
|
2015-01-09 17:02:36 +00:00
|
|
|
return this.client.escapeId(name);
|
2012-12-14 14:01:44 +00:00
|
|
|
};
|
|
|
|
|
2015-05-13 17:17:15 +00:00
|
|
|
/**
|
|
|
|
* Build the LIMIT clause
|
|
|
|
* @param {string} model Model name
|
|
|
|
* @param {number} limit The limit
|
|
|
|
* @param {number} offset The offset
|
|
|
|
* @returns {string} The LIMIT clause
|
|
|
|
*/
|
2015-05-18 19:02:25 +00:00
|
|
|
MySQL.prototype._buildLimit = function(model, limit, offset) {
|
2014-05-30 22:15:27 +00:00
|
|
|
if (isNaN(limit)) {
|
|
|
|
limit = 0;
|
|
|
|
}
|
|
|
|
if (isNaN(offset)) {
|
|
|
|
offset = 0;
|
|
|
|
}
|
2015-05-13 17:17:15 +00:00
|
|
|
if (!limit && !offset) {
|
|
|
|
return '';
|
|
|
|
}
|
2014-05-30 22:15:27 +00:00
|
|
|
return 'LIMIT ' + (offset ? (offset + ',' + limit) : limit);
|
2016-08-10 18:41:03 +00:00
|
|
|
};
|
2013-07-21 05:37:59 +00:00
|
|
|
|
2015-05-13 17:17:15 +00:00
|
|
|
MySQL.prototype.applyPagination = function(model, stmt, filter) {
|
2020-08-28 06:43:09 +00:00
|
|
|
const limitClause = this._buildLimit(model, filter.limit,
|
2015-05-13 17:17:15 +00:00
|
|
|
filter.offset || filter.skip);
|
|
|
|
return stmt.merge(limitClause);
|
2013-08-18 17:04:58 +00:00
|
|
|
};
|
2012-12-14 14:01:44 +00:00
|
|
|
|
2013-08-18 17:04:58 +00:00
|
|
|
/**
|
2015-05-13 17:17:15 +00:00
|
|
|
* Get the place holder in SQL for identifiers, such as ??
|
|
|
|
* @param {String} key Optional key, such as 1 or id
|
|
|
|
* @returns {String} The place holder
|
2013-08-18 17:04:58 +00:00
|
|
|
*/
|
2015-05-18 19:02:25 +00:00
|
|
|
MySQL.prototype.getPlaceholderForIdentifier = function(key) {
|
2015-05-13 17:17:15 +00:00
|
|
|
return '??';
|
2012-12-14 14:01:44 +00:00
|
|
|
};
|
|
|
|
|
2013-10-03 21:50:38 +00:00
|
|
|
/**
|
2015-05-13 17:17:15 +00:00
|
|
|
* Get the place holder in SQL for values, such as :1 or ?
|
|
|
|
* @param {String} key Optional key, such as 1 or id
|
|
|
|
* @returns {String} The place holder
|
2013-10-08 20:44:58 +00:00
|
|
|
*/
|
2015-05-18 19:02:25 +00:00
|
|
|
MySQL.prototype.getPlaceholderForValue = function(key) {
|
2015-05-13 17:17:15 +00:00
|
|
|
return '?';
|
2013-10-08 20:44:58 +00:00
|
|
|
};
|
|
|
|
|
2015-05-13 17:17:15 +00:00
|
|
|
MySQL.prototype.getCountForAffectedRows = function(model, info) {
|
2020-08-28 06:43:09 +00:00
|
|
|
const affectedRows = info && typeof info.affectedRows === 'number' ?
|
2015-05-13 17:17:15 +00:00
|
|
|
info.affectedRows : undefined;
|
|
|
|
return affectedRows;
|
2012-12-14 14:01:44 +00:00
|
|
|
};
|
|
|
|
|
2013-11-27 01:40:31 +00:00
|
|
|
/**
|
2013-11-27 16:42:40 +00:00
|
|
|
* Disconnect from MySQL
|
2013-11-27 01:40:31 +00:00
|
|
|
*/
|
2015-05-18 19:02:25 +00:00
|
|
|
MySQL.prototype.disconnect = function(cb) {
|
2014-02-13 00:57:06 +00:00
|
|
|
if (this.debug) {
|
2014-04-08 05:16:26 +00:00
|
|
|
debug('disconnect');
|
2014-02-13 00:57:06 +00:00
|
|
|
}
|
|
|
|
if (this.client) {
|
2019-05-09 11:26:56 +00:00
|
|
|
this.client.end((err) => {
|
|
|
|
this.client = null;
|
|
|
|
cb(err);
|
|
|
|
});
|
2015-05-13 17:17:15 +00:00
|
|
|
} else {
|
|
|
|
process.nextTick(cb);
|
2014-02-13 00:57:06 +00:00
|
|
|
}
|
2013-11-27 01:40:31 +00:00
|
|
|
};
|
|
|
|
|
2014-08-20 23:12:46 +00:00
|
|
|
MySQL.prototype.ping = function(cb) {
|
2015-05-13 17:17:15 +00:00
|
|
|
this.execute('SELECT 1 AS result', cb);
|
2014-08-20 23:12:46 +00:00
|
|
|
};
|
|
|
|
|
2015-07-21 04:50:47 +00:00
|
|
|
MySQL.prototype.buildExpression = function(columnName, operator, operatorValue,
|
2017-07-24 16:48:26 +00:00
|
|
|
propertyDefinition) {
|
2021-08-23 14:11:19 +00:00
|
|
|
let clause;
|
|
|
|
switch (operator) {
|
|
|
|
case 'regexp':
|
2023-06-18 18:04:31 +00:00
|
|
|
// https://dev.mysql.com/doc/refman/8.0/en/regexp.html#function_regexp-like
|
|
|
|
// REGEXP_LIKE(expr, pat[, match_type]) - match_type parameter now support c,i and m flags of RegExp
|
|
|
|
let matchType = '';
|
|
|
|
if (operatorValue.ignoreCase === false) {
|
|
|
|
matchType += 'c';
|
|
|
|
} else if (operatorValue.ignoreCase === true) {
|
|
|
|
matchType += 'i';
|
2021-08-23 14:11:19 +00:00
|
|
|
}
|
2015-07-21 04:50:47 +00:00
|
|
|
|
2023-06-18 18:04:31 +00:00
|
|
|
if (operatorValue.multiline) {
|
|
|
|
matchType += 'm';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (operatorValue.global) {
|
2021-08-23 14:11:19 +00:00
|
|
|
g.warn('{{MySQL}} {{regex}} syntax does not respect the {{`g`}} flag');
|
2023-06-18 18:04:31 +00:00
|
|
|
}
|
2021-08-23 14:11:19 +00:00
|
|
|
|
2023-06-18 18:04:31 +00:00
|
|
|
if (!!matchType) {
|
|
|
|
clause = `REGEXP_LIKE(${columnName}, ?, '${matchType}')`;
|
|
|
|
} else {
|
|
|
|
clause = `REGEXP_LIKE(${columnName}, ?)`;
|
|
|
|
}
|
2021-08-23 14:11:19 +00:00
|
|
|
|
|
|
|
return new ParameterizedSQL(clause,
|
|
|
|
[operatorValue.source]);
|
|
|
|
case 'matchnl':
|
|
|
|
case 'matchqe':
|
|
|
|
case 'matchnlqe':
|
|
|
|
case 'matchbool':
|
|
|
|
case 'match':
|
|
|
|
let mode;
|
|
|
|
switch (operator) {
|
|
|
|
case 'matchbool':
|
|
|
|
mode = ' IN BOOLEAN MODE';
|
|
|
|
break;
|
|
|
|
case 'matchnl':
|
|
|
|
mode = ' IN NATURAL LANGUAGE MODE';
|
|
|
|
break;
|
|
|
|
case 'matchqe':
|
|
|
|
mode = ' WITH QUERY EXPANSION';
|
|
|
|
break;
|
|
|
|
case 'matchnlqe':
|
|
|
|
mode = ' IN NATURAL LANGUAGE MODE WITH QUERY EXPANSION';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
mode = '';
|
|
|
|
}
|
|
|
|
clause = ` MATCH (${columnName}) AGAINST (?${mode})`;
|
2015-07-21 04:50:47 +00:00
|
|
|
|
2021-08-23 14:11:19 +00:00
|
|
|
return new ParameterizedSQL(clause, [operatorValue]);
|
2015-07-21 04:50:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// invoke the base implementation of `buildExpression`
|
|
|
|
return this.invokeSuper('buildExpression', columnName, operator,
|
2017-07-24 16:48:26 +00:00
|
|
|
operatorValue, propertyDefinition);
|
2015-07-21 04:50:47 +00:00
|
|
|
};
|
|
|
|
|
2015-05-13 17:17:15 +00:00
|
|
|
require('./migration')(MySQL, mysql);
|
|
|
|
require('./discovery')(MySQL, mysql);
|
2015-05-18 19:02:25 +00:00
|
|
|
require('./transaction')(MySQL, mysql);
|