pylhb 0.3.0__tar.gz → 0.3.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pylhb-0.3.0 → pylhb-0.3.1}/PKG-INFO +1 -1
- {pylhb-0.3.0 → pylhb-0.3.1}/pyproject.toml +1 -1
- {pylhb-0.3.0 → pylhb-0.3.1}/src/pylhb/myodbc.py +92 -2
- {pylhb-0.3.0 → pylhb-0.3.1}/LICENSE +0 -0
- {pylhb-0.3.0 → pylhb-0.3.1}/README.md +0 -0
- {pylhb-0.3.0 → pylhb-0.3.1}/src/pylhb/__init__.py +0 -0
- {pylhb-0.3.0 → pylhb-0.3.1}/src/pylhb/myconfig.py +0 -0
- {pylhb-0.3.0 → pylhb-0.3.1}/src/pylhb/myjson.py +0 -0
- {pylhb-0.3.0 → pylhb-0.3.1}/src/pylhb/mylog.py +0 -0
- {pylhb-0.3.0 → pylhb-0.3.1}/src/pylhb/mysqlite.py +0 -0
|
@@ -7,9 +7,11 @@ https://learn.microsoft.com/zh-cn/sql/connect/odbc/download-odbc-driver-for-sql-
|
|
|
7
7
|
注意,如果是ODBC Driver 18 for SQL Server,那实例化时记得传driver.
|
|
8
8
|
'''
|
|
9
9
|
import pyodbc
|
|
10
|
+
import asyncio
|
|
11
|
+
from concurrent.futures import ThreadPoolExecutor
|
|
10
12
|
|
|
11
13
|
class MSSQL:
|
|
12
|
-
def __init__(self,*,server=None,user=None,password=None,database=None,port=1433,timeout=0,autoCommit=False,trusted=False,driver="ODBC Driver 17 for SQL Server") -> None:
|
|
14
|
+
def __init__(self,*,server=None,user=None,password=None,database=None,port=1433,timeout=0,autoCommit=False,trusted=False,driver="ODBC Driver 17 for SQL Server",maxWorker=5) -> None:
|
|
13
15
|
self.server=server
|
|
14
16
|
self.user=user
|
|
15
17
|
self.password=password
|
|
@@ -21,7 +23,10 @@ class MSSQL:
|
|
|
21
23
|
self.driver=driver
|
|
22
24
|
self.conn=None
|
|
23
25
|
self.cursor=None
|
|
26
|
+
self.executor = ThreadPoolExecutor(max_workers=maxWorker)
|
|
27
|
+
self.loop = asyncio.get_event_loop()
|
|
24
28
|
|
|
29
|
+
# 获取连接字符串
|
|
25
30
|
def getConnectString(self) -> str:
|
|
26
31
|
conn_str = f"DRIVER={{{self.driver}}};SERVER={self.server},{self.port};DATABASE={self.database};"
|
|
27
32
|
|
|
@@ -34,7 +39,15 @@ class MSSQL:
|
|
|
34
39
|
conn_str += f"Connection Timeout={self.timeout};"
|
|
35
40
|
|
|
36
41
|
return conn_str
|
|
42
|
+
|
|
43
|
+
# 异步连接数据库
|
|
44
|
+
async def connect4Async(self)->tuple[bool,str]:
|
|
45
|
+
return await self.loop.run_in_executor(
|
|
46
|
+
self.executor,
|
|
47
|
+
self.connect,
|
|
48
|
+
)
|
|
37
49
|
|
|
50
|
+
# 同步连接数据库
|
|
38
51
|
def connect(self) -> tuple[bool,str]:
|
|
39
52
|
try:
|
|
40
53
|
connectString = self.getConnectString()
|
|
@@ -66,6 +79,15 @@ class MSSQL:
|
|
|
66
79
|
def IsAutoCommit(self):
|
|
67
80
|
return self.conn.autocommit
|
|
68
81
|
|
|
82
|
+
# 异步插入记录
|
|
83
|
+
async def insert4Async(self, tableName: str, data: dict):
|
|
84
|
+
return await self.loop.run_in_executor(
|
|
85
|
+
self.executor,
|
|
86
|
+
self.insert,
|
|
87
|
+
tableName,
|
|
88
|
+
data
|
|
89
|
+
)
|
|
90
|
+
|
|
69
91
|
# 插入记录
|
|
70
92
|
def insert(self, tableName: str, data: dict):
|
|
71
93
|
if not self.conn:
|
|
@@ -79,6 +101,17 @@ class MSSQL:
|
|
|
79
101
|
except Exception as e:
|
|
80
102
|
return (False,str(e))
|
|
81
103
|
|
|
104
|
+
# 异步修改记录
|
|
105
|
+
async def update4Async(self, table_name: str, data: dict, where: str, params: tuple[any]):
|
|
106
|
+
return await self.loop.run_in_executor(
|
|
107
|
+
self.executor,
|
|
108
|
+
self.update,
|
|
109
|
+
table_name,
|
|
110
|
+
data,
|
|
111
|
+
where,
|
|
112
|
+
params
|
|
113
|
+
)
|
|
114
|
+
|
|
82
115
|
# 修改记录
|
|
83
116
|
def update(self, table_name: str, data: dict, where: str, params: tuple[any]):
|
|
84
117
|
if not self.conn:
|
|
@@ -92,6 +125,16 @@ class MSSQL:
|
|
|
92
125
|
except Exception as e:
|
|
93
126
|
return (False,str(e))
|
|
94
127
|
|
|
128
|
+
# 异步删除记录
|
|
129
|
+
async def delete4Async(self, table_name: str, where: str, params: tuple[any]):
|
|
130
|
+
return await self.loop.run_in_executor(
|
|
131
|
+
self.executor,
|
|
132
|
+
self.delete,
|
|
133
|
+
table_name,
|
|
134
|
+
where,
|
|
135
|
+
params
|
|
136
|
+
)
|
|
137
|
+
|
|
95
138
|
# 删除记录
|
|
96
139
|
def delete(self, table_name: str, where: str, params: tuple[any]):
|
|
97
140
|
if not self.conn:
|
|
@@ -103,6 +146,18 @@ class MSSQL:
|
|
|
103
146
|
except Exception as e:
|
|
104
147
|
return (False,str(e))
|
|
105
148
|
|
|
149
|
+
# 异步查询数据
|
|
150
|
+
async def select4Async(self, table_name, columns: tuple[str] = None, where=None, params: tuple[any]=None,toDict=True):
|
|
151
|
+
return await self.loop.run_in_executor(
|
|
152
|
+
self.executor,
|
|
153
|
+
self.select,
|
|
154
|
+
table_name,
|
|
155
|
+
columns,
|
|
156
|
+
where,
|
|
157
|
+
params,
|
|
158
|
+
toDict
|
|
159
|
+
)
|
|
160
|
+
|
|
106
161
|
# 查询数据
|
|
107
162
|
def select(self, table_name, columns: tuple[str] = None, where=None, params: tuple[any]=None,toDict=True):
|
|
108
163
|
if not self.conn:
|
|
@@ -131,6 +186,15 @@ class MSSQL:
|
|
|
131
186
|
except Exception as e:
|
|
132
187
|
return False,str(e),None
|
|
133
188
|
|
|
189
|
+
# 异步查询数据
|
|
190
|
+
async def get4Async(self,sql,toDict=True):
|
|
191
|
+
return await self.loop.run_in_executor(
|
|
192
|
+
self.executor,
|
|
193
|
+
self.get,
|
|
194
|
+
sql,
|
|
195
|
+
toDict
|
|
196
|
+
)
|
|
197
|
+
|
|
134
198
|
# 查询数据
|
|
135
199
|
def get(self,sql,toDict=True):
|
|
136
200
|
if not self.conn:
|
|
@@ -150,6 +214,14 @@ class MSSQL:
|
|
|
150
214
|
except:
|
|
151
215
|
return None
|
|
152
216
|
|
|
217
|
+
# 异步执行SQL
|
|
218
|
+
async def exec4Async(self,sql):
|
|
219
|
+
return await self.loop.run_in_executor(
|
|
220
|
+
self.executor,
|
|
221
|
+
self.exec,
|
|
222
|
+
sql
|
|
223
|
+
)
|
|
224
|
+
|
|
153
225
|
# 执行SQL
|
|
154
226
|
def exec(self,sql):
|
|
155
227
|
if not self.conn:
|
|
@@ -160,6 +232,15 @@ class MSSQL:
|
|
|
160
232
|
except Exception as e:
|
|
161
233
|
return (False,str(e))
|
|
162
234
|
|
|
235
|
+
# 异步执行存储过程
|
|
236
|
+
async def execProc4Async(self,procName,params: tuple[any] = None):
|
|
237
|
+
return await self.loop.run_in_executor(
|
|
238
|
+
self.executor,
|
|
239
|
+
self.execProc,
|
|
240
|
+
procName,
|
|
241
|
+
params
|
|
242
|
+
)
|
|
243
|
+
|
|
163
244
|
# 执行存储过程
|
|
164
245
|
def execProc(self,procName,params: tuple[any] = None):
|
|
165
246
|
if not self.conn:
|
|
@@ -176,6 +257,15 @@ class MSSQL:
|
|
|
176
257
|
except Exception as e:
|
|
177
258
|
return (False,str(e))
|
|
178
259
|
|
|
260
|
+
# 异步执行存储过程
|
|
261
|
+
async def execProcGet4Async(self,procName,params: list[any] = None):
|
|
262
|
+
return await self.loop.run_in_executor(
|
|
263
|
+
self.executor,
|
|
264
|
+
self.execProcGet,
|
|
265
|
+
procName,
|
|
266
|
+
params
|
|
267
|
+
)
|
|
268
|
+
|
|
179
269
|
# 执行存储过程并返回数据
|
|
180
270
|
def execProcGet(self,procName,params: list[any] = None):
|
|
181
271
|
if not self.conn:
|
|
@@ -220,4 +310,4 @@ class MSSQL:
|
|
|
220
310
|
self.cursor=None
|
|
221
311
|
if self.conn:
|
|
222
312
|
self.conn.close()
|
|
223
|
-
self.conn=None
|
|
313
|
+
self.conn=None
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|