const { debug } = require('./config').getPreDbConfig()
const QueryResult = require('../models/QueryResult.js')
const createSocksConnection = require('./socks.js')

const clients = {
  mysql: require('../drivers/mysql').runQuery,
  crate: require('../drivers/crate').runQuery,
  presto: require('../drivers/presto').runQuery,
  postgres: require('../drivers/pg').runQuery,
  sqlserver: require('../drivers/mssql').runQuery,
  vertica: require('../drivers/vertica').runQuery,
  hdb: require('../drivers/hdb').runQuery
}

module.exports = function runQuery(query, connection, callback) {
  var queryResult = new QueryResult()
  queryResult.timerStart()
  var conn = Object.assign({}, connection, {
    stream: createSocksConnection(connection)
  })
  clients[conn.driver](query, conn, queryResult, function(err, queryResult) {
    queryResult.timerStop()
    if (debug) {
      var resultRowCount =
        queryResult && queryResult.rows && queryResult.rows.length
          ? queryResult.rows.length
          : 0
      console.log('\n--- lib/run-query.js ---')
      console.log('CONNECTION: ' + conn.name)
      console.log('START TIME: ' + queryResult.startTime.toISOString())
      console.log('END TIME: ' + queryResult.stopTime.toISOString())
      console.log('ELAPSED MS: ' + queryResult.queryRunTime)
      console.log('RESULT ROWS: ' + resultRowCount)
      console.log('QUERY: ')
      console.log(query)
      console.log()
    }
    callback(err, queryResult)
  })
}

@@ -516,367 +516,136 @@
 %0A%7D%0A%0A
-module.exports = function runQuery(query, connection, callback) %7B%0A  var queryResult = new QueryResult()%0A  queryResult.timerStart()%0A  var conn = Object.assign(%7B%7D, connection, %7B%0A    stream: createSocksConnection(connection)%0A  %7D)%0A  clients%5Bconn.driver%5D(query, conn, queryResult, function(err, queryResult) %7B%0A    queryResult.timerStop()%0A    if (debug) %7B%0A      var
+// TODO log this debug information in another format somewhere useful%0Afunction getInfo(connection, queryResult, query) %7B%0A  const
  res
@@ -658,20 +658,16 @@
 Count =%0A
-    
     quer
@@ -717,28 +717,24 @@
 rows.length%0A
-    
       ? quer
@@ -763,129 +763,81 @@
     
-    : 0%0A      console.log('%5Cn--- lib/run-query.js ---')%0A      console.log('CONNECTION: ' + conn.name)%0A      console.log('
+: 0%0A%0A  return %60%0A--- lib/run-query.js ---%0ACONNECTION:  $%7Bconnection.name%7D%0A
 STAR
@@ -840,28 +840,27 @@
 START TIME: 
-' + 
+ $%7B
 queryResult.
@@ -882,37 +882,18 @@
 String()
-)%0A      console.log('
+%7D%0A
 END TIME
@@ -894,20 +894,21 @@
 D TIME: 
-' + 
+   $%7B
 queryRes
@@ -933,37 +933,18 @@
 String()
-)%0A      console.log('
+%7D%0A
 ELAPSED 
@@ -947,20 +947,19 @@
 SED MS: 
-' + 
+ $%7B
 queryRes
@@ -974,37 +974,18 @@
 yRunTime
-)%0A      console.log('
+%7D%0A
 RESULT R
@@ -993,12 +993,10 @@
 WS: 
-' + 
+$%7B
 resu
@@ -1009,82 +1009,432 @@
 ount
-)%0A      console.log('QUERY: ')%0A      console.log(query)%0A      console.log(
+%7D%0AQUERY: %0A$%7Bquery%7D%0A%60%0A%7D%0A%0Amodule.exports = function runQuery(query, connection, callback) %7B%0A  const queryResult = new QueryResult()%0A  queryResult.timerStart()%0A  const conn = Object.assign(%7B%7D, connection, %7B%0A    stream: createSocksConnection(connection)%0A  %7D)%0A  clients%5Bconn.driver%5D(query, conn, queryResult, (err, queryResult) =%3E %7B%0A    queryResult.timerStop()%0A    if (debug) %7B%0A      console.log(getInfo(conn, queryResult, query)
 )%0A  

Tidy up run-query.js