Fixed some database errors.

This commit is contained in:
Jeremy Fincher 2003-08-19 07:48:43 +00:00
parent 7aabcfd41d
commit 8b093f3b2b

View File

@ -38,7 +38,6 @@ from baseplugin import *
import time import time
import os.path import os.path
import operator
import sqlite import sqlite
@ -48,8 +47,8 @@ import utils
import ircdb import ircdb
import ircmsgs import ircmsgs
import privmsgs import privmsgs
import callbacks
import ircutils import ircutils
import callbacks
dbfilename = os.path.join(conf.dataDir, 'Notes.db') dbfilename = os.path.join(conf.dataDir, 'Notes.db')
@ -62,15 +61,14 @@ class Notes(callbacks.Privmsg):
"create Notes database and tables" "create Notes database and tables"
if os.path.exists(filename): if os.path.exists(filename):
self.db = sqlite.connect(filename) self.db = sqlite.connect(filename)
self.cursor = self.db.cursor()
return return
self.db = sqlite.connect(filename, converters={'bool': bool}) self.db = sqlite.connect(filename, converters={'bool': bool})
self.cursor = self.db.cursor() cursor = self.db.cursor()
self.cursor.execute("""CREATE TABLE users ( cursor.execute("""CREATE TABLE users (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
name TEXT UNIQUE ON CONFLICT IGNORE name TEXT UNIQUE ON CONFLICT IGNORE
)""") )""")
self.cursor.execute("""CREATE TABLE notes ( cursor.execute("""CREATE TABLE notes (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
from_id INTEGER, from_id INTEGER,
to_id INTEGER, to_id INTEGER,
@ -84,34 +82,39 @@ class Notes(callbacks.Privmsg):
def _addUser(self, username): def _addUser(self, username):
"Not callable from channel, used to add users to database." "Not callable from channel, used to add users to database."
self.cursor.execute('INSERT INTO users VALUES (NULL, %s)', username) cursor = self.db.cursor()
cursor.execute('INSERT INTO users VALUES (NULL, %s)', username)
self.db.commit() self.db.commit()
def getUserId(self, username): def getUserId(self, username):
"Returns the user id matching the given username from the users table." "Returns the user id matching the given username from the users table."
self.cursor.execute('SELECT id FROM users where name=%s', username) cursor = self.db.cursor()
if self.cursor.rowcount != 0: cursor.execute('SELECT id FROM users where name=%s', username)
return self.cursor.fetchone()[0] if cursor.rowcount != 0:
return cursor.fetchone()[0]
else: else:
raise KeyError, username raise KeyError, username
def getUserName(self, userid): def getUserName(self, userid):
"Returns the username matching the given user id from the users table." "Returns the username matching the given user id from the users table."
self.cursor.execute('SELECT name FROM users WHERE id=%s', userid) cursor = self.db.cursor()
if self.cursor.rowcount != 0: cursor.execute('SELECT name FROM users WHERE id=%s', userid)
return self.cursor.fetchone()[0] if cursor.rowcount != 0:
return cursor.fetchone()[0]
else: else:
raise KeyError, userid raise KeyError, userid
def setAsRead(self, noteid): def setAsRead(self, noteid):
"Changes a message's 'read' value to true in the notes table." "Changes a message's 'read' value to true in the notes table."
self.cursor.execute("""UPDATE notes cursor = self.db.cursor()
cursor.execute("""UPDATE notes
SET read=1, notified=1 SET read=1, notified=1
WHERE id=%s""", noteid) WHERE id=%s""", noteid)
self.db.commit() self.db.commit()
def die(self): def die(self):
"Called when module is unloaded/reloaded." "Called when module is unloaded/reloaded."
self.db.commit()
self.db.close() self.db.close()
def doJoin(self, irc, msg): def doJoin(self, irc, msg):
@ -166,7 +169,8 @@ class Notes(callbacks.Privmsg):
public = 1 public = 1
else: else:
public = 0 public = 0
self.cursor.execute("""INSERT INTO notes VALUES cursor = self.db.cursor()
cursor.execute("""INSERT INTO notes VALUES
(NULL, %s, %s, %s, 0, 0, %s, %s)""", (NULL, %s, %s, %s, 0, 0, %s, %s)""",
senderId, recipId, int(time.time()), senderId, recipId, int(time.time()),
public, note) public, note)
@ -185,17 +189,18 @@ class Notes(callbacks.Privmsg):
except KeyError: except KeyError:
irc.error(msg, conf.replyNoUser) irc.error(msg, conf.replyNoUser)
return return
self.cursor.execute("""SELECT notes.note, notes.to_id, notes.from_id, cursor = self.db.cursor()
cursor.execute("""SELECT notes.note, notes.to_id, notes.from_id,
notes.added_at, notes.public notes.added_at, notes.public
FROM users, notes FROM users, notes
WHERE users.name=%s AND WHERE users.name=%s AND
notes.to_id=users.id AND notes.to_id=users.id AND
notes.id=%s notes.id=%s
LIMIT 1""", sender, noteid) LIMIT 1""", sender, noteid)
if self.cursor.rowcount == 0: if cursor.rowcount == 0:
irc.error(msg, 'That\'s not a valid note id.') irc.error(msg, 'That\'s not a valid note id.')
return return
note, to_id, from_id, added_at, public = self.cursor.fetchone() (note, to_id, from_id, added_at, public) = cursor.fetchone()
author = self.getUserName(from_id) author = self.getUserName(from_id)
if senderId != to_id: if senderId != to_id:
irc.error(msg, 'You are not the recipient of note %s.' % noteid) irc.error(msg, 'You are not the recipient of note %s.' % noteid)
@ -219,14 +224,15 @@ class Notes(callbacks.Privmsg):
except KeyError: except KeyError:
irc.error(msg, conf.replyNoUser) irc.error(msg, conf.replyNoUser)
return return
self.cursor.execute("""SELECT notes.id, notes.from_id, cursor = self.db.cursor()
cursor.execute("""SELECT notes.id, notes.from_id,
notes.public, notes.read notes.public, notes.read
FROM users, notes FROM users, notes
WHERE users.name=%s AND WHERE users.name=%s AND
notes.to_id=users.id AND notes.to_id=users.id AND
notes.read=0""", sender) notes.read=0""", sender)
count = self.cursor.rowcount count = cursor.rowcount
notes = self.cursor.fetchall() notes = cursor.fetchall()
L = [] L = []
more = False more = False
if count == 0: if count == 0: