annotate semiconginev2/storage.nim @ 1226:c8e3037aca66 compiletime-tests

add: contrib stuff
author sam <sam@basx.dev>
date Wed, 17 Jul 2024 23:41:51 +0700
parents 56781cc0fc7c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1199
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
1 import ./thirdparty/db_connector/db_sqlite
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
2
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
3 const STORAGE_NAME = Path("storage.db")
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
4 const DEFAULT_KEY_VALUE_TABLE_NAME = "shelf"
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
5
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
6 type
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
7 StorageType* = enum
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
8 SystemStorage
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
9 UserStorage
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
10 # ? level storage type ?
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
11
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
12 var db: Table[StorageType, DbConn]
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
13
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
14 proc path(storageType: StorageType): Path =
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
15 case storageType:
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
16 of SystemStorage:
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
17 Path(getAppDir()) / STORAGE_NAME
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
18 of UserStorage:
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
19 string(Path(getDataDir()) / Path(AppName())).createDir()
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
20 Path(getDataDir()) / Path(AppName()) / STORAGE_NAME
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
21
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
22 proc ensureExists(storageType: StorageType) =
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
23 if storageType in db:
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
24 return
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
25 db[storageType] = open(string(storageType.path), "", "", "")
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
26
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
27 proc ensureExists(storageType: StorageType, table: string) =
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
28 storageType.ensureExists()
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
29 db[storageType].exec(sql(&"""CREATE TABLE IF NOT EXISTS {table} (
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
30 key TEXT NOT NULL UNIQUE,
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
31 value TEXT NOT NULL
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
32 )"""))
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
33
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
34 proc Store*[T](storageType: StorageType, key: string, value: T, table = DEFAULT_KEY_VALUE_TABLE_NAME) =
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
35 storageType.ensureExists(table)
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
36 db[storageType].exec(sql(&"""INSERT INTO {table} VALUES(?, ?)
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
37 ON CONFLICT(key) DO UPDATE SET value=excluded.value
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
38 """), key, $$value)
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
39
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
40 proc Load*[T](storageType: StorageType, key: string, default: T, table = DEFAULT_KEY_VALUE_TABLE_NAME): T =
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
41 storageType.ensureExists(table)
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
42 let dbResult = db[storageType].getValue(sql(&"""SELECT value FROM {table} WHERE key = ? """), key)
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
43 if dbResult == "":
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
44 return default
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
45 return to[T](dbResult)
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
46
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
47 proc List*[T](storageType: StorageType, table = DEFAULT_KEY_VALUE_TABLE_NAME): seq[string] =
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
48 storageType.ensureExists(table)
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
49 for row in db[storageType].fastRows(sql(&"""SELECT key FROM {table}""")):
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
50 result.add row[0]
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
51
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
52 proc Purge*(storageType: StorageType) =
ba1af13233ee add: working main-renderloop
sam <sam@basx.dev>
parents:
diff changeset
53 storageType.path().string.removeFile()