handle mssql cols without name

This commit is contained in:
Jan Prochazka 2020-04-13 14:26:14 +02:00
parent abb3f6e09c
commit fc98409583

View File

@ -14,12 +14,39 @@ const dialect = {
};
function extractColumns(columns) {
return _.sortBy(_.values(columns), 'index').map((col) => ({
const mapper = {};
const res = _.sortBy(_.values(columns), 'index').map((col) => ({
...col,
columnName: col.name,
notNull: !col.nullable,
autoIncrement: !!col.identity,
}));
const generateName = () => {
let index = 1;
while (res.find((x) => x.columnName == `col${index}`)) index += 1;
return `col${index}`;
};
// const groups = _.groupBy(res, 'columnName');
// for (const colname of _.keys(groups)) {
// if (groups[colname].length == 1) continue;
// mapper[colname] = [];
// for (const col of groups[colname]) {
// col.columnName = generateName();
// mapper[colname].push(colname);
// }
// }
for (const col of res) {
if (!col.columnName) {
const newName = generateName();
mapper[col.columnName] = newName;
col.columnName = newName;
}
}
return [res, mapper];
}
/** @type {import('@dbgate/types').EngineDriver} */
@ -38,6 +65,7 @@ const driver = {
pool._nativeModules = nativeModules;
return pool;
},
// @ts-ignore
async query(pool, sql) {
const resp = await pool.request().query(sql);
// console.log(Object.keys(resp.recordset));
@ -45,7 +73,8 @@ const driver = {
const res = {};
if (resp.recordset) {
res.columns = extractColumns(resp.recordset.columns);
const [columns] = extractColumns(resp.recordset.columns);
res.columns = columns;
res.rows = resp.recordset;
}
if (resp.rowsAffected) {
@ -55,6 +84,7 @@ const driver = {
},
async stream(pool, sql, options) {
const request = await pool.request();
let currentMapper = null;
const handleInfo = (info) => {
const { message, lineNumber, procName } = info;
@ -73,11 +103,33 @@ const driver = {
};
const handleRow = (row) => {
// if (currentMapper) {
// for (const colname of _.keys(currentMapper)) {
// let index = 0;
// for (const newcolname of currentMapper[colname]) {
// row[newcolname] = row[colname][index];
// index += 1;
// }
// delete row[colname];
// }
// }
if (currentMapper) {
row = { ...row };
for (const colname of _.keys(currentMapper)) {
const newcolname = currentMapper[colname];
row[newcolname] = row[colname];
if (_.isArray(row[newcolname])) row[newcolname] = row[newcolname].join(',');
delete row[colname];
}
}
options.row(row);
};
const handleRecordset = (columns) => {
options.recordset(extractColumns(columns));
const [extractedColumns, mapper] = extractColumns(columns);
currentMapper = mapper;
options.recordset(extractedColumns);
};
const handleError = (error) => {