Skip to content
Snippets Groups Projects
Commit 2d3cc115 authored by Doug Freed's avatar Doug Freed Committed by Svetlana Tkachenko
Browse files

plugins/seen: Utilize UNIQUE restrictions to cheat


Takes advantage of the UNIQUE constraint to simplify code.  Includes a
conversion mechanism for those with tables that don't have the
constraint.

Signed-off-by: default avatargry <Gry@Gshellz.org>
parent 28658c77
No related branches found
No related tags found
No related merge requests found
...@@ -42,23 +42,29 @@ class Seen(object): ...@@ -42,23 +42,29 @@ class Seen(object):
self.cursor = self.connection.cursor() self.cursor = self.connection.cursor()
if (not self.db_exists): if (not self.db_exists):
# create the db # create the db
self.cursor.execute('''CREATE TABLE seen (nick text, timestamp text, network text, channel text, message text)''') self.cursor.execute('CREATE TABLE seen (nick text, timestamp text, network text, channel text, message text, UNIQUE ( nick, network ))')
self.connection.commit()
print("[DEBUG] Database created for the first time") print("[DEBUG] Database created for the first time")
else:
# This is hacky, but I'm lazy
self.cursor.execute('PRAGMA index_list(seen)')
if not self.cursor.fetchone():
# Gotta give the table the unique constraint; shifting data to a temp table so we can DROP the original, and then rename the temp table
self.cursor.execute('CREATE TABLE seen_temp (nick text, timestamp text, network text, channel text, message text, UNIQUE ( nick, network ))')
self.cursor.execute('INSERT INTO seen_temp SELECT * FROM seen')
self.cursor.execute('DROP TABLE seen')
self.cursor.execute('ALTER TABLE seen_temp RENAME TO seen')
print("[DEBUG] Database schema updated")
self.connection.commit()
def handle_message(self, channel, nick, message): def handle_message(self, channel, nick, message):
# print("Message '{0}' from '{1}' in '{2}'".format(message, nick, channel)) # print("Message '{0}' from '{1}' in '{2}'".format(message, nick, channel))
# add to db # add to db
# nick, timestamp, network, channel message # nick, timestamp, network, channel message
self.cursor.execute('SELECT * FROM seen WHERE nick=? and network = ?', (nick, self.network)) self.cursor.execute('INSERT OR REPLACE INTO seen VALUES (?, ?, ?, ?, ?)', (nick, time.time(), self.network, channel, message))
if (self.cursor.fetchone()):
self.cursor.execute('UPDATE seen SET nick = ?, timestamp = ?, network = ?, channel = ?, message = ? WHERE nick=? and network = ?',(nick, time.time(), self.network, channel, message, nick, self.network) )
else:
self.cursor.execute('INSERT INTO seen VALUES (?, ?, ?, ?, ?)', (nick, time.time(), self.network, channel, message))
self.connection.commit() self.connection.commit()
def handle_command(self, channel, user, cmd, args): def handle_command(self, channel, user, cmd, args):
if cmd == "seen": if cmd == "seen":
nick = args[0] nick = args[0]
self.cursor.execute('SELECT * FROM seen WHERE nick=? and network = ?', (nick, self.network)) self.cursor.execute('SELECT * FROM seen WHERE nick = ? and network = ?', (nick, self.network))
info = self.cursor.fetchone() info = self.cursor.fetchone()
if (info): if (info):
# ('gry', '2013-01-31 14:50:02.419198', 'gnu', '#guppy', '-seen gry') # ('gry', '2013-01-31 14:50:02.419198', 'gnu', '#guppy', '-seen gry')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment