compiler.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _reduce2 = require('lodash/reduce');
  4. var _reduce3 = _interopRequireDefault(_reduce2);
  5. var _assign2 = require('lodash/assign');
  6. var _assign3 = _interopRequireDefault(_assign2);
  7. var _inherits = require('inherits');
  8. var _inherits2 = _interopRequireDefault(_inherits);
  9. var _compiler = require('../../../query/compiler');
  10. var _compiler2 = _interopRequireDefault(_compiler);
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. // PostgreSQL Query Builder & Compiler
  13. // ------
  14. function QueryCompiler_PG(client, builder) {
  15. _compiler2.default.call(this, client, builder);
  16. }
  17. (0, _inherits2.default)(QueryCompiler_PG, _compiler2.default);
  18. (0, _assign3.default)(QueryCompiler_PG.prototype, {
  19. // Compiles a truncate query.
  20. truncate: function truncate() {
  21. return 'truncate ' + this.tableName + ' restart identity';
  22. },
  23. // is used if the an array with multiple empty values supplied
  24. _defaultInsertValue: 'default',
  25. // Compiles an `insert` query, allowing for multiple
  26. // inserts using a single query statement.
  27. insert: function insert() {
  28. var sql = _compiler2.default.prototype.insert.call(this);
  29. if (sql === '') return sql;
  30. var returning = this.single.returning;
  31. return {
  32. sql: sql + this._returning(returning),
  33. returning: returning
  34. };
  35. },
  36. // Compiles an `update` query, allowing for a return value.
  37. update: function update() {
  38. var updateData = this._prepUpdate(this.single.update);
  39. var wheres = this.where();
  40. var returning = this.single.returning;
  41. return {
  42. sql: this.with() + ('update ' + (this.single.only ? 'only ' : '') + this.tableName + ' ') + ('set ' + updateData.join(', ')) + (wheres ? ' ' + wheres : '') + this._returning(returning),
  43. returning: returning
  44. };
  45. },
  46. // Compiles an `update` query, allowing for a return value.
  47. del: function del() {
  48. var sql = _compiler2.default.prototype.del.apply(this, arguments);
  49. var returning = this.single.returning;
  50. return {
  51. sql: sql + this._returning(returning),
  52. returning: returning
  53. };
  54. },
  55. _returning: function _returning(value) {
  56. return value ? ' returning ' + this.formatter.columnize(value) : '';
  57. },
  58. forUpdate: function forUpdate() {
  59. return 'for update';
  60. },
  61. forShare: function forShare() {
  62. return 'for share';
  63. },
  64. // Compiles a columnInfo query
  65. columnInfo: function columnInfo() {
  66. var column = this.single.columnInfo;
  67. var sql = 'select * from information_schema.columns where table_name = ? and table_catalog = ?';
  68. var bindings = [this.single.table, this.client.database()];
  69. if (this.single.schema) {
  70. sql += ' and table_schema = ?';
  71. bindings.push(this.single.schema);
  72. } else {
  73. sql += ' and table_schema = current_schema';
  74. }
  75. return {
  76. sql: sql,
  77. bindings: bindings,
  78. output: function output(resp) {
  79. var out = (0, _reduce3.default)(resp.rows, function (columns, val) {
  80. columns[val.column_name] = {
  81. type: val.data_type,
  82. maxLength: val.character_maximum_length,
  83. nullable: val.is_nullable === 'YES',
  84. defaultValue: val.column_default
  85. };
  86. return columns;
  87. }, {});
  88. return column && out[column] || out;
  89. }
  90. };
  91. }
  92. });
  93. exports.default = QueryCompiler_PG;
  94. module.exports = exports['default'];