work on getting moobotfactoids to work with sqlite3

note: needs sqlite3 version > 3.3.1, so that it is not restricted against cross-thread usage.
This commit is contained in:
Daniel Folkinshteyn 2009-11-18 22:47:56 -05:00
parent b77c649c8a
commit e303cab7ae

View File

@ -98,7 +98,7 @@ class SqliteMoobotDB(object):
def _getDb(self, channel): def _getDb(self, channel):
try: try:
import sqlite import sqlite3 as sqlite
except ImportError: except ImportError:
raise callbacks.Error, \ raise callbacks.Error, \
'You need to have PySQLite installed to use this ' \ 'You need to have PySQLite installed to use this ' \
@ -133,11 +133,12 @@ class SqliteMoobotDB(object):
db = self._getDb(channel) db = self._getDb(channel)
cursor = db.cursor() cursor = db.cursor()
cursor.execute("""SELECT fact FROM factoids cursor.execute("""SELECT fact FROM factoids
WHERE key LIKE %s""", key) WHERE key LIKE ?""", (key,))
if cursor.rowcount == 0: results = cursor.fetchall()
if len(results) == 0:
return None return None
else: else:
return cursor.fetchall()[0] return results[0]
def getFactinfo(self, channel, key): def getFactinfo(self, channel, key):
db = self._getDb(channel) db = self._getDb(channel)
@ -147,63 +148,65 @@ class SqliteMoobotDB(object):
last_requested_by, last_requested_at, last_requested_by, last_requested_at,
requested_count, locked_by, locked_at requested_count, locked_by, locked_at
FROM factoids FROM factoids
WHERE key LIKE %s""", key) WHERE key LIKE ?""", (key,))
if cursor.rowcount == 0: results = cursor.fetchall()
if len(results) == 0:
return None return None
else: else:
return cursor.fetchone() return results[0]
def randomFactoid(self, channel): def randomFactoid(self, channel):
db = self._getDb(channel) db = self._getDb(channel)
cursor = db.cursor() cursor = db.cursor()
cursor.execute("""SELECT fact, key FROM factoids cursor.execute("""SELECT fact, key FROM factoids
ORDER BY random() LIMIT 1""") ORDER BY random() LIMIT 1""")
if cursor.rowcount == 0: results = cursor.fetchall()
if len(results) == 0:
return None return None
else: else:
return cursor.fetchone() return results[0]
def addFactoid(self, channel, key, value, creator_id): def addFactoid(self, channel, key, value, creator_id):
db = self._getDb(channel) db = self._getDb(channel)
cursor = db.cursor() cursor = db.cursor()
cursor.execute("""INSERT INTO factoids VALUES cursor.execute("""INSERT INTO factoids VALUES
(%s, %s, %s, NULL, NULL, NULL, NULL, (?, ?, ?, NULL, NULL, NULL, NULL,
NULL, NULL, %s, 0)""", NULL, NULL, ?, 0)""",
key, creator_id, int(time.time()), value) (key, creator_id, int(time.time()), value))
db.commit() db.commit()
def updateFactoid(self, channel, key, newvalue, modifier_id): def updateFactoid(self, channel, key, newvalue, modifier_id):
db = self._getDb(channel) db = self._getDb(channel)
cursor = db.cursor() cursor = db.cursor()
cursor.execute("""UPDATE factoids cursor.execute("""UPDATE factoids
SET fact=%s, modified_by=%s, SET fact=?, modified_by=?,
modified_at=%s WHERE key LIKE %s""", modified_at=? WHERE key LIKE ?""",
newvalue, modifier_id, int(time.time()), key) (newvalue, modifier_id, int(time.time()), key))
db.commit() db.commit()
def updateRequest(self, channel, key, hostmask): def updateRequest(self, channel, key, hostmask):
db = self._getDb(channel) db = self._getDb(channel)
cursor = db.cursor() cursor = db.cursor()
cursor.execute("""UPDATE factoids SET cursor.execute("""UPDATE factoids SET
last_requested_by = %s, last_requested_by = ?,
last_requested_at = %s, last_requested_at = ?,
requested_count = requested_count + 1 requested_count = requested_count + 1
WHERE key = %s""", WHERE key = ?""",
hostmask, int(time.time()), key) (hostmask, int(time.time()), key))
db.commit() db.commit()
def removeFactoid(self, channel, key): def removeFactoid(self, channel, key):
db = self._getDb(channel) db = self._getDb(channel)
cursor = db.cursor() cursor = db.cursor()
cursor.execute("""DELETE FROM factoids WHERE key LIKE %s""", cursor.execute("""DELETE FROM factoids WHERE key LIKE ?""",
key) (key,))
db.commit() db.commit()
def locked(self, channel, key): def locked(self, channel, key):
db = self._getDb(channel) db = self._getDb(channel)
cursor = db.cursor() cursor = db.cursor()
cursor.execute ("""SELECT locked_by FROM factoids cursor.execute ("""SELECT locked_by FROM factoids
WHERE key LIKE %s""", key) WHERE key LIKE ?""", (key,))
if cursor.fetchone()[0] is None: if cursor.fetchone()[0] is None:
return False return False
else: else:
@ -213,17 +216,17 @@ class SqliteMoobotDB(object):
db = self._getDb(channel) db = self._getDb(channel)
cursor = db.cursor() cursor = db.cursor()
cursor.execute("""UPDATE factoids cursor.execute("""UPDATE factoids
SET locked_by=%s, locked_at=%s SET locked_by=?, locked_at=?
WHERE key LIKE %s""", WHERE key LIKE ?""",
locker_id, int(time.time()), key) (locker_id, int(time.time()), key))
db.commit() db.commit()
def unlock(self, channel, key): def unlock(self, channel, key):
db = self._getDb(channel) db = self._getDb(channel)
cursor = db.cursor() cursor = db.cursor()
cursor.execute("""UPDATE factoids cursor.execute("""UPDATE factoids
SET locked_by=%s, locked_at=%s SET locked_by=?, locked_at=?
WHERE key LIKE %s""", None, None, key) WHERE key LIKE ?""", (None, None, key))
db.commit() db.commit()
def mostAuthored(self, channel, limit): def mostAuthored(self, channel, limit):
@ -231,14 +234,14 @@ class SqliteMoobotDB(object):
cursor = db.cursor() cursor = db.cursor()
cursor.execute("""SELECT created_by, count(key) FROM factoids cursor.execute("""SELECT created_by, count(key) FROM factoids
GROUP BY created_by GROUP BY created_by
ORDER BY count(key) DESC LIMIT %s""", limit) ORDER BY count(key) DESC LIMIT ?""", (limit,))
return cursor.fetchall() return cursor.fetchall()
def mostRecent(self, channel, limit): def mostRecent(self, channel, limit):
db = self._getDb(channel) db = self._getDb(channel)
cursor = db.cursor() cursor = db.cursor()
cursor.execute("""SELECT key FROM factoids cursor.execute("""SELECT key FROM factoids
ORDER BY created_at DESC LIMIT %s""", limit) ORDER BY created_at DESC LIMIT ?""", (limit,))
return cursor.fetchall() return cursor.fetchall()
def mostPopular(self, channel, limit): def mostPopular(self, channel, limit):
@ -246,43 +249,35 @@ class SqliteMoobotDB(object):
cursor = db.cursor() cursor = db.cursor()
cursor.execute("""SELECT key, requested_count FROM factoids cursor.execute("""SELECT key, requested_count FROM factoids
WHERE requested_count > 0 WHERE requested_count > 0
ORDER BY requested_count DESC LIMIT %s""", limit) ORDER BY requested_count DESC LIMIT ?""", (limit,))
if cursor.rowcount == 0: results = cursor.fetchall()
return [] return results
else:
return cursor.fetchall()
def getKeysByAuthor(self, channel, authorId): def getKeysByAuthor(self, channel, authorId):
db = self._getDb(channel) db = self._getDb(channel)
cursor = db.cursor() cursor = db.cursor()
cursor.execute("""SELECT key FROM factoids WHERE created_by=%s cursor.execute("""SELECT key FROM factoids WHERE created_by=?
ORDER BY key""", authorId) ORDER BY key""", (authorId,))
if cursor.rowcount == 0: results = cursor.fetchall()
return [] return results
else:
return cursor.fetchall()
def getKeysByGlob(self, channel, glob): def getKeysByGlob(self, channel, glob):
db = self._getDb(channel) db = self._getDb(channel)
cursor = db.cursor() cursor = db.cursor()
glob = '%%%s%%' % glob glob = '%%%s%%' % glob
cursor.execute("""SELECT key FROM factoids WHERE key LIKE %s cursor.execute("""SELECT key FROM factoids WHERE key LIKE ?
ORDER BY key""", glob) ORDER BY key""", (glob,))
if cursor.rowcount == 0: results = cursor.fetchall()
return [] return results
else:
return cursor.fetchall()
def getKeysByValueGlob(self, channel, glob): def getKeysByValueGlob(self, channel, glob):
db = self._getDb(channel) db = self._getDb(channel)
cursor = db.cursor() cursor = db.cursor()
glob = '%%%s%%' % glob glob = '%%%s%%' % glob
cursor.execute("""SELECT key FROM factoids WHERE fact LIKE %s cursor.execute("""SELECT key FROM factoids WHERE fact LIKE ?
ORDER BY key""", glob) ORDER BY key""", (glob,))
if cursor.rowcount == 0: results = cursor.fetchall()
return [] return results
else:
return cursor.fetchall()
MoobotDB = plugins.DB('MoobotFactoids', {'sqlite': SqliteMoobotDB}) MoobotDB = plugins.DB('MoobotFactoids', {'sqlite': SqliteMoobotDB})