columncompiler.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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(columnBuilder);
  35. this.sequence = [];
  36. this.modifiers = [];
  37. }
  38. ColumnCompiler.prototype.pushQuery = helpers.pushQuery;
  39. ColumnCompiler.prototype.pushAdditional = helpers.pushAdditional;
  40. ColumnCompiler.prototype._defaultMap = {
  41. 'columnName': function columnName() {
  42. if (!this.isIncrements) {
  43. throw new Error('You did not specify a column name for the ' + this.type + ' column.');
  44. }
  45. return 'id';
  46. }
  47. };
  48. ColumnCompiler.prototype.defaults = function (label) {
  49. if (this._defaultMap.hasOwnProperty(label)) {
  50. return this._defaultMap[label].bind(this)();
  51. } else {
  52. throw new Error('There is no default for the specified identifier ' + label);
  53. }
  54. };
  55. // To convert to sql, we first go through and build the
  56. // column as it would be in the insert statement
  57. ColumnCompiler.prototype.toSQL = function () {
  58. this.pushQuery(this.compileColumn());
  59. if (this.sequence.additional) {
  60. this.sequence = this.sequence.concat(this.sequence.additional);
  61. }
  62. return this.sequence;
  63. };
  64. // Compiles a column.
  65. ColumnCompiler.prototype.compileColumn = function () {
  66. return this.formatter.wrap(this.getColumnName()) + ' ' + this.getColumnType() + this.getModifiers();
  67. };
  68. // Assumes the autoincrementing key is named `id` if not otherwise specified.
  69. ColumnCompiler.prototype.getColumnName = function () {
  70. var value = (0, _first3.default)(this.args);
  71. return value || this.defaults('columnName');
  72. };
  73. ColumnCompiler.prototype.getColumnType = function () {
  74. var type = this[this.type];
  75. return typeof type === 'function' ? type.apply(this, (0, _tail3.default)(this.args)) : type;
  76. };
  77. ColumnCompiler.prototype.getModifiers = function () {
  78. var modifiers = [];
  79. for (var i = 0, l = this.modifiers.length; i < l; i++) {
  80. var modifier = this.modifiers[i];
  81. //Cannot allow 'nullable' modifiers on increments types
  82. if (!this.isIncrements || this.isIncrements && modifier === 'comment') {
  83. if ((0, _has3.default)(this.modified, modifier)) {
  84. var val = this[modifier].apply(this, this.modified[modifier]);
  85. if (val) modifiers.push(val);
  86. }
  87. }
  88. }
  89. return modifiers.length > 0 ? ' ' + modifiers.join(' ') : '';
  90. };
  91. // Types
  92. // ------
  93. ColumnCompiler.prototype.increments = 'integer not null primary key autoincrement';
  94. ColumnCompiler.prototype.bigincrements = 'integer not null primary key autoincrement';
  95. ColumnCompiler.prototype.integer = ColumnCompiler.prototype.smallint = ColumnCompiler.prototype.mediumint = 'integer';
  96. ColumnCompiler.prototype.biginteger = 'bigint';
  97. ColumnCompiler.prototype.varchar = function (length) {
  98. return 'varchar(' + this._num(length, 255) + ')';
  99. };
  100. ColumnCompiler.prototype.text = 'text';
  101. ColumnCompiler.prototype.tinyint = 'tinyint';
  102. ColumnCompiler.prototype.floating = function (precision, scale) {
  103. return 'float(' + this._num(precision, 8) + ', ' + this._num(scale, 2) + ')';
  104. };
  105. ColumnCompiler.prototype.decimal = function (precision, scale) {
  106. if (precision === null) {
  107. throw new Error('Specifying no precision on decimal columns is not supported for that SQL dialect.');
  108. }
  109. return 'decimal(' + this._num(precision, 8) + ', ' + this._num(scale, 2) + ')';
  110. };
  111. ColumnCompiler.prototype.binary = 'blob';
  112. ColumnCompiler.prototype.bool = 'boolean';
  113. ColumnCompiler.prototype.date = 'date';
  114. ColumnCompiler.prototype.datetime = 'datetime';
  115. ColumnCompiler.prototype.time = 'time';
  116. ColumnCompiler.prototype.timestamp = 'timestamp';
  117. ColumnCompiler.prototype.enu = 'varchar';
  118. ColumnCompiler.prototype.bit = ColumnCompiler.prototype.json = 'text';
  119. ColumnCompiler.prototype.uuid = 'char(36)';
  120. ColumnCompiler.prototype.specifictype = function (type) {
  121. return type;
  122. };
  123. // Modifiers
  124. // -------
  125. ColumnCompiler.prototype.nullable = function (nullable) {
  126. return nullable === false ? 'not null' : 'null';
  127. };
  128. ColumnCompiler.prototype.notNullable = function () {
  129. return this.nullable(false);
  130. };
  131. ColumnCompiler.prototype.defaultTo = function (value) {
  132. if (value === void 0) {
  133. return '';
  134. } else if (value === null) {
  135. value = "null";
  136. } else if (value instanceof _raw2.default) {
  137. value = value.toQuery();
  138. } else if (this.type === 'bool') {
  139. if (value === 'false') value = 0;
  140. value = '\'' + (value ? 1 : 0) + '\'';
  141. } else if (this.type === 'json' && (0, _isObject3.default)(value)) {
  142. return (0, _stringify2.default)(value);
  143. } else {
  144. value = '\'' + value + '\'';
  145. }
  146. return 'default ' + value;
  147. };
  148. ColumnCompiler.prototype._num = function (val, fallback) {
  149. if (val === undefined || val === null) return fallback;
  150. var number = parseInt(val, 10);
  151. return isNaN(number) ? fallback : number;
  152. };
  153. exports.default = ColumnCompiler;
  154. module.exports = exports['default'];