sqlite related stuff as part of making sqlite the canonical backend

* Module.py __init__ sets up sqlite db connection by default
* Module.py __init__ calls init_db() which is empty, expects subclasses to implement as necessary
* Module.py doesn't close sqlite connection by default

Changes call for a couple updates in Karma.py, namely implementing db_init
and excepting sqlite3.Error rather than closing the connection
This commit is contained in:
Brian S. Stephan 2010-10-27 17:59:01 -05:00
parent 70b49ecbcc
commit 1d73deda1c
2 changed files with 29 additions and 8 deletions

View File

@ -44,6 +44,14 @@ class Module(object):
self.server = server
self.modlist = modlist
# open database connection
dbfile = self.config.get('dr.botzo', 'database')
self.conn = sqlite3.connect(dbfile)
self.conn.row_factory = sqlite3.Row
# set up database for this module
self.db_init()
# add self to the object list
modlist.append(self)
@ -295,15 +303,14 @@ class Module(object):
See also db_module_registered, below.
"""
dbfile = self.config.get('dr.botzo', 'database')
conn = sqlite3.connect(dbfile)
return conn
return self.conn
def db_module_registered(self, modulename):
"""
ask the database for a version number for a module. Return that version
number if the module has registered, or None if not
"""
conn = self.get_db()
version = None
try:
@ -313,9 +320,16 @@ class Module(object):
version = cur.fetchone()
if (version != None):
version = version[0]
finally: conn.close()
except sqlite3.Error as e:
print("sqlite error:" + str(e))
return version
def db_init(self):
"""
Set up the database tables and so on, if subclass is planning on using it.
"""
def do(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
"""
Do the primary thing this module was intended to do.

View File

@ -17,6 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import re
import sqlite3
from Module import Module
@ -39,6 +40,7 @@ class Karma(Module):
self.karmare = re.compile(karmapattern)
self.queryre = re.compile(querypattern)
def db_init(self):
# need to init the database if karma tables don't already exist
version = self.db_module_registered(self.__class__.__name__)
if (version == None):
@ -65,7 +67,10 @@ class Karma(Module):
sql = 'INSERT INTO drbotzo_modules VALUES (?,?)'
conn.execute(sql, (self.__class__.__name__, 1))
conn.commit()
finally: conn.close()
except sqlite3.Error as e:
conn.rollback()
print("sqlite error: " + str(e))
raise
def do(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
"""look for karma strings at the start of messages"""
@ -96,7 +101,9 @@ class Karma(Module):
'''
conn.execute(sql, (key, value, nick, userhost))
conn.commit()
finally: conn.close()
except sqlite3.Error as e:
conn.rollback()
return self.reply(connection, replypath, "sqlite error: " + str(e))
def handle_karma_query(self, connection, nick, userhost, replypath, what):
match = self.queryre.match(what)
@ -121,8 +128,8 @@ class Karma(Module):
reply = '{nick}: {key} has {value[0]!s} points of karma (rank {rank})'.format(
nick=nick, key=key, value=value, rank=rank)
finally: conn.close()
except sqlite3.Error as e:
return self.reply(connection, replypath, "sqlite error: " + str(e))
self.reply(connection, replypath, reply)