From cabad5f7b3174ed0ed826137a6250fd892f7b50f Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Wed, 3 Aug 2016 22:31:24 +0200 Subject: [PATCH] Preparing for issuetools --- dbtools/__init__.py | 69 ++++++++++++++++++++++++++++++++++++++++++ issuetools/__init__.py | 0 2 files changed, 69 insertions(+) create mode 100644 dbtools/__init__.py create mode 100644 issuetools/__init__.py diff --git a/dbtools/__init__.py b/dbtools/__init__.py new file mode 100644 index 0000000..1f065c2 --- /dev/null +++ b/dbtools/__init__.py @@ -0,0 +1,69 @@ +import setuptools +import sqlite3, pymysql, pymysql.cursors + +SQLITE = 0 +MYSQL = 1 +MARIADB = MYSQL + +MIN = 0 +MAX = 1 + +class dbObject: + +# --------------------------------------------- Initialization ------------------------------------------------- + + def initMySQL(self, host, port, user, pwd, db): + self.conn = pymysql.connect(host = host, port = port, user = user, password = pwd, db = db, charset = "utf8mb4", cursorclass = pymysql.cursors.DictCursor) + self.cur = self.conn.cursor() + self.dbtype = MYSQL + self.host = host + self.port = port + self.user = user + self.pwd = pwd + self.db = db + + def initSQLite(self, path): + self.conn = sqlite3.connect(path) + self.cur = self.conn.cursor() + self.dbtype = SQLITE + self.path = path + + def __init__(self, dbtype = SQLITE, path = None, host = None, port = None, user = None, pwd = None, db = None): + + if dbtype == SQLITE: + self.initSQLite(path or 'Database.db') + + elif dbtype == MYSQL: + self.initMySQL(host or 'localhost', port or 3306, user, pwd, db) + + else: + raise ValueError("Unknown database type %s." % str(dbtype)) + +# ---------------------------------------------- No more initialization ---------------------------------------- + + def closeConnection(self): + return self.conn.close() + + def commit(self): + return self.conn.commit() + + def executeQuery(self, query): + return self.cur.execute(query) + + def getNext(self): + return self.cur.fetchone() + + def isInitialized(self): + try: + self.executeQuery("SELECT * FROM issues") + return True + except: + return False + +def dbHelper(): + if setuptools.dbtype() == SQLITE: + return dbObject(dbtype=SQLITE, path=setuptools.dbpath()) + elif setuptools.dbtype() == MYSQL: + return dbObject(dbtype=MYSQL, host=setuptools.dbhost(), user=setuptools.dbuser(), pwd=setuptools.dbpass(), db=setuptools.dbname()) + else: + raise setuptools.SetupException() diff --git a/issuetools/__init__.py b/issuetools/__init__.py new file mode 100644 index 0000000..e69de29