some work in progress getting plugins to work with sqlite3

This commit is contained in:
Daniel Folkinshteyn 2009-11-18 01:48:11 -05:00
parent a594d0e95c
commit 11446c7cb5
2 changed files with 36 additions and 34 deletions

View File

@ -41,7 +41,7 @@ import supybot.ircutils as ircutils
import supybot.callbacks as callbacks import supybot.callbacks as callbacks
try: try:
import sqlite import sqlite3 as sqlite
except ImportError: except ImportError:
raise callbacks.Error, 'You need to have PySQLite installed to use this ' \ raise callbacks.Error, 'You need to have PySQLite installed to use this ' \
'plugin. Download it at ' \ 'plugin. Download it at ' \
@ -126,11 +126,11 @@ class Factoids(callbacks.Plugin, plugins.ChannelDBHandler):
def learn(self, irc, msg, args, channel, key, factoid): def learn(self, irc, msg, args, channel, key, factoid):
db = self.getDb(channel) db = self.getDb(channel)
cursor = db.cursor() cursor = db.cursor()
cursor.execute("SELECT id, locked FROM keys WHERE key LIKE %s", key) cursor.execute("SELECT id, locked FROM keys WHERE key LIKE ?", (key,))
if cursor.rowcount == 0: if cursor.rowcount <= 0:
cursor.execute("""INSERT INTO keys VALUES (NULL, %s, 0)""", key) cursor.execute("""INSERT INTO keys VALUES (NULL, ?, 0)""", (key,))
db.commit() db.commit()
cursor.execute("SELECT id, locked FROM keys WHERE key LIKE %s",key) cursor.execute("SELECT id, locked FROM keys WHERE key LIKE ?", (key,))
(id, locked) = map(int, cursor.fetchone()) (id, locked) = map(int, cursor.fetchone())
capability = ircdb.makeChannelCapability(channel, 'factoids') capability = ircdb.makeChannelCapability(channel, 'factoids')
if not locked: if not locked:
@ -139,8 +139,8 @@ class Factoids(callbacks.Plugin, plugins.ChannelDBHandler):
else: else:
name = msg.nick name = msg.nick
cursor.execute("""INSERT INTO factoids VALUES cursor.execute("""INSERT INTO factoids VALUES
(NULL, %s, %s, %s, %s)""", (NULL, ?, ?, ?, ?)""",
id, name, int(time.time()), factoid) (id, name, int(time.time()), factoid))
db.commit() db.commit()
irc.replySuccess() irc.replySuccess()
else: else:
@ -160,9 +160,9 @@ class Factoids(callbacks.Plugin, plugins.ChannelDBHandler):
db = self.getDb(channel) db = self.getDb(channel)
cursor = db.cursor() cursor = db.cursor()
cursor.execute("""SELECT factoids.fact FROM factoids, keys cursor.execute("""SELECT factoids.fact FROM factoids, keys
WHERE keys.key LIKE %s AND factoids.key_id=keys.id WHERE keys.key LIKE ? AND factoids.key_id=keys.id
ORDER BY factoids.id ORDER BY factoids.id
LIMIT 20""", key) LIMIT 20""", (key,))
return [t[0] for t in cursor.fetchall()] return [t[0] for t in cursor.fetchall()]
def _replyFactoids(self, irc, msg, key, factoids, def _replyFactoids(self, irc, msg, key, factoids,
@ -229,7 +229,7 @@ class Factoids(callbacks.Plugin, plugins.ChannelDBHandler):
""" """
db = self.getDb(channel) db = self.getDb(channel)
cursor = db.cursor() cursor = db.cursor()
cursor.execute("UPDATE keys SET locked=1 WHERE key LIKE %s", key) cursor.execute("UPDATE keys SET locked=1 WHERE key LIKE ?", (key,))
db.commit() db.commit()
irc.replySuccess() irc.replySuccess()
lock = wrap(lock, ['channel', 'text']) lock = wrap(lock, ['channel', 'text'])
@ -243,7 +243,7 @@ class Factoids(callbacks.Plugin, plugins.ChannelDBHandler):
""" """
db = self.getDb(channel) db = self.getDb(channel)
cursor = db.cursor() cursor = db.cursor()
cursor.execute("UPDATE keys SET locked=0 WHERE key LIKE %s", key) cursor.execute("UPDATE keys SET locked=0 WHERE key LIKE ?", (key,))
db.commit() db.commit()
irc.replySuccess() irc.replySuccess()
unlock = wrap(unlock, ['channel', 'text']) unlock = wrap(unlock, ['channel', 'text'])
@ -271,25 +271,26 @@ class Factoids(callbacks.Plugin, plugins.ChannelDBHandler):
cursor = db.cursor() cursor = db.cursor()
cursor.execute("""SELECT keys.id, factoids.id cursor.execute("""SELECT keys.id, factoids.id
FROM keys, factoids FROM keys, factoids
WHERE key LIKE %s AND WHERE key LIKE ? AND
factoids.key_id=keys.id""", key) factoids.key_id=keys.id""", (key,))
if cursor.rowcount == 0: results = cursor.fetchall()
if len(results) == 0:
irc.error('There is no such factoid.') irc.error('There is no such factoid.')
elif cursor.rowcount == 1 or number is True: elif len(results) == 1 or number is True:
(id, _) = cursor.fetchone() (id, _) = results[0]
cursor.execute("""DELETE FROM factoids WHERE key_id=%s""", id) cursor.execute("""DELETE FROM factoids WHERE key_id=?""", (id,))
cursor.execute("""DELETE FROM keys WHERE key LIKE %s""", key) cursor.execute("""DELETE FROM keys WHERE key LIKE ?""", (key,))
db.commit() db.commit()
irc.replySuccess() irc.replySuccess()
else: else:
if number is not None: if number is not None:
results = cursor.fetchall() #results = cursor.fetchall()
try: try:
(_, id) = results[number-1] (_, id) = results[number-1]
except IndexError: except IndexError:
irc.error('Invalid factoid number.') irc.error('Invalid factoid number.')
return return
cursor.execute("DELETE FROM factoids WHERE id=%s", id) cursor.execute("DELETE FROM factoids WHERE id=?", (id,))
db.commit() db.commit()
irc.replySuccess() irc.replySuccess()
else: else:
@ -313,7 +314,7 @@ class Factoids(callbacks.Plugin, plugins.ChannelDBHandler):
if cursor.rowcount != 0: if cursor.rowcount != 0:
L = [] L = []
for (factoid, id) in cursor.fetchall(): for (factoid, id) in cursor.fetchall():
cursor.execute("""SELECT key FROM keys WHERE id=%s""", id) cursor.execute("""SELECT key FROM keys WHERE id=?""", (id,))
(key,) = cursor.fetchone() (key,) = cursor.fetchone()
L.append('"%s": %s' % (ircutils.bold(key), factoid)) L.append('"%s": %s' % (ircutils.bold(key), factoid))
irc.reply('; '.join(L)) irc.reply('; '.join(L))
@ -330,14 +331,14 @@ class Factoids(callbacks.Plugin, plugins.ChannelDBHandler):
""" """
db = self.getDb(channel) db = self.getDb(channel)
cursor = db.cursor() cursor = db.cursor()
cursor.execute("SELECT id, locked FROM keys WHERE key LIKE %s", key) cursor.execute("SELECT id, locked FROM keys WHERE key LIKE ?", (key,))
if cursor.rowcount == 0: if cursor.rowcount == 0:
irc.error('No factoid matches that key.') irc.error('No factoid matches that key.')
return return
(id, locked) = map(int, cursor.fetchone()) (id, locked) = map(int, cursor.fetchone())
cursor.execute("""SELECT added_by, added_at FROM factoids cursor.execute("""SELECT added_by, added_at FROM factoids
WHERE key_id=%s WHERE key_id=?
ORDER BY id""", id) ORDER BY id""", (id,))
factoids = cursor.fetchall() factoids = cursor.fetchall()
L = [] L = []
counter = 0 counter = 0
@ -364,16 +365,17 @@ class Factoids(callbacks.Plugin, plugins.ChannelDBHandler):
cursor = db.cursor() cursor = db.cursor()
cursor.execute("""SELECT factoids.id, factoids.fact cursor.execute("""SELECT factoids.id, factoids.fact
FROM keys, factoids FROM keys, factoids
WHERE keys.key LIKE %s AND WHERE keys.key LIKE ? AND
keys.id=factoids.key_id""", key) keys.id=factoids.key_id""", (key,))
if cursor.rowcount == 0: results = cursor.fetchall()
if len(results) == 0:
irc.error(format('I couldn\'t find any key %q', key)) irc.error(format('I couldn\'t find any key %q', key))
return return
elif cursor.rowcount < number: elif len(results) < number:
irc.errorInvalid('key id') irc.errorInvalid('key id')
(id, fact) = cursor.fetchall()[number-1] (id, fact) = results[number-1]
newfact = replacer(fact) newfact = replacer(fact)
cursor.execute("UPDATE factoids SET fact=%s WHERE id=%s", newfact, id) cursor.execute("UPDATE factoids SET fact=? WHERE id=?", (newfact, id))
db.commit() db.commit()
irc.replySuccess() irc.replySuccess()
change = wrap(change, ['channel', 'something', change = wrap(change, ['channel', 'something',
@ -408,10 +410,10 @@ class Factoids(callbacks.Plugin, plugins.ChannelDBHandler):
db.create_function(predicateName, 1, p) db.create_function(predicateName, 1, p)
predicateName += 'p' predicateName += 'p'
for glob in globs: for glob in globs:
criteria.append('TARGET LIKE %s') criteria.append('TARGET LIKE ?')
formats.append(glob.translate(self._sqlTrans)) formats.append(glob.translate(self._sqlTrans))
cursor = db.cursor() cursor = db.cursor()
sql = """SELECT keys.key FROM %s WHERE %s""" % \ sql = """SELECT keys.key FROM ? WHERE ?""" % \
(', '.join(tables), ' AND '.join(criteria)) (', '.join(tables), ' AND '.join(criteria))
sql = sql.replace('TARGET', target) sql = sql.replace('TARGET', target)
cursor.execute(sql, formats) cursor.execute(sql, formats)

View File

@ -62,7 +62,7 @@ try:
mxCrap[name] = module mxCrap[name] = module
sys.modules.pop(name) sys.modules.pop(name)
# Now that the mx crap is gone, we can import sqlite. # Now that the mx crap is gone, we can import sqlite.
import sqlite import sqlite3 as sqlite
# And now we'll put it back, even though it sucks. # And now we'll put it back, even though it sucks.
sys.modules.update(mxCrap) sys.modules.update(mxCrap)
# Just in case, we'll do this as well. It doesn't seem to work fine by # Just in case, we'll do this as well. It doesn't seem to work fine by
@ -176,7 +176,7 @@ class ChannelDBHandler(object):
db = self.makeDb(self.makeFilename(channel)) db = self.makeDb(self.makeFilename(channel))
else: else:
db = self.dbCache[channel] db = self.dbCache[channel]
db.autocommit = 1 db.isolation_level = None
return db return db
def die(self): def die(self):