summaryrefslogtreecommitdiffstats
path: root/modules/mysql.py
blob: 5ae9e93be8ad20b87cf81e458c5aeab9310e7b01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from . import ModuleBase
import MySQLdb

class Module(ModuleBase):
    def __init__(self, manager):
        ModuleBase.__init__(self, manager)
        
        self.conf = manager.config['mysql']
        
        self.db = False
        self._connect()
    
    def _connect(self):
        if self.db:
            self.db.close()
        self.db = MySQLdb.connect(host = self.conf['host'], user = self.conf['user'], passwd = self.conf['passwd'], db = self.conf['db'], use_unicode = True, charset = 'utf8')        
    
    def cursor(self):
        try:
            self.db.ping()
            return self.db.cursor()
        except MySQLdb.OperationalError:
            self._connect()
            return self.db.cursor()