fix: process blob values, update firebird dialect

This commit is contained in:
Pavel
2025-06-05 15:15:31 +02:00
parent dd90851477
commit e4cc4b6f58
3 changed files with 51 additions and 7 deletions

View File

@@ -4,6 +4,7 @@ const stream = require('stream');
const driverBase = require('../frontend/driver');
const Analyser = require('./Analyser');
const Firebird = require('node-firebird');
const { normalizeRow } = require('./helpers');
const { getLogger, extractErrorLogData, createBulkInsertStreamBase } = require('dbgate-tools');
const sql = require('./sql');
@@ -62,7 +63,7 @@ const driver = {
const columns = res?.[0] ? Object.keys(res[0]).map(i => ({ columnName: i })) : [];
return {
rows: res ?? [],
rows: res ? await Promise.all(res.map(normalizeRow)) : [],
columns,
};
},
@@ -108,10 +109,6 @@ const driver = {
}
},
async writeTable(dbhan, name, options) {
return createBulkInsertStreamBase(this, stream, dbhan, name, options);
},
async script(dbhan, sql, { useTransaction } = {}) {
if (useTransaction) return this.runSqlInTransaction(dbhan, sql);

View File

@@ -89,6 +89,43 @@ function getFormattedDefaultValue(defaultValue) {
return defaultValue.replace(/^default\s*/i, '');
}
function blobStreamToString(stream, encoding = 'utf8') {
return new Promise((resolve, reject) => {
const chunks = [];
stream.on('data', chunk => {
chunks.push(chunk);
});
stream.on('end', () => {
resolve(Buffer.concat(chunks).toString(encoding));
});
stream.on('error', err => {
reject(err);
});
});
}
async function normalizeRow(row) {
const entries = await Promise.all(
Object.entries(row).map(async ([key, value]) => {
if (value === null || value === undefined) return [key, null];
if (typeof value === 'function') {
const result = await new Promise((resolve, reject) => {
value(async (_err, _name, eventEmitter) => {
try {
const stringValue = await blobStreamToString(eventEmitter, 'utf8');
resolve(stringValue);
} catch (error) {
reject(error);
}
});
});
return [key, result];
}
return [key, value];
})
);
return Object.fromEntries(entries);
}
module.exports = {
getDataTypeString,
@@ -96,4 +133,6 @@ module.exports = {
getTriggerTiming,
getFormattedDefaultValue,
getTriggerCreateSql,
blobStreamToString,
normalizeRow,
};