mysql default value #455

This commit is contained in:
Jan Prochazka 2023-02-03 11:06:59 +01:00
parent 47e0173f84
commit 6a5157140e
2 changed files with 36 additions and 15 deletions

View File

@ -5,18 +5,33 @@ const sql = require('./sql');
const { DatabaseAnalyser } = require('dbgate-tools');
const { isTypeString, isTypeNumeric } = require('dbgate-tools');
function getColumnInfo({
isNullable,
extra,
columnName,
dataType,
charMaxLength,
numericPrecision,
numericScale,
defaultValue,
columnComment,
columnType,
}) {
function quoteDefaultValue(value) {
if (value == null) return value;
if (!isNaN(value) && !isNaN(parseFloat(value))) return value;
if (_.isString(value) && value.startsWith('CURRENT_')) return value;
if (_.isString(value)) {
return `'${value.replace("'", "\\'")}'`;
}
return value;
}
function getColumnInfo(
{
isNullable,
extra,
columnName,
dataType,
charMaxLength,
numericPrecision,
numericScale,
defaultValue,
columnComment,
columnType,
},
driver
) {
const { quoteDefaultValues } = driver.__analyserInternals;
const columnTypeTokens = _.isString(columnType) ? columnType.split(' ').map(x => x.trim().toLowerCase()) : [];
let fullDataType = dataType;
if (charMaxLength && isTypeString(dataType)) fullDataType = `${dataType}(${charMaxLength})`;
@ -28,7 +43,7 @@ function getColumnInfo({
columnName,
columnComment,
dataType: fullDataType,
defaultValue,
defaultValue: quoteDefaultValues ? quoteDefaultValue(defaultValue) : defaultValue,
isUnsigned: columnTypeTokens.includes('unsigned'),
isZerofill: columnTypeTokens.includes('zerofill'),
};
@ -101,7 +116,7 @@ class Analyser extends DatabaseAnalyser {
...table,
objectId: table.pureName,
contentHash: _.isDate(table.modifyDate) ? table.modifyDate.toISOString() : table.modifyDate,
columns: columns.rows.filter(col => col.pureName == table.pureName).map(getColumnInfo),
columns: columns.rows.filter(col => col.pureName == table.pureName).map(x => getColumnInfo(x, this.driver)),
primaryKey: DatabaseAnalyser.extractPrimaryKeys(table, pkColumns.rows),
foreignKeys: DatabaseAnalyser.extractForeignKeys(table, fkColumns.rows),
tableRowCount: table.tableRowCount,
@ -139,7 +154,7 @@ class Analyser extends DatabaseAnalyser {
...view,
objectId: view.pureName,
contentHash: _.isDate(view.modifyDate) ? view.modifyDate.toISOString() : view.modifyDate,
columns: columns.rows.filter(col => col.pureName == view.pureName).map(getColumnInfo),
columns: columns.rows.filter(col => col.pureName == view.pureName).map(x => getColumnInfo(x, this.driver)),
createSql: viewTexts[view.pureName],
requiresFormat: true,
})),

View File

@ -136,6 +136,9 @@ const mysqlDriver = {
...mysqlDriverBase,
engine: 'mysql@dbgate-plugin-mysql',
title: 'MySQL',
__analyserInternals: {
quoteDefaultValues: true,
},
};
/** @type {import('dbgate-types').EngineDriver} */
@ -143,6 +146,9 @@ const mariaDriver = {
...mysqlDriverBase,
engine: 'mariadb@dbgate-plugin-mysql',
title: 'MariaDB',
__analyserInternals: {
quoteDefaultValues: false,
},
};
module.exports = [mysqlDriver, mariaDriver];