# Path: Utils.py
# def areEquals(o1,o2):
# 	'''
# 	retourne True si o1 == o2 (case insensitive)
# 	'''
# 	if o1 == None : 
# 		o1 = ""
# 	if o2 == None : 
# 		o2 = ""
# 	if type(o1) is datetime : 
# 		o1 = o1.ctime()
# 	if type(o2) is datetime : 
# 		o2 = o2.ctime()
# 	if type(o1) is str and type(o2) is str:
# 		if o1.lower() == o2.lower() : return True
# 		else : return False
# 	else : logging.error("Bad comparison in the areEquals function: o1:{0}, o2:{1}".format(type(o1),type(o2)))
# 
# def checkOracleVersion(args):
# 	'''
# 	'''
# 	VERSIONS_TESTED = [11]
# 	cursorRep = execThisQuery(args,"select * from product_component_version",["PRODUCT","VERSION","STATUS"])
# 	for l in cursorRep:
# 		if "Oracle Database" in l['PRODUCT']:
# 			logging.info("The '{0}' version is: {1}".format(l['PRODUCT'],l['VERSION']))
# 	if l['VERSION'][:2] in VERSIONS_TESTED : logging.warn("The version of the Oracle database is not in {0}".format(', '.join(VERSIONS_TESTED)))
# 
# def getOracleConnection(args, connectId):
# 	'''
# 	Return an Oracle object connected to the database thanks to the Oracle connection string (connectId)
# 	'''
# 	try: 
# 		if args['SYSDBA'] == True :	
# 			return cx_Oracle.connect(connectId, mode=cx_Oracle.SYSDBA)
# 		elif args['SYSOPER'] == True :	
# 			return cx_Oracle.connect(connectId, mode=cx_Oracle.SYSOPER)
# 		else :
# 			return cx_Oracle.connect(connectId)
# 	except Exception as e:
# 		logging.error("Impossible to connect to the database: {0}".format(str(e)))
# 		exit(-1)
# 
# class ErrorSQLRequest(Exception):
# 	'''
# 	'''
# 	def __init__(self, e, query=None):
# 		'''
# 		'''
# 		self.errormsg = str(e)
# 		if query != None : 
# 			self.query = query
# 		else :
# 			self.query = ""
# 
# 	def __str__(self):
# 		'''
# 		'''
# 		return '`'+str(self.errormsg.replace('\n',' ').replace('\t',' '))+'`'
# 
# 	def generateInfoAboutError(self, query=None):
# 		'''
# 		Return explanations,proofs,complianceStatus
# 		'''
# 		if query == None :
# 			explanations = "Error with the query: '{0}'".format(self.query)
# 		else : 
# 			explanations = "Error with the query: '{0}'".format(query) 
# 		proofs = "Error message: {0}".format(self.__str__())
# 		complianceStatus = -2
# 		return explanations, proofs, complianceStatus


import logging, random, string, cx_Oracle
from Utils import areEquals,checkOracleVersion,getOracleConnection,ErrorSQLRequest
from progressbar import *
from time import sleep
from sys import exit
from Constants import *
        return True

    def __execPLSQLwithDbmsOutput__(self,request,addLineBreak=False):
        '''
        Execute the request containing dbms_output  
        '''
        responsedata = ""
        cursor = cx_Oracle.Cursor(self.args['dbcon'])
        try :       
            cursor.callproc("dbms_output.enable")
            try:
                cursor.execute(request)
            except Exception as e:
                logging.info("Impossible to execute the query `{0}`: {1}".format(request, self.cleanError(e)))
                return ErrorSQLRequest(e)
            else :
                statusVar = cursor.var(cx_Oracle.NUMBER)
                lineVar = cursor.var(cx_Oracle.STRING)
                while True:
                    cursor.callproc("dbms_output.get_line", (lineVar, statusVar))
                    if statusVar.getvalue() != 0:
                        break
                    line = lineVar.getvalue()
                    if line == None : 
                        line = ''
                    responsedata += line
                    if addLineBreak == True : responsedata +='\n'
                cursor.close()
        except Exception as e: 
            logging.info("Error with the request: {0}".format(str(e)))

