utils.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. 'use strict';
  2. exports.__esModule = true;
  3. exports.ReturningHelper = exports.wrapSqlWithCatch = exports.generateCombinedName = undefined;
  4. var _helpers = require('../../helpers');
  5. var helpers = _interopRequireWildcard(_helpers);
  6. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  7. function generateCombinedName(postfix, name, subNames) {
  8. var crypto = require('crypto');
  9. var limit = 30;
  10. if (!Array.isArray(subNames)) subNames = subNames ? [subNames] : [];
  11. var table = name.replace(/\.|-/g, '_');
  12. var subNamesPart = subNames.join('_');
  13. var result = (table + '_' + (subNamesPart.length ? subNamesPart + '_' : '') + postfix).toLowerCase();
  14. if (result.length > limit) {
  15. helpers.warn('Automatically generated name "' + result + '" exceeds ' + limit + ' character ' + 'limit for Oracle. Using base64 encoded sha1 of that name instead.');
  16. // generates the sha1 of the name and encode it with base64
  17. result = crypto.createHash('sha1').update(result).digest('base64').replace('=', '');
  18. }
  19. return result;
  20. }
  21. function wrapSqlWithCatch(sql, errorNumberToCatch) {
  22. return 'begin execute immediate \'' + sql.replace(/'/g, "''") + '\'; ' + ('exception when others then if sqlcode != ' + errorNumberToCatch + ' then raise; ') + 'end if; ' + 'end;';
  23. }
  24. function ReturningHelper(columnName) {
  25. this.columnName = columnName;
  26. }
  27. ReturningHelper.prototype.toString = function () {
  28. return '[object ReturningHelper:' + this.columnName + ']';
  29. };
  30. exports.generateCombinedName = generateCombinedName;
  31. exports.wrapSqlWithCatch = wrapSqlWithCatch;
  32. exports.ReturningHelper = ReturningHelper;