2121
2222import base64
2323import logging
24+ import sqlite3
2425import struct
2526import time
2627from pathlib import Path
2728from typing import Any
2829
29- import aiosqlite
30-
3130from hydrogram import raw , utils
3231
3332from .base import BaseStorage , InputPeer
@@ -105,7 +104,7 @@ def __init__(
105104 else (workdir / (self .name + self .FILE_EXTENSION ) if workdir else ":memory:" )
106105 )
107106 self .session_string : str | None = session_string
108- self .conn : aiosqlite .Connection | None = None
107+ self .conn : sqlite3 .Connection | None = None
109108
110109 async def update (self ) -> None :
111110 if not self .conn :
@@ -115,47 +114,47 @@ async def update(self) -> None:
115114 version : int | None = await self .version ()
116115
117116 if version == 1 :
118- await self .conn .execute ("DELETE FROM peers" )
117+ self .conn .execute ("DELETE FROM peers" )
119118 version += 1
120119
121120 if version == 2 :
122- await self .conn .execute ("ALTER TABLE sessions ADD api_id INTEGER" )
121+ self .conn .execute ("ALTER TABLE sessions ADD api_id INTEGER" )
123122 version += 1
124123
125124 await self .version (version )
126- await self .conn .commit ()
125+ self .conn .commit ()
127126
128127 async def create (self ) -> None :
129128 if not self .conn :
130129 logging .warning ("Database connection is not available." )
131130 return
132131
133- await self .conn .executescript (SCHEMA )
134- await self .conn .execute (
132+ self .conn .executescript (SCHEMA )
133+ self .conn .execute (
135134 "INSERT INTO version VALUES (?)" ,
136135 (self .VERSION ,),
137136 )
138- await self .conn .execute (
137+ self .conn .execute (
139138 "INSERT INTO sessions VALUES (?, ?, ?, ?, ?, ?, ?)" ,
140139 (2 , None , None , None , 0 , None , None ),
141140 )
142- await self .conn .commit ()
141+ self .conn .commit ()
143142
144143 async def open (self ) -> None :
145144 path = self .database
146145 file_exists = isinstance (path , Path ) and path .is_file ()
147146
148- self .conn = await aiosqlite .connect (self .database )
147+ self .conn = sqlite3 .connect (self .database )
149148
150- await self .conn .execute ("PRAGMA journal_mode=WAL" )
149+ self .conn .execute ("PRAGMA journal_mode=WAL" )
151150
152151 if file_exists :
153152 await self .update ()
154- await self .conn .execute ("VACUUM" )
153+ self .conn .execute ("VACUUM" )
155154 else :
156155 await self .create ()
157156
158- await self .conn .commit ()
157+ self .conn .commit ()
159158
160159 if self .session_string :
161160 await self ._load_session_string ()
@@ -187,11 +186,11 @@ async def save(self) -> None:
187186 return
188187
189188 await self .date (int (time .time ()))
190- await self .conn .commit ()
189+ self .conn .commit ()
191190
192191 async def close (self ) -> None :
193192 if self .conn :
194- await self .conn .close ()
193+ self .conn .close ()
195194
196195 async def delete (self ) -> None :
197196 if self .database != ":memory:" :
@@ -204,22 +203,22 @@ async def update_peers(
204203 logging .warning ("Database connection is not available." )
205204 return
206205
207- await self .conn .executemany (
206+ self .conn .executemany (
208207 "REPLACE INTO peers (id, access_hash, type, username, phone_number) "
209208 "VALUES (?, ?, ?, ?, ?)" ,
210209 peers ,
211210 )
212- await self .conn .commit ()
211+ self .conn .commit ()
213212
214213 async def get_peer_by_id (self , peer_id : int ) -> InputPeer | None :
215214 if not self .conn :
216215 logging .warning ("Database connection is not available." )
217216 return None
218217
219- q = await self .conn .execute (
218+ q = self .conn .execute (
220219 "SELECT id, access_hash, type FROM peers WHERE id = ?" , (peer_id ,)
221220 )
222- r = await q .fetchone ()
221+ r = q .fetchone ()
223222 if not r :
224223 raise KeyError (f"ID not found: { peer_id } " )
225224
@@ -230,14 +229,14 @@ async def get_peer_by_username(self, username: str) -> InputPeer | None:
230229 logging .warning ("Database connection is not available." )
231230 return None
232231
233- q = await self .conn .execute (
232+ q = self .conn .execute (
234233 "SELECT id, access_hash, type, last_update_on "
235234 "FROM peers "
236235 "WHERE username = ? "
237236 "ORDER BY last_update_on DESC" ,
238237 (username ,),
239238 )
240- r = await q .fetchone ()
239+ r = q .fetchone ()
241240 if not r :
242241 raise KeyError (f"Username not found: { username } " )
243242
@@ -251,10 +250,10 @@ async def get_peer_by_phone_number(self, phone_number: str) -> InputPeer | None:
251250 logging .warning ("Database connection is not available." )
252251 return None
253252
254- q = await self .conn .execute (
253+ q = self .conn .execute (
255254 "SELECT id, access_hash, type FROM peers WHERE phone_number = ?" , (phone_number ,)
256255 )
257- r = await q .fetchone ()
256+ r = q .fetchone ()
258257 if not r :
259258 raise KeyError (f"Phone number not found: { phone_number } " )
260259
@@ -265,17 +264,17 @@ async def _get(self, attr: str) -> Any:
265264 logging .warning ("Database connection is not available." )
266265 return None
267266
268- q = await self .conn .execute (f"SELECT { attr } FROM sessions" )
269- row = await q .fetchone ()
267+ q = self .conn .execute (f"SELECT { attr } FROM sessions" )
268+ row = q .fetchone ()
270269 return row [0 ] if row else None
271270
272271 async def _set (self , attr : str , value : Any ) -> None :
273272 if not self .conn :
274273 logging .warning ("Database connection is not available." )
275274 return
276275
277- await self .conn .execute (f"UPDATE sessions SET { attr } = ?" , (value ,))
278- await self .conn .commit ()
276+ self .conn .execute (f"UPDATE sessions SET { attr } = ?" , (value ,))
277+ self .conn .commit ()
279278
280279 async def _accessor (self , attr : str , value : Any = object ) -> Any | None :
281280 if not self .conn :
@@ -315,10 +314,10 @@ async def version(self, value: int | object = object) -> int | None:
315314 return None
316315
317316 if value is object :
318- q = await self .conn .execute ("SELECT number FROM version" )
319- row = await q .fetchone ()
317+ q = self .conn .execute ("SELECT number FROM version" )
318+ row = q .fetchone ()
320319 return row [0 ] if row else None
321320
322- await self .conn .execute ("UPDATE version SET number = ?" , (value ,))
323- await self .conn .commit ()
321+ self .conn .execute ("UPDATE version SET number = ?" , (value ,))
322+ self .conn .commit ()
324323 return None
0 commit comments