"use strict";
/**
 * Created by zhiyuan.huang@ddder.net.
 */
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var QueryRunnerAlreadyReleasedError_1 = require("../../error/QueryRunnerAlreadyReleasedError");
var QueryFailedError_1 = require("../../error/QueryFailedError");
var AbstractSqliteQueryRunner_1 = require("../sqlite-abstract/AbstractSqliteQueryRunner");
var Broadcaster_1 = require("../../subscriber/Broadcaster");
var DdderQueryRunner = /** @class */ (function (_super) {
    tslib_1.__extends(DdderQueryRunner, _super);
    function DdderQueryRunner(driver) {
        var _this = _super.call(this) || this;
        _this.driver = driver;
        _this.connection = driver.connection;
        _this.broadcaster = new Broadcaster_1.Broadcaster(_this);
        return _this;
    }
    /**
     * Executes a given SQL query.
     * */
    DdderQueryRunner.prototype.query = function (query, parameters) {
        var _this = this;
        if (this.isReleased)
            throw new QueryRunnerAlreadyReleasedError_1.QueryRunnerAlreadyReleasedError();
        var connection = this.driver.connection;
        return new Promise(function (ok, fail) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
            var isInsertQuery, isSelectQuery, handler, databaseConnection, queryStartTime;
            return tslib_1.__generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        handler = function (err, result) {
                            // log slow queries if maxQueryExecution time is set
                            var maxQueryExecutionTime = connection.options.maxQueryExecutionTime;
                            var queryEndTime = +new Date();
                            var queryExecutionTime = queryEndTime - queryStartTime;
                            if (maxQueryExecutionTime && queryExecutionTime > maxQueryExecutionTime)
                                connection.logger.logQuerySlow(queryExecutionTime, query, parameters, this);
                            if (err) {
                                connection.logger.logQueryError(err, query, parameters, this);
                                fail(new QueryFailedError_1.QueryFailedError(query, parameters, err));
                            }
                            else {
                                ok(isInsertQuery ? result[1] : result);
                            }
                        };
                        return [4 /*yield*/, this.connect()];
                    case 1:
                        databaseConnection = _a.sent();
                        this.driver.connection.logger.logQuery(query, parameters, this);
                        queryStartTime = +new Date();
                        isInsertQuery = query.substr(0, 11) === "INSERT INTO";
                        isSelectQuery = query.substr(0, 6) === "SELECT";
                        if (isSelectQuery) {
                            databaseConnection.fetch(query, parameters).then(function (result) {
                                handler(undefined, result);
                            }, function (err) {
                                handler(err, undefined);
                            });
                        }
                        else {
                            databaseConnection.exec(query, parameters).then(function (result) {
                                handler(undefined, result);
                            }, function (err) {
                                handler(err, undefined);
                            });
                        }
                        return [2 /*return*/];
                }
            });
        }); });
    };
    return DdderQueryRunner;
}(AbstractSqliteQueryRunner_1.AbstractSqliteQueryRunner));
exports.DdderQueryRunner = DdderQueryRunner;

//# sourceMappingURL=DdderQueryRunner.js.map
