oracle: bulk inserter

This commit is contained in:
Jan Prochazka 2024-06-01 16:28:46 +02:00
parent 1c03a8ce9e
commit f4fbb28124
2 changed files with 40 additions and 2 deletions

View File

@ -0,0 +1,38 @@
const { createBulkInsertStreamBase } = require('dbgate-tools');
const _ = require('lodash');
/**
*
* @param {import('dbgate-types').EngineDriver} driver
*/
function createOracleBulkInsertStream(driver, stream, pool, name, options) {
const fullNameQuoted = name.schemaName
? `${driver.dialect.quoteIdentifier(name.schemaName)}.${driver.dialect.quoteIdentifier(name.pureName)}`
: driver.dialect.quoteIdentifier(name.pureName);
const writable = createBulkInsertStreamBase(driver, stream, pool, name, {
...options,
// this is really not used, send method below is used instead
commitAfterInsert: true,
});
writable.send = async () => {
const dmp = driver.createDumper();
dmp.putRaw(`INSERT INTO ${fullNameQuoted} (`);
dmp.putCollection(',', writable.columnNames, col => dmp.putRaw(driver.dialect.quoteIdentifier(col)));
dmp.putRaw(')\n VALUES (\n');
dmp.put(
'%,s',
writable.columnNames.map((c, i) => `:C${i}`)
);
dmp.putRaw(')');
const rows = writable.buffer.map(row => _.mapKeys(row, (v, k) => `c${writable.columnNames.indexOf(k)}`));
await pool.executeMany(dmp.s, rows, { autoCommit: true });
writable.buffer = [];
};
return writable;
}
module.exports = createOracleBulkInsertStream;

View File

@ -6,6 +6,7 @@ const Analyser = require('./Analyser');
//--const pg = require('pg');
const oracledb = require('oracledb');
const { createBulkInsertStreamBase, makeUniqueColumnNames } = require('dbgate-tools');
const createOracleBulkInsertStream = require('./createOracleBulkInsertStream');
/*
pg.types.setTypeParser(1082, 'text', val => val); // date
@ -288,8 +289,7 @@ const drivers = driverBases.map(driverBase => ({
return pass;
},
async writeTable(pool, name, options) {
// @ts-ignore
return createBulkInsertStreamBase(this, stream, pool, name, { ...options, commitAfterInsert: true });
return createOracleBulkInsertStream(this, stream, pool, name, options);
},
async listDatabases(client) {
const { rows } = await this.query(client, 'SELECT username as "name" from all_users order by username');