knex/src/dialects/oracle/utils.js

43 lines
1.3 KiB
JavaScript
Raw Normal View History

function generateCombinedName(logger, postfix, name, subNames) {
const crypto = require('crypto');
const limit = 30;
2015-05-09 13:58:18 -04:00
if (!Array.isArray(subNames)) subNames = subNames ? [subNames] : [];
const table = name.replace(/\.|-/g, '_');
const subNamesPart = subNames.join('_');
let result = `${table}_${
subNamesPart.length ? subNamesPart + '_' : ''
}${postfix}`.toLowerCase();
2015-05-09 13:58:18 -04:00
if (result.length > limit) {
logger.warn(
`Automatically generated name "${result}" exceeds ${limit} character ` +
`limit for Oracle. Using base64 encoded sha1 of that name instead.`
);
2015-05-09 13:58:18 -04:00
// generates the sha1 of the name and encode it with base64
result = crypto
.createHash('sha1')
2015-05-09 13:58:18 -04:00
.update(result)
.digest('base64')
.replace('=', '');
}
return result;
}
function wrapSqlWithCatch(sql, errorNumberToCatch) {
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;
}
ReturningHelper.prototype.toString = function() {
return `[object ReturningHelper:${this.columnName}]`;
};
2015-05-09 13:58:18 -04:00
module.exports = { generateCombinedName, wrapSqlWithCatch, ReturningHelper };