update quotegrabs to sqlite3

This commit is contained in:
Daniel Folkinshteyn 2010-04-21 01:24:13 -04:00 committed by Valentin Lorentz
parent 4af9d8735f
commit 92fc1e308f

View File

@ -43,6 +43,15 @@ import supybot.callbacks as callbacks
from supybot.i18n import PluginInternationalization, internationalizeDocstring from supybot.i18n import PluginInternationalization, internationalizeDocstring
_ = PluginInternationalization('QuoteGrabs') _ = PluginInternationalization('QuoteGrabs')
try:
import sqlite3
except ImportError:
from pysqlite2 import dbapi2 as sqlite3 # for python2.4
import traceback
#sqlite3.register_converter('bool', bool)
class QuoteGrabsRecord(dbi.Record): class QuoteGrabsRecord(dbi.Record):
__fields__ = [ __fields__ = [
'by', 'by',
@ -67,29 +76,27 @@ class SqliteQuoteGrabsDB(object):
db.close() db.close()
def _getDb(self, channel): def _getDb(self, channel):
try:
import sqlite
except ImportError:
raise callbacks.Error, 'You need to have PySQLite installed to ' \
'use QuoteGrabs. Download it at ' \
'<http://code.google.com/p/pysqlite/>'
filename = plugins.makeChannelFilename(self.filename, channel) filename = plugins.makeChannelFilename(self.filename, channel)
def p(s1, s2): def p(s1, s2):
return int(ircutils.nickEqual(s1, s2)) # text_factory seems to only apply as an output adapter,
# so doesn't apply to created functions; so we use str()
return ircutils.nickEqual(str(s1), str(s2))
if filename in self.dbs: if filename in self.dbs:
return self.dbs[filename] return self.dbs[filename]
if os.path.exists(filename): if os.path.exists(filename):
self.dbs[filename] = sqlite.connect(filename, db = sqlite3.connect(filename)
converters={'bool': bool}) db.text_factory = str
self.dbs[filename].create_function('nickeq', 2, p) db.create_function('nickeq', 2, p)
return self.dbs[filename] self.dbs[filename] = db
db = sqlite.connect(filename, converters={'bool': bool}) return db
db = sqlite3.connect(filename)
db.text_factory = str
db.create_function('nickeq', 2, p)
self.dbs[filename] = db self.dbs[filename] = db
self.dbs[filename].create_function('nickeq', 2, p)
cursor = db.cursor() cursor = db.cursor()
cursor.execute("""CREATE TABLE quotegrabs ( cursor.execute("""CREATE TABLE quotegrabs (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
nick TEXT, nick BLOB,
hostmask TEXT, hostmask TEXT,
added_by TEXT, added_by TEXT,
added_at TIMESTAMP, added_at TIMESTAMP,
@ -102,10 +109,11 @@ class SqliteQuoteGrabsDB(object):
db = self._getDb(channel) db = self._getDb(channel)
cursor = db.cursor() cursor = db.cursor()
cursor.execute("""SELECT id, nick, quote, hostmask, added_at, added_by cursor.execute("""SELECT id, nick, quote, hostmask, added_at, added_by
FROM quotegrabs WHERE id = %s""", id) FROM quotegrabs WHERE id = ?""", (id,))
if cursor.rowcount == 0: results = cursor.fetchall()
if len(results) == 0:
raise dbi.NoRecordError raise dbi.NoRecordError
(id, by, quote, hostmask, at, grabber) = cursor.fetchone() (id, by, quote, hostmask, at, grabber) = results[0]
return QuoteGrabsRecord(id, by=by, text=quote, hostmask=hostmask, return QuoteGrabsRecord(id, by=by, text=quote, hostmask=hostmask,
at=int(at), grabber=grabber) at=int(at), grabber=grabber)
@ -114,46 +122,50 @@ class SqliteQuoteGrabsDB(object):
cursor = db.cursor() cursor = db.cursor()
if nick: if nick:
cursor.execute("""SELECT quote FROM quotegrabs cursor.execute("""SELECT quote FROM quotegrabs
WHERE nickeq(nick, %s) WHERE nickeq(nick, ?)
ORDER BY random() LIMIT 1""", ORDER BY random() LIMIT 1""",
nick) (nick,))
else: else:
cursor.execute("""SELECT quote FROM quotegrabs cursor.execute("""SELECT quote FROM quotegrabs
ORDER BY random() LIMIT 1""") ORDER BY random() LIMIT 1""")
if cursor.rowcount == 0: results = cursor.fetchall()
if len(results) == 0:
raise dbi.NoRecordError raise dbi.NoRecordError
return cursor.fetchone()[0] return results[0][0]
def list(self, channel, nick): def list(self, channel, nick):
db = self._getDb(channel) db = self._getDb(channel)
cursor = db.cursor() cursor = db.cursor()
cursor.execute("""SELECT id, quote FROM quotegrabs cursor.execute("""SELECT id, quote FROM quotegrabs
WHERE nickeq(nick, %s) WHERE nickeq(nick, ?)
ORDER BY id DESC""", nick) ORDER BY id DESC""", (nick,))
if cursor.rowcount == 0: results = cursor.fetchall()
if len(results) == 0:
raise dbi.NoRecordError raise dbi.NoRecordError
return [QuoteGrabsRecord(id, text=quote) return [QuoteGrabsRecord(id, text=quote)
for (id, quote) in cursor.fetchall()] for (id, quote) in results]
def getQuote(self, channel, nick): def getQuote(self, channel, nick):
db = self._getDb(channel) db = self._getDb(channel)
cursor = db.cursor() cursor = db.cursor()
cursor.execute("""SELECT quote FROM quotegrabs cursor.execute("""SELECT quote FROM quotegrabs
WHERE nickeq(nick, %s) WHERE nickeq(nick, ?)
ORDER BY id DESC LIMIT 1""", nick) ORDER BY id DESC LIMIT 1""", (nick,))
if cursor.rowcount == 0: results = cursor.fetchall()
if len(results) == 0:
raise dbi.NoRecordError raise dbi.NoRecordError
return cursor.fetchone()[0] return results[0][0]
def select(self, channel, nick): def select(self, channel, nick):
db = self._getDb(channel) db = self._getDb(channel)
cursor = db.cursor() cursor = db.cursor()
cursor.execute("""SELECT added_at FROM quotegrabs cursor.execute("""SELECT added_at FROM quotegrabs
WHERE nickeq(nick, %s) WHERE nickeq(nick, ?)
ORDER BY id DESC LIMIT 1""", nick) ORDER BY id DESC LIMIT 1""", (nick,))
if cursor.rowcount == 0: results = cursor.fetchall()
if len(results) == 0:
raise dbi.NoRecordError raise dbi.NoRecordError
return cursor.fetchone()[0] return results[0][0]
def add(self, channel, msg, by): def add(self, channel, msg, by):
db = self._getDb(channel) db = self._getDb(channel)
@ -161,14 +173,15 @@ class SqliteQuoteGrabsDB(object):
text = ircmsgs.prettyPrint(msg) text = ircmsgs.prettyPrint(msg)
# Check to see if the latest quotegrab is identical # Check to see if the latest quotegrab is identical
cursor.execute("""SELECT quote FROM quotegrabs cursor.execute("""SELECT quote FROM quotegrabs
WHERE nick=%s WHERE nick=?
ORDER BY id DESC LIMIT 1""", msg.nick) ORDER BY id DESC LIMIT 1""", (msg.nick,))
if cursor.rowcount != 0: results = cursor.fetchall()
if text == cursor.fetchone()[0]: if len(results) != 0:
if text == results[0][0]:
return return
cursor.execute("""INSERT INTO quotegrabs cursor.execute("""INSERT INTO quotegrabs
VALUES (NULL, %s, %s, %s, %s, %s)""", VALUES (NULL, ?, ?, ?, ?, ?)""",
msg.nick, msg.prefix, by, int(time.time()), text) (msg.nick, msg.prefix, by, int(time.time()), text,))
db.commit() db.commit()
def remove(self, channel, grab=None): def remove(self, channel, grab=None):
@ -179,14 +192,16 @@ class SqliteQuoteGrabsDB(object):
# strictly unnecessary -- the DELETE operation would "succeed" # strictly unnecessary -- the DELETE operation would "succeed"
# anyway, but it's silly to just keep saying 'OK' no matter what, # anyway, but it's silly to just keep saying 'OK' no matter what,
# so... # so...
cursor.execute("""SELECT * FROM quotegrabs WHERE id = %s""", grab) cursor.execute("""SELECT * FROM quotegrabs WHERE id = ?""", (grab,))
if cursor.rowcount == 0: results = cursor.fetchall()
if len(results) == 0:
raise dbi.NoRecordError raise dbi.NoRecordError
cursor.execute("""DELETE FROM quotegrabs WHERE id = %s""", grab) cursor.execute("""DELETE FROM quotegrabs WHERE id = ?""", (grab,))
else: else:
cursor.execute("""SELECT * FROM quotegrabs WHERE id = (SELECT MAX(id) cursor.execute("""SELECT * FROM quotegrabs WHERE id = (SELECT MAX(id)
FROM quotegrabs)""") FROM quotegrabs)""")
if cursor.rowcount == 0: results = cursor.fetchall()
if len(results) == 0:
raise dbi.NoRecordError raise dbi.NoRecordError
cursor.execute("""DELETE FROM quotegrabs WHERE id = (SELECT MAX(id) cursor.execute("""DELETE FROM quotegrabs WHERE id = (SELECT MAX(id)
FROM quotegrabs)""") FROM quotegrabs)""")
@ -197,14 +212,15 @@ class SqliteQuoteGrabsDB(object):
cursor = db.cursor() cursor = db.cursor()
text = '%' + text + '%' text = '%' + text + '%'
cursor.execute("""SELECT id, nick, quote FROM quotegrabs cursor.execute("""SELECT id, nick, quote FROM quotegrabs
WHERE quote LIKE %s WHERE quote LIKE ?
ORDER BY id DESC""", text) ORDER BY id DESC""", (text,))
if cursor.rowcount == 0: results = cursor.fetchall()
if len(results) == 0:
raise dbi.NoRecordError raise dbi.NoRecordError
return [QuoteGrabsRecord(id, text=quote, by=nick) return [QuoteGrabsRecord(id, text=quote, by=nick)
for (id, nick, quote) in cursor.fetchall()] for (id, nick, quote) in results]
QuoteGrabsDB = plugins.DB('QuoteGrabs', {'sqlite': SqliteQuoteGrabsDB}) QuoteGrabsDB = plugins.DB('QuoteGrabs', {'sqlite3': SqliteQuoteGrabsDB})
class QuoteGrabs(callbacks.Plugin): class QuoteGrabs(callbacks.Plugin):
"""Add the help for "@help QuoteGrabs" here.""" """Add the help for "@help QuoteGrabs" here."""