"use strict";
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const Sqlite = __importStar(require("sqlite3"));
class DB {
    open(path) {
        return new Promise((resolve, reject) => {
            this.db = new (Sqlite.verbose()).Database(path, Sqlite.OPEN_READWRITE, error => {
                if (error) {
                    reject(error);
                }
                else {
                    resolve();
                }
            });
        });
    }
    close() {
        return new Promise((resolve, reject) => {
            var _a;
            (_a = this.db) === null || _a === void 0 ? void 0 : _a.close(error => {
                if (error) {
                    reject(error);
                }
                else {
                    resolve();
                }
            });
        });
    }
    update(sql, values) {
        return new Promise((resolve, reject) => {
            var _a;
            (_a = this.db) === null || _a === void 0 ? void 0 : _a.serialize(() => {
                var _a, _b;
                if (values) {
                    const stmt = (_a = this.db) === null || _a === void 0 ? void 0 : _a.prepare(sql);
                    stmt === null || stmt === void 0 ? void 0 : stmt.run(...values);
                    stmt === null || stmt === void 0 ? void 0 : stmt.finalize();
                    resolve();
                }
                else {
                    (_b = this.db) === null || _b === void 0 ? void 0 : _b.run(sql);
                    resolve();
                }
            });
        });
    }
    select(sql, values) {
        return new Promise((resolve, reject) => {
            var _a;
            (_a = this.db) === null || _a === void 0 ? void 0 : _a.serialize(() => {
                var _a, _b;
                if (values) {
                    (_a = this.db) === null || _a === void 0 ? void 0 : _a.all(sql, values, (err, rows) => {
                        if (err) {
                            reject(err);
                        }
                        else {
                            resolve(rows);
                        }
                    });
                }
                else {
                    (_b = this.db) === null || _b === void 0 ? void 0 : _b.all(sql, (err, rows) => {
                        if (err) {
                            reject(err);
                        }
                        else {
                            resolve(rows);
                        }
                    });
                }
            });
        });
    }
}
exports.DB = DB;
//# sourceMappingURL=db.js.map