2021-06-17 16:17:15 +02:00
|
|
|
'use strict';
|
|
|
|
|
2021-06-29 16:27:35 +02:00
|
|
|
const path = require('path');
|
|
|
|
const fse = require('fs-extra');
|
|
|
|
|
2021-06-24 18:28:36 +02:00
|
|
|
const errors = require('../errors');
|
|
|
|
|
2021-06-17 16:17:15 +02:00
|
|
|
class Dialect {
|
2021-06-28 12:34:29 +02:00
|
|
|
constructor(db) {
|
|
|
|
this.db = db;
|
|
|
|
}
|
|
|
|
|
2021-06-28 21:37:44 +02:00
|
|
|
usesForeignKeys() {
|
2021-06-17 16:17:15 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-06-24 18:28:36 +02:00
|
|
|
|
2021-06-28 21:37:44 +02:00
|
|
|
useReturning() {
|
|
|
|
return false;
|
2021-06-24 18:28:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: pass query info to display some more metadata
|
|
|
|
transformErrors(error) {
|
2021-07-08 18:15:32 +02:00
|
|
|
if (error instanceof Error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2021-07-02 02:26:14 +02:00
|
|
|
throw new Error(error.message);
|
2021-06-24 18:28:36 +02:00
|
|
|
}
|
2021-06-17 16:17:15 +02:00
|
|
|
}
|
2021-06-24 18:28:36 +02:00
|
|
|
class PostgresDialect extends Dialect {
|
2021-06-17 16:17:15 +02:00
|
|
|
useReturning() {
|
|
|
|
return true;
|
|
|
|
}
|
2021-06-24 18:28:36 +02:00
|
|
|
|
2021-06-28 12:34:29 +02:00
|
|
|
initialize() {
|
2021-07-19 21:17:34 +02:00
|
|
|
// FIXME:
|
2021-06-28 21:37:44 +02:00
|
|
|
// this.db.connection.context.client.types.setTypeParser(1700, 'text', parseFloat);
|
|
|
|
}
|
|
|
|
|
|
|
|
usesForeignKeys() {
|
|
|
|
return false;
|
2021-06-28 12:34:29 +02:00
|
|
|
}
|
|
|
|
|
2021-06-24 18:28:36 +02:00
|
|
|
transformErrors(error) {
|
|
|
|
switch (error.code) {
|
|
|
|
case '23502': {
|
|
|
|
throw new errors.NotNullConstraint({ column: error.column });
|
|
|
|
}
|
|
|
|
default: {
|
2021-06-30 20:00:03 +02:00
|
|
|
super.transformErrors(error);
|
2021-06-24 18:28:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MysqlDialect extends Dialect {
|
2021-06-28 12:34:29 +02:00
|
|
|
initialize() {
|
2021-06-29 16:27:35 +02:00
|
|
|
this.db.config.connection.connection.supportBigNumbers = true;
|
|
|
|
this.db.config.connection.connection.bigNumberStrings = true;
|
|
|
|
this.db.config.connection.connection.typeCast = (field, next) => {
|
2021-06-28 12:34:29 +02:00
|
|
|
if (field.type == 'DECIMAL' || field.type === 'NEWDECIMAL') {
|
|
|
|
var value = field.string();
|
|
|
|
return value === null ? null : Number(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (field.type == 'TINY' && field.length == 1) {
|
|
|
|
let value = field.string();
|
|
|
|
return value ? value == '1' : null;
|
|
|
|
}
|
|
|
|
return next();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-06-28 21:37:44 +02:00
|
|
|
usesForeignKeys() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-06-24 18:28:36 +02:00
|
|
|
transformErrors(error) {
|
2021-06-30 20:00:03 +02:00
|
|
|
super.transformErrors(error);
|
2021-06-24 18:28:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class SqliteDialect extends Dialect {
|
2021-06-28 12:34:29 +02:00
|
|
|
async initialize() {
|
|
|
|
// Create the directory if it does not exist.
|
|
|
|
|
2021-06-29 16:27:35 +02:00
|
|
|
// TODO: get strapi.dir from somewhere else
|
|
|
|
|
|
|
|
this.db.config.connection.connection.filename = path.resolve(
|
|
|
|
this.db.config.connection.connection.filename
|
|
|
|
);
|
|
|
|
|
|
|
|
const dbDir = path.dirname(this.db.config.connection.connection.filename);
|
2021-06-28 12:34:29 +02:00
|
|
|
|
2021-06-29 16:27:35 +02:00
|
|
|
await fse.ensureDir(dbDir);
|
2021-06-28 12:34:29 +02:00
|
|
|
}
|
|
|
|
|
2021-06-24 18:28:36 +02:00
|
|
|
transformErrors(error) {
|
|
|
|
switch (error.errno) {
|
|
|
|
case 19: {
|
|
|
|
throw new errors.NotNullConstraint();
|
|
|
|
}
|
|
|
|
default: {
|
2021-06-30 20:00:03 +02:00
|
|
|
super.transformErrors(error);
|
2021-06-24 18:28:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-17 16:17:15 +02:00
|
|
|
}
|
|
|
|
|
2021-06-28 12:34:29 +02:00
|
|
|
const getDialect = db => {
|
2021-06-29 16:27:35 +02:00
|
|
|
const { client } = db.config.connection;
|
2021-06-17 16:17:15 +02:00
|
|
|
|
|
|
|
switch (client) {
|
|
|
|
case 'postgres':
|
2021-06-28 12:34:29 +02:00
|
|
|
return new PostgresDialect(db);
|
2021-06-24 18:28:36 +02:00
|
|
|
case 'mysql':
|
2021-06-28 12:34:29 +02:00
|
|
|
return new MysqlDialect(db);
|
2021-06-24 18:28:36 +02:00
|
|
|
case 'sqlite':
|
2021-06-28 12:34:29 +02:00
|
|
|
return new SqliteDialect(db);
|
2021-06-17 16:17:15 +02:00
|
|
|
default:
|
2021-06-29 16:27:35 +02:00
|
|
|
throw new Error(`Unknow dialect ${client}`);
|
2021-06-17 16:17:15 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getDialect,
|
|
|
|
};
|