compiler.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _identity2 = require('lodash/identity');
  4. var _identity3 = _interopRequireDefault(_identity2);
  5. var _reduce2 = require('lodash/reduce');
  6. var _reduce3 = _interopRequireDefault(_reduce2);
  7. var _assign2 = require('lodash/assign');
  8. var _assign3 = _interopRequireDefault(_assign2);
  9. var _inherits = require('inherits');
  10. var _inherits2 = _interopRequireDefault(_inherits);
  11. var _compiler = require('../../../query/compiler');
  12. var _compiler2 = _interopRequireDefault(_compiler);
  13. var _compiler3 = require('../../postgres/query/compiler');
  14. var _compiler4 = _interopRequireDefault(_compiler3);
  15. var _helpers = require('../../../helpers');
  16. var helpers = _interopRequireWildcard(_helpers);
  17. 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; } }
  18. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  19. // Redshift Query Builder & Compiler
  20. // ------
  21. function QueryCompiler_Redshift(client, builder) {
  22. _compiler4.default.call(this, client, builder);
  23. }
  24. (0, _inherits2.default)(QueryCompiler_Redshift, _compiler4.default);
  25. (0, _assign3.default)(QueryCompiler_Redshift.prototype, {
  26. truncate: function truncate() {
  27. return 'truncate ' + this.tableName.toLowerCase();
  28. },
  29. // Compiles an `insert` query, allowing for multiple
  30. // inserts using a single query statement.
  31. insert: function insert() {
  32. var sql = _compiler2.default.prototype.insert.apply(this, arguments);
  33. if (sql === '') return sql;
  34. this._slightReturn();
  35. return {
  36. sql: sql
  37. };
  38. },
  39. // Compiles an `update` query, warning on unsupported returning
  40. update: function update() {
  41. var sql = _compiler2.default.prototype.update.apply(this, arguments);
  42. this._slightReturn();
  43. return {
  44. sql: sql
  45. };
  46. },
  47. // Compiles an `delete` query, warning on unsupported returning
  48. del: function del() {
  49. var sql = _compiler2.default.prototype.del.apply(this, arguments);
  50. this._slightReturn();
  51. return {
  52. sql: sql
  53. };
  54. },
  55. // simple: if trying to return, warn
  56. _slightReturn: function _slightReturn() {
  57. if (this.single.isReturning) {
  58. helpers.warn('insert/update/delete returning is not supported by redshift dialect');
  59. }
  60. },
  61. forUpdate: function forUpdate() {
  62. helpers.warn('table lock is not supported by redshift dialect');
  63. return '';
  64. },
  65. forShare: function forShare() {
  66. helpers.warn('lock for share is not supported by redshift dialect');
  67. return '';
  68. },
  69. // Compiles a columnInfo query
  70. columnInfo: function columnInfo() {
  71. var column = this.single.columnInfo;
  72. var schema = this.single.schema;
  73. // The user may have specified a custom wrapIdentifier function in the config. We
  74. // need to run the identifiers through that function, but not format them as
  75. // identifiers otherwise.
  76. var table = this.client.customWrapIdentifier(this.single.table, _identity3.default);
  77. if (schema) {
  78. schema = this.client.customWrapIdentifier(schema, _identity3.default);
  79. }
  80. var sql = 'select * from information_schema.columns where table_name = ? and table_catalog = ?';
  81. var bindings = [table.toLowerCase(), this.client.database().toLowerCase()];
  82. if (schema) {
  83. sql += ' and table_schema = ?';
  84. bindings.push(schema);
  85. } else {
  86. sql += ' and table_schema = current_schema()';
  87. }
  88. return {
  89. sql: sql,
  90. bindings: bindings,
  91. output: function output(resp) {
  92. var out = (0, _reduce3.default)(resp.rows, function (columns, val) {
  93. columns[val.column_name] = {
  94. type: val.data_type,
  95. maxLength: val.character_maximum_length,
  96. nullable: val.is_nullable === 'YES',
  97. defaultValue: val.column_default
  98. };
  99. return columns;
  100. }, {});
  101. return column && out[column] || out;
  102. }
  103. };
  104. }
  105. });
  106. exports.default = QueryCompiler_Redshift;
  107. module.exports = exports['default'];