compiler.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 _compiler = require('../../../schema/compiler');
  8. var _compiler2 = _interopRequireDefault(_compiler);
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. // MySQL Schema Compiler
  11. // -------
  12. function SchemaCompiler_MSSQL(client, builder) {
  13. _compiler2.default.call(this, client, builder);
  14. }
  15. (0, _inherits2.default)(SchemaCompiler_MSSQL, _compiler2.default);
  16. (0, _assign3.default)(SchemaCompiler_MSSQL.prototype, {
  17. dropTablePrefix: 'DROP TABLE ',
  18. dropTableIfExists: function dropTableIfExists(tableName) {
  19. var name = this.formatter.wrap(prefixedTableName(this.schema, tableName));
  20. this.pushQuery('if object_id(\'' + name + '\', \'U\') is not null DROP TABLE ' + name);
  21. },
  22. // Rename a table on the schema.
  23. renameTable: function renameTable(tableName, to) {
  24. this.pushQuery('exec sp_rename ' + this.formatter.parameter(tableName) + ', ' + this.formatter.parameter(to));
  25. },
  26. // Check whether a table exists on the query.
  27. hasTable: function hasTable(tableName) {
  28. var formattedTable = this.formatter.parameter(this.formatter.wrap(tableName));
  29. var sql = 'select object_id from sys.tables ' + ('where object_id = object_id(' + formattedTable + ')');
  30. this.pushQuery({ sql: sql, output: function output(resp) {
  31. return resp.length > 0;
  32. } });
  33. },
  34. // Check whether a column exists on the schema.
  35. hasColumn: function hasColumn(tableName, column) {
  36. var formattedColumn = this.formatter.parameter(column);
  37. var formattedTable = this.formatter.parameter(this.formatter.wrap(tableName));
  38. var sql = 'select object_id from sys.columns ' + ('where name = ' + formattedColumn + ' ') + ('and object_id = object_id(' + formattedTable + ')');
  39. this.pushQuery({ sql: sql, output: function output(resp) {
  40. return resp.length > 0;
  41. } });
  42. }
  43. });
  44. function prefixedTableName(prefix, table) {
  45. return prefix ? prefix + '.' + table : table;
  46. }
  47. exports.default = SchemaCompiler_MSSQL;
  48. module.exports = exports['default'];