Changed to use a DBHandler.

This commit is contained in:
Jeremy Fincher 2003-11-22 00:55:08 +00:00
parent f2f61fdfad
commit 4ed97b0ba5

View File

@ -55,8 +55,6 @@ import ircutils
import privmsgs import privmsgs
import callbacks import callbacks
dbFilename = os.path.join(conf.dataDir, 'FunDB.db')
tableCreateStatements = { tableCreateStatements = {
'larts': ("""CREATE TABLE larts ( 'larts': ("""CREATE TABLE larts (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
@ -91,20 +89,21 @@ tableCreateStatements = {
"""CREATE INDEX sorted_words_word ON sorted_words (word)"""), """CREATE INDEX sorted_words_word ON sorted_words (word)"""),
} }
def makeDb(dbfilename, replace=False): class FunDBDB(plugins.DBHandler):
if os.path.exists(dbfilename): def makeDb(self, dbfilename, replace=False):
if replace: if os.path.exists(dbfilename):
os.remove(dbfilename) if replace:
db = sqlite.connect(dbfilename) os.remove(dbfilename)
cursor = db.cursor() db = sqlite.connect(dbfilename)
for table in tableCreateStatements: cursor = db.cursor()
try: for table in tableCreateStatements:
cursor.execute("""SELECT * FROM %s LIMIT 1""" % table) try:
except sqlite.DatabaseError: # The table doesn't exist. cursor.execute("""SELECT * FROM %s LIMIT 1""" % table)
for sql in tableCreateStatements[table]: except sqlite.DatabaseError: # The table doesn't exist.
cursor.execute(sql) for sql in tableCreateStatements[table]:
db.commit() cursor.execute(sql)
return db db.commit()
return db
def addWord(db, word, commit=False): def addWord(db, word, commit=False):
word = word.strip().lower() word = word.strip().lower()
@ -129,13 +128,13 @@ class FunDB(callbacks.Privmsg):
_tables = sets.Set(['lart', 'insult', 'excuse', 'praise']) _tables = sets.Set(['lart', 'insult', 'excuse', 'praise'])
def __init__(self): def __init__(self):
callbacks.Privmsg.__init__(self) callbacks.Privmsg.__init__(self)
self.db = makeDb(dbFilename) self.dbHandler = FunDBDB(os.path.join(conf.dataDir, 'FunDB'))
cursor = self.db.cursor()
def die(self): def die(self):
self.db.commit() db = self.dbHandler.getDb()
self.db.close() db.commit()
del self.db db.close()
del db
def insult(self, irc, msg, args): def insult(self, irc, msg, args):
"""<nick> """<nick>
@ -145,7 +144,8 @@ class FunDB(callbacks.Privmsg):
nick = privmsgs.getArgs(args) nick = privmsgs.getArgs(args)
if not nick: if not nick:
raise callbacks.ArgumentError raise callbacks.ArgumentError
cursor = self.db.cursor() db = self.dbHandler.getDb()
cursor = db.cursor()
cursor.execute("""SELECT id, insult FROM insults cursor.execute("""SELECT id, insult FROM insults
WHERE insult NOT NULL WHERE insult NOT NULL
ORDER BY random() ORDER BY random()
@ -169,7 +169,8 @@ class FunDB(callbacks.Privmsg):
('_') to denote blank spaces. ('_') to denote blank spaces.
""" """
word = privmsgs.getArgs(args).lower() word = privmsgs.getArgs(args).lower()
cursor = self.db.cursor() db = self.dbHandler.getDb()
cursor = db.cursor()
if '%' in word: if '%' in word:
irc.error(msg, '"%" isn\'t allowed in the word.') irc.error(msg, '"%" isn\'t allowed in the word.')
return return
@ -186,7 +187,8 @@ class FunDB(callbacks.Privmsg):
<id>. <id>.
""" """
id = privmsgs.getArgs(args, required=0, optional=1) id = privmsgs.getArgs(args, required=0, optional=1)
cursor = self.db.cursor() db = self.dbHandler.getDb()
cursor = db.cursor()
if id: if id:
try: try:
id = int(id) id = int(id)
@ -235,10 +237,11 @@ class FunDB(callbacks.Privmsg):
irc.error(msg, '"%s" is not valid. Valid values include %s.' % irc.error(msg, '"%s" is not valid. Valid values include %s.' %
(table, utils.commaAndify(self._tables))) (table, utils.commaAndify(self._tables)))
return return
cursor = self.db.cursor() db = self.dbHandler.getDb()
cursor = db.cursor()
sql = """INSERT INTO %ss VALUES (NULL, %%s, %%s)""" % table sql = """INSERT INTO %ss VALUES (NULL, %%s, %%s)""" % table
cursor.execute(sql, s, name) cursor.execute(sql, s, name)
self.db.commit() db.commit()
sql = """SELECT id FROM %ss WHERE %s=%%s""" % (table, table) sql = """SELECT id FROM %ss WHERE %s=%%s""" % (table, table)
cursor.execute(sql, s) cursor.execute(sql, s)
id = cursor.fetchone()[0] id = cursor.fetchone()[0]
@ -267,10 +270,11 @@ class FunDB(callbacks.Privmsg):
irc.error(msg, '"%s" is not valid. Valid values include %s.' % irc.error(msg, '"%s" is not valid. Valid values include %s.' %
(table, utils.commaAndify(self._tables))) (table, utils.commaAndify(self._tables)))
return return
cursor = self.db.cursor() db = self.dbHandler.getDb()
cursor = db.cursor()
sql = """DELETE FROM %ss WHERE id=%%s""" % table sql = """DELETE FROM %ss WHERE id=%%s""" % table
cursor.execute(sql, id) cursor.execute(sql, 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):
@ -304,7 +308,8 @@ class FunDB(callbacks.Privmsg):
except re.error, e: except re.error, e:
irc.error(msg, debug.exnToString(e)) irc.error(msg, debug.exnToString(e))
return return
cursor = self.db.cursor() db = self.dbHandler.getDb()
cursor = db.cursor()
sql = """SELECT %s FROM %ss WHERE id=%%s""" % (table, table) sql = """SELECT %s FROM %ss WHERE id=%%s""" % (table, table)
cursor.execute(sql, id) cursor.execute(sql, id)
if cursor.rowcount == 0: if cursor.rowcount == 0:
@ -315,7 +320,7 @@ class FunDB(callbacks.Privmsg):
sql = """UPDATE %ss SET %s=%%s, added_by=%%s WHERE id=%%s""" % \ sql = """UPDATE %ss SET %s=%%s, added_by=%%s WHERE id=%%s""" % \
(table, table) (table, table)
cursor.execute(sql, new_entry, name, id) cursor.execute(sql, new_entry, name, id)
self.db.commit() db.commit()
irc.reply(msg, conf.replySuccess) irc.reply(msg, conf.replySuccess)
def num(self, irc, msg, args): def num(self, irc, msg, args):
@ -330,7 +335,8 @@ class FunDB(callbacks.Privmsg):
irc.error(msg, '%r is not valid. Valid values include %s.' % irc.error(msg, '%r is not valid. Valid values include %s.' %
(table, utils.commaAndify(self._tables))) (table, utils.commaAndify(self._tables)))
return return
cursor = self.db.cursor() db = self.dbHandler.getDb()
cursor = db.cursor()
sql = """SELECT count(*) FROM %ss""" % table sql = """SELECT count(*) FROM %ss""" % table
cursor.execute(sql) cursor.execute(sql)
total = int(cursor.fetchone()[0]) total = int(cursor.fetchone()[0])
@ -353,7 +359,8 @@ class FunDB(callbacks.Privmsg):
irc.error(msg, '"%s" is not valid. Valid values include %s.' % irc.error(msg, '"%s" is not valid. Valid values include %s.' %
(table, utils.commaAndify(self._tables))) (table, utils.commaAndify(self._tables)))
return return
cursor = self.db.cursor() db = self.dbHandler.getDb()
cursor = db.cursor()
sql = """SELECT %s FROM %ss WHERE id=%%s""" % (table, table) sql = """SELECT %s FROM %ss WHERE id=%%s""" % (table, table)
cursor.execute(sql, id) cursor.execute(sql, id)
if cursor.rowcount == 0: if cursor.rowcount == 0:
@ -378,7 +385,8 @@ class FunDB(callbacks.Privmsg):
irc.error(msg, '"%s" is not valid. Valid values include %s.' % irc.error(msg, '"%s" is not valid. Valid values include %s.' %
(table, utils.commaAndify(self._tables))) (table, utils.commaAndify(self._tables)))
return return
cursor = self.db.cursor() db = self.dbHandler.getDb()
cursor = db.cursor()
sql = """SELECT added_by FROM %ss WHERE id=%%s""" % table sql = """SELECT added_by FROM %ss WHERE id=%%s""" % table
cursor.execute(sql, id) cursor.execute(sql, id)
if cursor.rowcount == 0: if cursor.rowcount == 0:
@ -412,7 +420,8 @@ class FunDB(callbacks.Privmsg):
utils.itersplit('for'.__eq__, nick.split(), 1)) utils.itersplit('for'.__eq__, nick.split(), 1))
except ValueError: except ValueError:
reason = '' reason = ''
cursor = self.db.cursor() db = self.dbHandler.getDb()
cursor = db.cursor()
if id: if id:
cursor.execute("""SELECT id, lart FROM larts WHERE id=%s""", id) cursor.execute("""SELECT id, lart FROM larts WHERE id=%s""", id)
if cursor.rowcount == 0: if cursor.rowcount == 0:
@ -461,7 +470,8 @@ class FunDB(callbacks.Privmsg):
utils.itersplit('for'.__eq__, nick.split(), 1)) utils.itersplit('for'.__eq__, nick.split(), 1))
except ValueError: except ValueError:
reason = '' reason = ''
cursor = self.db.cursor() db = self.dbHandler.getDb()
cursor = db.cursor()
if id: if id:
cursor.execute("""SELECT id, praise FROM praises WHERE id=%s""",id) cursor.execute("""SELECT id, praise FROM praises WHERE id=%s""",id)
if cursor.rowcount == 0: if cursor.rowcount == 0:
@ -497,7 +507,7 @@ class FunDB(callbacks.Privmsg):
word = privmsgs.getArgs(args) word = privmsgs.getArgs(args)
if word.translate(string.ascii, string.ascii_letters) != '': if word.translate(string.ascii, string.ascii_letters) != '':
irc.error(msg, 'Word must contain only letters.') irc.error(msg, 'Word must contain only letters.')
addWord(self.db, word, commit=True) addWord(self.dbHandler.getDb(), word, commit=True)
irc.reply(msg, conf.replySuccess) irc.reply(msg, conf.replySuccess)
def anagram(self, irc, msg, args): def anagram(self, irc, msg, args):
@ -506,7 +516,8 @@ class FunDB(callbacks.Privmsg):
Using the words database, determines if a word has any anagrams. Using the words database, determines if a word has any anagrams.
""" """
word = privmsgs.getArgs(args).strip().lower() word = privmsgs.getArgs(args).strip().lower()
cursor = self.db.cursor() db = self.dbHandler.getDb()
cursor = db.cursor()
cursor.execute("""SELECT words.word FROM words cursor.execute("""SELECT words.word FROM words
WHERE sorted_word_id=( WHERE sorted_word_id=(
SELECT sorted_word_id FROM words SELECT sorted_word_id FROM words
@ -537,7 +548,8 @@ if __name__ == '__main__':
added_by = sys.argv[3] added_by = sys.argv[3]
else: else:
added_by = '<console>' added_by = '<console>'
db = makeDb(dbFilename) dbHandler = FunDBDB(os.path.join(conf.dataDir, 'FunDB'))
db = dbHandler.getDb()
cursor = db.cursor() cursor = db.cursor()
for line in open(filename, 'r'): for line in open(filename, 'r'):
line = line.rstrip() line = line.rstrip()