Skip to content

Commit c139c1a

Browse files
committed
1. add key_type arg to sqlite.KV, to support int key.
1 parent fc533bd commit c139c1a

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
### 1.2.4 (2025-03-08)
2+
1. add `key_type` arg to `sqlite.KV`, to support `int` key.
3+
14
### 1.2.3 (2025-03-07)
2-
1. add `sqlite.KV`: A key-value store using sqlite3. Light-weight `sqlitedict`.
5+
1. add `sqlite.KV`: A key-value store using sqlite3, lightweight `sqlitedict`.
36

47
### 1.2.2 (2025-03-04)
58
1. Change the default alphabet in `utils.base_encode` to "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", which follows the order of ASCII characters.

morebuiltins/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "1.2.3"
1+
__version__ = "1.2.4"
22
__all__ = [
33
"morebuiltins.utils",
44
"morebuiltins.date",

morebuiltins/sqlite.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def __init__(
2222
pickle.dumps,
2323
pickle.loads,
2424
),
25+
key_type="TEXT",
2526
**kwargs,
2627
):
2728
"""A key-value store using sqlite3.
@@ -37,6 +38,7 @@ def __init__(
3738
database (str, optional): database file path. Defaults to ":memory:".
3839
table (str, optional): table name. Defaults to "table1".
3940
encoder (typing.Tuple[str, typing.Optional[typing.Callable], typing.Optional[typing.Callable]], optional): encoder for the value. Defaults to ("BLOB", pickle.dumps, pickle.loads).
41+
key_type (str, optional): key type. Defaults to "TEXT".
4042
4143
Examples:
4244
@@ -91,11 +93,39 @@ def __init__(
9193
1
9294
>>> kv.count("k")
9395
1
96+
>>> # test int key
97+
>>> kv = KV(key_type="INTEGER")
98+
>>> kv.set(1, "v1")
99+
1
100+
>>> kv.get(1)
101+
'v1'
102+
>>> kv.delete(1)
103+
1
104+
>>> _ = [kv.set(i, i) for i in range(10000)]
105+
>>> kv.count()
106+
10000
107+
>>> kv[5000]
108+
5000
109+
>>> # test json value
110+
>>> import json
111+
>>> kv = KV(encoder=("TEXT", json.dumps, json.loads))
112+
>>> kv.set("k1", {"k": "v"})
113+
1
114+
>>> kv.get("k1")
115+
{'k': 'v'}
116+
>>> # test zlib compress value
117+
>>> import zlib
118+
>>> kv = KV(encoder=("BLOB", zlib.compress, zlib.decompress))
119+
>>> kv.set("k1", b"v1")
120+
1
121+
>>> kv.get("k1")
122+
b'v1'
94123
"""
95124
self.database, self.table = (database, table)
96125
self.value_type, encoder_v, decoder_v = encoder
97126
self.encoder_v = encoder_v or self.nothing
98127
self.decoder_v = decoder_v or self.nothing
128+
self.key_type = key_type
99129
self.non_commit_ctx = nullcontext()
100130
self.init_conn(**kwargs)
101131
self.column_handlers = {
@@ -112,7 +142,7 @@ def init_conn(self, **kwargs):
112142
self.conn = sqlite3.connect(self.database, **kwargs)
113143
with self.conn:
114144
self.conn.execute(
115-
f"CREATE TABLE IF NOT EXISTS `{self.table}` (k TEXT PRIMARY KEY, v {self.value_type})",
145+
f"CREATE TABLE IF NOT EXISTS `{self.table}` (k {self.key_type} PRIMARY KEY, v {self.value_type})",
116146
)
117147

118148
def transaction_ctx(self, commit=True):

0 commit comments

Comments
 (0)