tablecompiler.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 _tablecompiler = require('../../../schema/tablecompiler');
  8. var _tablecompiler2 = _interopRequireDefault(_tablecompiler);
  9. var _helpers = require('../../../helpers');
  10. var helpers = _interopRequireWildcard(_helpers);
  11. var _bluebird = require('bluebird');
  12. var _bluebird2 = _interopRequireDefault(_bluebird);
  13. 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; } }
  14. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  15. // Table Compiler
  16. // ------
  17. /* eslint max-len:0 */
  18. // MSSQL Table Builder & Compiler
  19. // -------
  20. function TableCompiler_MSSQL() {
  21. _tablecompiler2.default.apply(this, arguments);
  22. }
  23. (0, _inherits2.default)(TableCompiler_MSSQL, _tablecompiler2.default);
  24. (0, _assign3.default)(TableCompiler_MSSQL.prototype, {
  25. createAlterTableMethods: ['foreign', 'primary', 'unique'],
  26. createQuery: function createQuery(columns, ifNot) {
  27. var createStatement = ifNot ? 'if object_id(\'' + this.tableName() + '\', \'U\') is null CREATE TABLE ' : 'CREATE TABLE ';
  28. var sql = createStatement + this.tableName() + (this._formatting ? ' (\n ' : ' (') + columns.sql.join(this._formatting ? ',\n ' : ', ') + ')';
  29. if (this.single.comment) {
  30. var comment = this.single.comment || '';
  31. if (comment.length > 60) helpers.warn('The max length for a table comment is 60 characters');
  32. }
  33. this.pushQuery(sql);
  34. },
  35. lowerCase: false,
  36. addColumnsPrefix: 'ADD ',
  37. dropColumnPrefix: 'DROP COLUMN ',
  38. alterColumnPrefix: 'ALTER COLUMN ',
  39. // Compiles column add. Multiple columns need only one ADD clause (not one ADD per column) so core addColumns doesn't work. #1348
  40. addColumns: function addColumns(columns, prefix) {
  41. prefix = prefix || this.addColumnsPrefix;
  42. if (columns.sql.length > 0) {
  43. this.pushQuery({
  44. sql: (this.lowerCase ? 'alter table ' : 'ALTER TABLE ') + this.tableName() + ' ' + prefix + columns.sql.join(', '),
  45. bindings: columns.bindings
  46. });
  47. }
  48. },
  49. // Compiles column drop. Multiple columns need only one DROP clause (not one DROP per column) so core dropColumn doesn't work. #1348
  50. dropColumn: function dropColumn() {
  51. var _this2 = this;
  52. var columns = helpers.normalizeArr.apply(null, arguments);
  53. var drops = (Array.isArray(columns) ? columns : [columns]).map(function (column) {
  54. return _this2.formatter.wrap(column);
  55. });
  56. this.pushQuery((this.lowerCase ? 'alter table ' : 'ALTER TABLE ') + this.tableName() + ' ' + this.dropColumnPrefix + drops.join(', '));
  57. },
  58. // Compiles the comment on the table.
  59. comment: function comment() {},
  60. changeType: function changeType() {},
  61. // Renames a column on the table.
  62. renameColumn: function renameColumn(from, to) {
  63. this.pushQuery('exec sp_rename ' + this.formatter.parameter(this.tableName() + '.' + from) + ', ' + this.formatter.parameter(to) + ', \'COLUMN\'');
  64. },
  65. dropFKRefs: function dropFKRefs(runner, refs) {
  66. var formatter = this.client.formatter();
  67. return _bluebird2.default.all(refs.map(function (ref) {
  68. var constraintName = formatter.wrap(ref.CONSTRAINT_NAME);
  69. var tableName = formatter.wrap(ref.TABLE_NAME);
  70. return runner.query({
  71. sql: 'ALTER TABLE ' + tableName + ' DROP CONSTRAINT ' + constraintName
  72. });
  73. }));
  74. },
  75. createFKRefs: function createFKRefs(runner, refs) {
  76. var formatter = this.client.formatter();
  77. return _bluebird2.default.all(refs.map(function (ref) {
  78. var tableName = formatter.wrap(ref.TABLE_NAME);
  79. var keyName = formatter.wrap(ref.CONSTRAINT_NAME);
  80. var column = formatter.columnize(ref.COLUMN_NAME);
  81. var references = formatter.columnize(ref.REFERENCED_COLUMN_NAME);
  82. var inTable = formatter.wrap(ref.REFERENCED_TABLE_NAME);
  83. var onUpdate = ' ON UPDATE ' + ref.UPDATE_RULE;
  84. var onDelete = ' ON DELETE ' + ref.DELETE_RULE;
  85. return runner.query({
  86. sql: 'ALTER TABLE ' + tableName + ' ADD CONSTRAINT ' + keyName + ' FOREIGN KEY (' + column + ') REFERENCES ' + inTable + ' (' + references + ')' + onUpdate + onDelete
  87. });
  88. }));
  89. },
  90. index: function index(columns, indexName) {
  91. indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('index', this.tableNameRaw, columns);
  92. this.pushQuery('CREATE INDEX ' + indexName + ' ON ' + this.tableName() + ' (' + this.formatter.columnize(columns) + ')');
  93. },
  94. primary: function primary(columns, constraintName) {
  95. constraintName = constraintName ? this.formatter.wrap(constraintName) : this.formatter.wrap(this.tableNameRaw + '_pkey');
  96. if (!this.forCreate) {
  97. this.pushQuery('ALTER TABLE ' + this.tableName() + ' ADD CONSTRAINT ' + constraintName + ' PRIMARY KEY (' + this.formatter.columnize(columns) + ')');
  98. } else {
  99. this.pushQuery('CONSTRAINT ' + constraintName + ' PRIMARY KEY (' + this.formatter.columnize(columns) + ')');
  100. }
  101. },
  102. unique: function unique(columns, indexName) {
  103. indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('unique', this.tableNameRaw, columns);
  104. if (!this.forCreate) {
  105. this.pushQuery('CREATE UNIQUE INDEX ' + indexName + ' ON ' + this.tableName() + ' (' + this.formatter.columnize(columns) + ')');
  106. } else {
  107. this.pushQuery('CONSTRAINT ' + indexName + ' UNIQUE (' + this.formatter.columnize(columns) + ')');
  108. }
  109. },
  110. // Compile a drop index command.
  111. dropIndex: function dropIndex(columns, indexName) {
  112. indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('index', this.tableNameRaw, columns);
  113. this.pushQuery('DROP INDEX ' + indexName + ' ON ' + this.tableName());
  114. },
  115. // Compile a drop foreign key command.
  116. dropForeign: function dropForeign(columns, indexName) {
  117. indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('foreign', this.tableNameRaw, columns);
  118. this.pushQuery('ALTER TABLE ' + this.tableName() + ' DROP CONSTRAINT ' + indexName);
  119. },
  120. // Compile a drop primary key command.
  121. dropPrimary: function dropPrimary(constraintName) {
  122. constraintName = constraintName ? this.formatter.wrap(constraintName) : this.formatter.wrap(this.tableNameRaw + '_pkey');
  123. this.pushQuery('ALTER TABLE ' + this.tableName() + ' DROP CONSTRAINT ' + constraintName);
  124. },
  125. // Compile a drop unique key command.
  126. dropUnique: function dropUnique(column, indexName) {
  127. indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('unique', this.tableNameRaw, column);
  128. this.pushQuery('ALTER TABLE ' + this.tableName() + ' DROP CONSTRAINT ' + indexName);
  129. }
  130. });
  131. exports.default = TableCompiler_MSSQL;
  132. module.exports = exports['default'];