Mercurial > games > semicongine
diff semiconginev2/old/storage.nim @ 1218:56781cc0fc7c compiletime-tests
did: renamge main package
author | sam <sam@basx.dev> |
---|---|
date | Wed, 17 Jul 2024 21:01:37 +0700 |
parents | semicongine/old/storage.nim@a3eb305bcac2 |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/semiconginev2/old/storage.nim Wed Jul 17 21:01:37 2024 +0700 @@ -0,0 +1,61 @@ +import std/marshal +import std/tables +import std/strformat +import std/paths +import std/os + +import ./thirdparty/db_connector/db_sqlite + +import ./core + +const STORAGE_NAME = Path("storage.db") +const DEFAULT_KEY_VALUE_TABLE_NAME = "shelf" + +type + StorageType* = enum + SystemStorage + UserStorage + # ? level storage type ? + +var db: Table[StorageType, DbConn] + +proc path(storageType: StorageType): Path = + case storageType: + of SystemStorage: + Path(getAppDir()) / STORAGE_NAME + of UserStorage: + string(Path(getDataDir()) / Path(AppName())).createDir() + Path(getDataDir()) / Path(AppName()) / STORAGE_NAME + +proc ensureExists(storageType: StorageType) = + if storageType in db: + return + db[storageType] = open(string(storageType.path), "", "", "") + +proc ensureExists(storageType: StorageType, table: string) = + storageType.ensureExists() + db[storageType].exec(sql(&"""CREATE TABLE IF NOT EXISTS {table} ( + key TEXT NOT NULL UNIQUE, + value TEXT NOT NULL + )""")) + +proc Store*[T](storageType: StorageType, key: string, value: T, table = DEFAULT_KEY_VALUE_TABLE_NAME) = + storageType.ensureExists(table) + db[storageType].exec(sql(&"""INSERT INTO {table} VALUES(?, ?) + ON CONFLICT(key) DO UPDATE SET value=excluded.value + """), key, $$value) + +proc Load*[T](storageType: StorageType, key: string, default: T, table = DEFAULT_KEY_VALUE_TABLE_NAME): T = + storageType.ensureExists(table) + let dbResult = db[storageType].getValue(sql(&"""SELECT value FROM {table} WHERE key = ? """), key) + if dbResult == "": + return default + return to[T](dbResult) + +proc List*[T](storageType: StorageType, table = DEFAULT_KEY_VALUE_TABLE_NAME): seq[string] = + storageType.ensureExists(table) + for row in db[storageType].fastRows(sql(&"""SELECT key FROM {table}""")): + result.add row[0] + +proc Purge*(storageType: StorageType) = + storageType.path().string.removeFile()