pyircbot/pyircbot/modules/SQLite.py

82 lines
2.4 KiB
Python
Raw Normal View History

2014-10-07 19:43:08 -07:00
#!/usr/bin/env python
"""
.. module:: SQLite
2015-11-01 18:03:11 -08:00
:synopsis: Module providing a sqlite type service
2014-10-07 19:43:08 -07:00
.. moduleauthor:: Dave Pedu <dave@davepedu.com>
"""
2015-06-18 19:37:08 -07:00
from pyircbot.modulebase import ModuleBase
2014-10-07 19:43:08 -07:00
import sqlite3
2017-01-01 14:59:01 -08:00
2014-10-07 19:43:08 -07:00
class SQLite(ModuleBase):
2015-11-01 18:03:11 -08:00
def __init__(self, bot, moduleName):
2017-01-01 14:59:01 -08:00
ModuleBase.__init__(self, bot, moduleName)
self.services = ["sqlite"]
2015-11-01 18:03:11 -08:00
def opendb(self, dbname):
return Connection(self, dbname)
2017-01-01 14:59:01 -08:00
2014-10-07 19:43:08 -07:00
class Connection:
2015-11-01 18:03:11 -08:00
def __init__(self, master, dbname):
self.master = master
self.log = master.log
self.dbname = dbname
self.connection = None
self._connect()
2017-01-01 14:59:01 -08:00
2015-11-01 18:03:11 -08:00
# Check if the table requested exists
def tableExists(self, tablename):
c = self.getCursor()
c.execute("SELECT * FROM SQLITE_MASTER WHERE `type`='table' AND `name`=?", (tablename,))
tables = c.fetchall()
2017-01-01 14:59:01 -08:00
if len(tables) == 0:
return False
2015-11-01 18:03:11 -08:00
return True
2017-01-01 14:59:01 -08:00
2015-11-01 18:03:11 -08:00
@staticmethod
def dict_factory(cursor, row):
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d
2014-10-07 19:43:08 -07:00
2015-11-01 18:03:11 -08:00
def query(self, queryText, args=()):
"""Execute a Sqlite query and return the cursor
2017-01-01 14:59:01 -08:00
2015-11-01 18:03:11 -08:00
:param queryText: the sqlite query as a string, using '%s' for token replacement
:type queryText: str
:param args: arguments to be escaped into the query
:type args: tuple
:returns: cursor -- the sql cursor"""
c = self.getCursor()
2017-01-01 14:59:01 -08:00
if len(args) == 0:
2015-11-01 18:03:11 -08:00
c.execute(queryText)
else:
c.execute(queryText, args)
return c
2017-01-01 14:59:01 -08:00
2015-11-01 18:03:11 -08:00
# Returns a cusor object, after checking for connectivity
def getCursor(self):
2017-01-01 14:59:01 -08:00
c = self.connection.cursor()
2015-11-01 18:03:11 -08:00
return c
2017-01-01 14:59:01 -08:00
2017-12-03 20:54:39 -08:00
# Opens the sqlite database / attempts to create it if it doesn't exist yet
2015-11-01 18:03:11 -08:00
def _connect(self):
self.log.info("Sqlite: opening database %s" % self.master.getFilePath(self.dbname))
self.connection = sqlite3.connect(self.master.getFilePath(self.dbname), check_same_thread=False)
self.connection.row_factory = Connection.dict_factory
self.connection.isolation_level = None
self.log.info("Sqlite: Connected.")
2017-01-01 14:59:01 -08:00
2015-11-01 18:03:11 -08:00
# Test the connection
c = self.connection.cursor()
2017-01-01 14:59:01 -08:00
derp = c.execute("SELECT * FROM SQLITE_MASTER") # NOQA
2015-11-01 18:03:11 -08:00
c.close()
2017-01-01 14:59:01 -08:00
2015-11-01 18:03:11 -08:00
def close(self):
self.connection.close()