Python—12、DB操作

文档目录

  • 接口设计
    • 公共方法

接口设计

  • 动态加载

import sys

class UtilGetConnection:

    __DBtype=""
    __TypeTuple=("mysql","oracle","postgresql","gaussdb200","odps")


    def __init__(self,dbtype):
        self.__DBtype=dbtype.lower()
        if(self.__DBtype=="oracle"):
            pass
        elif(self.__DBtype in ("postrgres","greenplum")):
            pass
        elif(self.__DBtype=="gaussdb200"):
            pass
        else:
            print("the dbtype is not in {}".format(self.__DBtype))
            sys.exit()
            

备注:此处实现按照不同的数据库类型进行驱动动态加载;

  • 公共模块
# -*- coding= utf-8 -*-
"""
@DevTool  : PyCharm
@Author   : xxx
@DateTime : 2022/6/9 13:44
@FileName : BaseOperate.py
"""


class BaseOperate:
    def __init__(self):
        pass

    def sqlDqlOper(self, v_cursor, v_sql):
        pass

    def sqlDmlOper(self, v_cursor, v_sql):
        pass

    def sqlDdlOPer(self, v_cursor, v_sql):
        pass

    def sqlDclOper(self, v_cursor, v_sql):
        pass

备注:该模块作为公共模块或公共接口供不同类型数据库重写或重载;

公共方法

  • 空值转换

def nullToString(s_vaule):
    if s_vaule is None:
        return ""
    else:
        return s_vaule
  • 类型转换

def OutputTypeHandler(cursor, name, defaultType, size, precision, scale):
    if defaultType in (dataType_a, dataType_b):
        return cursor.var(str, size, cur.arraysize, outconverter=nullToString())
    elif defaultType in (dataType_c, dataType_d):
        return cursor.var(str, size, cur.arraysize, outconverter=nullToString())
    else:
        return cursor.var(str, size, cur.arraysize, outconverter=nullToString())
        

================================ over ========================================

你可能感兴趣的:(Python编程,数据库)