oracle: handle statements in stream

This commit is contained in:
Jan Prochazka 2024-06-01 10:49:38 +02:00
parent 0cc7a98391
commit ca0ae2084c

View File

@ -98,78 +98,108 @@ const drivers = driverBases.map(driverBase => ({
});
*/
// console.log('queryStream', sql);
const query = client.queryStream(sql);
// const consumeStream = new Promise((resolve, reject) => {
let rowcount = 0;
let wasHeader = false;
query.on('metadata', row => {
// console.log('metadata', row);
if (!wasHeader) {
columns = extractOracleColumns(row);
if (columns && columns.length > 0) {
options.recordset(columns);
if (sql.trim().toLowerCase().startsWith('select')) {
const query = client.queryStream(sql);
// const consumeStream = new Promise((resolve, reject) => {
let rowcount = 0;
let wasHeader = false;
query.on('metadata', row => {
// console.log('metadata', row);
if (!wasHeader) {
columns = extractOracleColumns(row);
if (columns && columns.length > 0) {
options.recordset(columns);
}
wasHeader = true;
}
wasHeader = true;
}
// options.row(zipDataRow(row, columns));
});
query.on('data', row => {
// console.log('stream DATA');
if (!wasHeader) {
columns = extractOracleColumns(row);
if (columns && columns.length > 0) {
options.recordset(columns);
}
wasHeader = true;
}
options.row(zipDataRow(row, columns));
});
query.on('end', () => {
const { command, rowCount } = query._result || {};
if (command != 'SELECT' && _.isNumber(rowCount)) {
options.info({
message: `${rowCount} rows affected`,
time: new Date(),
severity: 'info',
});
}
if (!wasHeader) {
columns = extractOracleColumns(query._result);
if (columns && columns.length > 0) {
options.recordset(columns);
}
wasHeader = true;
}
options.done();
});
query.on('error', error => {
console.log('ERROR', error);
const { message, lineNumber, procName } = error;
options.info({
message,
line: lineNumber,
procedure: procName,
time: new Date(),
severity: 'error',
// options.row(zipDataRow(row, columns));
});
options.done();
});
query.on('close', function () {
//console.log("stream 'close' event");
// The underlying ResultSet has been closed, so the connection can now
// be closed, if desired. Note: do not close connections on 'end'.
//resolve(rowcount);
});
//});
query.on('data', row => {
// console.log('stream DATA');
if (!wasHeader) {
columns = extractOracleColumns(row);
if (columns && columns.length > 0) {
options.recordset(columns);
}
wasHeader = true;
}
options.row(zipDataRow(row, columns));
});
query.on('end', () => {
const { command, rowCount } = query._result || {};
if (command != 'SELECT' && _.isNumber(rowCount)) {
options.info({
message: `${rowCount} rows affected`,
time: new Date(),
severity: 'info',
});
}
if (!wasHeader) {
columns = extractOracleColumns(query._result);
if (columns && columns.length > 0) {
options.recordset(columns);
}
wasHeader = true;
}
options.done();
});
query.on('error', error => {
console.log('ERROR', error);
const { message, lineNumber, procName } = error;
options.info({
message,
line: lineNumber,
procedure: procName,
time: new Date(),
severity: 'error',
});
options.done();
});
query.on('close', function () {
//console.log("stream 'close' event");
// The underlying ResultSet has been closed, so the connection can now
// be closed, if desired. Note: do not close connections on 'end'.
//resolve(rowcount);
});
//});
} else {
client.execute(sql, (err, res) => {
if (err) {
console.log('Error query', err, sql);
options.info({
message: err.message,
time: new Date(),
severity: 'error',
});
} else {
const { rowsAffected, metaData, rows } = res || {};
if (rows && metaData) {
const columns = extractOracleColumns(metaData);
options.recordset(columns);
for (const row of rows) {
options.row(zipDataRow(row, columns));
}
} else if (rowsAffected) {
options.info({
message: `${rowsAffected} rows affected`,
time: new Date(),
severity: 'info',
});
}
}
options.done();
});
}
//const numrows = await consumeStream;
//console.log('Rows selected: ' + numrows);
//client.query(query);