2018-05-29 17:42:03 +02:00
|
|
|
function generateCombinedName(logger, postfix, name, subNames) {
|
2016-05-17 01:01:34 +10:00
|
|
|
const crypto = require('crypto');
|
2016-05-18 19:59:24 +10:00
|
|
|
const limit = 30;
|
2015-05-09 13:58:18 -04:00
|
|
|
if (!Array.isArray(subNames)) subNames = subNames ? [subNames] : [];
|
2016-05-17 01:01:34 +10:00
|
|
|
const table = name.replace(/\.|-/g, '_');
|
|
|
|
const subNamesPart = subNames.join('_');
|
2018-07-09 08:10:34 -04:00
|
|
|
let result = `${table}_${
|
|
|
|
subNamesPart.length ? subNamesPart + '_' : ''
|
|
|
|
}${postfix}`.toLowerCase();
|
2015-05-09 13:58:18 -04:00
|
|
|
if (result.length > limit) {
|
2018-05-29 17:42:03 +02:00
|
|
|
logger.warn(
|
2016-05-17 01:01:34 +10:00
|
|
|
`Automatically generated name "${result}" exceeds ${limit} character ` +
|
2018-07-09 08:10:34 -04:00
|
|
|
`limit for Oracle. Using base64 encoded sha1 of that name instead.`
|
2016-05-17 01:01:34 +10:00
|
|
|
);
|
2015-05-09 13:58:18 -04:00
|
|
|
// generates the sha1 of the name and encode it with base64
|
2018-07-09 08:10:34 -04:00
|
|
|
result = crypto
|
|
|
|
.createHash('sha1')
|
2015-05-09 13:58:18 -04:00
|
|
|
.update(result)
|
|
|
|
.digest('base64')
|
|
|
|
.replace('=', '');
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function wrapSqlWithCatch(sql, errorNumberToCatch) {
|
2016-05-17 01:01:34 +10:00
|
|
|
return (
|
|
|
|
`begin execute immediate '${sql.replace(/'/g, "''")}'; ` +
|
|
|
|
`exception when others then if sqlcode != ${errorNumberToCatch} then raise; ` +
|
|
|
|
`end if; ` +
|
|
|
|
`end;`
|
|
|
|
);
|
2015-05-09 13:58:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function ReturningHelper(columnName) {
|
|
|
|
this.columnName = columnName;
|
|
|
|
}
|
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
ReturningHelper.prototype.toString = function() {
|
2016-05-17 01:01:34 +10:00
|
|
|
return `[object ReturningHelper:${this.columnName}]`;
|
2018-07-09 08:10:34 -04:00
|
|
|
};
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2019-06-04 00:37:17 +02:00
|
|
|
module.exports = { generateCombinedName, wrapSqlWithCatch, ReturningHelper };
|