ezKit 1.12.13__py3-none-any.whl → 1.12.15__py3-none-any.whl
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.
- ezKit/database.py +58 -3
- {ezkit-1.12.13.dist-info → ezkit-1.12.15.dist-info}/METADATA +1 -1
- {ezkit-1.12.13.dist-info → ezkit-1.12.15.dist-info}/RECORD +6 -6
- {ezkit-1.12.13.dist-info → ezkit-1.12.15.dist-info}/WHEEL +0 -0
- {ezkit-1.12.13.dist-info → ezkit-1.12.15.dist-info}/licenses/LICENSE +0 -0
- {ezkit-1.12.13.dist-info → ezkit-1.12.15.dist-info}/top_level.txt +0 -0
ezKit/database.py
CHANGED
@@ -15,6 +15,8 @@ from typing import Any, Dict, Optional, Tuple, Type
|
|
15
15
|
import pandas as pd
|
16
16
|
from loguru import logger
|
17
17
|
from sqlalchemy import CursorResult, Engine, Index, bindparam, create_engine, text
|
18
|
+
from sqlalchemy.engine import Result
|
19
|
+
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
|
18
20
|
from sqlalchemy.orm import DeclarativeBase, Session, declarative_base
|
19
21
|
|
20
22
|
from . import utils
|
@@ -125,7 +127,7 @@ def build_sqlalchemy_where_clause(data: dict) -> Optional[Tuple[str, Dict[str, A
|
|
125
127
|
|
126
128
|
for field, value in data.items():
|
127
129
|
|
128
|
-
#
|
130
|
+
# 特殊处理日期字段
|
129
131
|
if field == "date" or field.startswith("date__"):
|
130
132
|
field = field.replace("date", "datetime::date", 1)
|
131
133
|
|
@@ -136,6 +138,10 @@ def build_sqlalchemy_where_clause(data: dict) -> Optional[Tuple[str, Dict[str, A
|
|
136
138
|
# 避免字段名冲突
|
137
139
|
param_key = f"{base}_{op}"
|
138
140
|
|
141
|
+
# 特殊处理日期字段
|
142
|
+
if param_key.startswith("datetime::date"):
|
143
|
+
param_key = param_key.replace("datetime::date", "datetime_date", 1)
|
144
|
+
|
139
145
|
if op == "like":
|
140
146
|
sql_parts.append(f"{base} LIKE :{param_key}")
|
141
147
|
param_dict[param_key] = f"%{value}%"
|
@@ -165,8 +171,15 @@ def build_sqlalchemy_where_clause(data: dict) -> Optional[Tuple[str, Dict[str, A
|
|
165
171
|
|
166
172
|
else:
|
167
173
|
|
168
|
-
|
169
|
-
|
174
|
+
param_key = field
|
175
|
+
|
176
|
+
# 特殊处理日期字段
|
177
|
+
if param_key.startswith("datetime::date"):
|
178
|
+
param_key = param_key.replace("datetime::date", "datetime_date", 1)
|
179
|
+
|
180
|
+
sql_parts.append(f"{field} = :{param_key}")
|
181
|
+
|
182
|
+
param_dict[param_key] = value
|
170
183
|
|
171
184
|
where_clause = " AND ".join(sql_parts)
|
172
185
|
|
@@ -575,3 +588,45 @@ class Database:
|
|
575
588
|
logger.error(f"{info} [failed]")
|
576
589
|
logger.exception(e)
|
577
590
|
return False
|
591
|
+
|
592
|
+
|
593
|
+
# ----------------------------------------------------------------------------------------------
|
594
|
+
|
595
|
+
|
596
|
+
class DatabaseAsyncSession:
|
597
|
+
"""Database Async Session"""
|
598
|
+
|
599
|
+
AsyncSessionLocal: async_sessionmaker[AsyncSession]
|
600
|
+
|
601
|
+
def __init__(self, session: async_sessionmaker[AsyncSession]):
|
602
|
+
"""Initiation"""
|
603
|
+
self.AsyncSessionLocal = session
|
604
|
+
|
605
|
+
# 执行器
|
606
|
+
async def operater(self, stmt, params, **kwargs) -> Result | None:
|
607
|
+
|
608
|
+
async with self.AsyncSessionLocal() as session:
|
609
|
+
try:
|
610
|
+
await session.begin()
|
611
|
+
result = await session.execute(stmt, params, **kwargs)
|
612
|
+
await session.commit()
|
613
|
+
return result
|
614
|
+
except Exception as e:
|
615
|
+
await session.rollback()
|
616
|
+
logger.exception(e)
|
617
|
+
return None
|
618
|
+
|
619
|
+
# 精确返回一个标量结果
|
620
|
+
async def operate_return_scalar_one(self, stmt, params, **kwargs) -> Any | None:
|
621
|
+
result = await self.operater(stmt, params, **kwargs)
|
622
|
+
if result is None:
|
623
|
+
return None
|
624
|
+
return result.scalar_one()
|
625
|
+
|
626
|
+
# 返回所有结果
|
627
|
+
async def operate_return_mappings_all(self, stmt, params, **kwargs) -> list:
|
628
|
+
result = await self.operater(stmt, params, **kwargs)
|
629
|
+
if result is None:
|
630
|
+
return []
|
631
|
+
rows = result.mappings().all()
|
632
|
+
return [dict(row) for row in rows]
|
@@ -3,7 +3,7 @@ ezKit/_file.py,sha256=0qRZhwYuagTgTGrhm-tzAMvEQT4HTJA_xZKqF2bo0ho,1207
|
|
3
3
|
ezKit/bottle.py,sha256=43h4v1kzz6qrLvCt5IMN0H-gFtaT0koG9wETqteXsps,181666
|
4
4
|
ezKit/bottle_extensions.py,sha256=27rogmfK7mL2qUSjXH79IMGZbCVULtYEikql_N9O6Zs,1165
|
5
5
|
ezKit/cipher.py,sha256=7jBarRH7ukSYzkz-Anl8B8JzluhnRz4CLHidPRRj_cg,2939
|
6
|
-
ezKit/database.py,sha256=
|
6
|
+
ezKit/database.py,sha256=6TMzofiGUvU4EPqWCMi8dMUkY9w45zxNaic92aErQDM,21433
|
7
7
|
ezKit/dockerhub.py,sha256=j-wQO-71BsOgExHZhYynuy2k_hCX3on-vg0TH7QCit4,1996
|
8
8
|
ezKit/http.py,sha256=zhNxJF-x91UqGncXWxVXnhZVpFo_wmmpGnMXVT11y9E,1832
|
9
9
|
ezKit/markdown_to_html.template,sha256=21G2sSVGJn6aJvHd0NN4zY5YiDteKe4UtW36AzBwSdk,22274
|
@@ -15,8 +15,8 @@ ezKit/token.py,sha256=Ac-i9xfq4TqpGyfCzakjrh4NYzxHiN2sCQrMk1tzVi8,1716
|
|
15
15
|
ezKit/utils.py,sha256=VyAw2yEs15VER9RCpMgskx1l0zrSpRlV34X___eOia0,42416
|
16
16
|
ezKit/xftp.py,sha256=-XQXyhMqeigT63P6sXkSS7r4GROXyqqlkzKxITLWG-g,8278
|
17
17
|
ezKit/zabbix.py,sha256=PkMnfu7mcuotwwIIsHaC9FsNg-gap6hD1xvm0AwSL1Y,33777
|
18
|
-
ezkit-1.12.
|
19
|
-
ezkit-1.12.
|
20
|
-
ezkit-1.12.
|
21
|
-
ezkit-1.12.
|
22
|
-
ezkit-1.12.
|
18
|
+
ezkit-1.12.15.dist-info/licenses/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
|
19
|
+
ezkit-1.12.15.dist-info/METADATA,sha256=DNH4AJqIW5rgiGMn_uRjjrCZo50rhTuLv0tohcmd36o,317
|
20
|
+
ezkit-1.12.15.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
21
|
+
ezkit-1.12.15.dist-info/top_level.txt,sha256=aYLB_1WODsqNTsTFWcKP-BN0KCTKcV-HZJ4zlHkCFw8,6
|
22
|
+
ezkit-1.12.15.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|