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