mirror of
https://github.com/openmax-server/server.git
synced 2026-05-22 19:41:41 +03:00
Fix OpenMAX mobile compatibility and Telegram auth fallback (#30)
* Fix OpenMAX mobile compatibility and Telegram auth fallback * Common: Убрал скобку в конфиге --------- Co-authored-by: Alexey Polyakov <starwear3000@mail.ru>
This commit is contained in:
committed by
GitHub
parent
d9cbafc4e3
commit
d9798a6fc6
69
src/main.py
69
src/main.py
@@ -12,6 +12,70 @@ 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():
|
||||
"""Инициализация базы данных"""
|
||||
|
||||
@@ -32,8 +96,9 @@ async def init_db():
|
||||
elif server_config.db_type == "sqlite":
|
||||
import aiosqlite
|
||||
|
||||
raw_db = await aiosqlite.connect(server_config.db_file)
|
||||
db["acquire"] = lambda: raw_db
|
||||
raw_db = await aiosqlite.connect(server_config.db_file, isolation_level=None)
|
||||
raw_db.row_factory = aiosqlite.Row
|
||||
db = SQLitePoolCompat(raw_db)
|
||||
|
||||
# Возвращаем
|
||||
return db
|
||||
|
||||
Reference in New Issue
Block a user