2022-02-02 04:05:31 +00:00
|
|
|
const ejs = require('ejs');
|
|
|
|
const fs = require('fs-extra');
|
2022-12-29 13:27:16 +00:00
|
|
|
const SqlString = require('sqlstring');
|
2022-02-02 04:05:31 +00:00
|
|
|
|
2022-12-21 12:34:17 +00:00
|
|
|
module.exports = class Exporter {
|
2023-01-25 17:16:43 +00:00
|
|
|
constructor(objectType, replace) {
|
|
|
|
Object.assign(this, {
|
|
|
|
objectType,
|
|
|
|
replace
|
|
|
|
});
|
2022-02-02 04:05:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async init() {
|
2022-12-21 12:34:17 +00:00
|
|
|
const templateDir = `${__dirname}/../exporters/${this.objectType}`;
|
2022-02-02 04:05:31 +00:00
|
|
|
this.sql = await fs.readFile(`${templateDir}.sql`, 'utf8');
|
|
|
|
|
|
|
|
const templateFile = await fs.readFile(`${templateDir}.ejs`, 'utf8');
|
|
|
|
this.template = ejs.compile(templateFile);
|
|
|
|
this.attrs = require(`${templateDir}.js`);
|
|
|
|
}
|
|
|
|
|
2022-12-29 13:27:16 +00:00
|
|
|
async query(conn, schema, name) {
|
2022-02-02 04:05:31 +00:00
|
|
|
const ops = [];
|
|
|
|
function addOp(col, value) {
|
|
|
|
ops.push(conn.format('?? = ?', [col, value]));
|
|
|
|
}
|
|
|
|
if (schema)
|
|
|
|
addOp(this.attrs.schemaCol, schema);
|
|
|
|
if (name)
|
|
|
|
addOp(this.attrs.nameCol, name);
|
|
|
|
|
|
|
|
const filter = {
|
|
|
|
toSqlString() {
|
|
|
|
return ops.join(' AND ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const [res] = await conn.query(this.sql, [filter]);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
format(params) {
|
2022-12-29 13:27:16 +00:00
|
|
|
const {attrs} = this;
|
2023-01-25 17:16:43 +00:00
|
|
|
params = Object.assign({}, attrs.defaults, params)
|
|
|
|
const data = {params};
|
2022-02-07 10:46:13 +00:00
|
|
|
|
|
|
|
if (attrs.formatter)
|
2022-12-29 13:27:16 +00:00
|
|
|
attrs.formatter(params);
|
2022-02-02 04:05:31 +00:00
|
|
|
|
2022-02-07 10:46:13 +00:00
|
|
|
if (attrs.escapeCols)
|
|
|
|
for (const escapeCol of attrs.escapeCols) {
|
|
|
|
if (params[escapeCol])
|
2022-12-29 13:27:16 +00:00
|
|
|
params[escapeCol] = SqlString.escape(params[escapeCol])
|
2022-02-07 10:46:13 +00:00
|
|
|
}
|
2022-02-02 04:05:31 +00:00
|
|
|
|
|
|
|
const split = params.definer.split('@');
|
2023-01-25 17:16:43 +00:00
|
|
|
data.definer =
|
2022-12-29 13:27:16 +00:00
|
|
|
SqlString.escapeId(split[0], true) + '@' +
|
|
|
|
SqlString.escapeId(split[1], true);
|
2022-02-02 04:05:31 +00:00
|
|
|
|
2023-01-25 17:16:43 +00:00
|
|
|
data.name =
|
|
|
|
SqlString.escapeId(params.schema, true) + '.' +
|
|
|
|
SqlString.escapeId(params.name, true);
|
|
|
|
|
|
|
|
const objectType = this.objectType.toUpperCase();
|
|
|
|
const delimiter = '$$';
|
|
|
|
const replace = `CREATE OR REPLACE`;
|
|
|
|
|
|
|
|
let create;
|
|
|
|
if (this.replace) {
|
|
|
|
create = `DELIMITER ${delimiter}\n${replace}`
|
|
|
|
} else {
|
|
|
|
create =
|
|
|
|
`DROP ${objectType} IF EXISTS ${data.name};\n`
|
|
|
|
+`DELIMITER ${delimiter}\n`
|
|
|
|
+`CREATE`;
|
|
|
|
}
|
|
|
|
|
|
|
|
data.createOrReplace = `${replace} DEFINER=${data.definer}`
|
|
|
|
data.begin = `${create} DEFINER=${data.definer}`;
|
|
|
|
data.typeAndName = `${objectType} ${data.name}`;
|
|
|
|
data.end = `${delimiter}\nDELIMITER ;`;
|
|
|
|
|
|
|
|
return this.template(data);
|
2022-02-02 04:05:31 +00:00
|
|
|
}
|
|
|
|
}
|