Advertisement
Guest User

Untitled

a guest
Nov 29th, 2020
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. import sqlite3
  2.  
  3.  
  4. class dbworker:
  5. def __init__(self,database_file):
  6. ''' Констуктор '''
  7. self.connection = sqlite3.connect(database_file)
  8. self.cursor = self.connection.cursor()
  9.  
  10. def user_exists(self, user_id):
  11. ''' Проверка есть ли юзер в бд '''
  12. with self.connection:
  13. result = self.cursor.execute('SELECT * FROM `users` WHERE `telegram_id` = ?', (user_id,)).fetchall()
  14. return bool(len(result))
  15.  
  16. def add_user(self,telegram_username,telegram_id):
  17. '''Добавляем нового юзера'''
  18. with self.connection:
  19. return self.cursor.execute("INSERT INTO `users` (`telegram_username`, `telegram_id`) VALUES(?,?)", (telegram_username,telegram_id))
  20.  
  21. def edit_sex(self,sex,telegram_id):
  22. ''' Изменения пола '''
  23. with self.connection:
  24. self.cursor.execute('UPDATE `users` SET `sex` = ? WHERE `telegram_id` = ?',(sex,telegram_id)) # True - мужчина, False - женщина
  25.  
  26. def search(self,sex):
  27. ''' Поиск '''
  28. with self.connection:
  29. search = self.cursor.execute('SELECT `telegram_id` FROM `queue` WHERE `sex` = ?',(str(sex))).fetchone()
  30.  
  31. return search
  32.  
  33. def get_sex_user(self,telegram_id):
  34. ''' Получить информацию о поле юзера по его айдишнику '''
  35. with self.connection:
  36. result = self.cursor.execute('SELECT `sex` FROM `users` WHERE `telegram_id` = ?',(telegram_id,)).fetchone()
  37. return result
  38.  
  39. def add_to_queue(self,telegram_id,sex):
  40. ''' Добавление в очередь '''
  41. with self.connection:
  42. if sex == 1:
  43. sex = bool(0)
  44. else:
  45. sex = bool(1)
  46. self.cursor.execute("INSERT INTO `queue` (`telegram_id`, `sex`) VALUES(?,?)", (telegram_id,sex))
  47.  
  48. def delete_from_queue(self, telegram_id):
  49. ''' Функция удаляет из очереди '''
  50. with self.connection:
  51. self.cursor.execute('DELETE FROM `queue` WHERE `telegram_id` = ?',(telegram_id,))
  52.  
  53. def update_connect_with(self,connect_with,telegram_id):
  54. ''' Обновление с кем общается пользователь '''
  55. with self.connection:
  56. self.cursor.execute('UPDATE `users` SET `connect_with` = ? WHERE `telegram_id` = ?',(connect_with,telegram_id))
  57.  
  58. def select_connect_with(self,telegram_id):
  59. ''' Функция для получения айдишника с кем общается человек '''
  60. with self.connection:
  61. return self.cursor.execute('SELECT `connect_with` FROM `users` WHERE `telegram_id` = ?',(telegram_id,)).fetchone()
  62.  
  63. def select_connect_with_self(self, telegram_id):
  64. ''' Функция для получения айдишника по айдишнику с кем общается человек '''
  65. with self.connection:
  66. return self.cursor.execute('SELECT `telegram_id` FROM `users` WHERE `connect_with` = ?',(telegram_id,)).fetchone()
  67.  
  68. def log_msg(self,telegram_id,msg):
  69. ''' Функция которая логирует все сообщения юзеров друг другу '''
  70. with self.connection:
  71. self.cursor.execute('INSERT INTO `all_messages` (`sender`,`message`) VALUES (?,?)',(telegram_id,msg))
  72.  
  73. def queue_exists(self,telegram_id):
  74. ''' Функция возвращает есть ли пользователь в очереди '''
  75. with self.connection:
  76. result = self.cursor.execute('SELECT * FROM `queue` WHERE `telegram_id` = ?', (telegram_id,)).fetchall()
  77. return bool(len(result))
  78.  
  79. def count_user(self):
  80. '''вывод количества юзеров'''
  81. with self.connection:
  82. result = self.cursor.execute('SELECT COUNT(*) FROM `users`').fetchone()
  83. return result[0]
  84.  
  85. def add_count_msg(self,telegram_id):
  86. ''' добавления кол-ва сообщений пользователю'''
  87. with self.connection:
  88. self.cursor.execute('UPDATE `users` SET `all_msg` = `all_msg` + 1 WHERE `telegram_id` = ?',(telegram_id,))
  89.  
  90. def top_rating(self):
  91. '''вывод топа по рейтингу'''
  92. with self.connection:
  93. return self.cursor.execute('SELECT `telegram_id` FROM `users` ORDER BY `all_msg` DESC LIMIT 5').fetchall()
  94.  
  95. def get_name_user(self,telegram_id):
  96. ''' Получить информацию о поле юзера по его айдишнику '''
  97. with self.connection:
  98. result = self.cursor.execute('SELECT `telegram_username` FROM `users` WHERE `telegram_id` = ?',(telegram_id,)).fetchone()
  99. return result[0]
  100.  
  101. def get_count_all_msg(self,telegram_id):
  102. '''вывод количества сообщений у юзера'''
  103. with self.connection:
  104. result = self.cursor.execute('SELECT `all_msg` FROM `users` WHERE `telegram_id` = ?',(telegram_id,)).fetchone()
  105. return result[0]
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement