feat: firebird use attachOrCreate on connect, add dbFileExtension and locaiton on server to tests

This commit is contained in:
Pavel
2025-05-15 15:04:54 +02:00
parent 7ac6cfcf25
commit 951bfa23f3
7 changed files with 54 additions and 17 deletions

View File

@@ -5,7 +5,12 @@ const crypto = require('crypto');
function randomDbName(dialect) { function randomDbName(dialect) {
const generatedKey = crypto.randomBytes(6); const generatedKey = crypto.randomBytes(6);
const newKey = generatedKey.toString('hex'); const newKey = generatedKey.toString('hex');
const res = `db${newKey}`; let res = `db${newKey}`;
if (dialect.dbFileExtension) {
res += dialect.dbFileExtension;
}
if (dialect.upperCaseAllDbObjectNames) return res.toUpperCase(); if (dialect.upperCaseAllDbObjectNames) return res.toUpperCase();
return res; return res;
} }
@@ -17,7 +22,7 @@ async function connect(engine, database) {
if (engine.generateDbFile) { if (engine.generateDbFile) {
const conn = await driver.connect({ const conn = await driver.connect({
...connection, ...connection,
databaseFile: `dbtemp/${database}`, databaseFile: (engine.databaseFileLocationOnServer ?? 'dbtemp/') + database,
}); });
return conn; return conn;
} else { } else {

View File

@@ -265,11 +265,11 @@ export class SqlDumper implements AlterProcessor {
this.columnDefault(column); this.columnDefault(column);
} }
if (includeNullable && !this.dialect?.specificNullabilityImplementation) { if (includeNullable && !this.dialect?.specificNullabilityImplementation) {
this.put(column.notNull ? '^not ^null' : '^null'); this.put(column.notNull ? '^not ^null' : this.dialect.implicitNullDeclaration ? '^null' : '');
} }
} else { } else {
if (includeNullable && !this.dialect?.specificNullabilityImplementation) { if (includeNullable && !this.dialect?.specificNullabilityImplementation) {
this.put(column.notNull ? '^not ^null' : '^null'); this.put(column.notNull ? '^not ^null' : this.dialect.implicitNullDeclaration ? '^null' : '');
} }
if (includeDefault && column.defaultValue?.toString()?.trim()) { if (includeDefault && column.defaultValue?.toString()?.trim()) {
this.columnDefault(column); this.columnDefault(column);

View File

@@ -48,6 +48,7 @@ export interface SqlDialect {
namedDefaultConstraint?: boolean; namedDefaultConstraint?: boolean;
specificNullabilityImplementation?: boolean; specificNullabilityImplementation?: boolean;
implicitNullDeclaration?: boolean;
omitForeignKeys?: boolean; omitForeignKeys?: boolean;
omitUniqueConstraints?: boolean; omitUniqueConstraints?: boolean;
omitIndexes?: boolean; omitIndexes?: boolean;
@@ -67,6 +68,7 @@ export interface SqlDialect {
requireFromDual?: boolean; requireFromDual?: boolean;
userDatabaseNamePrefix?: string; // c## in Oracle userDatabaseNamePrefix?: string; // c## in Oracle
upperCaseAllDbObjectNames?: boolean; upperCaseAllDbObjectNames?: boolean;
dbFileExtension?: string;
defaultValueBeforeNullability?: boolean; defaultValueBeforeNullability?: boolean;
predefinedDataTypes: string[]; predefinedDataTypes: string[];

View File

@@ -60,6 +60,8 @@ export type TestEngineInfo = {
defaultSchemaName?: string; defaultSchemaName?: string;
generateDbFile?: boolean; generateDbFile?: boolean;
generateDbFileOnServer?: boolean;
databaseFileLocationOnServer?: string;
dbSnapshotBySeconds?: boolean; dbSnapshotBySeconds?: boolean;
dumpFile?: string; dumpFile?: string;
dumpChecks?: Array<{ sql: string; res: string }>; dumpChecks?: Array<{ sql: string; res: string }>;

View File

@@ -1,4 +1,5 @@
const _ = require('lodash'); const _ = require('lodash');
const stream = require('stream');
const driverBase = require('../frontend/driver'); const driverBase = require('../frontend/driver');
const Analyser = require('./Analyser'); const Analyser = require('./Analyser');
const Firebird = require('node-firebird'); const Firebird = require('node-firebird');
@@ -22,7 +23,7 @@ const driver = {
/**@type {Firebird.Database} */ /**@type {Firebird.Database} */
const db = await new Promise((resolve, reject) => { const db = await new Promise((resolve, reject) => {
Firebird.attach(options, (err, db) => { Firebird.attachOrCreate(options, (err, db) => {
if (err) { if (err) {
reject(err); reject(err);
return; return;
@@ -47,18 +48,14 @@ const driver = {
resolve(result); resolve(result);
}); });
}); });
const columns = res[0] ? Object.keys(res[0]).map(i => ({ columnName: i })) : []; const columns = res?.[0] ? Object.keys(res[0]).map(i => ({ columnName: i })) : [];
return { return {
rows: res, rows: res ?? [],
columns, columns,
}; };
}, },
async script(dbhan, sql) {
throw new Error('Not implemented');
},
async stream(dbhan, sql, options) { async stream(dbhan, sql, options) {
try { try {
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
@@ -101,7 +98,36 @@ const driver = {
}, },
async readQuery(dbhan, sql, structure) { async readQuery(dbhan, sql, structure) {
throw new Error('Not implemented'); console.log('readQuery from SQL:', sql);
const pass = new stream.PassThrough({
objectMode: true,
highWaterMark: 100,
});
let hasSentColumns = false;
dbhan.client.sequentially(
sql,
[],
(row, index) => {
if (!hasSentColumns) {
hasSentColumns = true;
const columns = Object.keys(row).map(i => ({ columnName: i }));
pass.write({
__isStreamHeader: true,
...(structure || { columns }),
});
}
pass.write(row);
},
err => {
pass.end();
}
);
return pass;
}, },
async writeTable(dbhan, name, options) { async writeTable(dbhan, name, options) {
@@ -126,10 +152,6 @@ const driver = {
]; ];
}, },
async createDatabase(dbhan, name) {},
async dropDatabase(dbhan, name) {},
async close(dbhan) { async close(dbhan) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
dbhan.client.detach(err => { dbhan.client.detach(err => {

View File

@@ -1,5 +1,9 @@
const { SqlDumper } = global.DBGATE_PACKAGES['dbgate-tools']; const { SqlDumper } = global.DBGATE_PACKAGES['dbgate-tools'];
class Dumper extends SqlDumper {} class Dumper extends SqlDumper {
autoIncrement() {
this.put(' ^generated ^by ^default ^as ^identity');
}
}
module.exports = Dumper; module.exports = Dumper;

View File

@@ -17,6 +17,8 @@ const dialect = {
return `"${s}"`; return `"${s}"`;
}, },
dbFileExtension: '.fdb',
createColumn: true, createColumn: true,
dropColumn: true, dropColumn: true,
changeColumn: true, changeColumn: true,