columncompiler.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 _columncompiler = require('../../../schema/columncompiler');
  8. var _columncompiler2 = _interopRequireDefault(_columncompiler);
  9. var _helpers = require('../../../helpers');
  10. var helpers = _interopRequireWildcard(_helpers);
  11. 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; } }
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. function ColumnCompiler_MySQL() {
  14. _columncompiler2.default.apply(this, arguments);
  15. this.modifiers = ['unsigned', 'nullable', 'defaultTo', 'first', 'after', 'comment', 'collate'];
  16. }
  17. // MySQL Column Compiler
  18. // -------
  19. (0, _inherits2.default)(ColumnCompiler_MySQL, _columncompiler2.default);
  20. // Types
  21. // ------
  22. (0, _assign3.default)(ColumnCompiler_MySQL.prototype, {
  23. increments: 'int unsigned not null auto_increment primary key',
  24. bigincrements: 'bigint unsigned not null auto_increment primary key',
  25. bigint: 'bigint',
  26. double: function double(precision, scale) {
  27. if (!precision) return 'double';
  28. return 'double(' + this._num(precision, 8) + ', ' + this._num(scale, 2) + ')';
  29. },
  30. integer: function integer(length) {
  31. length = length ? '(' + this._num(length, 11) + ')' : '';
  32. return 'int' + length;
  33. },
  34. mediumint: 'mediumint',
  35. smallint: 'smallint',
  36. tinyint: function tinyint(length) {
  37. length = length ? '(' + this._num(length, 1) + ')' : '';
  38. return 'tinyint' + length;
  39. },
  40. text: function text(column) {
  41. switch (column) {
  42. case 'medium':
  43. case 'mediumtext':
  44. return 'mediumtext';
  45. case 'long':
  46. case 'longtext':
  47. return 'longtext';
  48. default:
  49. return 'text';
  50. }
  51. },
  52. mediumtext: function mediumtext() {
  53. return this.text('medium');
  54. },
  55. longtext: function longtext() {
  56. return this.text('long');
  57. },
  58. enu: function enu(allowed) {
  59. return 'enum(\'' + allowed.join("', '") + '\')';
  60. },
  61. datetime: 'datetime',
  62. timestamp: 'timestamp',
  63. bit: function bit(length) {
  64. return length ? 'bit(' + this._num(length) + ')' : 'bit';
  65. },
  66. binary: function binary(length) {
  67. return length ? 'varbinary(' + this._num(length) + ')' : 'blob';
  68. },
  69. // Modifiers
  70. // ------
  71. defaultTo: function defaultTo(value) {
  72. var defaultVal = ColumnCompiler_MySQL.super_.prototype.defaultTo.apply(this, arguments);
  73. if (this.type !== 'blob' && this.type.indexOf('text') === -1) {
  74. return defaultVal;
  75. }
  76. return '';
  77. },
  78. unsigned: function unsigned() {
  79. return 'unsigned';
  80. },
  81. first: function first() {
  82. return 'first';
  83. },
  84. after: function after(column) {
  85. return 'after ' + this.formatter.wrap(column);
  86. },
  87. comment: function comment(_comment) {
  88. if (_comment && _comment.length > 255) {
  89. helpers.warn('Your comment is longer than the max comment length for MySQL');
  90. }
  91. return _comment && 'comment \'' + _comment + '\'';
  92. },
  93. collate: function collate(collation) {
  94. return collation && 'collate \'' + collation + '\'';
  95. }
  96. });
  97. exports.default = ColumnCompiler_MySQL;
  98. module.exports = exports['default'];