compiler.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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_MySQL(client, builder) {
  13. _compiler2.default.call(this, client, builder);
  14. }
  15. (0, _inherits2.default)(SchemaCompiler_MySQL, _compiler2.default);
  16. (0, _assign3.default)(SchemaCompiler_MySQL.prototype, {
  17. // Rename a table on the schema.
  18. renameTable: function renameTable(tableName, to) {
  19. this.pushQuery('rename table ' + this.formatter.wrap(tableName) + ' to ' + this.formatter.wrap(to));
  20. },
  21. // Check whether a table exists on the query.
  22. hasTable: function hasTable(tableName) {
  23. var sql = 'select * from information_schema.tables where table_name = ?';
  24. var bindings = [tableName];
  25. if (this.schema) {
  26. sql += ' and table_schema = ?';
  27. bindings.push(this.schema);
  28. } else {
  29. sql += ' and table_schema = database()';
  30. }
  31. this.pushQuery({
  32. sql: sql,
  33. bindings: bindings,
  34. output: function output(resp) {
  35. return resp.length > 0;
  36. }
  37. });
  38. },
  39. // Check whether a column exists on the schema.
  40. hasColumn: function hasColumn(tableName, column) {
  41. this.pushQuery({
  42. sql: 'show columns from ' + this.formatter.wrap(tableName) + ' like ' + this.formatter.parameter(column),
  43. output: function output(resp) {
  44. return resp.length > 0;
  45. }
  46. });
  47. }
  48. });
  49. exports.default = SchemaCompiler_MySQL;
  50. module.exports = exports['default'];