76 lines
2.7 KiB
JavaScript
76 lines
2.7 KiB
JavaScript
'use strict';
|
|
|
|
var dialect = require('../dialect.js');
|
|
var schemaInspector = require('./schema-inspector.js');
|
|
var databaseInspector = require('./database-inspector.js');
|
|
|
|
class MysqlDialect extends dialect {
|
|
configure() {
|
|
const connection = this.db.config.connection.connection;
|
|
connection.supportBigNumbers = true;
|
|
// Only allow bigNumberStrings option set to be true if no connection option passed
|
|
// Otherwise bigNumberStrings option should be allowed to used from DB config
|
|
if (connection.bigNumberStrings === undefined) {
|
|
connection.bigNumberStrings = true;
|
|
}
|
|
connection.typeCast = (field, next)=>{
|
|
if (field.type === 'DECIMAL' || field.type === 'NEWDECIMAL') {
|
|
const value = field.string();
|
|
return value === null ? null : Number(value);
|
|
}
|
|
if (field.type === 'TINY' && field.length === 1) {
|
|
const value = field.string();
|
|
return value ? value === '1' : null;
|
|
}
|
|
if (field.type === 'DATE') {
|
|
return field.string();
|
|
}
|
|
return next();
|
|
};
|
|
}
|
|
async initialize(nativeConnection) {
|
|
try {
|
|
await this.db.connection.raw(`set session sql_require_primary_key = 0;`).connection(nativeConnection);
|
|
} catch (err) {
|
|
// Ignore error due to lack of session permissions
|
|
}
|
|
// We only need to get info on the first connection in the pool
|
|
/**
|
|
* Note: There is a race condition here where if two connections are opened at the same time, both will retrieve
|
|
* db info, but it doesn't cause issues, it's just one wasted query one time, so we can safely leave it to avoid
|
|
* adding extra complexity
|
|
* */ if (!this.info) {
|
|
this.info = await this.databaseInspector.getInformation(nativeConnection);
|
|
}
|
|
}
|
|
async startSchemaUpdate() {
|
|
try {
|
|
await this.db.connection.raw(`set foreign_key_checks = 0;`);
|
|
await this.db.connection.raw(`set session sql_require_primary_key = 0;`);
|
|
} catch (err) {
|
|
// Ignore error due to lack of session permissions
|
|
}
|
|
}
|
|
async endSchemaUpdate() {
|
|
await this.db.connection.raw(`set foreign_key_checks = 1;`);
|
|
}
|
|
supportsUnsigned() {
|
|
return true;
|
|
}
|
|
usesForeignKeys() {
|
|
return true;
|
|
}
|
|
transformErrors(error) {
|
|
super.transformErrors(error);
|
|
}
|
|
constructor(db){
|
|
super(db, 'mysql');
|
|
this.info = null;
|
|
this.schemaInspector = new schemaInspector(db);
|
|
this.databaseInspector = new databaseInspector(db);
|
|
}
|
|
}
|
|
|
|
module.exports = MysqlDialect;
|
|
//# sourceMappingURL=index.js.map
|