包含4个类数据库连接(com.zxy.db_Self.ConnectionPool_Self.py)、数据库操作类(com.zxy.db_Self.Db_Common_Self.py)、数据库管理类(com.zxy.db_Self.DBManager_Self.py)、数据库连接池类(com.zxy.db_Self.PooledConnection_Self.py)
据库连接(com.zxy.db_Self.ConnectionPool_Self.py)
#! python3
# -*- coding: utf-8 -
'''
Created on 2023年08月28日
@author: zxyong 13738196011
'''
import sqlite3,time,mysql.connector,threading
from com.zxy.z_debug import z_debug
from com.zxy.db_Self.PooledConnection_Self import PooledConnection_Self
#监测数据采集物联网应用--数据库连接
class ConnectionPool_Self(z_debug):
attJdbcDriver = ""
attDbUrl = ""
attDbUsername = ""
attDbPassword = ""
attInitialConnections = 5
attIncrementalConnections = 2
attMaxConnections = 10
attPooledConnection_Selfs = []
def __init__(self, inputJdbcDriver, inputDbUrl, inputDbUsername, inputDbPassword):
self.attJdbcDriver = inputJdbcDriver
if inputJdbcDriver == "org.sqlite.JDBC":
self.attInitialConnections = 2
self.attMaxConnections = 5
self.attDbUrl = inputDbUrl
self.attDbUsername = inputDbUsername
self.attDbPassword = inputDbPassword
try:
self.createPool()
except Exception as e:
if str(type(self)) == "":
self.debug_in(self,repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
else:
self.debug_in(repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
finally:
pass
def createPool(self):
if len(self.attPooledConnection_Selfs) == 0:
lock = threading.Lock()
if lock.acquire():
self.createConnections(self.attInitialConnections)
if str(type(self)) == "":
self.debug_in(self,"myself db create pool")#打印异常信息
else:
self.debug_in("myself db create pool")#打印异常信息
lock.release()
def createConnections(self, inputNumConnections):
if self.attMaxConnections > 0 and len(self.attPooledConnection_Selfs) >= self.attMaxConnections:
if str(type(self)) == "":
self.debug_in(self,"myself db connections is max")#打印异常信息
else:
self.debug_in("myself db connections is max")#打印异常信息
self.findFreeConnection()
for iIndex in range(1,inputNumConnections):
try:
temCon = self.newConnection()
temPolCon = PooledConnection_Self(temCon)
self.attPooledConnection_Selfs.append(temPolCon)
except Exception as e:
if str(type(self)) == "":
self.debug_in(self,repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
else:
self.debug_in(repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
def newConnection(self):
if self.attJdbcDriver == "org.sqlite.JDBC":
temConn = sqlite3.connect(self.attDbUrl,check_same_thread = False)
return temConn
elif self.attJdbcDriver == "com.mysql.jdbc.Driver":
try:
temConn = mysql.connector.Connect(host=self.attDbUrl.split(":")[0],user=self.attDbUsername,db=self.attDbUrl.split(":")[2],passwd=self.attDbPassword,port=self.attDbUrl.split(":")[1])
return temConn
except Exception as e:
if str(type(self)) == "":
self.debug_in(self,repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
else:
self.debug_in(repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
else:
return None
def getConnection(self):
temReturnResult = None
lock = threading.Lock()
if lock.acquire():
if len(self.attPooledConnection_Selfs) == 0:
return None
else:
temReturnResult = self.getFreeConnection()
while self.attPooledConnection_Selfs is None:
time.sleep(0.2)
temReturnResult = self.getFreeConnection()
lock.release()
return temReturnResult
def getFreeConnection(self):
temConn_self = self.findFreeConnection()
if temConn_self is None:
self.createConnections(self.attIncrementalConnections)
temConn_self = self.findFreeConnection()
if temConn_self is None:
return None
return temConn_self
def findFreeConnection(self):
temPc = None
while temPc is None:
for i in range(len(self.attPooledConnection_Selfs)):
temPc = self.attPooledConnection_Selfs[i]
if temPc.attBusy == False or temPc.attConnection is None:
temPc.attBusy = True
try:
if temPc.attConnection is None :
temPc.attConnection = self.newConnection()
except Exception as e:
del self.attPooledConnection_Selfs[i]
i = i - 1
if str(type(self)) == "":
self.debug_in(self,repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
else:
self.debug_in(repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
continue
break
if temPc.attConnection is not None:
break
else:
time.sleep(0.5)
return temPc
def closeConnection(self,inputConn):
try:
if str(type(self)) == "":
self.debug_in(self,"the myself close db")#打印异常信息
else:
self.debug_in("the myself close db")#打印异常信息
inputConn.close()
except Exception as e:
if str(type(self)) == "":
self.debug_in(self,repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
else:
self.debug_in(repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
def returnConnection(self,inputConn):
if len(self.attPooledConnection_Selfs) == 0:
if str(type(self)) == "":
self.debug_in(self,"myself db returnConnection!")#打印异常信息
else:
self.debug_in("myself db returnConnection!")#打印异常信息
return None
for i in range(len(self.attPooledConnection_Selfs)):
temPConn = self.attPooledConnection_Selfs[i]
if temPConn.attConnection == inputConn and temPConn.attBusy:
temPConn.attBusy = False
Break
数据库操作类(com.zxy.db_Self.Db_Common_Self.py)
#! python3
# -*- coding: utf-8 -
'''
Created on 2023年08月28日
@author: zxyong 13738196011
'''
from com.zxy.adminlog.UsAdmin_Log import UsAdmin_Log
from com.zxy.common import Com_Para
from com.zxy.db_Self.DBManager_Self import DBManager_Self
from com.zxy.z_debug import z_debug
#监测数据采集物联网应用--数据库操作
class Db_Common_Self(z_debug):
attSqlException = ""
attRs_out = None
attConn_a = None
attColumnNames = []
def __init__(self):
pass
def Common_SqlNoCommit(self, inputStrSql):
temRs = None
try:
temDs = DBManager_Self()
self.attConn_a = temDs.getConnection()
if str(self.attConn_a.attConnection).find("sqlite3.Connection") != -1:
Com_Para.Dblock1.acquire()
temRs = self.attConn_a.executeQueryNoCommit(inputStrSql)
except Exception as e:
temLog = ""
if str(type(self)) == "":
temLog = self.debug_info(self)+inputStrSql+"==>"+repr(e)
self.debug_in(self,inputStrSql+"==>"+repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
else:
temLog = self.debug_info()+inputStrSql+"==>"+repr(e)
self.debug_in(inputStrSql+"==>"+repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
uL = UsAdmin_Log(Com_Para.ApplicationPath, temLog)
uL.WriteLog()
finally:
temDs.returnConnection(self.attConn_a.attConnection)
self.Close_Conn()
if str(self.attConn_a.attConnection).find("sqlite3.Connection") != -1:
Com_Para.Dblock1.release()
return temRs
def Common_Sql(self, inputStrSql):
temRs = None
try:
temDs = DBManager_Self()
self.attConn_a = temDs.getConnection()
temRs = self.attConn_a.executeQuery(inputStrSql)
self.attColumnNames = self.attConn_a.attColumnNames
except Exception as e:
temLog = ""
if str(type(self)) == "":
temLog = self.debug_info(self)+inputStrSql+"==>"+repr(e)
self.debug_in(self,inputStrSql+"==>"+repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
else:
temLog = self.debug_info()+inputStrSql+"==>"+repr(e)
self.debug_in(inputStrSql+"==>"+repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
uL = UsAdmin_Log(Com_Para.ApplicationPath, temLog)
uL.WriteLog()
finally:
temDs.returnConnection(self.attConn_a.attConnection)
self.Close_Conn()
return temRs
def CommonExec_SqlRowID(self, inputStrSql):
temIResult = -1
try:
temDs = DBManager_Self()
self.attConn_a = temDs.getConnection()
if str(self.attConn_a.attConnection).find("sqlite3.Connection") != -1:
Com_Para.Dblock1.acquire()
temIResult = self.attConn_a.executeUpdateRowID(inputStrSql)
except Exception as e:
temLog = ""
if str(type(self)) == "":
temLog = self.debug_info(self)+inputStrSql+"==>"+repr(e)
self.debug_in(self,inputStrSql+"==>"+repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
else:
temLog = self.debug_info()+inputStrSql+"==>"+repr(e)
self.debug_in(inputStrSql+"==>"+repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
uL = UsAdmin_Log(Com_Para.ApplicationPath, temLog)
uL.WriteLog()
temIResult = -1
finally:
temDs.returnConnection(self.attConn_a.attConnection)
self.Close_Conn()
if str(self.attConn_a.attConnection).find("sqlite3.Connection") != -1:
Com_Para.Dblock1.release()
return temIResult
def CommonExec_Sql(self, inputStrSql):
temIResult = -1
try:
temDs = DBManager_Self()
self.attConn_a = temDs.getConnection()
if str(self.attConn_a.attConnection).find("sqlite3.Connection") != -1:
Com_Para.Dblock1.acquire()
temIResult = self.attConn_a.executeUpdate(inputStrSql)
except Exception as e:
temLog = ""
if str(type(self)) == "":
temLog = self.debug_info(self)+inputStrSql+"==>"+repr(e)
self.debug_in(self,inputStrSql+"==>"+repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
else:
temLog = self.debug_info()+inputStrSql+"==>"+repr(e)
self.debug_in(inputStrSql+"==>"+repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
uL = UsAdmin_Log(Com_Para.ApplicationPath, temLog)
uL.WriteLog()
temIResult = -1
finally:
temDs.returnConnection(self.attConn_a.attConnection)
self.Close_Conn()
if str(self.attConn_a.attConnection).find("sqlite3.Connection") != -1:
Com_Para.Dblock1.release()
return temIResult
# ProName 存储过程名 Parameters输入参数 ParamTypes参数类型String Int float Date
# ParamOutName输出参数名 ParamOutType输出参数类型
def Common_Sql_Proc(self,inputProName, inputParameters, inputParamTypes, inputParamOutName, inputParamOutType, inputTrn):
try:
temDs = DBManager_Self()
self.attConn_a = temDs.getConnection()
if str(self.attConn_a.attConnection).find("sqlite3.Connection") != -1:
Com_Para.Dblock1.acquire()
if inputTrn.attINF_TYPE == "1":
self.attRs_out = self.attConn_a.ParamExecuteQuery(inputProName,inputParameters,inputParamTypes,inputParamOutName,inputParamOutType)
else:
self.attRs_out = self.attConn_a.ParamExecuteQuery(inputProName,inputParameters,inputParamTypes,inputParamOutName,inputParamOutType,inputTrn.attINF_EN_SQL)
self.attColumnNames = self.attConn_a.attColumnNames
except Exception as e:
self.attSqlException = "数据库操作出错请查看程序错误日志文件:" + inputProName + " "+ repr(e)
temLog = ""
if str(type(self)) == "":
temLog = self.debug_info(self)+inputProName+"==>"+repr(e)
self.debug_in(self,inputProName+"==>"+repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
else:
temLog = self.debug_info()+inputProName+"==>"+repr(e)
self.debug_in(inputProName+"==>"+repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
uL = UsAdmin_Log(Com_Para.ApplicationPath, temLog)
uL.WriteLog()
finally:
temDs.returnConnection(self.attConn_a.attConnection)
self.Close_Conn()
if str(self.attConn_a.attConnection).find("sqlite3.Connection") != -1:
Com_Para.Dblock1.release()
return self.attRs_out
def Close_Conn(self):
if not self.attConn_a.attConnection is None:
pass
数据库管理类(com.zxy.db_Self.DBManager_Self.py)
#! python3
# -*- coding: utf-8 -
'''
Created on 2023年08月28日
@author: zxyong 13738196011
'''
from com.zxy.common import Com_Para
from com.zxy.common.DbConfigSelf import DbConfigSelf
from com.zxy.db_Self.ConnectionPool_Self import ConnectionPool_Self
from com.zxy.z_debug import z_debug
#监测数据采集物联网应用--数据库管理
class DBManager_Self(z_debug):
attConn = None
attConnectionPool = None
def __init__(self):
if Com_Para.url == "":
DbConfigSelf.GetDbConfigSelfNew()
self.attConnectionPool = ConnectionPool_Self(Com_Para.driverClassName,Com_Para.url,Com_Para.username,Com_Para.password)
try:
self.attConnectionPool.createPool()
except Exception as e:
if str(type(self)) == "":
self.debug_in(self,repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
else:
self.debug_in(repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
finally:
pass
def getConnection(self):
try:
self.attConn = self.attConnectionPool.getConnection()
except Exception as e:
if str(type(self)) == "":
self.debug_in(self,repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
else:
self.debug_in(repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
finally:
return self.attConn
def returnConnection(self,inputConn):
return self.attConnectionPool.returnConnection(inputConn)
@staticmethod
def closeConnectionPoolTimeOut(self):
try:
self.attConnectionPool.closeConnectionPoolTimeOut()
except Exception as e:
if str(type(self)) == "":
self.debug_in(self,repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
else:
self.debug_in(repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
finally:
Pass
数据库连接池类(com.zxy.db_Self.PooledConnection_Self.py)
#! python3
# -*- coding: utf-8 -
'''
Created on 2023年08月28日
@author: zxyong 13738196011
'''
from urllib.parse import unquote
from com.zxy.adminlog.UsAdmin_Log import UsAdmin_Log
from com.zxy.common import Com_Para
from com.zxy.common.Com_Fun import Com_Fun
from com.zxy.z_debug import z_debug
#监测数据采集物联网应用--数据库连接池
class PooledConnection_Self(z_debug):
attUpdtime = 0
attB_nocursor = True
attConnection = None
attBusy = False
attColumnNames = []
attlastrowid = -1
def __init__(self, inputConn):
self.attB_nocursor = True
self.attConnection = inputConn
self.attUpdtime = Com_Fun.getTimeLong()
def executeQueryNoCommit(self, inputSql):
temCursor = None
temValues = None
try:
self.attUpdtime = Com_Fun.getTimeLong()
# 建立cursor
temCursor = self.attConnection.cursor()
# 执行sql select
temCursor.execute(inputSql)
# 利用featchall获取数据
temValues = temCursor.fetchall()
self.attColumnNames = temCursor.description
self.attConnection.commit()
except Exception as e:
temLog = ""
if str(type(self)) == "":
temLog = self.debug_info(self)+inputSql+"==>"+repr(e)
self.debug_in(self,inputSql+"==>"+repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
else:
temLog = self.debug_info()+inputSql+"==>"+repr(e)
self.debug_in(inputSql+"==>"+repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
uL = UsAdmin_Log(Com_Para.ApplicationPath, temLog)
uL.WriteLog()
finally:
if not temCursor is None:
temCursor.close()
return temValues
def executeQuery(self, inputSql):
temCursor = None
temValues = None
try:
self.attUpdtime = Com_Fun.getTimeLong()
# 建立cursor
temCursor = self.attConnection.cursor()
# 执行sql select
temCursor.execute(inputSql)
# 利用featchall获取数据
temValues = temCursor.fetchall()
self.attColumnNames = temCursor.description
if inputSql.lower().find("insert into") == 0 or inputSql.lower().find("update ") == 0 or inputSql.lower().find("delete ") == 0:
self.attConnection.commit()
except Exception as e:
temLog = ""
if str(type(self)) == "":
temLog = self.debug_info(self)+inputSql+"==>"+repr(e)
self.debug_in(self,inputSql+"==>"+repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
else:
temLog = self.debug_info()+inputSql+"==>"+repr(e)
self.debug_in(inputSql+"==>"+repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
uL = UsAdmin_Log(Com_Para.ApplicationPath, temLog)
uL.WriteLog()
finally:
if not temCursor is None:
temCursor.close()
return temValues
def executeUpdateRowID(self, inputSql):
temResult = -1
temCursor = None
try:
self.attUpdtime = Com_Fun.getTimeLong()
# 建立cursor
temCursor = self.attConnection.cursor()
# 执行sql insert update delete t
temCursor.execute(inputSql)
temResult = temCursor.lastrowid
self.attConnection.commit()
except Exception as e:
temLog = ""
if str(type(self)) == "":
temLog = self.debug_info(self)+inputSql+"==>"+repr(e)
self.debug_in(self,inputSql+"==>"+repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
else:
temLog = self.debug_info()+inputSql+"==>"+repr(e)
self.debug_in(inputSql+"==>"+repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
uL = UsAdmin_Log(Com_Para.ApplicationPath, temLog)
uL.WriteLog()
finally:
if not temCursor:
temCursor.close()
return temResult
def executeUpdate(self, inputSql):
temResult = -1
temCursor = None
try:
self.attUpdtime = Com_Fun.getTimeLong()
# 建立cursor
temCursor = self.attConnection.cursor()
# 执行sql insert update delete t
temCursor.execute(inputSql)
temResult = temCursor.rowcount
self.attConnection.commit()
except Exception as e:
temLog = ""
if str(type(self)) == "":
temLog = self.debug_info(self)+inputSql+"==>"+repr(e)
self.debug_in(self,inputSql+"==>"+repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
else:
temLog = self.debug_info()+inputSql+"==>"+repr(e)
self.debug_in(inputSql+"==>"+repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
uL = UsAdmin_Log(Com_Para.ApplicationPath, temLog)
uL.WriteLog()
finally:
if not temCursor:
temCursor.close()
return temResult
def ParamExecuteQuery(self,inputProName, inputParameters, inputParamTypes, inputParamOutName, inputParamOutType, inputStrSql):
self.attUpdtime = Com_Fun.getTimeLong()
temValues = None
# 建立cursor
temCursor = self.attConnection.cursor()
if len(inputParameters) == len(inputParamTypes) and len(inputParamOutName) == len(inputParamOutType):
i = 0
for temParamTypes in inputParamTypes:
if temParamTypes == "LIST":
j = 0
temStr_V = ""
for iIn in temParamTypes.split(","):
if j != 0:
temStr_V += ","
temStr_V += "?"
j += 1
inputStrSql = inputStrSql.replace("@\\?",temStr_V,1)
if temParamTypes.upper() == "STRING":
inputParameters[i] = inputParameters[i]#Com_Fun.py_urldecode(inputParameters[i])#unquote(inputParameters[i],Com_Para.U_CODE)
pass
elif temParamTypes.upper() == "INT":
inputParameters[i] = int(inputParameters[i])
pass
elif temParamTypes.upper() == "FLOAT":
inputParameters[i] = float(inputParameters[i])
pass
elif temParamTypes.upper() == "DATE":
inputParameters[i] = unquote(inputParameters[i].replace("+"," "),Com_Para.U_CODE)
pass
elif temParamTypes.upper() == "LIST":
pass
elif temParamTypes.upper() == "LIKESTRING":
inputParameters[i] = unquote(inputParameters[i],Com_Para.U_CODE)
pass
i += 1
if inputStrSql.upper().strip().find("INSERT INTO") == 0 or inputStrSql.upper().strip().find("UPDATE") == 0:
# 执行sql select
iCount = temCursor.execute(inputStrSql,inputParameters)
self.attlastrowid = temCursor.lastrowid
if iCount.rowcount != -1:
# 执行sql insert update delete t
temCursor.execute("select '1' as 's_result','成功,"+str(iCount.rowcount)+"' as 'error_desc'")
else:
temCursor.execute("select '0' as 's_result','失败' as 'error_desc'")
# 利用featchall获取数据
temValues = temCursor.fetchall()
self.attColumnNames = temCursor.description
self.attConnection.commit()
elif inputStrSql.upper().strip().find("DELETE") == 0:
if inputStrSql.upper().strip().find(";") != -1:
iCount = None
for strSqls in inputStrSql.split(";"):
# 执行多个sql
iCount = temCursor.execute(strSqls)
if iCount.rowcount != -1:
# 执行sql insert update delete t
temCursor.execute("select '1' as 's_result','成功' as 'error_desc'")
else:
temCursor.execute("select '0' as 's_result','失败' as 'error_desc'")
# 利用featchall获取数据
temValues = temCursor.fetchall()
self.attColumnNames = temCursor.description
self.attConnection.commit()
else:
# 执行sql select
iCount = temCursor.execute(inputStrSql,inputParameters)
if iCount.rowcount != -1:
# 执行sql insert update delete t
temCursor.execute("select '1' as 's_result','成功' as 'error_desc'")
else:
temCursor.execute("select '0' as 's_result','失败' as 'error_desc'")
# 利用featchall获取数据
temValues = temCursor.fetchall()
self.attColumnNames = temCursor.description
self.attConnection.commit()
elif inputStrSql.upper().strip().find("SELECT") == 0:
iCount = temCursor.execute(inputStrSql,inputParameters)
# 利用featchall获取数据
temValues = temCursor.fetchall()
self.attColumnNames = temCursor.description
self.attConnection.commit()
return temValues