text: "use strict";
  
  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  exports.default = void 0;
  
  var _inherits = _interopRequireDefault(require("inherits"));
  
  var _compiler = _interopRequireDefault(require("../../../query/compiler"));
  
  var _lodash = require("lodash");
  
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  
  // PostgreSQL Query Builder & Compiler
  // ------
  function QueryCompiler_PG(client, builder) {
    _compiler.default.call(this, client, builder);
  }
  
  (0, _inherits.default)(QueryCompiler_PG, _compiler.default);
  (0, _lodash.assign)(QueryCompiler_PG.prototype, {
    // Compiles a truncate query.
    truncate() {
      return `truncate ${this.tableName} restart identity`;
    },
  
    // is used if the an array with multiple empty values supplied
    _defaultInsertValue: 'default',
  
    // Compiles an `insert` query, allowing for multiple
    // inserts using a single query statement.
    insert() {
      const sql = _compiler.default.prototype.insert.call(this);
  
      if (sql === '') return sql;
      const returning = this.single.returning;
      return {
        sql: sql + this._returning(returning),
        returning
      };
    },
  
    // Compiles an `update` query, allowing for a return value.
    update() {
      const withSQL = this.with();
  
      const updateData = this._prepUpdate(this.single.update);
  
      const wheres = this.where();
      const returning = this.single.returning;
      return {
        sql: withSQL + `update ${this.single.only ? 'only ' : ''}${this.tableName} ` + `set ${updateData.join(', ')}` + (wheres ? ` ${wheres}` : '') + this._returning(returning),
        returning
      };
    },
  
    // Compiles an `update` query, allowing for a return value.
    del() {
      const sql = _compiler.default.prototype.del.apply(this, arguments);
  
      const returning = this.single.returning;
      return {
        sql: sql + this._returning(returning),
        returning
      };
    },
  
    aggregate(stmt) {
      return this._aggregate(stmt, {
        distinctParentheses: true
      });
    },
  
    _returning(value) {
      return value ? ` returning ${this.formatter.columnize(value)}` : '';
    },
  
    // Join array of table names and apply default schema.
    _tableNames(tables) {
      const schemaName = this.single.schema;
      const sql = [];
  
      for (let i = 0; i < tables.length; i++) {
        let tableName = tables[i];
  
        if (tableName) {
          if (schemaName) {
            tableName = `${schemaName}.${tableName}`;
          }
  
          sql.push(this.formatter.wrap(tableName));
        }
      }
  
      return sql.join(', ');
    },
  
    forUpdate() {
      const tables = this.single.lockTables || [];
      return 'for update' + (tables.length ? ' of ' + this._tableNames(tables) : '');
    },
  
    forShare() {
      const tables = this.single.lockTables || [];
      return 'for share' + (tables.length ? ' of ' + this._tableNames(tables) : '');
    },
  
    // Compiles a columnInfo query
    columnInfo() {
      const column = this.single.columnInfo;
      let schema = this.single.schema; // The user may have specified a custom wrapIdentifier function in the config. We
      // need to run the identifiers through that function, but not format them as
      // identifiers otherwise.
  
      const table = this.client.customWrapIdentifier(this.single.table, _lodash.identity);
  
      if (schema) {
        schema = this.client.customWrapIdentifier(schema, _lodash.identity);
      }
  
      let sql = 'select * from information_schema.columns where table_name = ? and table_catalog = ?';
      const bindings = [table, this.client.database()];
  
      if (schema) {
        sql += ' and table_schema = ?';
        bindings.push(schema);
      } else {
        sql += ' and table_schema = current_schema()';
      }
  
      return {
        sql,
        bindings,
  
        output(resp) {
          const out = (0, _lodash.reduce)(resp.rows, function (columns, val) {
            columns[val.column_name] = {
              type: val.data_type,
              maxLength: val.character_maximum_length,
              nullable: val.is_nullable === 'YES',
              defaultValue: val.column_default
            };
            return columns;
          }, {});
          return column && out[column] || out;
        }
  
      };
    }
  
  });
  var _default = QueryCompiler_PG;
  exports.default = _default;
  module.exports = exports.default;