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,8 +136,11 @@ 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)
except sqlite.DatabaseError:
cursor.execute("CREATE TABLE %s (key TEXT, value TEXT)" % name)
sql = "INSERT INTO %s VALUES (%%s, %%s)" % name
for line in fd: for line in fd:
line = line.rstrip('\r\n') line = line.rstrip('\r\n')
if not line or line.startswith('#'): if not line or line.startswith('#'):
@ -144,7 +149,9 @@ class Lookup(callbacks.Privmsg):
(key, value) = self._splitRe.split(line, 1) (key, value) = self._splitRe.split(line, 1)
key = key.replace('\\:', ':') key = key.replace('\\:', ':')
except ValueError: except ValueError:
irc.error(msg, 'Invalid line in %s: %r' % (filename, line)) cursor.execute("""DROP TABLE %s""" % name)
s = 'Invalid line in %s: %r' % (filename, line)
irc.error(msg, s)
return return
cursor.execute(sql, key, value) cursor.execute(sql, key, value)
cursor.execute("CREATE INDEX %s_keys ON %s (key)" %(name,name)) cursor.execute("CREATE INDEX %s_keys ON %s (key)" %(name,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