2021-10-29 12:58:37 +02:00
|
|
|
import moment from 'moment';
|
|
|
|
import { BaseExporter } from '../BaseExporter';
|
|
|
|
|
|
|
|
export class SqlExporter extends BaseExporter {
|
2021-10-31 17:22:59 +01:00
|
|
|
constructor (client, tables, options) {
|
|
|
|
super(tables, options);
|
2021-10-29 12:58:37 +02:00
|
|
|
this._client = client;
|
2022-03-27 11:41:35 +02:00
|
|
|
this._commentChar = '--';
|
|
|
|
this._postTablesSql = '';
|
2021-10-29 12:58:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get schemaName () {
|
|
|
|
return this._options.schema;
|
|
|
|
}
|
|
|
|
|
|
|
|
get host () {
|
|
|
|
return this._client._params.host;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getServerVersion () {
|
|
|
|
const version = await this._client.getVersion();
|
|
|
|
return `${version.name} ${version.number}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
async dump () {
|
2021-10-31 17:22:59 +01:00
|
|
|
const { includes } = this._options;
|
|
|
|
const extraItems = Object.keys(includes).filter(key => includes[key]);
|
|
|
|
const totalTableToProcess = this._tables.filter(
|
|
|
|
t => t.includeStructure || t.includeContent || t.includeDropStatement
|
|
|
|
).length;
|
|
|
|
const processingItemCount = totalTableToProcess + extraItems.length;
|
|
|
|
|
2021-10-29 12:58:37 +02:00
|
|
|
const exportState = {
|
2021-10-31 17:22:59 +01:00
|
|
|
totalItems: processingItemCount,
|
2021-10-29 12:58:37 +02:00
|
|
|
currentItemIndex: 0,
|
|
|
|
currentItem: '',
|
|
|
|
op: ''
|
|
|
|
};
|
|
|
|
|
|
|
|
const header = await this.getSqlHeader();
|
|
|
|
this.writeString(header);
|
|
|
|
this.writeString('\n\n\n');
|
|
|
|
|
2021-10-31 17:22:59 +01:00
|
|
|
for (const item of this._tables) {
|
2021-10-29 12:58:37 +02:00
|
|
|
// user abort operation
|
2021-10-31 17:22:59 +01:00
|
|
|
if (this.isCancelled) return;
|
2021-10-29 12:58:37 +02:00
|
|
|
|
|
|
|
// skip item if not set to output any detail for them
|
2021-10-31 17:22:59 +01:00
|
|
|
if (
|
|
|
|
!item.includeStructure &&
|
|
|
|
!item.includeContent &&
|
|
|
|
!item.includeDropStatement
|
|
|
|
)
|
2021-10-29 12:58:37 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
exportState.currentItemIndex++;
|
|
|
|
exportState.currentItem = item.table;
|
2021-11-04 22:36:45 +01:00
|
|
|
exportState.op = 'FETCH';
|
2021-10-29 12:58:37 +02:00
|
|
|
|
|
|
|
this.emitUpdate(exportState);
|
|
|
|
|
2021-10-31 17:22:59 +01:00
|
|
|
const tableHeader = this.buildComment(
|
|
|
|
`Dump of table ${item.table}\n------------------------------------------------------------`
|
|
|
|
);
|
2021-10-29 12:58:37 +02:00
|
|
|
this.writeString(tableHeader);
|
|
|
|
this.writeString('\n\n');
|
|
|
|
|
|
|
|
if (item.includeDropStatement) {
|
|
|
|
const dropTableSyntax = this.getDropTable(item.table);
|
|
|
|
this.writeString(dropTableSyntax);
|
|
|
|
this.writeString('\n\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item.includeStructure) {
|
|
|
|
const createTableSyntax = await this.getCreateTable(item.table);
|
|
|
|
this.writeString(createTableSyntax);
|
|
|
|
this.writeString('\n\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item.includeContent) {
|
|
|
|
exportState.op = 'WRITE';
|
|
|
|
this.emitUpdate(exportState);
|
2021-11-04 22:36:45 +01:00
|
|
|
for await (const sqlStr of this.getTableInsert(item.table)) {
|
|
|
|
if (this.isCancelled) return;
|
|
|
|
this.writeString(sqlStr);
|
|
|
|
}
|
|
|
|
|
2021-10-29 12:58:37 +02:00
|
|
|
this.writeString('\n\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.writeString('\n\n');
|
|
|
|
}
|
|
|
|
|
2022-03-27 11:41:35 +02:00
|
|
|
// SQL to execute after tables creation
|
|
|
|
if (this._postTablesSql) {
|
|
|
|
this.writeString(this._postTablesSql);
|
2021-10-29 12:58:37 +02:00
|
|
|
this.writeString('\n\n');
|
|
|
|
}
|
|
|
|
|
2021-10-31 17:22:59 +01:00
|
|
|
for (const item of extraItems) {
|
2021-11-04 22:36:45 +01:00
|
|
|
const processingMethod = `get${item.charAt(0).toUpperCase() + item.slice(1)}`;
|
2021-10-31 17:22:59 +01:00
|
|
|
exportState.currentItemIndex++;
|
|
|
|
exportState.currentItem = item;
|
|
|
|
exportState.op = 'PROCESSING';
|
|
|
|
this.emitUpdate(exportState);
|
|
|
|
|
|
|
|
if (this[processingMethod]) {
|
|
|
|
const data = await this[processingMethod]();
|
|
|
|
if (data !== '') {
|
|
|
|
const header =
|
|
|
|
this.buildComment(
|
|
|
|
`Dump of ${item}\n------------------------------------------------------------`
|
|
|
|
) + '\n\n';
|
|
|
|
|
|
|
|
this.writeString(header);
|
|
|
|
this.writeString(data);
|
|
|
|
this.writeString('\n\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-29 12:58:37 +02:00
|
|
|
const footer = await this.getFooter();
|
|
|
|
this.writeString(footer);
|
|
|
|
}
|
|
|
|
|
|
|
|
buildComment (text) {
|
2021-10-31 17:22:59 +01:00
|
|
|
return text
|
|
|
|
.split('\n')
|
|
|
|
.map(txt => `${this._commentChar} ${txt}`)
|
|
|
|
.join('\n');
|
2021-10-29 12:58:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async getSqlHeader () {
|
|
|
|
const serverVersion = await this.getServerVersion();
|
|
|
|
const header = `************************************************************
|
|
|
|
Antares - SQL Client
|
2022-02-16 12:58:24 +01:00
|
|
|
Version ${process.env.PACKAGE_VERSION}
|
2021-10-29 12:58:37 +02:00
|
|
|
|
|
|
|
https://antares-sql.app/
|
2022-03-31 16:04:47 +02:00
|
|
|
https://github.com/antares-sql/antares
|
2021-10-29 12:58:37 +02:00
|
|
|
|
|
|
|
Host: ${this.host} (${serverVersion})
|
|
|
|
Database: ${this.schemaName}
|
|
|
|
Generation time: ${moment().format()}
|
|
|
|
************************************************************`;
|
|
|
|
|
|
|
|
return this.buildComment(header);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getFooter () {
|
2021-10-31 17:22:59 +01:00
|
|
|
return this.buildComment(`Dump completed on ${moment().format()}`);
|
2021-10-29 12:58:37 +02:00
|
|
|
}
|
|
|
|
|
2022-04-15 14:56:13 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
getCreateTable (_tableName) {
|
2021-10-31 17:22:59 +01:00
|
|
|
throw new Error(
|
|
|
|
'Sql Exporter must implement the "getCreateTable" method'
|
|
|
|
);
|
2021-10-29 12:58:37 +02:00
|
|
|
}
|
|
|
|
|
2022-04-15 14:56:13 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
getDropTable (_tableName) {
|
2021-10-29 12:58:37 +02:00
|
|
|
throw new Error('Sql Exporter must implement the "getDropTable" method');
|
|
|
|
}
|
|
|
|
|
2022-04-15 14:56:13 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
getTableInsert (_tableName) {
|
2021-10-31 17:22:59 +01:00
|
|
|
throw new Error(
|
|
|
|
'Sql Exporter must implement the "getTableInsert" method'
|
|
|
|
);
|
2021-10-29 12:58:37 +02:00
|
|
|
}
|
|
|
|
}
|