tablecompiler.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _assign2 = require('lodash/assign');
  4. var _assign3 = _interopRequireDefault(_assign2);
  5. var _inherits = require('inherits');
  6. var _inherits2 = _interopRequireDefault(_inherits);
  7. var _utils = require('../utils');
  8. var utils = _interopRequireWildcard(_utils);
  9. var _tablecompiler = require('../../../schema/tablecompiler');
  10. var _tablecompiler2 = _interopRequireDefault(_tablecompiler);
  11. var _helpers = require('../../../helpers');
  12. var helpers = _interopRequireWildcard(_helpers);
  13. var _trigger = require('./trigger');
  14. var _trigger2 = _interopRequireDefault(_trigger);
  15. 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; } }
  16. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  17. // Table Compiler
  18. // ------
  19. function TableCompiler_Oracle() {
  20. _tablecompiler2.default.apply(this, arguments);
  21. } /* eslint max-len:0 */
  22. (0, _inherits2.default)(TableCompiler_Oracle, _tablecompiler2.default);
  23. (0, _assign3.default)(TableCompiler_Oracle.prototype, {
  24. // Compile a rename column command.
  25. renameColumn: function renameColumn(from, to) {
  26. // Remove quotes around tableName
  27. var tableName = this.tableName().slice(1, -1);
  28. return this.pushQuery(_trigger2.default.renameColumnTrigger(tableName, from, to));
  29. },
  30. compileAdd: function compileAdd(builder) {
  31. var table = this.formatter.wrap(builder);
  32. var columns = this.prefixArray('add column', this.getColumns(builder));
  33. return this.pushQuery({
  34. sql: 'alter table ' + table + ' ' + columns.join(', ')
  35. });
  36. },
  37. // Adds the "create" query to the query sequence.
  38. createQuery: function createQuery(columns, ifNot) {
  39. var sql = 'create table ' + this.tableName() + ' (' + columns.sql.join(', ') + ')';
  40. this.pushQuery({
  41. // catch "name is already used by an existing object" for workaround for "if not exists"
  42. sql: ifNot ? utils.wrapSqlWithCatch(sql, -955) : sql,
  43. bindings: columns.bindings
  44. });
  45. if (this.single.comment) this.comment(this.single.comment);
  46. },
  47. // Compiles the comment on the table.
  48. comment: function comment(_comment) {
  49. this.pushQuery('comment on table ' + this.tableName() + ' is \'' + (_comment || '') + '\'');
  50. },
  51. addColumnsPrefix: 'add ',
  52. alterColumnsPrefix: 'modify ',
  53. dropColumn: function dropColumn() {
  54. var columns = helpers.normalizeArr.apply(null, arguments);
  55. this.pushQuery('alter table ' + this.tableName() + ' drop (' + this.formatter.columnize(columns) + ')');
  56. },
  57. changeType: function changeType() {
  58. // alter table + table + ' modify ' + wrapped + '// type';
  59. },
  60. _indexCommand: function _indexCommand(type, tableName, columns) {
  61. return this.formatter.wrap(utils.generateCombinedName(type, tableName, columns));
  62. },
  63. primary: function primary(columns, constraintName) {
  64. constraintName = constraintName ? this.formatter.wrap(constraintName) : this.formatter.wrap(this.tableNameRaw + '_pkey');
  65. this.pushQuery('alter table ' + this.tableName() + ' add constraint ' + constraintName + ' primary key (' + this.formatter.columnize(columns) + ')');
  66. },
  67. dropPrimary: function dropPrimary(constraintName) {
  68. constraintName = constraintName ? this.formatter.wrap(constraintName) : this.formatter.wrap(this.tableNameRaw + '_pkey');
  69. this.pushQuery('alter table ' + this.tableName() + ' drop constraint ' + constraintName);
  70. },
  71. index: function index(columns, indexName) {
  72. indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('index', this.tableNameRaw, columns);
  73. this.pushQuery('create index ' + indexName + ' on ' + this.tableName() + ' (' + this.formatter.columnize(columns) + ')');
  74. },
  75. dropIndex: function dropIndex(columns, indexName) {
  76. indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('index', this.tableNameRaw, columns);
  77. this.pushQuery('drop index ' + indexName);
  78. },
  79. unique: function unique(columns, indexName) {
  80. indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('unique', this.tableNameRaw, columns);
  81. this.pushQuery('alter table ' + this.tableName() + ' add constraint ' + indexName + ' unique (' + this.formatter.columnize(columns) + ')');
  82. },
  83. dropUnique: function dropUnique(columns, indexName) {
  84. indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('unique', this.tableNameRaw, columns);
  85. this.pushQuery('alter table ' + this.tableName() + ' drop constraint ' + indexName);
  86. },
  87. dropForeign: function dropForeign(columns, indexName) {
  88. indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('foreign', this.tableNameRaw, columns);
  89. this.pushQuery('alter table ' + this.tableName() + ' drop constraint ' + indexName);
  90. }
  91. });
  92. exports.default = TableCompiler_Oracle;
  93. module.exports = exports['default'];