columncompiler.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _stringify = require('babel-runtime/core-js/json/stringify');
  4. var _stringify2 = _interopRequireDefault(_stringify);
  5. var _isObject2 = require('lodash/isObject');
  6. var _isObject3 = _interopRequireDefault(_isObject2);
  7. var _has2 = require('lodash/has');
  8. var _has3 = _interopRequireDefault(_has2);
  9. var _tail2 = require('lodash/tail');
  10. var _tail3 = _interopRequireDefault(_tail2);
  11. var _first2 = require('lodash/first');
  12. var _first3 = _interopRequireDefault(_first2);
  13. var _groupBy2 = require('lodash/groupBy');
  14. var _groupBy3 = _interopRequireDefault(_groupBy2);
  15. var _raw = require('../raw');
  16. var _raw2 = _interopRequireDefault(_raw);
  17. var _helpers = require('./helpers');
  18. var helpers = _interopRequireWildcard(_helpers);
  19. 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; } }
  20. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  21. // Column Compiler
  22. // Used for designating column definitions
  23. // during the table "create" / "alter" statements.
  24. // -------
  25. function ColumnCompiler(client, tableCompiler, columnBuilder) {
  26. this.client = client;
  27. this.tableCompiler = tableCompiler;
  28. this.columnBuilder = columnBuilder;
  29. this.args = columnBuilder._args;
  30. this.type = columnBuilder._type.toLowerCase();
  31. this.grouped = (0, _groupBy3.default)(columnBuilder._statements, 'grouping');
  32. this.modified = columnBuilder._modifiers;
  33. this.isIncrements = this.type.indexOf('increments') !== -1;
  34. this.formatter = client.formatter();
  35. this.sequence = [];
  36. this.modifiers = [];
  37. }
  38. ColumnCompiler.prototype.pushQuery = helpers.pushQuery;
  39. ColumnCompiler.prototype.pushAdditional = helpers.pushAdditional;
  40. // To convert to sql, we first go through and build the
  41. // column as it would be in the insert statement
  42. ColumnCompiler.prototype.toSQL = function () {
  43. this.pushQuery(this.compileColumn());
  44. if (this.sequence.additional) {
  45. this.sequence = this.sequence.concat(this.sequence.additional);
  46. }
  47. return this.sequence;
  48. };
  49. // Compiles a column.
  50. ColumnCompiler.prototype.compileColumn = function () {
  51. return this.formatter.wrap(this.getColumnName()) + ' ' + this.getColumnType() + this.getModifiers();
  52. };
  53. // Assumes the autoincrementing key is named `id` if not otherwise specified.
  54. ColumnCompiler.prototype.getColumnName = function () {
  55. var value = (0, _first3.default)(this.args);
  56. if (value) return value;
  57. if (this.isIncrements) {
  58. return 'id';
  59. } else {
  60. throw new Error('You did not specify a column name for the ' + this.type + 'column.');
  61. }
  62. };
  63. ColumnCompiler.prototype.getColumnType = function () {
  64. var type = this[this.type];
  65. return typeof type === 'function' ? type.apply(this, (0, _tail3.default)(this.args)) : type;
  66. };
  67. ColumnCompiler.prototype.getModifiers = function () {
  68. var modifiers = [];
  69. if (this.type.indexOf('increments') === -1) {
  70. for (var i = 0, l = this.modifiers.length; i < l; i++) {
  71. var modifier = this.modifiers[i];
  72. if ((0, _has3.default)(this.modified, modifier)) {
  73. var val = this[modifier].apply(this, this.modified[modifier]);
  74. if (val) modifiers.push(val);
  75. }
  76. }
  77. }
  78. return modifiers.length > 0 ? ' ' + modifiers.join(' ') : '';
  79. };
  80. // Types
  81. // ------
  82. ColumnCompiler.prototype.increments = 'integer not null primary key autoincrement';
  83. ColumnCompiler.prototype.bigincrements = 'integer not null primary key autoincrement';
  84. ColumnCompiler.prototype.integer = ColumnCompiler.prototype.smallint = ColumnCompiler.prototype.mediumint = 'integer';
  85. ColumnCompiler.prototype.biginteger = 'bigint';
  86. ColumnCompiler.prototype.varchar = function (length) {
  87. return 'varchar(' + this._num(length, 255) + ')';
  88. };
  89. ColumnCompiler.prototype.text = 'text';
  90. ColumnCompiler.prototype.tinyint = 'tinyint';
  91. ColumnCompiler.prototype.floating = function (precision, scale) {
  92. return 'float(' + this._num(precision, 8) + ', ' + this._num(scale, 2) + ')';
  93. };
  94. ColumnCompiler.prototype.decimal = function (precision, scale) {
  95. return 'decimal(' + this._num(precision, 8) + ', ' + this._num(scale, 2) + ')';
  96. };
  97. ColumnCompiler.prototype.binary = 'blob';
  98. ColumnCompiler.prototype.bool = 'boolean';
  99. ColumnCompiler.prototype.date = 'date';
  100. ColumnCompiler.prototype.datetime = 'datetime';
  101. ColumnCompiler.prototype.time = 'time';
  102. ColumnCompiler.prototype.timestamp = 'timestamp';
  103. ColumnCompiler.prototype.enu = 'varchar';
  104. ColumnCompiler.prototype.bit = ColumnCompiler.prototype.json = 'text';
  105. ColumnCompiler.prototype.uuid = 'char(36)';
  106. ColumnCompiler.prototype.specifictype = function (type) {
  107. return type;
  108. };
  109. // Modifiers
  110. // -------
  111. ColumnCompiler.prototype.nullable = function (nullable) {
  112. return nullable === false ? 'not null' : 'null';
  113. };
  114. ColumnCompiler.prototype.notNullable = function () {
  115. return this.nullable(false);
  116. };
  117. ColumnCompiler.prototype.defaultTo = function (value) {
  118. if (value === void 0) {
  119. return '';
  120. } else if (value === null) {
  121. value = "null";
  122. } else if (value instanceof _raw2.default) {
  123. value = value.toQuery();
  124. } else if (this.type === 'bool') {
  125. if (value === 'false') value = 0;
  126. value = '\'' + (value ? 1 : 0) + '\'';
  127. } else if (this.type === 'json' && (0, _isObject3.default)(value)) {
  128. return (0, _stringify2.default)(value);
  129. } else {
  130. value = '\'' + value + '\'';
  131. }
  132. return 'default ' + value;
  133. };
  134. ColumnCompiler.prototype._num = function (val, fallback) {
  135. if (val === undefined || val === null) return fallback;
  136. var number = parseInt(val, 10);
  137. return isNaN(number) ? fallback : number;
  138. };
  139. exports.default = ColumnCompiler;
  140. module.exports = exports['default'];