# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   Selenium_Project
# FileName:      db.py
# Author:       xxxxxxx
# Datetime:     2024/5/20 19:46
# Description:
# 命名规范：文件名全小写+下划线，类名大驼峰，方法和变量小写+下划线连接，
# 常量大写，变量和常量用名词，方法用动词
# ---------------------------------------------------------------------------
import pymysql

from api_test_0520 import log_decorator
from api_test_0520.common.readini import ReadIni


class DB:
    @log_decorator
    def __init__(self):
        ini = ReadIni()
        try:
            self.conn = pymysql.connect(
                host=ini.get_db_data("host"),
                port=int(ini.get_db_data("port")),
                user=ini.get_db_data("username"),
                password=ini.get_db_data("password"),
                database=ini.get_db_data("database"),
                charset="utf8"
            )
            self.course = self.conn.cursor()
        except Exception as e:
            raise e

    @log_decorator
    def close(self):
        self.conn.cursor()
        self.conn.close()

    @log_decorator
    def delete_func(self, sql):
        try:
            self.course.execute(sql)
            self.conn.commit()
        except Exception as e:
            print(e)
            raise e

    @log_decorator
    def select_func(self, sql):
        try:
            self.course.execute(sql)
            value = self.course.fetchall()
        except Exception as e:
            raise e
        else:
            if value:
                return value[0][0]

if __name__ == '__main__':
    db = DB()