# -*- coding:utf-8 -*-
# 作者：齐振鋆
# 日期：2023/1/10
# 描述:
import pymysql

from unit.public.YamlUtil import UtilYaml


class MysqlDataBase(object):
    __init_flag = False
    __instance = None

    def __new__(cls, connect_info):
        cls.__instance = object.__new__(cls) if cls.__instance is None else cls.__instance
        return cls.__instance

    def __init__(self, connect_info):
        if not MysqlDataBase.__init_flag:
            MysqlDataBase.__init_flag = True
            self.__db = pymysql.connect(host=connect_info['DB_HOST'],
                                        port=connect_info['DB_PORT'],
                                        user=connect_info['DB_USER'],
                                        passwd=connect_info['DB_PASSWD'],
                                        db=connect_info['DB_NAME'], charset='utf8')
            self.cursor = self.__db.cursor()

    def exec(self, sql):
        self.cursor.execute(sql)
        return self.cursor.fetchall()

    def commit(self):
        self.__db.commit()

    def final(self):
        self.cursor.close()
        self.__db.close()
