mirror of
https://github.com/dbgate/dbgate
synced 2024-11-07 20:26:23 +00:00
postgres - stream query reader
This commit is contained in:
parent
a88e38dcf7
commit
36dffe0a0f
@ -120,6 +120,51 @@ const driver = {
|
||||
const analyser = new PostgreAnalyser(pool, this);
|
||||
return analyser.incrementalAnalysis(structure);
|
||||
},
|
||||
async readQuery(client, sql, structure) {
|
||||
const query = new client._nativeModules.pgQueryStream(sql);
|
||||
const { stream } = client._nativeModules;
|
||||
|
||||
const queryStream = client.query(query);
|
||||
|
||||
let wasHeader = false;
|
||||
|
||||
const pass = new stream.PassThrough({
|
||||
objectMode: true,
|
||||
highWaterMark: 100,
|
||||
});
|
||||
|
||||
const handleEnd = (result) => {
|
||||
pass.end();
|
||||
};
|
||||
|
||||
const handleReadable = () => {
|
||||
let row = queryStream.read();
|
||||
if (!wasHeader && row) {
|
||||
pass.write(
|
||||
structure || {
|
||||
columns: _.keys(row).map((columnName) => ({ columnName })),
|
||||
}
|
||||
);
|
||||
wasHeader = true;
|
||||
}
|
||||
|
||||
while (row) {
|
||||
pass.write(row);
|
||||
row = queryStream.read();
|
||||
}
|
||||
};
|
||||
|
||||
const handleError = (error) => {
|
||||
console.error(error);
|
||||
pass.end();
|
||||
};
|
||||
|
||||
queryStream.on('error', handleError);
|
||||
queryStream.on('readable', handleReadable);
|
||||
queryStream.on('end', handleEnd);
|
||||
|
||||
return pass;
|
||||
},
|
||||
createDumper() {
|
||||
return new PostgreDumper(this);
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user