Spaces:
Sleeping
Sleeping
Update db_utils.py
Browse files- db_utils.py +21 -23
db_utils.py
CHANGED
@@ -1,52 +1,50 @@
|
|
1 |
# db_utils.py
|
|
|
2 |
import aiosqlite
|
3 |
import config
|
4 |
|
5 |
async def initialize_database():
|
6 |
async with aiosqlite.connect(config.DATABASE_NAME) as db:
|
7 |
-
await db.execute(
|
8 |
-
CREATE TABLE IF NOT EXISTS
|
9 |
short_id TEXT PRIMARY KEY,
|
10 |
file_id TEXT,
|
11 |
filename TEXT,
|
12 |
type TEXT,
|
13 |
size INTEGER
|
14 |
)
|
15 |
-
|
16 |
-
await db.execute(
|
17 |
CREATE TABLE IF NOT EXISTS users (
|
18 |
user_id INTEGER PRIMARY KEY,
|
19 |
username TEXT,
|
20 |
first_name TEXT
|
21 |
)
|
22 |
-
|
23 |
await db.commit()
|
24 |
|
25 |
-
async def
|
26 |
async with aiosqlite.connect(config.DATABASE_NAME) as db:
|
27 |
-
await db.execute(
|
28 |
-
INSERT OR REPLACE INTO
|
29 |
-
VALUES (?, ?,
|
30 |
-
|
31 |
await db.commit()
|
32 |
|
33 |
async def get_cached_file(short_id):
|
34 |
async with aiosqlite.connect(config.DATABASE_NAME) as db:
|
35 |
-
async with db.execute(
|
|
|
|
|
36 |
row = await cursor.fetchone()
|
37 |
if row:
|
38 |
-
return {
|
39 |
-
|
40 |
-
"name": row[1],
|
41 |
-
"type": row[2],
|
42 |
-
"size": row[3]
|
43 |
-
}
|
44 |
-
return None
|
45 |
|
46 |
-
async def
|
47 |
async with aiosqlite.connect(config.DATABASE_NAME) as db:
|
48 |
-
await db.execute(
|
49 |
-
INSERT OR REPLACE INTO
|
50 |
-
VALUES (?, ?, ?)
|
51 |
-
|
52 |
await db.commit()
|
|
|
1 |
# db_utils.py
|
2 |
+
|
3 |
import aiosqlite
|
4 |
import config
|
5 |
|
6 |
async def initialize_database():
|
7 |
async with aiosqlite.connect(config.DATABASE_NAME) as db:
|
8 |
+
await db.execute("""
|
9 |
+
CREATE TABLE IF NOT EXISTS cache (
|
10 |
short_id TEXT PRIMARY KEY,
|
11 |
file_id TEXT,
|
12 |
filename TEXT,
|
13 |
type TEXT,
|
14 |
size INTEGER
|
15 |
)
|
16 |
+
""")
|
17 |
+
await db.execute("""
|
18 |
CREATE TABLE IF NOT EXISTS users (
|
19 |
user_id INTEGER PRIMARY KEY,
|
20 |
username TEXT,
|
21 |
first_name TEXT
|
22 |
)
|
23 |
+
""")
|
24 |
await db.commit()
|
25 |
|
26 |
+
async def add_or_update_user_db(user_id, username, first_name):
|
27 |
async with aiosqlite.connect(config.DATABASE_NAME) as db:
|
28 |
+
await db.execute("""
|
29 |
+
INSERT OR REPLACE INTO users (user_id, username, first_name)
|
30 |
+
VALUES (?, ?, ?)
|
31 |
+
""", (user_id, username, first_name))
|
32 |
await db.commit()
|
33 |
|
34 |
async def get_cached_file(short_id):
|
35 |
async with aiosqlite.connect(config.DATABASE_NAME) as db:
|
36 |
+
async with db.execute("""
|
37 |
+
SELECT file_id, filename, type, size FROM cache WHERE short_id = ?
|
38 |
+
""", (short_id,)) as cursor:
|
39 |
row = await cursor.fetchone()
|
40 |
if row:
|
41 |
+
return {"file_id": row[0], "name": row[1], "type": row[2], "size": row[3]}
|
42 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
+
async def add_to_cache(short_id, file_id, filename, filetype, size):
|
45 |
async with aiosqlite.connect(config.DATABASE_NAME) as db:
|
46 |
+
await db.execute("""
|
47 |
+
INSERT OR REPLACE INTO cache (short_id, file_id, filename, type, size)
|
48 |
+
VALUES (?, ?, ?, ?, ?)
|
49 |
+
""", (short_id, file_id, filename, filetype, size))
|
50 |
await db.commit()
|