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.server = server
self.modlist = modlist 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 # add self to the object list
modlist.append(self) modlist.append(self)
@ -295,15 +303,14 @@ class Module(object):
See also db_module_registered, below. See also db_module_registered, below.
""" """
dbfile = self.config.get('dr.botzo', 'database') return self.conn
conn = sqlite3.connect(dbfile)
return conn
def db_module_registered(self, modulename): def db_module_registered(self, modulename):
""" """
ask the database for a version number for a module. Return that version ask the database for a version number for a module. Return that version
number if the module has registered, or None if not number if the module has registered, or None if not
""" """
conn = self.get_db() conn = self.get_db()
version = None version = None
try: try:
@ -313,9 +320,16 @@ class Module(object):
version = cur.fetchone() version = cur.fetchone()
if (version != None): if (version != None):
version = version[0] version = version[0]
finally: conn.close() except sqlite3.Error as e:
print("sqlite error:" + str(e))
return version 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): def do(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
""" """
Do the primary thing this module was intended to do. 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 re
import sqlite3
from Module import Module from Module import Module
@ -39,6 +40,7 @@ class Karma(Module):
self.karmare = re.compile(karmapattern) self.karmare = re.compile(karmapattern)
self.queryre = re.compile(querypattern) self.queryre = re.compile(querypattern)
def db_init(self):
# need to init the database if karma tables don't already exist # need to init the database if karma tables don't already exist
version = self.db_module_registered(self.__class__.__name__) version = self.db_module_registered(self.__class__.__name__)
if (version == None): if (version == None):
@ -65,7 +67,10 @@ class Karma(Module):
sql = 'INSERT INTO drbotzo_modules VALUES (?,?)' sql = 'INSERT INTO drbotzo_modules VALUES (?,?)'
conn.execute(sql, (self.__class__.__name__, 1)) conn.execute(sql, (self.__class__.__name__, 1))
conn.commit() 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): def do(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
"""look for karma strings at the start of messages""" """look for karma strings at the start of messages"""
@ -96,7 +101,9 @@ class Karma(Module):
''' '''
conn.execute(sql, (key, value, nick, userhost)) conn.execute(sql, (key, value, nick, userhost))
conn.commit() 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): def handle_karma_query(self, connection, nick, userhost, replypath, what):
match = self.queryre.match(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( reply = '{nick}: {key} has {value[0]!s} points of karma (rank {rank})'.format(
nick=nick, key=key, value=value, rank=rank) nick=nick, key=key, value=value, rank=rank)
except sqlite3.Error as e:
finally: conn.close() return self.reply(connection, replypath, "sqlite error: " + str(e))
self.reply(connection, replypath, reply) self.reply(connection, replypath, reply)