mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-30 01:03:58 +00:00
type analysers for mysql, postgre
This commit is contained in:
@@ -4,7 +4,7 @@ const sql = require('./sql');
|
|||||||
|
|
||||||
const DatabaseAnalyser = require('../default/DatabaseAnalyser');
|
const DatabaseAnalyser = require('../default/DatabaseAnalyser');
|
||||||
const { filter } = require('lodash');
|
const { filter } = require('lodash');
|
||||||
const { isTypeString } = require('@dbgate/tools');
|
const { isTypeString, isTypeNumeric } = require('@dbgate/tools');
|
||||||
|
|
||||||
function objectTypeToField(type) {
|
function objectTypeToField(type) {
|
||||||
switch (type.trim()) {
|
switch (type.trim()) {
|
||||||
@@ -25,9 +25,19 @@ function objectTypeToField(type) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getColumnInfo({ isNullable, isIdentity, columnName, dataType, charMaxLength }) {
|
function getColumnInfo({
|
||||||
|
isNullable,
|
||||||
|
isIdentity,
|
||||||
|
columnName,
|
||||||
|
dataType,
|
||||||
|
charMaxLength,
|
||||||
|
numericPrecision,
|
||||||
|
numericScale,
|
||||||
|
}) {
|
||||||
let fullDataType = dataType;
|
let fullDataType = dataType;
|
||||||
if (charMaxLength && isTypeString(dataType)) fullDataType = `${dataType}(${charMaxLength})`;
|
if (charMaxLength && isTypeString(dataType)) fullDataType = `${dataType}(${charMaxLength})`;
|
||||||
|
if (numericPrecision && numericScale && isTypeNumeric(dataType))
|
||||||
|
fullDataType = `${dataType}(${numericPrecision},${numericScale})`;
|
||||||
return {
|
return {
|
||||||
columnName,
|
columnName,
|
||||||
dataType: fullDataType,
|
dataType: fullDataType,
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ select c.name as columnName, t.name as dataType, c.object_id as objectId, c.is_i
|
|||||||
col.CHARACTER_MAXIMUM_LENGTH as charMaxLength,
|
col.CHARACTER_MAXIMUM_LENGTH as charMaxLength,
|
||||||
d.definition as defaultValue, d.name as defaultConstraint,
|
d.definition as defaultValue, d.name as defaultConstraint,
|
||||||
m.definition as computedExpression, m.is_persisted as isPersisted, c.column_id as columnId,
|
m.definition as computedExpression, m.is_persisted as isPersisted, c.column_id as columnId,
|
||||||
|
col.NUMERIC_PRECISION as numericPrecision,
|
||||||
|
col.NUMERIC_SCALE as numericScale,
|
||||||
-- TODO only if version >= 2008
|
-- TODO only if version >= 2008
|
||||||
c.is_sparse as isSparse
|
c.is_sparse as isSparse
|
||||||
from sys.columns c
|
from sys.columns c
|
||||||
|
|||||||
@@ -3,6 +3,30 @@ const _ = require('lodash');
|
|||||||
const sql = require('./sql');
|
const sql = require('./sql');
|
||||||
|
|
||||||
const DatabaseAnalayser = require('../default/DatabaseAnalyser');
|
const DatabaseAnalayser = require('../default/DatabaseAnalyser');
|
||||||
|
const { isTypeString, isTypeNumeric } = require('@dbgate/tools');
|
||||||
|
|
||||||
|
function getColumnInfo({
|
||||||
|
isNullable,
|
||||||
|
extra,
|
||||||
|
columnName,
|
||||||
|
dataType,
|
||||||
|
charMaxLength,
|
||||||
|
numericPrecision,
|
||||||
|
numericScale,
|
||||||
|
defaultValue,
|
||||||
|
}) {
|
||||||
|
let fullDataType = dataType;
|
||||||
|
if (charMaxLength && isTypeString(dataType)) fullDataType = `${dataType}(${charMaxLength})`;
|
||||||
|
if (numericPrecision && numericScale && isTypeNumeric(dataType))
|
||||||
|
fullDataType = `${dataType}(${numericPrecision},${numericScale})`;
|
||||||
|
return {
|
||||||
|
notNull: !isNullable,
|
||||||
|
autoIncrement: extra && extra.toLowerCase().includes('auto_increment'),
|
||||||
|
columnName,
|
||||||
|
dataType: fullDataType,
|
||||||
|
defaultValue,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
class MySqlAnalyser extends DatabaseAnalayser {
|
class MySqlAnalyser extends DatabaseAnalayser {
|
||||||
constructor(pool, driver) {
|
constructor(pool, driver) {
|
||||||
@@ -24,13 +48,7 @@ class MySqlAnalyser extends DatabaseAnalayser {
|
|||||||
return this.mergeAnalyseResult({
|
return this.mergeAnalyseResult({
|
||||||
tables: tables.rows.map((table) => ({
|
tables: tables.rows.map((table) => ({
|
||||||
...table,
|
...table,
|
||||||
columns: columns.rows
|
columns: columns.rows.filter((col) => col.pureName == table.pureName).map(getColumnInfo),
|
||||||
.filter((col) => col.pureName == table.pureName)
|
|
||||||
.map(({ isNullable, extra, ...col }) => ({
|
|
||||||
...col,
|
|
||||||
notNull: !isNullable,
|
|
||||||
autoIncrement: extra && extra.toLowerCase().includes('auto_increment'),
|
|
||||||
})),
|
|
||||||
primaryKey: DatabaseAnalayser.extractPrimaryKeys(table, pkColumns.rows),
|
primaryKey: DatabaseAnalayser.extractPrimaryKeys(table, pkColumns.rows),
|
||||||
foreignKeys: DatabaseAnalayser.extractForeignKeys(table, fkColumns.rows),
|
foreignKeys: DatabaseAnalayser.extractForeignKeys(table, fkColumns.rows),
|
||||||
})),
|
})),
|
||||||
|
|||||||
@@ -4,10 +4,10 @@ select
|
|||||||
COLUMN_NAME as columnName,
|
COLUMN_NAME as columnName,
|
||||||
IS_NULLABLE as isNullable,
|
IS_NULLABLE as isNullable,
|
||||||
DATA_TYPE as dataType,
|
DATA_TYPE as dataType,
|
||||||
CHARACTER_MAXIMUM_LENGTH,
|
CHARACTER_MAXIMUM_LENGTH as charMaxLength,
|
||||||
NUMERIC_PRECISION,
|
NUMERIC_PRECISION as numericPrecision,
|
||||||
NUMERIC_SCALE,
|
NUMERIC_SCALE as numericScale,
|
||||||
COLUMN_DEFAULT,
|
COLUMN_DEFAULT as defaultValue,
|
||||||
EXTRA as extra
|
EXTRA as extra
|
||||||
from INFORMATION_SCHEMA.COLUMNS
|
from INFORMATION_SCHEMA.COLUMNS
|
||||||
where TABLE_SCHEMA = '#DATABASE#' and TABLE_NAME =[OBJECT_NAME_CONDITION]
|
where TABLE_SCHEMA = '#DATABASE#' and TABLE_NAME =[OBJECT_NAME_CONDITION]
|
||||||
|
|||||||
@@ -3,6 +3,37 @@ const _ = require('lodash');
|
|||||||
const sql = require('./sql');
|
const sql = require('./sql');
|
||||||
|
|
||||||
const DatabaseAnalayser = require('../default/DatabaseAnalyser');
|
const DatabaseAnalayser = require('../default/DatabaseAnalyser');
|
||||||
|
const { isTypeString, isTypeNumeric } = require('@dbgate/tools');
|
||||||
|
|
||||||
|
function normalizeTypeName(dataType) {
|
||||||
|
if (dataType == 'character varying') return 'varchar';
|
||||||
|
if (dataType == 'timestamp without time zone') return 'timestamp';
|
||||||
|
return dataType;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getColumnInfo({
|
||||||
|
isNullable,
|
||||||
|
isIdentity,
|
||||||
|
columnName,
|
||||||
|
dataType,
|
||||||
|
charMaxLength,
|
||||||
|
numericPrecision,
|
||||||
|
numericScale,
|
||||||
|
defaultValue,
|
||||||
|
}) {
|
||||||
|
const normDataType = normalizeTypeName(dataType);
|
||||||
|
let fullDataType = normDataType;
|
||||||
|
if (charMaxLength && isTypeString(normDataType)) fullDataType = `${normDataType}(${charMaxLength})`;
|
||||||
|
if (numericPrecision && numericScale && isTypeNumeric(normDataType))
|
||||||
|
fullDataType = `${normDataType}(${numericPrecision},${numericScale})`;
|
||||||
|
return {
|
||||||
|
columnName,
|
||||||
|
dataType: fullDataType,
|
||||||
|
notNull: !isNullable,
|
||||||
|
autoIncrement: !!isIdentity,
|
||||||
|
defaultValue,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
class PostgreAnalyser extends DatabaseAnalayser {
|
class PostgreAnalyser extends DatabaseAnalayser {
|
||||||
constructor(pool, driver) {
|
constructor(pool, driver) {
|
||||||
@@ -26,10 +57,7 @@ class PostgreAnalyser extends DatabaseAnalayser {
|
|||||||
...table,
|
...table,
|
||||||
columns: columns.rows
|
columns: columns.rows
|
||||||
.filter((col) => col.pureName == table.pureName && col.schemaName == table.schemaName)
|
.filter((col) => col.pureName == table.pureName && col.schemaName == table.schemaName)
|
||||||
.map(({ isNullable, ...col }) => ({
|
.map(getColumnInfo),
|
||||||
...col,
|
|
||||||
notNull: !isNullable,
|
|
||||||
})),
|
|
||||||
primaryKey: DatabaseAnalayser.extractPrimaryKeys(table, pkColumns.rows),
|
primaryKey: DatabaseAnalayser.extractPrimaryKeys(table, pkColumns.rows),
|
||||||
foreignKeys: DatabaseAnalayser.extractForeignKeys(table, fkColumns.rows),
|
foreignKeys: DatabaseAnalayser.extractForeignKeys(table, fkColumns.rows),
|
||||||
})),
|
})),
|
||||||
|
|||||||
@@ -4,11 +4,11 @@ select
|
|||||||
table_name as "pureName",
|
table_name as "pureName",
|
||||||
column_name as "columnName",
|
column_name as "columnName",
|
||||||
is_nullable as "isNullable",
|
is_nullable as "isNullable",
|
||||||
data_type as dataType,
|
data_type as "dataType",
|
||||||
character_maximum_length,
|
character_maximum_length as "charMaxLength",
|
||||||
numeric_precision,
|
numeric_precision as "numericPrecision",
|
||||||
numeric_scale,
|
numeric_scale as "numericScale",
|
||||||
column_default
|
column_default as "defaultValue"
|
||||||
from information_schema.columns
|
from information_schema.columns
|
||||||
where
|
where
|
||||||
table_schema <> 'information_schema'
|
table_schema <> 'information_schema'
|
||||||
|
|||||||
@@ -23,5 +23,5 @@ export function isTypeLogical(dataType) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function isTypeDateTime(dataType) {
|
export function isTypeDateTime(dataType) {
|
||||||
return dataType && /date|time/i.test(dataType);
|
return dataType && /date|time|timestamp/i.test(dataType);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user