'use strict';

var Promise = require('bluebird');
var _ = require('lodash');
var tracker = require('../../../tracker');

var connection = {
  id : 'mockedConnection',
};

function mockClient(client) {
  client.driverName = 'mocked';

  client.acquireConnection = Promise.method(function() {
    return connection;
  });

  client.releaseConnection = Promise.method(_.noop);

  client.acquireRawConnection = Promise.method(function() {
    return {};
  });

  client.destroyRawConnection = function destroyRawConnection(connection, cb) {
    cb();
  };

  client.constructor.prototype._query = client._query = function(connection, obj) {
    return new Promise(function(resolver, rejecter) {
      tracker.queries.track(obj, resolver);
    });
  };

  client.constructor.prototype.processResponse = client.processResponse = function(obj) {
    obj = obj || {};
    
    if (obj.output) {
      obj.result = obj.output.call(this, obj.result);
    } else if (obj.method === 'first') {
      obj.result = Array.isArray(obj.result) ? obj.result[0] : obj.result;
    } else if (obj.method === 'pluck') {
      obj.result = _.pluck(obj.result, obj.pluck);
    }

    return obj.result;
  };
}

function mockRunner(Runner) {
  Runner.prototype = _.omit(Runner.prototype, 'connection');

  Runner.prototype.ensureConnection = function() {
    return Promise.resolve(this.connection || {});
  };

  Object.defineProperty(Runner.prototype, "connection", {
    get : function () {
      return connection;
    },
    set : function (val) {}
  });
}

module.exports = function (db) {
  mockClient(db.client);
  mockRunner(db.client.Runner);
};

@@ -626,24 +626,135 @@
 ion, obj) %7B%0A
+    var unbildQuery = %7B%0A      single: this.builder._single,%0A      statements: this.builder._statements%0A    %7D;%0A%0A
     return n
@@ -823,19 +823,57 @@
 s.track(
-obj
+_.extend(obj, %7BunbildQuery: unbildQuery%7D)
 , resolv
@@ -1006,13 +1006,9 @@
 %7B%7D;%0A
-    
 %0A
+
     
@@ -1355,16 +1355,16 @@
 nner) %7B%0A
-
   Runner
@@ -1417,16 +1417,167 @@
 ion');%0A%0A
+  var oldRun = Runner.prototype.run;%0A%0A  Runner.prototype.run = function()%7B%0A    this.client.builder = this.builder;%0A    return oldRun.call(this);%0A  %7D;%0A%0A
   Runner

Add unbuildQuery in query object for 0.8 adapter