Включена опция autocommit.
This commit is contained in:
parent
0111ddecca
commit
bb16256ecc
1 changed files with 1 additions and 17 deletions
18
database.py
18
database.py
|
|
@ -4,7 +4,7 @@ from typing import List, Union
|
||||||
|
|
||||||
class BasicDatabase:
|
class BasicDatabase:
|
||||||
def __init__(self, connection_string: str):
|
def __init__(self, connection_string: str):
|
||||||
self.conn = connect(connection_string)
|
self.conn = connect(connection_string, autocommit=True)
|
||||||
self.cursor = self.conn.cursor()
|
self.cursor = self.conn.cursor()
|
||||||
|
|
||||||
def get_chats(self):
|
def get_chats(self):
|
||||||
|
|
@ -17,16 +17,13 @@ class BasicDatabase:
|
||||||
|
|
||||||
def add_chat(self, chat_id: int):
|
def add_chat(self, chat_id: int):
|
||||||
self.cursor.execute("INSERT INTO chats (id) VALUES (?)", chat_id)
|
self.cursor.execute("INSERT INTO chats (id) VALUES (?)", chat_id)
|
||||||
self.conn.commit()
|
|
||||||
|
|
||||||
def chat_update(self, chat_id: int, **kwargs):
|
def chat_update(self, chat_id: int, **kwargs):
|
||||||
self.cursor.execute("UPDATE chats SET " + ", ".join(f + " = ?" for f in kwargs) +
|
self.cursor.execute("UPDATE chats SET " + ", ".join(f + " = ?" for f in kwargs) +
|
||||||
" WHERE id = ?", list(kwargs.values()) + [chat_id])
|
" WHERE id = ?", list(kwargs.values()) + [chat_id])
|
||||||
self.conn.commit()
|
|
||||||
|
|
||||||
def chat_delete(self, chat_id: int):
|
def chat_delete(self, chat_id: int):
|
||||||
self.cursor.execute("DELETE FROM chats WHERE id = ?", chat_id)
|
self.cursor.execute("DELETE FROM chats WHERE id = ?", chat_id)
|
||||||
self.conn.commit()
|
|
||||||
|
|
||||||
def get_user(self, chat_id: int, user_id: int):
|
def get_user(self, chat_id: int, user_id: int):
|
||||||
self.cursor.execute("SELECT * FROM users WHERE chat_id = ? AND user_id = ?", chat_id, user_id)
|
self.cursor.execute("SELECT * FROM users WHERE chat_id = ? AND user_id = ?", chat_id, user_id)
|
||||||
|
|
@ -38,14 +35,12 @@ class BasicDatabase:
|
||||||
|
|
||||||
def add_user(self, chat_id: int, user_id: int):
|
def add_user(self, chat_id: int, user_id: int):
|
||||||
self.cursor.execute("INSERT INTO users (chat_id, user_id) VALUES (?, ?)", chat_id, user_id)
|
self.cursor.execute("INSERT INTO users (chat_id, user_id) VALUES (?, ?)", chat_id, user_id)
|
||||||
self.conn.commit()
|
|
||||||
|
|
||||||
def user_set_last_message(self, chat_id: int, user_id: int, last_message: int):
|
def user_set_last_message(self, chat_id: int, user_id: int, last_message: int):
|
||||||
self.user_update(chat_id, user_id, last_message=last_message)
|
self.user_update(chat_id, user_id, last_message=last_message)
|
||||||
|
|
||||||
def user_increment_messages(self, chat_id: int, user_id: int):
|
def user_increment_messages(self, chat_id: int, user_id: int):
|
||||||
self.user_increment(chat_id, user_id, ['messages_today', 'messages_month'])
|
self.user_increment(chat_id, user_id, ['messages_today', 'messages_month'])
|
||||||
self.conn.commit()
|
|
||||||
|
|
||||||
def user_increment_warnings(self, chat_id: int, user_id: int):
|
def user_increment_warnings(self, chat_id: int, user_id: int):
|
||||||
self.user_increment(chat_id, user_id, ['warnings'])
|
self.user_increment(chat_id, user_id, ['warnings'])
|
||||||
|
|
@ -53,18 +48,13 @@ class BasicDatabase:
|
||||||
def user_increment(self, chat_id: int, user_id: int, fields: List[str]):
|
def user_increment(self, chat_id: int, user_id: int, fields: List[str]):
|
||||||
self.cursor.execute("UPDATE users SET " + ", ".join(f + " = " + f + " + 1" for f in fields) +
|
self.cursor.execute("UPDATE users SET " + ", ".join(f + " = " + f + " + 1" for f in fields) +
|
||||||
" WHERE chat_id = ? AND user_id = ?", chat_id, user_id)
|
" WHERE chat_id = ? AND user_id = ?", chat_id, user_id)
|
||||||
self.conn.commit()
|
|
||||||
|
|
||||||
def user_update(self, chat_id: int, user_id: int, **kwargs):
|
def user_update(self, chat_id: int, user_id: int, **kwargs):
|
||||||
sql = "UPDATE users SET " + ", ".join(f + " = ?" for f in kwargs) + " WHERE chat_id = ? AND user_id = ?"
|
|
||||||
args = [chat_id, user_id] + list(kwargs.values())
|
|
||||||
self.cursor.execute("UPDATE users SET " + ", ".join(f + " = ?" for f in kwargs) +
|
self.cursor.execute("UPDATE users SET " + ", ".join(f + " = ?" for f in kwargs) +
|
||||||
" WHERE chat_id = ? AND user_id = ?", list(kwargs.values()) + [chat_id, user_id])
|
" WHERE chat_id = ? AND user_id = ?", list(kwargs.values()) + [chat_id, user_id])
|
||||||
self.conn.commit()
|
|
||||||
|
|
||||||
def delete_user(self, chat_id: int, user_id: int):
|
def delete_user(self, chat_id: int, user_id: int):
|
||||||
self.cursor.execute("DELETE FROM users WHERE chat_id = ? AND user_id = ?", chat_id, user_id)
|
self.cursor.execute("DELETE FROM users WHERE chat_id = ? AND user_id = ?", chat_id, user_id)
|
||||||
self.conn.commit()
|
|
||||||
|
|
||||||
def get_top_messages_today(self, chat_id: int):
|
def get_top_messages_today(self, chat_id: int):
|
||||||
self.cursor.execute("""
|
self.cursor.execute("""
|
||||||
|
|
@ -104,17 +94,11 @@ class BasicDatabase:
|
||||||
""", chat_id)
|
""", chat_id)
|
||||||
return self._to_dict(self.cursor.fetchall())
|
return self._to_dict(self.cursor.fetchall())
|
||||||
|
|
||||||
def delete_users(self, chat_id: int):
|
|
||||||
self.cursor.execute("DELETE FROM users WHERE chat_id = ?", chat_id)
|
|
||||||
self.conn.commit()
|
|
||||||
|
|
||||||
def reset_messages_today(self):
|
def reset_messages_today(self):
|
||||||
self.cursor.execute("UPDATE users SET messages_today = 0")
|
self.cursor.execute("UPDATE users SET messages_today = 0")
|
||||||
self.conn.commit()
|
|
||||||
|
|
||||||
def reset_messages_month(self):
|
def reset_messages_month(self):
|
||||||
self.cursor.execute("UPDATE users SET messages_month = 0")
|
self.cursor.execute("UPDATE users SET messages_month = 0")
|
||||||
self.conn.commit()
|
|
||||||
|
|
||||||
def create_chat_if_not_exists(self, chat_id: int):
|
def create_chat_if_not_exists(self, chat_id: int):
|
||||||
chat = self.get_chat(chat_id)
|
chat = self.get_chat(chat_id)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue