python对于cx_Oracle模块的封装

使用的是cx_Oracle-6.1版本

封装目的:在cx_Oracle模板发生变更时,便于框架代码的维护; 方便调用oracle

# -*- coding:utf-8 -*-

import os
import cx_Oracle
from Tools.Log import Log
from Tools.Singleton import Singleton
from Tools.Config import Config
from Tools.tools import decoLOG

os.environ['NLS_LANG'] = 'AMERICAN_AMERICA.AL32UTF8'

'''
    功能:      Oracle类
    作者:     郭可昌
    作成时间:  20180213
    更新内容:
    更新时间:
'''

class Oracle(Singleton):
    def __init__(self):
        cfg = Config()
        self.name = cfg.getItem("database", "NAME")
        self.password = cfg.getItem("database", "PASSWORD")
        self.url = cfg.getItem("database", "URL")
        self.cursor = None
        self.isClosed = True

    # oracle connect
    @decoLOG
    def open(self):
        try:
            self.conn = cx_Oracle.connect(self.name, self.password, self.url)
            self.cursor = cx_Oracle.Cursor(self.conn)
            self.isClosed = False
        except Exception as e:
            Log().logger.error("Oracle connect error![name=%s,passowrd=%s,url=%s]"%(self.name,self.password,self.url))
            self.cursor = None
            self.isClosed = True
        return self.cursor

    # oracle connected or not
    def isOpen(self):
        if self.cursor is not None:
            return True
        else:
            return False

    # close oracle connect
    @decoLOG
    def close(self):
        self.cursor.close()
        self.conn.close()
        self.isClosed = True

    # execute SQL
    # return:
    #       None: error
    #       empty list: 0 result
    #       normal list: normal result, a list of tuples
    @decoLOG
    def executeSQL(self, sql):
        if not self.isOpen():
            conn = self.open()
            if not self.isOpen():
                return None
        else:
            conn = self.cursor
        try:
            r = conn.execute(sql)
            sqlRes = r.fetchall()
        except Exception as e:
            Log().logger.error("Excute sql error![MSG=%s,SQL=%s]" % (str(e),sql))
            return None
        return sqlRes




 
  

你可能感兴趣的:(自动化测试框架)