ezKit 1.12.27__py3-none-any.whl → 1.12.29__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 CHANGED
@@ -15,15 +15,17 @@ from typing import Any, Dict, List, Optional, Tuple, Type
15
15
 
16
16
  import pandas as pd
17
17
  from loguru import logger
18
- from sqlalchemy import CursorResult, Engine, Index, bindparam, create_engine, text
18
+ from sqlalchemy import CursorResult, Engine, Index, bindparam, create_engine, insert, text
19
19
  from sqlalchemy.engine import Result
20
20
  from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
21
21
  from sqlalchemy.orm import DeclarativeBase, Session, declarative_base
22
22
 
23
23
  from . import utils
24
+ from .errors import database_error, sqlalchemy_error
24
25
 
25
- Base = declarative_base()
26
+ # --------------------------------------------------------------------------------------------------
26
27
 
28
+ Base = declarative_base()
27
29
 
28
30
  # --------------------------------------------------------------------------------------------------
29
31
 
@@ -698,3 +700,175 @@ class DatabaseAsyncSession:
698
700
  return []
699
701
  rows = result.mappings().all()
700
702
  return [dict(row) for row in rows]
703
+
704
+
705
+ # --------------------------------------------------------------------------------------------------
706
+
707
+
708
+ async def db_async_create(db: DatabaseAsyncSession, schema: Type[DeclarativeBase], data: list) -> dict:
709
+
710
+ # 插入数据
711
+ result = await db.operater(insert(schema), data)
712
+
713
+ # 返回结果
714
+ if result is None:
715
+ return {"code": 1001, "data": 0, "message": database_error["create"]["message"]}
716
+
717
+ return {"code": 200, "data": len(data), "message": "create data success"}
718
+
719
+
720
+ # --------------------------------------------------------------------------------------------------
721
+
722
+
723
+ async def db_async_read(db: DatabaseAsyncSession, table_name: str, data: dict, statement_prefix: str, statement_end: str = "") -> dict:
724
+
725
+ # 初始返回结果 (适用于 count 等于 0)
726
+ result = {"count": 0, "data": None, "pageIndex": data["pageIndex"], "pageSize": 0}
727
+
728
+ # ----------------------------------------------------------------------------------------------
729
+
730
+ # 构建 WHERE
731
+
732
+ where_collections = build_sqlalchemy_select_where(data)
733
+
734
+ if where_collections is None:
735
+ return {"code": 1001, "data": None, "message": sqlalchemy_error["build_where"]["message"]}
736
+
737
+ where_clause, statement_params, bind_params = where_collections
738
+
739
+ # ----------------------------------------------------------------------------------------------
740
+
741
+ # 统计 WHERE
742
+
743
+ count_statement = text(f"SELECT COUNT(*) FROM {table_name} {where_clause}").bindparams(*bind_params)
744
+
745
+ count_result = await db.operate_return_scalar_one(count_statement, statement_params)
746
+
747
+ if count_result is None:
748
+ return {"code": 1002, "data": None, "message": sqlalchemy_error["statement_params"]["message"]}
749
+
750
+ if count_result == 0:
751
+ return {"code": 200, "data": result, "message": "read data success"}
752
+
753
+ # ----------------------------------------------------------------------------------------------
754
+
755
+ # 读取数据
756
+
757
+ read_statement = text(
758
+ f"""
759
+ {statement_prefix}
760
+ {where_clause}
761
+ {statement_end}
762
+ LIMIT :limit
763
+ OFFSET :offset;
764
+ """
765
+ ).bindparams(*bind_params)
766
+
767
+ read_result = await db.operate_return_mappings_all(read_statement, statement_params)
768
+
769
+ # ----------------------------------------------------------------------------------------------
770
+
771
+ # 数据整合
772
+ result["count"] = count_result
773
+ result["data"] = read_result
774
+ result["pageSize"] = len(read_result)
775
+
776
+ # ----------------------------------------------------------------------------------------------
777
+
778
+ # 返回结果
779
+ return {"code": 200, "data": result, "message": "read data success"}
780
+
781
+
782
+ # --------------------------------------------------------------------------------------------------
783
+
784
+
785
+ async def db_async_update(db: DatabaseAsyncSession, table_name: str, data: dict) -> dict:
786
+
787
+ # 构建 WHERE
788
+
789
+ where_collections = build_sqlalchemy_select_where(data["where"])
790
+
791
+ if where_collections is None:
792
+ return {"code": 1001, "data": 0, "message": sqlalchemy_error["build_where"]["message"]}
793
+
794
+ where_clause, statement_params, bind_params = where_collections
795
+
796
+ # ----------------------------------------------------------------------------------------------
797
+
798
+ # 统计 WHERE
799
+
800
+ count_statement = text(f"SELECT COUNT(*) FROM {table_name} {where_clause}").bindparams(*bind_params)
801
+
802
+ count_result = await db.operate_return_scalar_one(count_statement, statement_params)
803
+
804
+ if count_result is None:
805
+ return {"code": 1002, "data": None, "message": sqlalchemy_error["statement_params"]["message"]}
806
+
807
+ if count_result == 0:
808
+ return {"code": 1003, "data": 0, "message": database_error["update"]["message"]}
809
+
810
+ # ----------------------------------------------------------------------------------------------
811
+
812
+ # 更新数据
813
+
814
+ update_collections = build_sqlalchemy_update(table_name, data["where"], data["values"])
815
+
816
+ if update_collections is None:
817
+ return {"code": 1004, "data": None, "message": sqlalchemy_error["build_statement"]["message"]}
818
+
819
+ update_statement, update_params = update_collections
820
+
821
+ update_result = await db.operater(update_statement, update_params)
822
+
823
+ if update_result is None:
824
+ return {"code": 1005, "data": None, "message": database_error["update"]["message"]}
825
+
826
+ # ----------------------------------------------------------------------------------------------
827
+
828
+ # 返回结果
829
+ return {"code": 200, "data": count_result, "message": "update data success"}
830
+
831
+
832
+ # --------------------------------------------------------------------------------------------------
833
+
834
+
835
+ async def db_async_delete(db: DatabaseAsyncSession, table_name: str, data: dict) -> dict:
836
+
837
+ # 构建 WHERE
838
+
839
+ where_collections = build_sqlalchemy_select_where(data)
840
+
841
+ if where_collections is None:
842
+ return {"code": 1001, "data": 0, "message": sqlalchemy_error["build_where"]["message"]}
843
+
844
+ where_clause, statement_params, bind_params = where_collections
845
+
846
+ # ----------------------------------------------------------------------------------------------
847
+
848
+ # 统计 WHERE
849
+
850
+ count_statement = text(f"SELECT COUNT(*) FROM {table_name} {where_clause}").bindparams(*bind_params)
851
+
852
+ count_result = await db.operate_return_scalar_one(count_statement, statement_params)
853
+
854
+ if count_result is None:
855
+ return {"code": 1002, "data": None, "message": sqlalchemy_error["statement_params"]["message"]}
856
+
857
+ if count_result == 0:
858
+ return {"code": 1003, "data": 0, "message": database_error["delete"]["message"]}
859
+
860
+ # ----------------------------------------------------------------------------------------------
861
+
862
+ # 删除数据
863
+
864
+ delete_statement = text(f"DELETE FROM {table_name} {where_clause}").bindparams(*bind_params)
865
+
866
+ delete_result = await db.operater(delete_statement, statement_params)
867
+
868
+ if delete_result is None:
869
+ return {"code": 1004, "data": 0, "message": database_error["delete"]["message"]}
870
+
871
+ # ----------------------------------------------------------------------------------------------
872
+
873
+ # 返回结果
874
+ return {"code": 200, "data": count_result, "message": "delete data success"}
ezKit/errors.py CHANGED
@@ -48,8 +48,16 @@ sqlalchemy_error = {
48
48
  "code": 4003,
49
49
  "message": "SQLAlchemy statement or params error",
50
50
  },
