tablecompiler.js 5.6 KB

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