var queue = require('queue-async');
var QueryTablesApi = require('./query_tables_api');

function OverviewsApi(pgQueryRunner) {
   if (pgQueryRunner.pgQueryRunner !== undefined) {
       this.queryTablesApi = pgQueryRunner;
       this.pgQueryRunner = this.queryTablesApi.pgQueryRunner;
   } else {
       this.pgQueryRunner = pgQueryRunner;
       this.queryTablesApi = new QueryTablesApi(pgQueryRunner);
  }
}

module.exports = OverviewsApi;

OverviewsApi.prototype.getOverviewsMetadata = function (username, sql, callback) {
    this.queryTablesApi.getAffectedTablesInQuery(username, sql, function(err, tableNames){
        if (err) {
            callback(err);
        } else {
            var metadata = {};

            var parallelism = 2;
            var q = queue(parallelism);

            for ( var i=0; i < tableNames.length; ++i ) {
                q.defer(function(tableName, done){
                    var query = "SELECT * FROM CDB_Overviews('" + tableName + "');";
                    this.pgQueryRunner.run(username, query, handleOverviewsRows, function(err, table_metadata){
                        if (err) {
                            done(err);
                        } else {
                            metadata[tableName] = table_metadata;
                            done(null);
                        }
                    });
                }, tableNames[i]);
            }

            q.awaitAll(function(err){
              if (err) {
                  return callback(err);
              } else {
                  return callback(null, metadata);
              }
            });
        }
    });
};

function handleOverviewsRows(err, rows, callback) {
    if (err){
        var msg = err.message ? err.message : err;
        callback(new Error('could not get overviews metadata: ' + msg));
        return;
    }
    var metadata = {};
    for ( var i=0; i<rows.length; ++i ) {
      var row = rows[i];
      metadata[row.z] = { table: row.overview_table };
    }
    callback(null, metadata);
}

@@ -797,50 +797,45 @@
     
-for ( var i=0; i %3C tableNames.length; ++i 
+tableNames.forEach(function(tableName
 ) %7B%0A
@@ -871,19 +871,8 @@
 ion(
-tableName, 
 done
@@ -1355,23 +1355,8 @@
    %7D
-, tableNames%5Bi%5D
 );%0A 
@@ -1367,16 +1367,18 @@
        %7D
+);
 %0A%0A      

Refactor coding style