compiler.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14. // PostgreSQL Query Builder & Compiler
  15. // ------
  16. function QueryCompiler_PG(client, builder) {
  17. _compiler2.default.call(this, client, builder);
  18. }
  19. (0, _inherits2.default)(QueryCompiler_PG, _compiler2.default);
  20. (0, _assign3.default)(QueryCompiler_PG.prototype, {
  21. // Compiles a truncate query.
  22. truncate: function truncate() {
  23. return 'truncate ' + this.tableName + ' restart identity';
  24. },
  25. // is used if the an array with multiple empty values supplied
  26. _defaultInsertValue: 'default',
  27. // Compiles an `insert` query, allowing for multiple
  28. // inserts using a single query statement.
  29. insert: function insert() {
  30. var sql = _compiler2.default.prototype.insert.call(this);
  31. if (sql === '') return sql;
  32. var returning = this.single.returning;
  33. return {
  34. sql: sql + this._returning(returning),
  35. returning: returning
  36. };
  37. },
  38. // Compiles an `update` query, allowing for a return value.
  39. update: function update() {
  40. var updateData = this._prepUpdate(this.single.update);
  41. var wheres = this.where();
  42. var returning = this.single.returning;
  43. return {
  44. sql: this.with() + ('update ' + (this.single.only ? 'only ' : '') + this.tableName + ' ') + ('set ' + updateData.join(', ')) + (wheres ? ' ' + wheres : '') + this._returning(returning),
  45. returning: returning
  46. };
  47. },
  48. // Compiles an `update` query, allowing for a return value.
  49. del: function del() {
  50. var sql = _compiler2.default.prototype.del.apply(this, arguments);
  51. var returning = this.single.returning;
  52. return {
  53. sql: sql + this._returning(returning),
  54. returning: returning
  55. };
  56. },
  57. aggregate: function aggregate(stmt) {
  58. return this._aggregate(stmt, { distinctParentheses: true });
  59. },
  60. _returning: function _returning(value) {
  61. return value ? ' returning ' + this.formatter.columnize(value) : '';
  62. },
  63. forUpdate: function forUpdate() {
  64. return 'for update';
  65. },
  66. forShare: function forShare() {
  67. return 'for share';
  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, this.client.database()];
  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_PG;
  107. module.exports = exports['default'];