"use strict";

var pg                = require('pg'); //PostgreSQL
var logger            = require("../libs/utils.js").logger;
var csv               = require("fast-csv");
var loadSQLFile       = require("../libs/utils.js").loadSQLFile;
var replaceWith       = require("../libs/utils.js").replaceWith;

module.exports.exec = function executePostgre(process){

  function queryFormat(query, values) {
    if (!values) return query.replace(/(\:\/)/g,':');
    else {
      var _query = query.replace(/\:(\w+)/g, function (txt, key) {
        return values && key && values.hasOwnProperty(key)
          ? escape(replaceWith(values[key],process.values()))
          : null;
      }.bind(this)).replace(/(\:\/)/g,':');
    }
    return _query;
  }

  function executeQuery(process, configValues){
    return new Promise(function(resolve, reject) {

      process.execute_arg = process.args

      var client = new pg.Client({
        user     : configValues.user,
        password : configValues.password,
        database : configValues.database,
        host     : configValues.host || configValues.socketPath,
        port     : configValues.port
      });

      client.connect(function(err) {
        if(err) {
          logger.log('error',`Could not connect to Postgre: `+err);

          reject(err);
        }else{
          var finalQuery = queryFormat(process.exec.command, process.execute_arg);

          client.query(finalQuery, null, function(err, results){
            if(err){
              logger.log('error',`Error query Postgre (${finalQuery}): `+err);

              reject(`Error query Postgre (${finalQuery}): `+err);
            }else{
              if(results.hasOwnProperty('rows') && results.rows.length > 0){

                process.execute_db_results = JSON.stringify(results.rows);
                csv.writeToString(results.rows, {headers: true}, function(err, data){
                  if(err){
                    logger.log('error',`Generating csv output for execute_db_results_csv. id: ${process.id}: ${err}. Results: ${results}`);
                  }else{
                    process.execute_db_results_csv = data;
                  }

                  resolve();
                });

              }else{

                if(results instanceof Object){
                  process.execute_db_results      = '';
                  process.execute_db_results_csv  = '';
                  process.execute_db_fieldCount   = results.rowCount;
                  process.execute_db_affectedRows = '';
                  process.execute_db_changedRows  = '';
                  process.execute_db_insertId     = results.oid;
                  process.execute_db_warningCount = '';
                  process.execute_db_message      = '';
                }

                resolve();
              }
            }
            client.end();
          })
        }
      });
    });
  }

  return new Promise(function(resolve, reject) {

    if(process.exec.db_connection_id){
      process.loadDbConfig()
        .then((configValues) => {
          if(!process.exec.command){
            if(!process.exec.file_name){
              logger.log('error',`executePostgre dont have command or file_name`);
              process.execute_err_return = `executePostgre dont have command or file_name`;
              process.execute_return = '';
              process.error();
              process.write_output();
              reject(process, `executePostgre dont have command or file_name`);
            }else{
              loadSQLFile(process.exec.file_name)
                .then((fileContent) => {
                  process.exec.command = fileContent;
                  executeQuery(process, configValues)
                    .then((res) => {
                      process.execute_return = '';
                      process.execute_err_return = '';
                      process.end();
                      process.write_output();
                      resolve();
                    })
                    .catch(function(err){
                      logger.log('error',`executePostgre executeQuery from file: ${err}`);
                      process.execute_err_return = `executePostgre executeQuery from file: ${err}`;
                      process.execute_return = '';
                      process.error();
                      process.write_output();
                      reject(process, err);
                     });
                })
                .catch(function(err){
                    logger.log('error',`executePostgre loadSQLFile: ${err}`);
                    process.execute_err_return = `executePostgre loadSQLFile: ${err}`;
                    process.execute_return = '';
                    process.error();
                    process.write_output();
                    reject(process, err);
                  });
            }
          }else{
            console.log('> ANTES DE EXECUTE QUERY');
            executeQuery(process, configValues)
              .then((res) => {
                console.log('> EJECUTADO OK');
                process.execute_return = '';
                process.execute_err_return = '';
                process.end();
                process.write_output();
                resolve();
              })
              .catch(function(err){
                console.log('> EJECUTADO ERR');
                logger.log('error',`executePostgre executeQuery: ${err}`);
                process.execute_err_return = `executePostgre executeQuery: ${err}`;
                process.execute_return = '';
                process.error();
                process.write_output();
                reject(process, err);
              });
          }
        })
       .catch(function(err){
           logger.log('error',`executePostgre loadDbConfig: ${err}`);
           process.execute_err_return = `executePostgre loadDbConfig: ${err}`;
           process.execute_return = '';
           process.error();
           process.write_output();
           reject(process, err);
         });
    }else{
      logger.log('error',`executePostgre: db_connection_id not set for ${process.id}`);
      process.execute_err_return = `executePostgre: db_connection_id not set for ${process.id}`;
      process.execute_return = '';
      process.error();
      process.write_output();
      reject(process, `executePostgre: db_connection_id not set for ${process.id}`);
    }
  });

}
@@ -4883,61 +4883,8 @@
 se%7B%0A
-            console.log('%3E ANTES DE EXECUTE QUERY');%0A
     
@@ -4962,55 +4962,8 @@
 %3E %7B%0A
-                console.log('%3E EJECUTADO OK');%0A
     
@@ -5207,56 +5207,8 @@
 r)%7B%0A
-                console.log('%3E EJECUTADO ERR');%0A
     

Fix Iters into Iters