From 4d51c70f8e0a7d013eb2fc86bb3225477f69eed4 Mon Sep 17 00:00:00 2001 From: Alexey Polyakov Date: Fri, 8 May 2026 15:27:05 +0300 Subject: [PATCH] =?UTF-8?q?=D0=92=D1=8B=D0=BD=D0=B5=D1=81=20sqlite=20?= =?UTF-8?q?=D0=B2=20=D0=BE=D1=82=D0=B4=D0=B5=D0=BB=D1=8C=D0=BD=D1=8B=D0=B9?= =?UTF-8?q?=20=D0=BC=D0=BE=D0=B4=D1=83=D0=BB=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/sqlite.py | 60 +++++++++++++++++++++++++++++++++++++++ src/main.py | 67 +------------------------------------------- 2 files changed, 61 insertions(+), 66 deletions(-) create mode 100644 src/common/sqlite.py diff --git a/src/common/sqlite.py b/src/common/sqlite.py new file mode 100644 index 0000000..71ef9e6 --- /dev/null +++ b/src/common/sqlite.py @@ -0,0 +1,60 @@ +class SQLiteCursorCompat: + def __init__(self, connection): + self.connection = connection + self.cursor = None + + async def __aenter__(self): + self.cursor = await self.connection.cursor() + return self + + async def __aexit__(self, exc_type, exc, tb): + if self.cursor is not None: + await self.cursor.close() + self.cursor = None + + @property + def lastrowid(self): + return None if self.cursor is None else self.cursor.lastrowid + + def _normalize_query(self, query): + return query.replace("%s", "?").replace( + "UNIX_TIMESTAMP()", "CAST(strftime('%s','now') AS INTEGER)" + ) + + async def execute(self, query, params=()): + normalized_query = self._normalize_query(query) + if params is None: + params = () + elif not isinstance(params, (tuple, list, dict)): + params = (params,) + await self.cursor.execute(normalized_query, params) + + async def fetchone(self): + row = await self.cursor.fetchone() + if row is None: + return None + return dict(row) + + async def fetchall(self): + rows = await self.cursor.fetchall() + return [dict(row) for row in rows] + +class SQLiteConnectionCompat: + def __init__(self, connection): + self.connection = connection + + async def __aenter__(self): + return self + + async def __aexit__(self, exc_type, exc, tb): + return False + + def cursor(self): + return SQLiteCursorCompat(self.connection) + +class SQLitePoolCompat: + def __init__(self, connection): + self.connection = connection + + def acquire(self): + return SQLiteConnectionCompat(self.connection) diff --git a/src/main.py b/src/main.py index 448fc4a..e927428 100644 --- a/src/main.py +++ b/src/main.py @@ -8,6 +8,7 @@ import traceback from common.config import ServerConfig from common.push import PushService +from common.sqlite import SQLitePoolCompat from oneme.controller import OnemeController from tamtam.controller import TTController from telegrambot.controller import TelegramBotController @@ -15,71 +16,6 @@ from telegrambot.controller import TelegramBotController # Конфиг сервера server_config = ServerConfig() - -class SQLiteCursorCompat: - def __init__(self, connection): - self.connection = connection - self.cursor = None - - async def __aenter__(self): - self.cursor = await self.connection.cursor() - return self - - async def __aexit__(self, exc_type, exc, tb): - if self.cursor is not None: - await self.cursor.close() - self.cursor = None - - @property - def lastrowid(self): - return None if self.cursor is None else self.cursor.lastrowid - - def _normalize_query(self, query): - return query.replace("%s", "?").replace( - "UNIX_TIMESTAMP()", "CAST(strftime('%s','now') AS INTEGER)" - ) - - async def execute(self, query, params=()): - normalized_query = self._normalize_query(query) - if params is None: - params = () - elif not isinstance(params, (tuple, list, dict)): - params = (params,) - await self.cursor.execute(normalized_query, params) - - async def fetchone(self): - row = await self.cursor.fetchone() - if row is None: - return None - return dict(row) - - async def fetchall(self): - rows = await self.cursor.fetchall() - return [dict(row) for row in rows] - - -class SQLiteConnectionCompat: - def __init__(self, connection): - self.connection = connection - - async def __aenter__(self): - return self - - async def __aexit__(self, exc_type, exc, tb): - return False - - def cursor(self): - return SQLiteCursorCompat(self.connection) - - -class SQLitePoolCompat: - def __init__(self, connection): - self.connection = connection - - def acquire(self): - return SQLiteConnectionCompat(self.connection) - - async def init_db(): """Инициализация базы данных""" @@ -117,7 +53,6 @@ def init_ssl(): # Возвращаем return ssl_context - def set_logging(): """Настройка уровня логирования""" # Настройка уровня логирования