"use strict";

var now = Date.now;
var CircleQueue = require("../../misc/CircleQueue.js");

function TransCommandQueue(mainQueue, object) 


TransCommandQueue.prototype = {
	__name__ : "TransCommandQueue",
	"__proto__" : CircleQueue.prototype,
	constructor : TransCommandQueue,

	isClosed : false,
	isExecuted : false,

	execute : function () {
		if (this.isExecuted || !this.length || this.isClosed) {
			return;
		}

		var self = this;
		var task = this.shift();

		this.object.query(task.sql, task.options, function (err, result) {
			self.isExecuted = false;
			task.callback(err, result);
			self.execute();
		});

		this.isExecuted = true;
	},

	destroy : function () {
		if (this.isClosed) {
			return;
		}

		this.isClosed = true;

		this.clear();
		this.mainQueue.pool.returnObject(this.object);
		this.mainQueue.execute();
	}
};

module.exports = TransCommandQueue;
{
	CircleQueue.call(this);

	this.object = object;
	this.mainQueue = mainQueue;
}