# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   test64
# FileName:      db.py
# Author:       lao_zhao
# Datetime:     2024/12/26 14:00
# Description:
# 
# ---------------------------------------------------------------------------
import pymysql
from APIAutoTest_v3 import log_decorator
from APIAutoTest_v3.common.read_basic_ini import ReadBasicIni


class DB:
    @log_decorator
    def __init__(self):
        """连接数据库，获取连接对象和游标对象"""
        ini = ReadBasicIni()
        self.conn = pymysql.connect(
            host=ini.get_database_conn_msg("host"),
            port=int(ini.get_database_conn_msg("port")),
            user=ini.get_database_conn_msg("user"),
            password=ini.get_database_conn_msg("pwd"),
            database=ini.get_database_conn_msg("database"),
            charset="utf8"
        )
        self.cursor = self.conn.cursor()


    @log_decorator
    def close(self):
        """关闭游标对象和连接对象"""
        self.cursor.close()
        self.conn.close()

    @log_decorator
    def delete(self,sql_data):
        """执行删除的sql语句"""
        self.cursor.execute(sql_data)
        self.conn.commit()

    @log_decorator
    def select(self, sql_data):
        """执行查询的sql语句"""
        self.cursor.execute(sql_data)
        select_res = self.cursor.fetchall()
        if select_res:
            return select_res[0][0]