init source
This commit is contained in:
+145
@@ -0,0 +1,145 @@
|
||||
'use strict';
|
||||
|
||||
const AbstractConnectionManager = require('../abstract/connection-manager');
|
||||
const SequelizeErrors = require('../../errors');
|
||||
const Promise = require('../../promise');
|
||||
const { logger } = require('../../utils/logger');
|
||||
const DataTypes = require('../../data-types').mariadb;
|
||||
const momentTz = require('moment-timezone');
|
||||
const debug = logger.debugContext('connection:mariadb');
|
||||
const parserStore = require('../parserStore')('mariadb');
|
||||
|
||||
/**
|
||||
* MariaDB Connection Manager
|
||||
*
|
||||
* Get connections, validate and disconnect them.
|
||||
* AbstractConnectionManager pooling use it to handle MariaDB specific connections
|
||||
* Use https://github.com/MariaDB/mariadb-connector-nodejs to connect with MariaDB server
|
||||
*
|
||||
* @extends AbstractConnectionManager
|
||||
* @returns Class<ConnectionManager>
|
||||
* @private
|
||||
*/
|
||||
|
||||
class ConnectionManager extends AbstractConnectionManager {
|
||||
constructor(dialect, sequelize) {
|
||||
sequelize.config.port = sequelize.config.port || 3306;
|
||||
super(dialect, sequelize);
|
||||
this.lib = this._loadDialectModule('mariadb');
|
||||
this.refreshTypeParser(DataTypes);
|
||||
}
|
||||
|
||||
static _typecast(field, next) {
|
||||
if (parserStore.get(field.type)) {
|
||||
return parserStore.get(field.type)(field, this.sequelize.options, next);
|
||||
}
|
||||
return next();
|
||||
}
|
||||
|
||||
_refreshTypeParser(dataType) {
|
||||
parserStore.refresh(dataType);
|
||||
}
|
||||
|
||||
_clearTypeParser() {
|
||||
parserStore.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect with MariaDB database based on config, Handle any errors in connection
|
||||
* Set the pool handlers on connection.error
|
||||
* Also set proper timezone once connection is connected.
|
||||
*
|
||||
* @param {Object} config
|
||||
* @returns {Promise<Connection>}
|
||||
* @private
|
||||
*/
|
||||
connect(config) {
|
||||
// Named timezone is not supported in mariadb, convert to offset
|
||||
let tzOffset = this.sequelize.options.timezone;
|
||||
tzOffset = /\//.test(tzOffset) ? momentTz.tz(tzOffset).format('Z')
|
||||
: tzOffset;
|
||||
|
||||
const connectionConfig = {
|
||||
host: config.host,
|
||||
port: config.port,
|
||||
user: config.username,
|
||||
password: config.password,
|
||||
database: config.database,
|
||||
timezone: tzOffset,
|
||||
typeCast: ConnectionManager._typecast.bind(this),
|
||||
bigNumberStrings: false,
|
||||
supportBigNumbers: true,
|
||||
foundRows: false
|
||||
};
|
||||
|
||||
if (config.dialectOptions) {
|
||||
Object.assign(connectionConfig, config.dialectOptions);
|
||||
}
|
||||
|
||||
if (!this.sequelize.config.keepDefaultTimezone) {
|
||||
// set timezone for this connection
|
||||
if (connectionConfig.initSql) {
|
||||
if (!Array.isArray(
|
||||
connectionConfig.initSql)) {
|
||||
connectionConfig.initSql = [connectionConfig.initSql];
|
||||
}
|
||||
connectionConfig.initSql.push(`SET time_zone = '${tzOffset}'`);
|
||||
} else {
|
||||
connectionConfig.initSql = `SET time_zone = '${tzOffset}'`;
|
||||
}
|
||||
}
|
||||
|
||||
return this.lib.createConnection(connectionConfig)
|
||||
.then(connection => {
|
||||
this.sequelize.options.databaseVersion = connection.serverVersion();
|
||||
debug('connection acquired');
|
||||
connection.on('error', error => {
|
||||
switch (error.code) {
|
||||
case 'ESOCKET':
|
||||
case 'ECONNRESET':
|
||||
case 'EPIPE':
|
||||
case 'PROTOCOL_CONNECTION_LOST':
|
||||
this.pool.destroy(connection);
|
||||
}
|
||||
});
|
||||
return connection;
|
||||
})
|
||||
.catch(err => {
|
||||
switch (err.code) {
|
||||
case 'ECONNREFUSED':
|
||||
throw new SequelizeErrors.ConnectionRefusedError(err);
|
||||
case 'ER_ACCESS_DENIED_ERROR':
|
||||
case 'ER_ACCESS_DENIED_NO_PASSWORD_ERROR':
|
||||
throw new SequelizeErrors.AccessDeniedError(err);
|
||||
case 'ENOTFOUND':
|
||||
throw new SequelizeErrors.HostNotFoundError(err);
|
||||
case 'EHOSTUNREACH':
|
||||
case 'ENETUNREACH':
|
||||
case 'EADDRNOTAVAIL':
|
||||
throw new SequelizeErrors.HostNotReachableError(err);
|
||||
case 'EINVAL':
|
||||
throw new SequelizeErrors.InvalidConnectionError(err);
|
||||
default:
|
||||
throw new SequelizeErrors.ConnectionError(err);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
disconnect(connection) {
|
||||
// Don't disconnect connections with CLOSED state
|
||||
if (!connection.isValid()) {
|
||||
debug('connection tried to disconnect but was already at CLOSED state');
|
||||
return Promise.resolve();
|
||||
}
|
||||
//wrap native Promise into bluebird
|
||||
return Promise.resolve(connection.end());
|
||||
}
|
||||
|
||||
validate(connection) {
|
||||
return connection && connection.isValid();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ConnectionManager;
|
||||
module.exports.ConnectionManager = ConnectionManager;
|
||||
module.exports.default = ConnectionManager;
|
||||
+122
@@ -0,0 +1,122 @@
|
||||
'use strict';
|
||||
|
||||
const _ = require('lodash');
|
||||
const moment = require('moment-timezone');
|
||||
|
||||
module.exports = BaseTypes => {
|
||||
BaseTypes.ABSTRACT.prototype.dialectTypes = 'https://mariadb.com/kb/en/library/resultset/#field-types';
|
||||
|
||||
/**
|
||||
* types: [buffer_type, ...]
|
||||
* @see documentation : https://mariadb.com/kb/en/library/resultset/#field-types
|
||||
* @see connector implementation : https://github.com/MariaDB/mariadb-connector-nodejs/blob/master/lib/const/field-type.js
|
||||
*/
|
||||
|
||||
BaseTypes.DATE.types.mariadb = ['DATETIME'];
|
||||
BaseTypes.STRING.types.mariadb = ['VAR_STRING'];
|
||||
BaseTypes.CHAR.types.mariadb = ['STRING'];
|
||||
BaseTypes.TEXT.types.mariadb = ['BLOB'];
|
||||
BaseTypes.TINYINT.types.mariadb = ['TINY'];
|
||||
BaseTypes.SMALLINT.types.mariadb = ['SHORT'];
|
||||
BaseTypes.MEDIUMINT.types.mariadb = ['INT24'];
|
||||
BaseTypes.INTEGER.types.mariadb = ['LONG'];
|
||||
BaseTypes.BIGINT.types.mariadb = ['LONGLONG'];
|
||||
BaseTypes.FLOAT.types.mariadb = ['FLOAT'];
|
||||
BaseTypes.TIME.types.mariadb = ['TIME'];
|
||||
BaseTypes.DATEONLY.types.mariadb = ['DATE'];
|
||||
BaseTypes.BOOLEAN.types.mariadb = ['TINY'];
|
||||
BaseTypes.BLOB.types.mariadb = ['TINYBLOB', 'BLOB', 'LONGBLOB'];
|
||||
BaseTypes.DECIMAL.types.mariadb = ['NEWDECIMAL'];
|
||||
BaseTypes.UUID.types.mariadb = false;
|
||||
BaseTypes.ENUM.types.mariadb = false;
|
||||
BaseTypes.REAL.types.mariadb = ['DOUBLE'];
|
||||
BaseTypes.DOUBLE.types.mariadb = ['DOUBLE'];
|
||||
BaseTypes.GEOMETRY.types.mariadb = ['GEOMETRY'];
|
||||
BaseTypes.JSON.types.mariadb = ['JSON'];
|
||||
|
||||
class DECIMAL extends BaseTypes.DECIMAL {
|
||||
toSql() {
|
||||
let definition = super.toSql();
|
||||
if (this._unsigned) {
|
||||
definition += ' UNSIGNED';
|
||||
}
|
||||
if (this._zerofill) {
|
||||
definition += ' ZEROFILL';
|
||||
}
|
||||
return definition;
|
||||
}
|
||||
}
|
||||
|
||||
class DATE extends BaseTypes.DATE {
|
||||
toSql() {
|
||||
return `DATETIME${this._length ? `(${this._length})` : ''}`;
|
||||
}
|
||||
_stringify(date, options) {
|
||||
date = this._applyTimezone(date, options);
|
||||
return date.format('YYYY-MM-DD HH:mm:ss.SSS');
|
||||
}
|
||||
static parse(value, options) {
|
||||
value = value.string();
|
||||
if (value === null) {
|
||||
return value;
|
||||
}
|
||||
if (moment.tz.zone(options.timezone)) {
|
||||
value = moment.tz(value, options.timezone).toDate();
|
||||
}
|
||||
else {
|
||||
value = new Date(`${value} ${options.timezone}`);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
class DATEONLY extends BaseTypes.DATEONLY {
|
||||
static parse(value) {
|
||||
return value.string();
|
||||
}
|
||||
}
|
||||
|
||||
class UUID extends BaseTypes.UUID {
|
||||
toSql() {
|
||||
return 'CHAR(36) BINARY';
|
||||
}
|
||||
}
|
||||
|
||||
class GEOMETRY extends BaseTypes.GEOMETRY {
|
||||
constructor(type, srid) {
|
||||
super(type, srid);
|
||||
if (_.isEmpty(this.type)) {
|
||||
this.sqlType = this.key;
|
||||
}
|
||||
else {
|
||||
this.sqlType = this.type;
|
||||
}
|
||||
}
|
||||
toSql() {
|
||||
return this.sqlType;
|
||||
}
|
||||
}
|
||||
|
||||
class ENUM extends BaseTypes.ENUM {
|
||||
toSql(options) {
|
||||
return `ENUM(${this.values.map(value => options.escape(value)).join(', ')})`;
|
||||
}
|
||||
}
|
||||
|
||||
class JSONTYPE extends BaseTypes.JSON {
|
||||
_stringify(value, options) {
|
||||
return options.operation === 'where' && typeof value === 'string' ? value
|
||||
: JSON.stringify(value);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
ENUM,
|
||||
DATE,
|
||||
DATEONLY,
|
||||
UUID,
|
||||
GEOMETRY,
|
||||
DECIMAL,
|
||||
JSON: JSONTYPE
|
||||
};
|
||||
};
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
'use strict';
|
||||
|
||||
const _ = require('lodash');
|
||||
const AbstractDialect = require('../abstract');
|
||||
const ConnectionManager = require('./connection-manager');
|
||||
const Query = require('./query');
|
||||
const QueryGenerator = require('./query-generator');
|
||||
const DataTypes = require('../../data-types').mariadb;
|
||||
|
||||
class MariadbDialect extends AbstractDialect {
|
||||
constructor(sequelize) {
|
||||
super();
|
||||
this.sequelize = sequelize;
|
||||
this.connectionManager = new ConnectionManager(this, sequelize);
|
||||
this.QueryGenerator = new QueryGenerator({
|
||||
_dialect: this,
|
||||
sequelize
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
MariadbDialect.prototype.supports = _.merge(
|
||||
_.cloneDeep(AbstractDialect.prototype.supports), {
|
||||
'VALUES ()': true,
|
||||
'LIMIT ON UPDATE': true,
|
||||
lock: true,
|
||||
forShare: 'LOCK IN SHARE MODE',
|
||||
settingIsolationLevelDuringTransaction: false,
|
||||
schemas: true,
|
||||
inserts: {
|
||||
ignoreDuplicates: ' IGNORE',
|
||||
updateOnDuplicate: ' ON DUPLICATE KEY UPDATE'
|
||||
},
|
||||
index: {
|
||||
collate: false,
|
||||
length: true,
|
||||
parser: true,
|
||||
type: true,
|
||||
using: 1
|
||||
},
|
||||
constraints: {
|
||||
dropConstraint: false,
|
||||
check: false
|
||||
},
|
||||
indexViaAlter: true,
|
||||
NUMERIC: true,
|
||||
GEOMETRY: true,
|
||||
JSON: true,
|
||||
REGEXP: true
|
||||
});
|
||||
|
||||
ConnectionManager.prototype.defaultVersion = '5.5.3';
|
||||
MariadbDialect.prototype.Query = Query;
|
||||
MariadbDialect.prototype.QueryGenerator = QueryGenerator;
|
||||
MariadbDialect.prototype.DataTypes = DataTypes;
|
||||
MariadbDialect.prototype.name = 'mariadb';
|
||||
MariadbDialect.prototype.TICK_CHAR = '`';
|
||||
MariadbDialect.prototype.TICK_CHAR_LEFT = MariadbDialect.prototype.TICK_CHAR;
|
||||
MariadbDialect.prototype.TICK_CHAR_RIGHT = MariadbDialect.prototype.TICK_CHAR;
|
||||
|
||||
module.exports = MariadbDialect;
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
'use strict';
|
||||
|
||||
const MySQLQueryGenerator = require('../mysql/query-generator');
|
||||
|
||||
class MariaDBQueryGenerator extends MySQLQueryGenerator {
|
||||
createSchema(schema, options) {
|
||||
options = Object.assign({
|
||||
charset: null,
|
||||
collate: null
|
||||
}, options || {});
|
||||
|
||||
const charset = options.charset ? ` DEFAULT CHARACTER SET ${this.escape(options.charset)}` : '';
|
||||
const collate = options.collate ? ` DEFAULT COLLATE ${this.escape(options.collate)}` : '';
|
||||
|
||||
return `CREATE SCHEMA IF NOT EXISTS ${this.quoteIdentifier(schema)}${charset}${collate};`;
|
||||
}
|
||||
|
||||
dropSchema(schema) {
|
||||
return `DROP SCHEMA IF EXISTS ${this.quoteIdentifier(schema)};`;
|
||||
}
|
||||
|
||||
showSchemasQuery(options) {
|
||||
const skip = options.skip && Array.isArray(options.skip) && options.skip.length > 0 ? options.skip : null;
|
||||
return `SELECT SCHEMA_NAME as schema_name FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME NOT IN ('MYSQL', 'INFORMATION_SCHEMA', 'PERFORMANCE_SCHEMA'${skip ? skip.reduce( (sql, schemaName) => sql += `,${this.escape(schemaName)}`, '') : ''});`;
|
||||
}
|
||||
|
||||
showTablesQuery(database) {
|
||||
let query = 'SELECT TABLE_NAME, TABLE_SCHEMA FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = \'BASE TABLE\'';
|
||||
if (database) {
|
||||
query += ` AND TABLE_SCHEMA = ${this.escape(database)}`;
|
||||
} else {
|
||||
query += ' AND TABLE_SCHEMA NOT IN (\'MYSQL\', \'INFORMATION_SCHEMA\', \'PERFORMANCE_SCHEMA\')';
|
||||
}
|
||||
return `${query};`;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MariaDBQueryGenerator;
|
||||
+325
@@ -0,0 +1,325 @@
|
||||
'use strict';
|
||||
|
||||
const AbstractQuery = require('../abstract/query');
|
||||
const sequelizeErrors = require('../../errors');
|
||||
const _ = require('lodash');
|
||||
const DataTypes = require('../../data-types');
|
||||
const Promise = require('../../promise');
|
||||
const { logger } = require('../../utils/logger');
|
||||
|
||||
const ER_DUP_ENTRY = 1062;
|
||||
const ER_ROW_IS_REFERENCED = 1451;
|
||||
const ER_NO_REFERENCED_ROW = 1452;
|
||||
|
||||
const debug = logger.debugContext('sql:mariadb');
|
||||
|
||||
class Query extends AbstractQuery {
|
||||
constructor(connection, sequelize, options) {
|
||||
super(connection, sequelize, Object.assign({ showWarnings: false }, options));
|
||||
}
|
||||
|
||||
static formatBindParameters(sql, values, dialect) {
|
||||
const bindParam = [];
|
||||
const replacementFunc = (match, key, val) => {
|
||||
if (val[key] !== undefined) {
|
||||
bindParam.push(val[key]);
|
||||
return '?';
|
||||
}
|
||||
return undefined;
|
||||
};
|
||||
sql = AbstractQuery.formatBindParameters(sql, values, dialect,
|
||||
replacementFunc)[0];
|
||||
return [sql, bindParam.length > 0 ? bindParam : undefined];
|
||||
}
|
||||
|
||||
run(sql, parameters) {
|
||||
this.sql = sql;
|
||||
const { connection, options } = this;
|
||||
|
||||
const showWarnings = this.sequelize.options.showWarnings
|
||||
|| options.showWarnings;
|
||||
|
||||
const complete = this._logQuery(sql, debug, parameters);
|
||||
|
||||
if (parameters) {
|
||||
debug('parameters(%j)', parameters);
|
||||
}
|
||||
return Promise.resolve(
|
||||
connection.query(this.sql, parameters)
|
||||
.then(results => {
|
||||
complete();
|
||||
|
||||
// Log warnings if we've got them.
|
||||
if (showWarnings && results && results.warningStatus > 0) {
|
||||
return this.logWarnings(results);
|
||||
}
|
||||
return results;
|
||||
})
|
||||
.catch(err => {
|
||||
// MariaDB automatically rolls-back transactions in the event of a deadlock
|
||||
if (options.transaction && err.errno === 1213) {
|
||||
options.transaction.finished = 'rollback';
|
||||
}
|
||||
|
||||
complete();
|
||||
|
||||
err.sql = sql;
|
||||
err.parameters = parameters;
|
||||
throw this.formatError(err);
|
||||
})
|
||||
)
|
||||
// Log warnings if we've got them.
|
||||
.then(results => {
|
||||
if (showWarnings && results && results.warningStatus > 0) {
|
||||
return this.logWarnings(results);
|
||||
}
|
||||
return results;
|
||||
})
|
||||
// Return formatted results...
|
||||
.then(results => this.formatResults(results));
|
||||
}
|
||||
|
||||
/**
|
||||
* High level function that handles the results of a query execution.
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
* query.formatResults([
|
||||
* {
|
||||
* id: 1, // this is from the main table
|
||||
* attr2: 'snafu', // this is from the main table
|
||||
* Tasks.id: 1, // this is from the associated table
|
||||
* Tasks.title: 'task' // this is from the associated table
|
||||
* }
|
||||
* ])
|
||||
*
|
||||
* @param {Array} data - The result of the query execution.
|
||||
* @private
|
||||
*/
|
||||
formatResults(data) {
|
||||
let result = this.instance;
|
||||
|
||||
if (this.isBulkUpdateQuery() || this.isBulkDeleteQuery()
|
||||
|| this.isUpsertQuery()) {
|
||||
return data.affectedRows;
|
||||
}
|
||||
if (this.isInsertQuery(data)) {
|
||||
this.handleInsertQuery(data);
|
||||
|
||||
if (!this.instance) {
|
||||
// handle bulkCreate AI primary key
|
||||
if (this.model
|
||||
&& this.model.autoIncrementAttribute
|
||||
&& this.model.autoIncrementAttribute === this.model.primaryKeyAttribute
|
||||
&& this.model.rawAttributes[this.model.primaryKeyAttribute]
|
||||
) {
|
||||
//ONLY TRUE IF @auto_increment_increment is set to 1 !!
|
||||
//Doesn't work with GALERA => each node will reserve increment (x for first server, x+1 for next node ...
|
||||
const startId = data[this.getInsertIdField()];
|
||||
result = new Array(data.affectedRows);
|
||||
const pkField = this.model.rawAttributes[this.model.primaryKeyAttribute].field;
|
||||
for (let i = 0; i < data.affectedRows; i++) {
|
||||
result[i] = { [pkField]: startId + i };
|
||||
}
|
||||
return [result, data.affectedRows];
|
||||
}
|
||||
return [data[this.getInsertIdField()], data.affectedRows];
|
||||
}
|
||||
}
|
||||
|
||||
if (this.isSelectQuery()) {
|
||||
this.handleJsonSelectQuery(data);
|
||||
return this.handleSelectQuery(data);
|
||||
}
|
||||
if (this.isInsertQuery() || this.isUpdateQuery()) {
|
||||
return [result, data.affectedRows];
|
||||
}
|
||||
if (this.isCallQuery()) {
|
||||
return data[0];
|
||||
}
|
||||
if (this.isRawQuery()) {
|
||||
const meta = data.meta;
|
||||
delete data.meta;
|
||||
return [data, meta];
|
||||
}
|
||||
if (this.isShowIndexesQuery()) {
|
||||
return this.handleShowIndexesQuery(data);
|
||||
}
|
||||
if (this.isForeignKeysQuery() || this.isShowConstraintsQuery()) {
|
||||
return data;
|
||||
}
|
||||
if (this.isShowTablesQuery()) {
|
||||
return this.handleShowTablesQuery(data);
|
||||
}
|
||||
if (this.isDescribeQuery()) {
|
||||
result = {};
|
||||
|
||||
for (const _result of data) {
|
||||
result[_result.Field] = {
|
||||
type: _result.Type.toLowerCase().startsWith('enum') ? _result.Type.replace(/^enum/i,
|
||||
'ENUM') : _result.Type.toUpperCase(),
|
||||
allowNull: _result.Null === 'YES',
|
||||
defaultValue: _result.Default,
|
||||
primaryKey: _result.Key === 'PRI',
|
||||
autoIncrement: Object.prototype.hasOwnProperty.call(_result, 'Extra')
|
||||
&& _result.Extra.toLowerCase() === 'auto_increment',
|
||||
comment: _result.Comment ? _result.Comment : null
|
||||
};
|
||||
}
|
||||
return result;
|
||||
}
|
||||
if (this.isVersionQuery()) {
|
||||
return data[0].version;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
handleJsonSelectQuery(rows) {
|
||||
if (!this.model || !this.model.fieldRawAttributesMap) {
|
||||
return;
|
||||
}
|
||||
for (const _field of Object.keys(this.model.fieldRawAttributesMap)) {
|
||||
const modelField = this.model.fieldRawAttributesMap[_field];
|
||||
if (modelField.type instanceof DataTypes.JSON) {
|
||||
//value is return as String, no JSON
|
||||
rows = rows.map(row => {
|
||||
row[modelField.fieldName] = row[modelField.fieldName] ? JSON.parse(
|
||||
row[modelField.fieldName]) : null;
|
||||
if (DataTypes.JSON.parse) {
|
||||
return DataTypes.JSON.parse(modelField, this.sequelize.options,
|
||||
row[modelField.fieldName]);
|
||||
}
|
||||
return row;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
logWarnings(results) {
|
||||
return this.run('SHOW WARNINGS').then(warningResults => {
|
||||
const warningMessage = `MariaDB Warnings (${this.connection.uuid
|
||||
|| 'default'}): `;
|
||||
const messages = [];
|
||||
for (const _warningRow of warningResults) {
|
||||
if (_warningRow === undefined || typeof _warningRow[Symbol.iterator]
|
||||
!== 'function') {
|
||||
continue;
|
||||
}
|
||||
for (const _warningResult of _warningRow) {
|
||||
if (Object.prototype.hasOwnProperty.call(_warningResult, 'Message')) {
|
||||
messages.push(_warningResult.Message);
|
||||
} else {
|
||||
for (const _objectKey of _warningResult.keys()) {
|
||||
messages.push(
|
||||
[_objectKey, _warningResult[_objectKey]].join(': '));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.sequelize.log(warningMessage + messages.join('; '), this.options);
|
||||
|
||||
return results;
|
||||
});
|
||||
}
|
||||
|
||||
formatError(err) {
|
||||
switch (err.errno) {
|
||||
case ER_DUP_ENTRY: {
|
||||
const match = err.message.match(
|
||||
/Duplicate entry '([\s\S]*)' for key '?((.|\s)*?)'?\s.*$/);
|
||||
|
||||
let fields = {};
|
||||
let message = 'Validation error';
|
||||
const values = match ? match[1].split('-') : undefined;
|
||||
const fieldKey = match ? match[2] : undefined;
|
||||
const fieldVal = match ? match[1] : undefined;
|
||||
const uniqueKey = this.model && this.model.uniqueKeys[fieldKey];
|
||||
|
||||
if (uniqueKey) {
|
||||
if (uniqueKey.msg) {
|
||||
message = uniqueKey.msg;
|
||||
}
|
||||
fields = _.zipObject(uniqueKey.fields, values);
|
||||
} else {
|
||||
fields[fieldKey] = fieldVal;
|
||||
}
|
||||
|
||||
const errors = [];
|
||||
_.forOwn(fields, (value, field) => {
|
||||
errors.push(new sequelizeErrors.ValidationErrorItem(
|
||||
this.getUniqueConstraintErrorMessage(field),
|
||||
'unique violation', // sequelizeErrors.ValidationErrorItem.Origins.DB,
|
||||
field,
|
||||
value,
|
||||
this.instance,
|
||||
'not_unique'
|
||||
));
|
||||
});
|
||||
|
||||
return new sequelizeErrors.UniqueConstraintError(
|
||||
{ message, errors, parent: err, fields });
|
||||
}
|
||||
|
||||
case ER_ROW_IS_REFERENCED:
|
||||
case ER_NO_REFERENCED_ROW: {
|
||||
// e.g. CONSTRAINT `example_constraint_name` FOREIGN KEY (`example_id`) REFERENCES `examples` (`id`)
|
||||
const match = err.message.match(
|
||||
/CONSTRAINT ([`"])(.*)\1 FOREIGN KEY \(\1(.*)\1\) REFERENCES \1(.*)\1 \(\1(.*)\1\)/);
|
||||
const quoteChar = match ? match[1] : '`';
|
||||
const fields = match ? match[3].split(
|
||||
new RegExp(`${quoteChar}, *${quoteChar}`)) : undefined;
|
||||
return new sequelizeErrors.ForeignKeyConstraintError({
|
||||
reltype: err.errno === 1451 ? 'parent' : 'child',
|
||||
table: match ? match[4] : undefined,
|
||||
fields,
|
||||
value: fields && fields.length && this.instance
|
||||
&& this.instance[fields[0]] || undefined,
|
||||
index: match ? match[2] : undefined,
|
||||
parent: err
|
||||
});
|
||||
}
|
||||
|
||||
default:
|
||||
return new sequelizeErrors.DatabaseError(err);
|
||||
}
|
||||
}
|
||||
|
||||
handleShowTablesQuery(results) {
|
||||
return results.map(resultSet => ({
|
||||
tableName: resultSet.TABLE_NAME,
|
||||
schema: resultSet.TABLE_SCHEMA
|
||||
}));
|
||||
}
|
||||
|
||||
handleShowIndexesQuery(data) {
|
||||
|
||||
let currItem;
|
||||
const result = [];
|
||||
|
||||
data.forEach(item => {
|
||||
if (!currItem || currItem.name !== item.Key_name) {
|
||||
currItem = {
|
||||
primary: item.Key_name === 'PRIMARY',
|
||||
fields: [],
|
||||
name: item.Key_name,
|
||||
tableName: item.Table,
|
||||
unique: item.Non_unique !== 1,
|
||||
type: item.Index_type
|
||||
};
|
||||
result.push(currItem);
|
||||
}
|
||||
|
||||
currItem.fields[item.Seq_in_index - 1] = {
|
||||
attribute: item.Column_name,
|
||||
length: item.Sub_part || undefined,
|
||||
order: item.Collation === 'A' ? 'ASC' : undefined
|
||||
};
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Query;
|
||||
Reference in New Issue
Block a user