def update_user(user: User, conn: MySQLdb.Connection, commit=True): cursor = conn.cursor() cursor.execute( "UPDATE users SET name=%s,email=%s,notification_via=%s,lang=%s WHERE chat_id=%s", (user.name, user.email, user.notification_via, user.lang), ) if commit: conn.commit() cursor.close()
def get_or_save_user(chat_id: str, username: str, email: str, notification_via: str, conn: MySQLdb.Connection): cursor = conn.cursor() cursor.execute( "IF NOT EXISTS (SELECT id FROM users WHERE chat_id=%s) THEN " "INSERT INTO users(chat_id, name, email, notification_via) VALUES(%s, %s, %s, %s);" "END IF;", (chat_id, chat_id, username, email, notification_via)) conn.commit() return get_user(conn, chat_id=chat_id)
def insert_train_in_db(train: Train, conn: MySQLdb.Connection, commit=True): if train.depart_date >= datetime.now() or train.check_daily: cursor = conn.cursor() cursor.execute( "INSERT INTO monitoring (train, station, departDate, user, check_daily, check_interval, coach, seat) " "VALUES(%s, %s, %s, %s, %s, %s, %s, %s)", (train.code, train.depart_stat, train.depart_date, train.user, train.check_daily, train.check_interval, train.coach, train.seat)) if commit: conn.commit() cursor.close() else: raise TrainInPastError( f"Train depart date: {train.depart_date}; now: {datetime.now()}")
def update_user_lang(conn: MySQLdb.Connection, user_id="", chat_id="", lang="it", commit=True): cursor = conn.cursor() if user_id: cursor.execute("UPDATE users SET lang=%s WHERE id=%s", (lang, user_id)) else: cursor.execute("UPDATE users SET lang=%s WHERE chat_id=%s", (lang, chat_id)) if commit: conn.commit() cursor.close()
def store_feedback(feedback: str, chat_id: str, conn: MySQLdb.Connection): cursor = conn.cursor() cursor.execute("INSERT INTO feedbacks(user, feedback) VALUES(%s, %s)", (chat_id, feedback)) conn.commit() cursor.close()
def update_train_checked(pk: int, conn: MySQLdb.Connection): cursor = conn.cursor() cursor.execute("UPDATE monitoring set checked = checked + 1 WHERE id=%s", (pk, )) conn.commit() cursor.close()
def delete_train(pk: int, conn: MySQLdb.Connection): cursor = conn.cursor() cursor.execute("DELETE FROM monitoring WHERE id=%s", (pk, )) conn.commit() cursor.close()