51
- "build_where": {
51
+ "build_statement": {
52
52
  "code": 4004,
53
+ "message": "SQLAlchemy build statement error",
54
+ },
55
+ "build_params": {
56
+ "code": 4005,
57
+ "message": "SQLAlchemy build params error",
58
+ },
59
+ "build_where": {
60
+ "code": 4006,
53
61
  "message": "SQLAlchemy build WHERE error",
54
62
  },
55
63
  }
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ezKit
3
- Version: 1.12.27
3
+ Version: 1.12.29
4
4
  Summary: Easy Kit
5
5
  Author: septvean
6
6
  Author-email: septvean@gmail.com
@@ -3,9 +3,9 @@ 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=yxi8HZw9ijbfKYhCl8OApoLDwLW7tYjPPucsTw6D_Is,23761
6
+ ezKit/database.py,sha256=5MsIYDhyN6VtQ8MJy7pbMGqeaXguy9DzRHgJ6NJD5Gs,30126
7
7
  ezKit/dockerhub.py,sha256=j-wQO-71BsOgExHZhYynuy2k_hCX3on-vg0TH7QCit4,1996
8
- ezKit/errors.py,sha256=P23_yiQJNA6jJpNpzXb2o27uqmGHM-mRKa5171hQWu8,1122
8
+ ezKit/errors.py,sha256=wLLzJ-YWCdAUqdLzsHM-jfnfZJL9pHtZoG_O_bVNI_A,1334
9
9
  ezKit/http.py,sha256=zhNxJF-x91UqGncXWxVXnhZVpFo_wmmpGnMXVT11y9E,1832
