Mercurial > games > semicongine
annotate semiconginev2/old/storage.nim @ 1220:59e683c4728f compiletime-tests
del: outdated examples
author | sam <sam@basx.dev> |
---|---|
date | Wed, 17 Jul 2024 21:04:09 +0700 |
parents | 56781cc0fc7c |
children |
rev | line source |
---|---|
984
4e487f06378b
add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff
changeset
|
1 import std/marshal |
988 | 2 import std/tables |
984
4e487f06378b
add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff
changeset
|
3 import std/strformat |
4e487f06378b
add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff
changeset
|
4 import std/paths |
4e487f06378b
add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff
changeset
|
5 import std/os |
4e487f06378b
add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff
changeset
|
6 |
1013 | 7 import ./thirdparty/db_connector/db_sqlite |
984
4e487f06378b
add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff
changeset
|
8 |
4e487f06378b
add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff
changeset
|
9 import ./core |
4e487f06378b
add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff
changeset
|
10 |
4e487f06378b
add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff
changeset
|
11 const STORAGE_NAME = Path("storage.db") |
1022
f888ac4825b8
did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents:
1013
diff
changeset
|
12 const DEFAULT_KEY_VALUE_TABLE_NAME = "shelf" |
984
4e487f06378b
add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff
changeset
|
13 |
4e487f06378b
add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff
changeset
|
14 type |
4e487f06378b
add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff
changeset
|
15 StorageType* = enum |
4e487f06378b
add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff
changeset
|
16 SystemStorage |
4e487f06378b
add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff
changeset
|
17 UserStorage |
4e487f06378b
add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff
changeset
|
18 # ? level storage type ? |
988 | 19 |
20 var db: Table[StorageType, DbConn] | |
21 | |
984
4e487f06378b
add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff
changeset
|
22 proc path(storageType: StorageType): Path = |
4e487f06378b
add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff
changeset
|
23 case storageType: |
4e487f06378b
add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff
changeset
|
24 of SystemStorage: |
4e487f06378b
add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff
changeset
|
25 Path(getAppDir()) / STORAGE_NAME |
4e487f06378b
add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff
changeset
|
26 of UserStorage: |
986 | 27 string(Path(getDataDir()) / Path(AppName())).createDir() |
984
4e487f06378b
add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff
changeset
|
28 Path(getDataDir()) / Path(AppName()) / STORAGE_NAME |
4e487f06378b
add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff
changeset
|
29 |
1022
f888ac4825b8
did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents:
1013
diff
changeset
|
30 proc ensureExists(storageType: StorageType) = |
988 | 31 if storageType in db: |
32 return | |
33 db[storageType] = open(string(storageType.path), "", "", "") | |
1022
f888ac4825b8
did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents:
1013
diff
changeset
|
34 |
f888ac4825b8
did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents:
1013
diff
changeset
|
35 proc ensureExists(storageType: StorageType, table: string) = |
f888ac4825b8
did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents:
1013
diff
changeset
|
36 storageType.ensureExists() |
f888ac4825b8
did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents:
1013
diff
changeset
|
37 db[storageType].exec(sql(&"""CREATE TABLE IF NOT EXISTS {table} ( |
984
4e487f06378b
add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff
changeset
|
38 key TEXT NOT NULL UNIQUE, |
985
0c02ca5de8e6
do: intermediate save before doing shit from scratch
sam <sam@basx.dev>
parents:
984
diff
changeset
|
39 value TEXT NOT NULL |
984
4e487f06378b
add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff
changeset
|
40 )""")) |
4e487f06378b
add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff
changeset
|
41 |
1138 | 42 proc Store*[T](storageType: StorageType, key: string, value: T, table = DEFAULT_KEY_VALUE_TABLE_NAME) = |
1022
f888ac4825b8
did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents:
1013
diff
changeset
|
43 storageType.ensureExists(table) |
f888ac4825b8
did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents:
1013
diff
changeset
|
44 db[storageType].exec(sql(&"""INSERT INTO {table} VALUES(?, ?) |
984
4e487f06378b
add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff
changeset
|
45 ON CONFLICT(key) DO UPDATE SET value=excluded.value |
4e487f06378b
add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff
changeset
|
46 """), key, $$value) |
4e487f06378b
add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff
changeset
|
47 |
1138 | 48 proc Load*[T](storageType: StorageType, key: string, default: T, table = DEFAULT_KEY_VALUE_TABLE_NAME): T = |
1022
f888ac4825b8
did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents:
1013
diff
changeset
|
49 storageType.ensureExists(table) |
f888ac4825b8
did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents:
1013
diff
changeset
|
50 let dbResult = db[storageType].getValue(sql(&"""SELECT value FROM {table} WHERE key = ? """), key) |
984
4e487f06378b
add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff
changeset
|
51 if dbResult == "": |
4e487f06378b
add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff
changeset
|
52 return default |
4e487f06378b
add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff
changeset
|
53 return to[T](dbResult) |
4e487f06378b
add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff
changeset
|
54 |
1138 | 55 proc List*[T](storageType: StorageType, table = DEFAULT_KEY_VALUE_TABLE_NAME): seq[string] = |
1022
f888ac4825b8
did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents:
1013
diff
changeset
|
56 storageType.ensureExists(table) |
f888ac4825b8
did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents:
1013
diff
changeset
|
57 for row in db[storageType].fastRows(sql(&"""SELECT key FROM {table}""")): |
f888ac4825b8
did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents:
1013
diff
changeset
|
58 result.add row[0] |
f888ac4825b8
did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents:
1013
diff
changeset
|
59 |
1138 | 60 proc Purge*(storageType: StorageType) = |
988 | 61 storageType.path().string.removeFile() |