# Path: Utils.py
# def areEquals(o1,o2):
# 	'''
# 	retourne True si o1 == o2 (case insensitive)
# 	'''
# 	if o1 == None : 
# 		o1 = ""
# 	if o2 == None : 
# 		o2 = ""
# 	if type(o1) is datetime : 
# 		o1 = o1.ctime()
# 	if type(o2) is datetime : 
# 		o2 = o2.ctime()
# 	if type(o1) is str and type(o2) is str:
# 		if o1.lower() == o2.lower() : return True
# 		else : return False
# 	else : logging.error("Bad comparison in the areEquals function: o1:{0}, o2:{1}".format(type(o1),type(o2)))
# 
# def checkOracleVersion(args):
# 	'''
# 	'''
# 	VERSIONS_TESTED = [11]
# 	cursorRep = execThisQuery(args,"select * from product_component_version",["PRODUCT","VERSION","STATUS"])
# 	for l in cursorRep:
# 		if "Oracle Database" in l['PRODUCT']:
# 			logging.info("The '{0}' version is: {1}".format(l['PRODUCT'],l['VERSION']))
# 	if l['VERSION'][:2] in VERSIONS_TESTED : logging.warn("The version of the Oracle database is not in {0}".format(', '.join(VERSIONS_TESTED)))
# 
# def getOracleConnection(args, connectId):
# 	'''
# 	Return an Oracle object connected to the database thanks to the Oracle connection string (connectId)
# 	'''
# 	try: 
# 		if args['SYSDBA'] == True :	
# 			return cx_Oracle.connect(connectId, mode=cx_Oracle.SYSDBA)
# 		elif args['SYSOPER'] == True :	
# 			return cx_Oracle.connect(connectId, mode=cx_Oracle.SYSOPER)
# 		else :
# 			return cx_Oracle.connect(connectId)
# 	except Exception as e:
# 		logging.error("Impossible to connect to the database: {0}".format(str(e)))
# 		exit(-1)
# 
# class ErrorSQLRequest(Exception):
# 	'''
# 	'''
# 	def __init__(self, e, query=None):
# 		'''
# 		'''
# 		self.errormsg = str(e)
# 		if query != None : 
# 			self.query = query
# 		else :
# 			self.query = ""
# 
# 	def __str__(self):
# 		'''
# 		'''
# 		return '`'+str(self.errormsg.replace('\n',' ').replace('\t',' '))+'`'
# 
# 	def generateInfoAboutError(self, query=None):
# 		'''
# 		Return explanations,proofs,complianceStatus
# 		'''
# 		if query == None :
# 			explanations = "Error with the query: '{0}'".format(self.query)
# 		else : 
# 			explanations = "Error with the query: '{0}'".format(query) 
# 		proofs = "Error message: {0}".format(self.__str__())
# 		complianceStatus = -2
# 		return explanations, proofs, complianceStatus


# Path: OracleDatabase.py
import logging, random, string, cx_Oracle
from Utils import areEquals,checkOracleVersion,getOracleConnection,ErrorSQLRequest
from progressbar import *
from time import sleep
from sys import exit
from Constants import *

        return True

    def __execPLSQLwithDbmsOutput__(self,request,addLineBreak=False):
        '''
        Execute the request containing dbms_output  
        '''
        responsedata = ""
        cursor = cx_Oracle.Cursor(self.args['dbcon'])
        try :       
            cursor.callproc("dbms_output.enable")
            try:
                cursor.execute(request)
            except Exception as e:
                logging.info("Impossible to execute the query `{0}`: {1}".format(request, self.cleanError(e)))
                return ErrorSQLRequest(e)
            else :
                statusVar = cursor.var(cx_Oracle.NUMBER)
                lineVar = cursor.var(cx_Oracle.STRING)
                while True:
                    cursor.callproc("dbms_output.get_line", (lineVar, statusVar))
                    if statusVar.getvalue() != 0:
                        break
                    line = lineVar.getvalue()
                    if line == None : 
                        line = ''
                    responsedata += line
                    if addLineBreak == True : responsedata +='\n'
                cursor.close()
        except Exception as e: 
            logging.info("Error with the request: {0}".format(str(e)))

            return ErrorSQLRequest(e)