33 lines
961 B
Python
33 lines
961 B
Python
# This Python file uses the following encoding: utf-8
|
|
import mariadb
|
|
|
|
class DbManager(object):
|
|
__connection = None
|
|
__con_param = None
|
|
__dbmanager = None
|
|
|
|
def __new__(cls, dbconf = None):
|
|
if cls.__dbmanager is None:
|
|
cls.__dbmanager = super(DbManager, cls).__new__(cls)
|
|
cls.__dbmanager.__initializeConfig(dbconf)
|
|
|
|
return cls.__dbmanager
|
|
|
|
def getConnection(cls):
|
|
if not cls.__connection:
|
|
try:
|
|
cls.__connection = mariadb.connect(**cls.__con_param)
|
|
|
|
except mariadb.Error as e:
|
|
print("Connection parameters are wrong: {e}")
|
|
return cls.__connection
|
|
|
|
def __initializeConfig(cls, dbconf):
|
|
cls.__con_param = { 'user': dbconf['DB_USER'], 'password': dbconf['DB_PASS'],
|
|
'port': int (dbconf['DB_PORT']), 'host': dbconf['DB_HOST'],
|
|
'database': dbconf['DB_NAME']
|
|
}
|
|
|
|
|
|
|