mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-19 14:16:01 +00:00
better type habndling, shell-tableReader for preserving table structure
This commit is contained in:
@@ -4,6 +4,7 @@ const sql = require('./sql');
|
||||
|
||||
const DatabaseAnalyser = require('../default/DatabaseAnalyser');
|
||||
const { filter } = require('lodash');
|
||||
const { isTypeString } = require('@dbgate/tools');
|
||||
|
||||
function objectTypeToField(type) {
|
||||
switch (type.trim()) {
|
||||
@@ -24,10 +25,12 @@ function objectTypeToField(type) {
|
||||
}
|
||||
}
|
||||
|
||||
function getColumnInfo({ isNullable, isIdentity, columnName, dataType }) {
|
||||
function getColumnInfo({ isNullable, isIdentity, columnName, dataType, charMaxLength }) {
|
||||
let fullDataType = dataType;
|
||||
if (charMaxLength && isTypeString(dataType)) fullDataType = `${dataType}(${charMaxLength})`;
|
||||
return {
|
||||
columnName,
|
||||
dataType,
|
||||
dataType: fullDataType,
|
||||
notNull: !isNullable,
|
||||
autoIncrement: !!isIdentity,
|
||||
};
|
||||
|
||||
@@ -22,7 +22,6 @@ function extractColumns(columns) {
|
||||
...col,
|
||||
columnName: col.name,
|
||||
notNull: !col.nullable,
|
||||
autoIncrement: !!col.identity,
|
||||
}));
|
||||
|
||||
const generateName = () => {
|
||||
@@ -164,7 +163,7 @@ const driver = {
|
||||
|
||||
return request;
|
||||
},
|
||||
async readQuery(pool, sql) {
|
||||
async readQuery(pool, sql, structure) {
|
||||
const request = await pool.request();
|
||||
const { stream } = pool._nativeModules;
|
||||
|
||||
@@ -176,7 +175,7 @@ const driver = {
|
||||
request.stream = true;
|
||||
request.on('recordset', (driverColumns) => {
|
||||
const [columns, mapper] = extractColumns(driverColumns);
|
||||
pass.write({ columns });
|
||||
pass.write(structure || { columns });
|
||||
});
|
||||
request.on('row', (row) => pass.write(row));
|
||||
request.on('error', (err) => {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
module.exports = `
|
||||
select c.name as columnName, t.name as dataType, c.object_id as objectId, c.is_identity as isIdentity,
|
||||
c.max_length as maxLength, c.precision, c.scale, c.is_nullable as isNullable,
|
||||
col.CHARACTER_MAXIMUM_LENGTH as charMaxLength,
|
||||
d.definition as defaultValue, d.name as defaultConstraint,
|
||||
m.definition as computedExpression, m.is_persisted as isPersisted, c.column_id as columnId,
|
||||
-- TODO only if version >= 2008
|
||||
@@ -8,8 +9,10 @@ select c.name as columnName, t.name as dataType, c.object_id as objectId, c.is_i
|
||||
from sys.columns c
|
||||
inner join sys.types t on c.system_type_id = t.system_type_id and c.user_type_id = t.user_type_id
|
||||
inner join sys.objects o on c.object_id = o.object_id
|
||||
INNER JOIN sys.schemas u ON u.schema_id=o.schema_id
|
||||
INNER JOIN INFORMATION_SCHEMA.COLUMNS col ON col.TABLE_NAME = o.name AND col.TABLE_SCHEMA = u.name and col.COLUMN_NAME = c.name
|
||||
left join sys.default_constraints d on c.default_object_id = d.object_id
|
||||
left join sys.computed_columns m on m.object_id = c.object_id and m.column_id = c.column_id
|
||||
where o.type = 'U' and o.object_id =[OBJECT_ID_CONDITION]
|
||||
order by c.column_id
|
||||
`;
|
||||
`;
|
||||
|
||||
@@ -6,7 +6,7 @@ select
|
||||
col.COLUMN_NAME as columnName,
|
||||
col.IS_NULLABLE as isNullable,
|
||||
col.DATA_TYPE as dataType,
|
||||
col.CHARACTER_MAXIMUM_LENGTH,
|
||||
col.CHARACTER_MAXIMUM_LENGTH as charMaxLength,
|
||||
col.NUMERIC_PRECISION as precision,
|
||||
col.NUMERIC_SCALE as scale,
|
||||
col.COLUMN_DEFAULT
|
||||
|
||||
Reference in New Issue
Block a user