Changed to use DBHandler.

This commit is contained in:
Jeremy Fincher 2003-12-02 22:40:50 +00:00
parent 2f7d2d7f61
commit fd06e1c7ee

View File

@ -83,15 +83,17 @@ def configure(onStart, afterConnect, advanced):
onStart.append('lookup add %s %s' % (command, filename)) onStart.append('lookup add %s %s' % (command, filename))
def getDb(): class LookupDB(plugins.DBHandler):
return sqlite.connect(os.path.join(conf.dataDir, 'Lookup.db')) def makeDb(self, filename):
return sqlite.connect(filename)
class Lookup(callbacks.Privmsg): class Lookup(callbacks.Privmsg):
def __init__(self):
callbacks.Privmsg.__init__(self)
self.dbHandler = LookupDB(name=os.path.join(conf.dataDir, 'Lookup'))
def die(self): def die(self):
db = getDb() self.dbHandler.die()
db.commit()
db.close()
del db
def remove(self, irc, msg, args): def remove(self, irc, msg, args):
"""<name> """<name>
@ -99,7 +101,7 @@ class Lookup(callbacks.Privmsg):
Removes the lookup for <name>. Removes the lookup for <name>.
""" """
name = privmsgs.getArgs(args) name = privmsgs.getArgs(args)
db = getDb() db = self.dbHandler.getDb()
cursor = db.cursor() cursor = db.cursor()
try: try:
cursor.execute("""DROP TABLE %s""" % name) cursor.execute("""DROP TABLE %s""" % name)
@ -120,7 +122,7 @@ class Lookup(callbacks.Privmsg):
""" """
(name, filename) = privmsgs.getArgs(args, required=2) (name, filename) = privmsgs.getArgs(args, required=2)
name = utils.depluralize(name) name = utils.depluralize(name)
db = getDb() db = self.dbHandler.getDb()
cursor = db.cursor() cursor = db.cursor()
try: try:
cursor.execute("""SELECT * FROM %s LIMIT 1""" % name) cursor.execute("""SELECT * FROM %s LIMIT 1""" % name)
@ -134,21 +136,26 @@ class Lookup(callbacks.Privmsg):
except EnvironmentError, e: except EnvironmentError, e:
irc.error(msg, 'Could not open %s: %s' % (filename, e.args[1])) irc.error(msg, 'Could not open %s: %s' % (filename, e.args[1]))
return return
cursor.execute("""CREATE TABLE %s (key TEXT, value TEXT)""" % name) try:
sql = """INSERT INTO %s VALUES (%%s, %%s)""" % name cursor.execute("""SELECT COUNT(*) FROM %s""" % name)
for line in fd: except sqlite.DatabaseError:
line = line.rstrip('\r\n') cursor.execute("CREATE TABLE %s (key TEXT, value TEXT)" % name)
if not line or line.startswith('#'): sql = "INSERT INTO %s VALUES (%%s, %%s)" % name
continue for line in fd:
try: line = line.rstrip('\r\n')
(key, value) = self._splitRe.split(line, 1) if not line or line.startswith('#'):
key = key.replace('\\:', ':') continue
except ValueError: try:
irc.error(msg, 'Invalid line in %s: %r' % (filename, line)) (key, value) = self._splitRe.split(line, 1)
return key = key.replace('\\:', ':')
cursor.execute(sql, key, value) except ValueError:
cursor.execute("CREATE INDEX %s_keys ON %s (key)" % (name, name)) cursor.execute("""DROP TABLE %s""" % name)
db.commit() s = 'Invalid line in %s: %r' % (filename, line)
irc.error(msg, s)
return
cursor.execute(sql, key, value)
cursor.execute("CREATE INDEX %s_keys ON %s (key)" %(name,name))
db.commit()
self.addCommand(name) self.addCommand(name)
cb = irc.getCallback('Alias') cb = irc.getCallback('Alias')
irc.reply(msg, '%s (lookup %s added)' % (conf.replySuccess, name)) irc.reply(msg, '%s (lookup %s added)' % (conf.replySuccess, name))
@ -157,7 +164,7 @@ class Lookup(callbacks.Privmsg):
def f(self, irc, msg, args): def f(self, irc, msg, args):
args.insert(0, name) args.insert(0, name)
self._lookup(irc, msg, args) self._lookup(irc, msg, args)
db = getDb() db = self.dbHandler.getDb()
cursor = db.cursor() cursor = db.cursor()
cursor.execute("""SELECT COUNT(*) FROM %s""" % name) cursor.execute("""SELECT COUNT(*) FROM %s""" % name)
rows = int(cursor.fetchone()[0]) rows = int(cursor.fetchone()[0])
@ -178,7 +185,7 @@ class Lookup(callbacks.Privmsg):
Looks up the value of <key> in the domain <name>. Looks up the value of <key> in the domain <name>.
""" """
(name, key) = privmsgs.getArgs(args, optional=1) (name, key) = privmsgs.getArgs(args, optional=1)
db = getDb() db = self.dbHandler.getDb()
cursor = db.cursor() cursor = db.cursor()
if key: if key:
sql = """SELECT value FROM %s WHERE key LIKE %%s""" % name sql = """SELECT value FROM %s WHERE key LIKE %%s""" % name