10
10
  ezKit/markdown_to_html.template,sha256=21G2sSVGJn6aJvHd0NN4zY5YiDteKe4UtW36AzBwSdk,22274
11
11
  ezKit/mongo.py,sha256=vsRCjJ2uWbNp-1bnGKICPohNYx25El8XnHp10o-lsM4,2397
@@ -16,8 +16,8 @@ ezKit/token.py,sha256=Ac-i9xfq4TqpGyfCzakjrh4NYzxHiN2sCQrMk1tzVi8,1716
16
16
  ezKit/utils.py,sha256=U457ahFkxIXuB-qWvS3995xJs-LlkFIX5_ZWVgmL5cY,43130
17
17
  ezKit/xftp.py,sha256=-XQXyhMqeigT63P6sXkSS7r4GROXyqqlkzKxITLWG-g,8278
18
18
  ezKit/zabbix.py,sha256=PkMnfu7mcuotwwIIsHaC9FsNg-gap6hD1xvm0AwSL1Y,33777
19
- ezkit-1.12.27.dist-info/licenses/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
20
- ezkit-1.12.27.dist-info/METADATA,sha256=WhxULotC-6zBWIc6OGXN6prPRVjqDWXsPfyIF3CSbiY,317
21
- ezkit-1.12.27.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
22
- ezkit-1.12.27.dist-info/top_level.txt,sha256=aYLB_1WODsqNTsTFWcKP-BN0KCTKcV-HZJ4zlHkCFw8,6
23
- ezkit-1.12.27.dist-info/RECORD,,
19
+ ezkit-1.12.29.dist-info/licenses/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
20
+ ezkit-1.12.29.dist-info/METADATA,sha256=8eEFRDzl6V1laRNNs2E1maD3xcSGOXBlNBVTr9s2geQ,317
21
+ ezkit-1.12.29.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
22
+ ezkit-1.12.29.dist-info/top_level.txt,sha256=aYLB_1WODsqNTsTFWcKP-BN0KCTKcV-HZJ4zlHkCFw8,6
23
+ ezkit-1.12.29.dist-info/RECORD,,