From 5eaf4a8790fa05ce7f6f23ffc6a02740a125358c Mon Sep 17 00:00:00 2001 From: jesopo Date: Fri, 31 Aug 2018 15:13:56 +0100 Subject: [PATCH] Add Database.UserChannelSettings.find_all_by_setting --- Database.py | 20 ++++++++++++++++---- IRCChannel.py | 3 +++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/Database.py b/Database.py index c315d801..8ea22b9e 100644 --- a/Database.py +++ b/Database.py @@ -207,13 +207,25 @@ class UserChannelSettings(Table): def find_prefix(self, user_id, channel_id, prefix, default=[]): return self.find_user_settings(user_id, channel_id, "%s%" % prefix, default) - def find_by_setting(self, server_id, user_id, setting, default=[]): + def find_by_setting(self, user_id, setting, default=[]): values = self.database.execute_fetchall( """SELECT channels.name, user_channel_settings.value FROM user_channel_settings INNER JOIN channels ON user_channel_settings.channel_id=channels.channel_id - WHERE channels.server_id=? AND - user_channel_settings.user_id=?""", [server_id, user_id]) + WHERE user_channel_settings.setting=? + AND user_channel_settings.user_id=?""", [setting, user_id]) + if values: + for i, value in enumerate(values): + values[i] = value[0], json.loads(value[1]) + return values + return default + def find_all_by_setting(self, channel_id, setting, default=[]): + values = self.database.execute_fetchall( + """SELECT users.name, user_channel_settings.value FROM + user_channel_settings INNER JOIN users ON + user_channel_settings.user_id=users.user_id + WHERE user_channel_setting.setting=? + AND user_channel_settings.channel_id=?""", [setting, channel_id]) if values: for i, value in enumerate(values): values[i] = value[0], json.loads(value[1]) @@ -354,4 +366,4 @@ class Database(object): DELETE CASCADE, PRIMARY KEY (user_id, channel_id, setting))""") self.execute("""CREATE INDEX user_channel_settings_index - ON user_channel_settings (user_id, channel_id, setting)""") \ No newline at end of file + ON user_channel_settings (user_id, channel_id, setting)""") diff --git a/IRCChannel.py b/IRCChannel.py index 15d2e5df..11a5bf9e 100644 --- a/IRCChannel.py +++ b/IRCChannel.py @@ -99,6 +99,9 @@ class Channel(object): def del_user_setting(self, user_id, setting): self.bot.database.user_channel_settings.delete(user_id, self.id, setting) + def find_all_by_setting(self, setting, default=[]): + return self.bot.database.user_channel_settings.find_all_by_setting( + self.id, setting, default) def send_message(self, text, prefix=None): self.server.send_message(self.name, text, prefix=prefix)