Converted to use a DBHandler.

This commit is contained in:
Jeremy Fincher 2003-12-08 13:08:14 +00:00
parent 028f25eeb2
commit 836ef993df

View File

@ -56,8 +56,6 @@ except ImportError:
raise callbacks.Error, 'You need to have PySQLite installed to use this ' \ raise callbacks.Error, 'You need to have PySQLite installed to use this ' \
'plugin. Download it at <http://pysqlite.sf.net/>' 'plugin. Download it at <http://pysqlite.sf.net/>'
dbfilename = os.path.join(conf.dataDir, 'Todo.db')
def configure(onStart, afterConnect, advanced): def configure(onStart, afterConnect, advanced):
# This will be called by setup.py to configure this module. onStart and # This will be called by setup.py to configure this module. onStart and
# afterConnect are both lists. Append to onStart the commands you would # afterConnect are both lists. Append to onStart the commands you would
@ -66,18 +64,14 @@ def configure(onStart, afterConnect, advanced):
from questions import expect, anything, something, yn from questions import expect, anything, something, yn
onStart.append('load Todo') onStart.append('load Todo')
class Todo(callbacks.Privmsg): class TodoDB(plugins.DBHandler):
def __init__(self): def makeDb(self, filename):
callbacks.Privmsg.__init__(self)
self.makeDB(dbfilename)
def makeDB(self, filename):
"""create Todo database and tables""" """create Todo database and tables"""
if os.path.exists(filename): if os.path.exists(filename):
self.db = sqlite.connect(filename) db = sqlite.connect(filename)
return else:
self.db = sqlite.connect(filename, converters={'bool': bool}) db = sqlite.connect(filename, converters={'bool': bool})
cursor = self.db.cursor() cursor = db.cursor()
cursor.execute("""CREATE TABLE todo ( cursor.execute("""CREATE TABLE todo (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
priority INTEGER, priority INTEGER,
@ -86,12 +80,18 @@ class Todo(callbacks.Privmsg):
task TEXT, task TEXT,
active BOOLEAN active BOOLEAN
)""") )""")
self.db.commit() db.commit()
return db
class Todo(callbacks.Privmsg):
def __init__(self):
callbacks.Privmsg.__init__(self)
self.dbHandler = TodoDB(os.path.join(conf.dataDir, 'Todo'))
def die(self): def die(self):
self.db.commit() self.dbHandler.die()
self.db.close() callbacks.Privmsg.die(self)
del self.db
def _shrink(self, s): def _shrink(self, s):
return utils.ellipsisify(s, 50) return utils.ellipsisify(s, 50)
@ -119,7 +119,8 @@ class Todo(callbacks.Privmsg):
irc.error(msg, irc.error(msg,
'%r is not a valid task id or username' % arg) '%r is not a valid task id or username' % arg)
return return
cursor = self.db.cursor() db = self.dbHandler.getDb()
cursor = db.cursor()
if not userid and not taskid: if not userid and not taskid:
try: try:
id = ircdb.users.getUserId(msg.prefix) id = ircdb.users.getUserId(msg.prefix)
@ -188,12 +189,13 @@ class Todo(callbacks.Privmsg):
irc.error(msg, '%r is an invalid priority' % arg) irc.error(msg, '%r is an invalid priority' % arg)
return return
text = privmsgs.getArgs(rest) text = privmsgs.getArgs(rest)
cursor = self.db.cursor() db = self.dbHandler.getDb()
cursor = db.cursor()
now = int(time.time()) now = int(time.time())
cursor.execute("""INSERT INTO todo cursor.execute("""INSERT INTO todo
VALUES (NULL, %s, %s, %s, %s, 1)""", VALUES (NULL, %s, %s, %s, %s, 1)""",
priority, now, id, text) priority, now, id, text)
self.db.commit() db.commit()
cursor.execute("""SELECT id FROM todo cursor.execute("""SELECT id FROM todo
WHERE added_at=%s AND userid=%s""", now, id) WHERE added_at=%s AND userid=%s""", now, id)
todoId = cursor.fetchone()[0] todoId = cursor.fetchone()[0]
@ -210,7 +212,8 @@ class Todo(callbacks.Privmsg):
irc.error(msg, conf.replyNotRegistered) irc.error(msg, conf.replyNotRegistered)
return return
taskid = privmsgs.getArgs(args) taskid = privmsgs.getArgs(args)
cursor = self.db.cursor() db = self.dbHandler.getDb()
cursor = db.cursor()
cursor.execute("""SELECT * FROM todo cursor.execute("""SELECT * FROM todo
WHERE id = %s AND userid = %s WHERE id = %s AND userid = %s
AND active = 1""", taskid, id) AND active = 1""", taskid, id)
@ -218,7 +221,7 @@ class Todo(callbacks.Privmsg):
irc.error(msg, 'None of your tasks match that id.') irc.error(msg, 'None of your tasks match that id.')
return return
cursor.execute("""UPDATE todo SET active = 0 WHERE id = %s""", taskid) cursor.execute("""UPDATE todo SET active = 0 WHERE id = %s""", taskid)
self.db.commit() db.commit()
irc.reply(msg, conf.replySuccess) irc.reply(msg, conf.replySuccess)
_sqlTrans = string.maketrans('*?', '%_') _sqlTrans = string.maketrans('*?', '%_')
@ -239,6 +242,7 @@ class Todo(callbacks.Privmsg):
(optlist, rest) = getopt.getopt(args, '', ['regexp=', 'exact=']) (optlist, rest) = getopt.getopt(args, '', ['regexp=', 'exact='])
if not optlist and not rest: if not optlist and not rest:
raise callbacks.ArgumentError raise callbacks.ArgumentError
db = self.dbHandler.getDb()
criteria = ['userid=%s' % id, 'active=1'] criteria = ['userid=%s' % id, 'active=1']
formats = [] formats = []
predicateName = 'p' predicateName = 'p'
@ -256,12 +260,12 @@ class Todo(callbacks.Privmsg):
return return
def p(s, r=r): def p(s, r=r):
return int(bool(r.search(s))) return int(bool(r.search(s)))
self.db.create_function(predicateName, 1, p) db.create_function(predicateName, 1, p)
predicateName += 'p' predicateName += 'p'
for glob in rest: for glob in rest:
criteria.append('task LIKE %s') criteria.append('task LIKE %s')
formats.append(glob.translate(self._sqlTrans)) formats.append(glob.translate(self._sqlTrans))
cursor = self.db.cursor() cursor = db.cursor()
sql = """SELECT id, task FROM todo WHERE %s""" % ' AND '.join(criteria) sql = """SELECT id, task FROM todo WHERE %s""" % ' AND '.join(criteria)
cursor.execute(sql, formats) cursor.execute(sql, formats)
if cursor.rowcount == 0: if cursor.rowcount == 0:
@ -282,7 +286,8 @@ class Todo(callbacks.Privmsg):
irc.error(msg, conf.replyNotRegistered) irc.error(msg, conf.replyNotRegistered)
return return
(id, priority) = privmsgs.getArgs(args, required=2) (id, priority) = privmsgs.getArgs(args, required=2)
cursor = self.db.cursor() db = self.dbHandler.getDb()
cursor = db.cursor()
cursor.execute("""SELECT userid, priority FROM todo cursor.execute("""SELECT userid, priority FROM todo
WHERE id = %s AND active = 1""", id) WHERE id = %s AND active = 1""", id)
if cursor.rowcount == 0: if cursor.rowcount == 0:
@ -295,7 +300,7 @@ class Todo(callbacks.Privmsg):
# If we make it here, we're okay # If we make it here, we're okay
cursor.execute("""UPDATE todo SET priority = %s cursor.execute("""UPDATE todo SET priority = %s
WHERE id = %s""", priority, id) WHERE id = %s""", priority, id)
self.db.commit() db.commit()
irc.reply(msg, conf.replySuccess) irc.reply(msg, conf.replySuccess)
def change(self, irc, msg, args): def change(self, irc, msg, args):
@ -315,7 +320,8 @@ class Todo(callbacks.Privmsg):
except ValueError: except ValueError:
irc.error(msg, '%r is not a valid regexp' % regexp) irc.error(msg, '%r is not a valid regexp' % regexp)
return return
cursor = self.db.cursor() db = self.dbHandler.getDb()
cursor = db.cursor()
cursor.execute("""SELECT task FROM todo cursor.execute("""SELECT task FROM todo
WHERE userid = %s AND id = %s WHERE userid = %s AND id = %s
AND active = 1""", userid, taskid) AND active = 1""", userid, taskid)
@ -325,7 +331,7 @@ class Todo(callbacks.Privmsg):
newtext = replacer(cursor.fetchone()[0]) newtext = replacer(cursor.fetchone()[0])
cursor.execute("""UPDATE todo SET task = %s cursor.execute("""UPDATE todo SET task = %s
WHERE id = %s""", newtext, taskid) WHERE id = %s""", newtext, taskid)
self.db.commit() db.commit()
irc.reply(msg, conf.replySuccess) irc.reply(msg, conf.replySuccess)
Class = Todo Class = Todo