trigger.js 5.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _utils = require('../utils');
  4. var utils = _interopRequireWildcard(_utils);
  5. 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; } }
  6. var trigger = {
  7. renameColumnTrigger: function renameColumnTrigger(tableName, columnName, to) {
  8. var triggerName = utils.generateCombinedName('autoinc_trg', tableName);
  9. var sequenceName = utils.generateCombinedName('seq', tableName);
  10. return 'DECLARE ' + 'PK_NAME VARCHAR(200); ' + 'IS_AUTOINC NUMBER := 0; ' + 'BEGIN' + (' EXECUTE IMMEDIATE (\'ALTER TABLE "' + tableName + '" RENAME COLUMN "' + columnName + '" TO "' + to + '"\');') + (' SELECT COUNT(*) INTO IS_AUTOINC from "USER_TRIGGERS" where trigger_name = \'' + triggerName + '\';') + ' IF (IS_AUTOINC > 0) THEN' + ' SELECT cols.column_name INTO PK_NAME' + ' FROM all_constraints cons, all_cons_columns cols' + ' WHERE cons.constraint_type = \'P\'' + ' AND cons.constraint_name = cols.constraint_name' + ' AND cons.owner = cols.owner' + (' AND cols.table_name = \'' + tableName + '\';') + (' IF (\'' + to + '\' = PK_NAME) THEN') + (' EXECUTE IMMEDIATE (\'DROP TRIGGER "' + triggerName + '"\');') + (' EXECUTE IMMEDIATE (\'create or replace trigger "' + triggerName + '"') + (' BEFORE INSERT on "' + tableName + '" for each row') + ' declare' + ' checking number := 1;' + ' begin' + (' if (:new."' + to + '" is null) then') + ' while checking >= 1 loop' + (' select "' + sequenceName + '".nextval into :new."' + to + '" from dual;') + (' select count("' + to + '") into checking from "' + tableName + '"') + (' where "' + to + '" = :new."' + to + '";') + ' end loop;' + ' end if;' + ' end;\');' + ' end if;' + ' end if;' + 'END;';
  11. },
  12. createAutoIncrementTrigger: function createAutoIncrementTrigger(tableName) {
  13. var triggerName = utils.generateCombinedName('autoinc_trg', tableName);
  14. var sequenceName = utils.generateCombinedName('seq', tableName);
  15. return 'DECLARE ' + 'PK_NAME VARCHAR(200); ' + 'BEGIN' + (' EXECUTE IMMEDIATE (\'CREATE SEQUENCE "' + sequenceName + '"\');') + ' SELECT cols.column_name INTO PK_NAME' + ' FROM all_constraints cons, all_cons_columns cols' + ' WHERE cons.constraint_type = \'P\'' + ' AND cons.constraint_name = cols.constraint_name' + ' AND cons.owner = cols.owner' + (' AND cols.table_name = \'' + tableName + '\';') + (' execute immediate (\'create or replace trigger "' + triggerName + '"') + (' BEFORE INSERT on "' + tableName + '"') + ' for each row' + ' declare' + ' checking number := 1;' + ' begin' + ' if (:new."\' || PK_NAME || \'" is null) then' + ' while checking >= 1 loop' + (' select "' + sequenceName + '".nextval into :new."\' || PK_NAME || \'" from dual;') + (' select count("\' || PK_NAME || \'") into checking from "' + tableName + '"') + ' where "\' || PK_NAME || \'" = :new."\' || PK_NAME || \'";' + ' end loop;' + ' end if;' + ' end;\'); ' + 'END;';
  16. },
  17. renameTableAndAutoIncrementTrigger: function renameTableAndAutoIncrementTrigger(tableName, to) {
  18. var triggerName = utils.generateCombinedName('autoinc_trg', tableName);
  19. var sequenceName = utils.generateCombinedName('seq', tableName);
  20. var toTriggerName = utils.generateCombinedName('autoinc_trg', to);
  21. var toSequenceName = utils.generateCombinedName('seq', to);
  22. return 'DECLARE ' + 'PK_NAME VARCHAR(200); ' + 'IS_AUTOINC NUMBER := 0; ' + 'BEGIN' + (' EXECUTE IMMEDIATE (\'RENAME "' + tableName + '" TO "' + to + '"\');') + (' SELECT COUNT(*) INTO IS_AUTOINC from "USER_TRIGGERS" where trigger_name = \'' + triggerName + '\';') + ' IF (IS_AUTOINC > 0) THEN' + (' EXECUTE IMMEDIATE (\'DROP TRIGGER "' + triggerName + '"\');') + (' EXECUTE IMMEDIATE (\'RENAME "' + sequenceName + '" TO "' + toSequenceName + '"\');') + ' SELECT cols.column_name INTO PK_NAME' + ' FROM all_constraints cons, all_cons_columns cols' + ' WHERE cons.constraint_type = \'P\'' + ' AND cons.constraint_name = cols.constraint_name' + ' AND cons.owner = cols.owner' + (' AND cols.table_name = \'' + to + '\';') + (' EXECUTE IMMEDIATE (\'create or replace trigger "' + toTriggerName + '"') + (' BEFORE INSERT on "' + to + '" for each row') + ' declare' + ' checking number := 1;' + ' begin' + ' if (:new."\' || PK_NAME || \'" is null) then' + ' while checking >= 1 loop' + (' select "' + toSequenceName + '".nextval into :new."\' || PK_NAME || \'" from dual;') + (' select count("\' || PK_NAME || \'") into checking from "' + to + '"') + ' where "\' || PK_NAME || \'" = :new."\' || PK_NAME || \'";' + ' end loop;' + ' end if;' + ' end;\');' + ' end if;' + 'END;';
  23. }
  24. };
  25. exports.default = trigger;
  26. module.exports = exports['default'];