columncompiler.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _map2 = require('lodash/map');
  4. var _map3 = _interopRequireDefault(_map2);
  5. var _uniq2 = require('lodash/uniq');
  6. var _uniq3 = _interopRequireDefault(_uniq2);
  7. var _assign2 = require('lodash/assign');
  8. var _assign3 = _interopRequireDefault(_assign2);
  9. var _inherits = require('inherits');
  10. var _inherits2 = _interopRequireDefault(_inherits);
  11. var _raw = require('../../../raw');
  12. var _raw2 = _interopRequireDefault(_raw);
  13. var _columncompiler = require('../../../schema/columncompiler');
  14. var _columncompiler2 = _interopRequireDefault(_columncompiler);
  15. var _trigger = require('./trigger');
  16. var _trigger2 = _interopRequireDefault(_trigger);
  17. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  18. // Column Compiler
  19. // -------
  20. function ColumnCompiler_Oracle() {
  21. _columncompiler2.default.apply(this, arguments);
  22. this.modifiers = ['defaultTo', 'checkIn', 'nullable', 'comment'];
  23. }
  24. (0, _inherits2.default)(ColumnCompiler_Oracle, _columncompiler2.default);
  25. (0, _assign3.default)(ColumnCompiler_Oracle.prototype, {
  26. // helper function for pushAdditional in increments() and bigincrements()
  27. _createAutoIncrementTriggerAndSequence: function _createAutoIncrementTriggerAndSequence() {
  28. // TODO Add warning that sequence etc is created
  29. this.pushAdditional(function () {
  30. var tableName = this.tableCompiler.tableNameRaw;
  31. var createTriggerSQL = _trigger2.default.createAutoIncrementTrigger(tableName);
  32. this.pushQuery(createTriggerSQL);
  33. });
  34. },
  35. increments: function increments() {
  36. this._createAutoIncrementTriggerAndSequence();
  37. return 'integer not null primary key';
  38. },
  39. bigincrements: function bigincrements() {
  40. this._createAutoIncrementTriggerAndSequence();
  41. return 'number(20, 0) not null primary key';
  42. },
  43. floating: function floating(precision) {
  44. var parsedPrecision = this._num(precision, 0);
  45. return 'float' + (parsedPrecision ? '(' + parsedPrecision + ')' : '');
  46. },
  47. double: function double(precision, scale) {
  48. // if (!precision) return 'number'; // TODO: Check If default is ok
  49. return 'number(' + this._num(precision, 8) + ', ' + this._num(scale, 2) + ')';
  50. },
  51. integer: function integer(length) {
  52. return length ? 'number(' + this._num(length, 11) + ')' : 'integer';
  53. },
  54. tinyint: 'smallint',
  55. smallint: 'smallint',
  56. mediumint: 'integer',
  57. biginteger: 'number(20, 0)',
  58. text: 'clob',
  59. enu: function enu(allowed) {
  60. allowed = (0, _uniq3.default)(allowed);
  61. var maxLength = (allowed || []).reduce(function (maxLength, name) {
  62. return Math.max(maxLength, String(name).length);
  63. }, 1);
  64. // implicitly add the enum values as checked values
  65. this.columnBuilder._modifiers.checkIn = [allowed];
  66. return 'varchar2(' + maxLength + ')';
  67. },
  68. time: 'timestamp with time zone',
  69. datetime: function datetime(without) {
  70. return without ? 'timestamp' : 'timestamp with time zone';
  71. },
  72. timestamp: function timestamp(without) {
  73. return without ? 'timestamp' : 'timestamp with time zone';
  74. },
  75. bit: 'clob',
  76. json: 'clob',
  77. bool: function bool() {
  78. // implicitly add the check for 0 and 1
  79. this.columnBuilder._modifiers.checkIn = [[0, 1]];
  80. return 'number(1, 0)';
  81. },
  82. varchar: function varchar(length) {
  83. return 'varchar2(' + this._num(length, 255) + ')';
  84. },
  85. // Modifiers
  86. // ------
  87. comment: function comment(_comment) {
  88. this.pushAdditional(function () {
  89. this.pushQuery('comment on column ' + this.tableCompiler.tableName() + '.' + this.formatter.wrap(this.args[0]) + " is '" + (_comment || '') + "'");
  90. }, _comment);
  91. },
  92. checkIn: function checkIn(value) {
  93. // TODO: Maybe accept arguments also as array
  94. // TODO: value(s) should be escaped properly
  95. if (value === undefined) {
  96. return '';
  97. } else if (value instanceof _raw2.default) {
  98. value = value.toQuery();
  99. } else if (Array.isArray(value)) {
  100. value = (0, _map3.default)(value, function (v) {
  101. return '\'' + v + '\'';
  102. }).join(', ');
  103. } else {
  104. value = '\'' + value + '\'';
  105. }
  106. return 'check (' + this.formatter.wrap(this.args[0]) + ' in (' + value + '))';
  107. }
  108. });
  109. exports.default = ColumnCompiler_Oracle;
  110. module.exports = exports['default'];