# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   test63_idea
# FileName:      db.py
# Author:       Jing
# Datetime:     2024/11/4 20:01
# Description:
# 
# ---------------------------------------------------------------------------
from v3.read_data.Readbasicini import *
from v3 import log
import pymysql
class DB:
    def __init__(self):
        ini = Readbasicini()
        try:
            self.conn=pymysql.connect(
                host=ini.get_sql_msg('host'),
                port=int(ini.get_sql_msg('port')),
                user=ini.get_sql_msg('user'),
                password=ini.get_sql_msg('pwd'),
                database=ini.get_sql_msg('database'),
                charset="utf8"
            )
            self.cursor=self.conn.cursor()
        except Exception as e :
            log.error(f"数据库连接出错，请求检查ini配置文件，错误类型为：{type(e)},具体描述为{e}")
            raise e

    def delete(self,sql_sentence):
        log.info(f"执行delete方法，传入的参数为{sql_sentence}")
        if isinstance(sql_sentence,str) and sql_sentence.strip().lower().startswith("delete"):
            try:
                self.cursor.execute(sql_sentence)
                self.conn.commit()
            except Exception as e:
                log.error(f"{sql_sentence}执行发生异常，错误类型wei:{type(3)}，错误描述为：{e}")
                raise e
        else:
            log.error(f"执行的delete语句出错，传入的参数为{sql_sentence}")
            raise ValueError(f"{sql_sentence}该语句异常")

    def select(self,sql_sentence):
        log.info(f"执行select方法，传入的参数为{sql_sentence}")
        if isinstance(sql_sentence,str) and sql_sentence.strip().lower().startswith("select"):
            try:
                self.cursor.execute(sql_sentence)
            except Exception as e:
                log.error(f"{sql_sentence}执行发生异常，错误类型wei:{type(3)}，错误描述为：{e}")
                raise e
            else:
                select_result=self.cursor.fetchall()
                if select_result:
                    return select_result[0][0]
        else:
            log.error(f"执行的select语句出错，传入的参数为{sql_sentence}")
            raise ValueError(f"{sql_sentence}该语句异常")


if __name__ == '__main__':
    db = DB()
    sql = """select ID_ from uc_demension where CODE_="M1111";"""
    print(db.select(sql))