"use strict";
/**
 * Created by zhiyuan.huang@ddder.net.
 */
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var AbstractSqliteDriver_1 = require("../sqlite-abstract/AbstractSqliteDriver");
var DdderQueryRunner_1 = require("./DdderQueryRunner");
var DriverOptionNotSetError_1 = require("../../error/DriverOptionNotSetError");
var DriverPackageNotInstalledError_1 = require("../../error/DriverPackageNotInstalledError");
var DdderDriver = /** @class */ (function (_super) {
    tslib_1.__extends(DdderDriver, _super);
    // -------------------------------------------------------------------------
    // Constructor
    // -------------------------------------------------------------------------
    function DdderDriver(connection) {
        var _this = _super.call(this, connection) || this;
        _this.database = _this.options.database;
        // validate options to make sure everything is set
        if (!_this.options.database)
            throw new DriverOptionNotSetError_1.DriverOptionNotSetError("database");
        // load sqlite package
        _this.loadDependencies();
        return _this;
    }
    /**
     * Closes connection with database.
     */
    DdderDriver.prototype.disconnect = function () {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var _this = this;
            return tslib_1.__generator(this, function (_a) {
                return [2 /*return*/, new Promise(function (ok, fail) {
                        _this.queryRunner = undefined;
                        _this.databaseConnection.close().then(ok, fail);
                    })];
            });
        });
    };
    /**
     * Creates connection with the database.
     */
    DdderDriver.prototype.createDatabaseConnection = function () {
        var _this = this;
        return new Promise(function (ok, fail) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
            var databaseConnection, err_1, err_2;
            return tslib_1.__generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        _a.trys.push([0, 6, , 7]);
                        return [4 /*yield*/, this.sqlite.openDatabase(this.options.database)];
                    case 1:
                        databaseConnection = _a.sent();
                        _a.label = 2;
                    case 2:
                        _a.trys.push([2, 4, , 5]);
                        return [4 /*yield*/, databaseConnection.exec("PRAGMA foreign_keys = ON;")];
                    case 3:
                        _a.sent();
                        ok(databaseConnection);
                        return [3 /*break*/, 5];
                    case 4:
                        err_1 = _a.sent();
                        fail(err_1);
                        return [3 /*break*/, 5];
                    case 5: return [3 /*break*/, 7];
                    case 6:
                        err_2 = _a.sent();
                        fail(err_2);
                        return [3 /*break*/, 7];
                    case 7: return [2 /*return*/];
                }
            });
        }); });
    };
    /**
     * Creates a query runner used to execute database queries.
     */
    DdderDriver.prototype.createQueryRunner = function (mode) {
        if (mode === void 0) { mode = "master"; }
        if (!this.queryRunner)
            this.queryRunner = new DdderQueryRunner_1.DdderQueryRunner(this);
        return this.queryRunner;
    };
    /**
     * If driver dependency is not given explicitly, then try to load it via "require".
     */
    DdderDriver.prototype.loadDependencies = function () {
        try {
            this.sqlite = require("ddder-sqlite-storage");
        }
        catch (e) {
            throw new DriverPackageNotInstalledError_1.DriverPackageNotInstalledError("Ddder", "ddder-sqlite-storage");
        }
    };
    return DdderDriver;
}(AbstractSqliteDriver_1.AbstractSqliteDriver));
exports.DdderDriver = DdderDriver;

//# sourceMappingURL=DdderDriver.js.map
