"use strict";
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module Workspace
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.WorkspaceSqliteDb = void 0;
const SQLiteDb_1 = require("../../SQLiteDb");
const Symbols_1 = require("../Symbols");
const WorkspaceImpl_1 = require("./WorkspaceImpl");
class WorkspaceSqliteDb extends SQLiteDb_1.VersionedSqliteDb {
    myVersion = "1.0.0";
    getRequiredVersions() {
        try {
            return super.getRequiredVersions();
        }
        catch {
            // early versions didn't have a version range, but they're fine
            return { readVersion: "^1", writeVersion: "^1" };
        }
    }
    createDDL(args) {
        const timeStampCol = "lastMod TIMESTAMP NOT NULL DEFAULT(julianday('now'))";
        this.executeSQL(`CREATE TABLE strings(id TEXT PRIMARY KEY NOT NULL,value TEXT,${timeStampCol})`);
        this.executeSQL(`CREATE TABLE blobs(id TEXT PRIMARY KEY NOT NULL,value BLOB,${timeStampCol})`);
        const createTrigger = (tableName) => {
            this.executeSQL(`CREATE TRIGGER ${tableName}_timeStamp AFTER UPDATE ON ${tableName} WHEN old.lastMod=new.lastMod AND old.lastMod != julianday('now') BEGIN UPDATE ${tableName} SET lastMod=julianday('now') WHERE id=new.id; END`);
        };
        createTrigger("strings");
        createTrigger("blobs");
        if (args?.manifest)
            this[Symbols_1._nativeDb].saveFileProperty(WorkspaceImpl_1.workspaceManifestProperty, JSON.stringify(args.manifest));
    }
}
exports.WorkspaceSqliteDb = WorkspaceSqliteDb;
//# sourceMappingURL=WorkspaceSqliteDb.js.map