lesscode-database 0.0.7__py3-none-any.whl → 0.0.9__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.
- lesscode_database/connect_pool.py +71 -6
- lesscode_database/mongo_base_model.py +169 -0
- lesscode_database/version.py +1 -1
- {lesscode_database-0.0.7.dist-info → lesscode_database-0.0.9.dist-info}/METADATA +1 -1
- lesscode_database-0.0.9.dist-info/RECORD +14 -0
- {lesscode_database-0.0.7.dist-info → lesscode_database-0.0.9.dist-info}/WHEEL +1 -1
- lesscode_database-0.0.7.dist-info/RECORD +0 -13
- {lesscode_database-0.0.7.dist-info → lesscode_database-0.0.9.dist-info}/LICENSE +0 -0
- {lesscode_database-0.0.7.dist-info → lesscode_database-0.0.9.dist-info}/top_level.txt +0 -0
|
@@ -657,13 +657,22 @@ class Pool:
|
|
|
657
657
|
@staticmethod
|
|
658
658
|
def sync_create_clickhouse_pool(conn_info: ConnectionInfo):
|
|
659
659
|
try:
|
|
660
|
-
|
|
660
|
+
pooled_db = importlib.import_module("dbutils.pooled_db")
|
|
661
|
+
except ImportError:
|
|
662
|
+
raise Exception(f"DBUtils is not exist,run:pip install DBUtils==3.0.2")
|
|
663
|
+
try:
|
|
664
|
+
clickhouse_driver = importlib.import_module("clickhouse_driver")
|
|
661
665
|
except ImportError:
|
|
662
|
-
raise
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
666
|
+
raise Exception(f"clickhouse_driver is not exist,run:pip install clickhouse_driver")
|
|
667
|
+
params = conn_info.params
|
|
668
|
+
if not isinstance(params, dict):
|
|
669
|
+
params = dict()
|
|
670
|
+
|
|
671
|
+
pool = pooled_db.PooledDB(creator=clickhouse_driver.connect, host=conn_info.host, port=conn_info.port,
|
|
672
|
+
user=conn_info.user,
|
|
673
|
+
password=conn_info.password, database=conn_info.db_name or "default",
|
|
674
|
+
**params)
|
|
675
|
+
return pool
|
|
667
676
|
|
|
668
677
|
@staticmethod
|
|
669
678
|
def create_sqlalchemy_pool(conn_info: ConnectionInfo):
|
|
@@ -781,6 +790,56 @@ class Pool:
|
|
|
781
790
|
True) if conn_info.params else True)
|
|
782
791
|
return engine
|
|
783
792
|
|
|
793
|
+
@staticmethod
|
|
794
|
+
def sync_create_dbutils_pool(conn_info: ConnectionInfo):
|
|
795
|
+
params = conn_info.params
|
|
796
|
+
if not isinstance(params, dict):
|
|
797
|
+
params = dict()
|
|
798
|
+
creator = params.pop("creator")
|
|
799
|
+
try:
|
|
800
|
+
generic = importlib.import_module(creator)
|
|
801
|
+
except ImportError:
|
|
802
|
+
raise Exception(f"{creator} is not exist,run:pip install {creator}")
|
|
803
|
+
try:
|
|
804
|
+
pooled_db = importlib.import_module("dbutils.pooled_db")
|
|
805
|
+
steady_db = importlib.import_module("dbutils.steady_db")
|
|
806
|
+
simple_pooled_db = importlib.import_module("dbutils.simple_pooled_db")
|
|
807
|
+
persistent_db = importlib.import_module("dbutils.persistent_db")
|
|
808
|
+
except ImportError:
|
|
809
|
+
raise Exception(f"DBUtils is not exist,run:pip install DBUtils==3.0.2")
|
|
810
|
+
pool_type = params.pop("pool_type", "PooledDB")
|
|
811
|
+
if pool_type not in ["SimplePooledDB", "SteadyDBConnection", "PersistentDB", "PooledDB"]:
|
|
812
|
+
pool_type = "PooledDB"
|
|
813
|
+
|
|
814
|
+
mincached = params.pop("mincached", conn_info.min_size)
|
|
815
|
+
maxusage = params.pop("maxusage", conn_info.min_size)
|
|
816
|
+
maxshared = params.pop("maxshared", conn_info.max_size)
|
|
817
|
+
maxcached = params.pop("maxcached", conn_info.max_size)
|
|
818
|
+
blocking = params.pop("blocking", True)
|
|
819
|
+
reset = params.pop("reset", True)
|
|
820
|
+
setsession = params.pop("setsession", None)
|
|
821
|
+
failures = params.pop("failures", None)
|
|
822
|
+
ping = params.pop("ping", 1)
|
|
823
|
+
threadlocal = params.pop("threadlocal", None)
|
|
824
|
+
closeable = params.pop("closeable", True)
|
|
825
|
+
maxconnections = params.pop("maxconnections", None)
|
|
826
|
+
if pool_type == "SimplePooledDB":
|
|
827
|
+
pool = simple_pooled_db.PooledDB(dbapi=creator, maxconnections=maxconnections, **params)
|
|
828
|
+
elif pool_type == "SteadyDBConnection":
|
|
829
|
+
pool = steady_db.SteadyDBConnection(creator=creator, maxusage=maxusage, setsession=setsession,
|
|
830
|
+
failures=failures,
|
|
831
|
+
ping=ping, closeable=closeable, **params)
|
|
832
|
+
elif pool_type == "PersistentDB":
|
|
833
|
+
pool = persistent_db.PersistentDB(creator=creator, maxusage=maxusage, setsession=setsession,
|
|
834
|
+
failures=failures, ping=ping,
|
|
835
|
+
closeable=closeable, threadlocal=threadlocal, **params)
|
|
836
|
+
else:
|
|
837
|
+
pool = pooled_db.PooledDB(creator=generic, mincached=mincached, maxcached=maxcached,
|
|
838
|
+
maxshared=maxshared, maxconnections=conn_info.max_size, blocking=blocking,
|
|
839
|
+
maxusage=maxusage, setsession=setsession, reset=reset,
|
|
840
|
+
failures=failures, ping=ping, **params)
|
|
841
|
+
return pool
|
|
842
|
+
|
|
784
843
|
|
|
785
844
|
def run_sync(func_instance):
|
|
786
845
|
if iscoroutine(func_instance):
|
|
@@ -886,5 +945,11 @@ def get_pool(conn_info: ConnectionInfo):
|
|
|
886
945
|
else:
|
|
887
946
|
return run_sync(Pool.sync_create_sqlalchemy_pool(conn_info)), conn_info
|
|
888
947
|
|
|
948
|
+
elif conn_info.dialect == "dbutils":
|
|
949
|
+
if conn_info.async_enable:
|
|
950
|
+
return run_sync(Pool.sync_create_dbutils_pool(conn_info)), conn_info
|
|
951
|
+
else:
|
|
952
|
+
return run_sync(Pool.sync_create_dbutils_pool(conn_info)), conn_info
|
|
953
|
+
|
|
889
954
|
else:
|
|
890
955
|
raise Exception(f"conn_info.dialect={conn_info.dialect} is not supported")
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import re
|
|
2
|
+
from abc import ABCMeta
|
|
3
|
+
from typing import List, Any, Union
|
|
4
|
+
|
|
5
|
+
from lesscode_database.ds_helper import DsHelper
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def to_camel_case(x):
|
|
9
|
+
"""转驼峰法命名"""
|
|
10
|
+
return re.sub('_([a-zA-Z])', lambda m: (m.group(1).upper()), x)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def to_upper_camel_case(x):
|
|
14
|
+
"""转大驼峰法命名"""
|
|
15
|
+
s = re.sub('_([a-zA-Z])', lambda m: (m.group(1).upper()), x)
|
|
16
|
+
return s[0].upper() + s[1:]
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def to_lower_camel_case(x):
|
|
20
|
+
"""转小驼峰法命名"""
|
|
21
|
+
s = re.sub('_([a-zA-Z])', lambda m: (m.group(1).upper()), x)
|
|
22
|
+
return s[0].lower() + s[1:]
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def model2dict(model: Union[list, dict]):
|
|
26
|
+
def _2dict(obj):
|
|
27
|
+
if isinstance(obj, dict):
|
|
28
|
+
_data_dict = dict()
|
|
29
|
+
for k, v in vars(obj).items():
|
|
30
|
+
if not k.startswith("__"):
|
|
31
|
+
_data_dict.update({k: v})
|
|
32
|
+
return _data_dict
|
|
33
|
+
else:
|
|
34
|
+
return [_2dict(_) for _ in model]
|
|
35
|
+
|
|
36
|
+
return _2dict(model)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class MongoBaseModel(metaclass=ABCMeta):
|
|
40
|
+
__connect_name__ = ""
|
|
41
|
+
__route_key__ = ""
|
|
42
|
+
|
|
43
|
+
def __init__(self, **kwargs):
|
|
44
|
+
pass
|
|
45
|
+
|
|
46
|
+
@classmethod
|
|
47
|
+
def count(cls, query: dict):
|
|
48
|
+
pool = DsHelper(cls.__connect_name__).pool
|
|
49
|
+
database_name, collection_name = cls.__route_key__.split(".")
|
|
50
|
+
collection = pool[database_name][collection_name]
|
|
51
|
+
total = collection.count_documents(query)
|
|
52
|
+
return total
|
|
53
|
+
|
|
54
|
+
@classmethod
|
|
55
|
+
def query_one(cls, query, find_column: Union[dict, list] = None, to_model: bool = False):
|
|
56
|
+
if find_column:
|
|
57
|
+
if isinstance(find_column, list):
|
|
58
|
+
find_column = {_: 1 for _ in find_column}
|
|
59
|
+
pool = DsHelper(cls.__connect_name__).pool
|
|
60
|
+
database_name, collection_name = cls.__route_key__.split(".")
|
|
61
|
+
collection = pool[database_name][collection_name]
|
|
62
|
+
res = collection.find_one(query, find_column)
|
|
63
|
+
if to_model and res:
|
|
64
|
+
res = dict2model(res, cls)
|
|
65
|
+
return res
|
|
66
|
+
|
|
67
|
+
@classmethod
|
|
68
|
+
def query_page(cls, query: dict, find_column: Union[dict, list] = None, page_num: int = 1, page_size: int = 10,
|
|
69
|
+
sort_list: List[List[Any]] = None, to_model: bool = False):
|
|
70
|
+
if find_column:
|
|
71
|
+
if isinstance(find_column, list):
|
|
72
|
+
find_column = {_: 1 for _ in find_column}
|
|
73
|
+
pool = DsHelper(cls.__connect_name__).pool
|
|
74
|
+
database_name, collection_name = cls.__route_key__.split(".")
|
|
75
|
+
collection = pool[database_name][collection_name]
|
|
76
|
+
total = collection.count_documents(query)
|
|
77
|
+
res = collection.find(query, find_column)
|
|
78
|
+
if sort_list:
|
|
79
|
+
res = res.sort(sort_list)
|
|
80
|
+
res = res.skip((page_num - 1) * page_size).limit(page_size)
|
|
81
|
+
if res and to_model:
|
|
82
|
+
res = [dict2model(_, cls) for _ in res]
|
|
83
|
+
else:
|
|
84
|
+
res = list(res)
|
|
85
|
+
return {"data_count": total, "data_list": res}
|
|
86
|
+
|
|
87
|
+
@classmethod
|
|
88
|
+
def query_all(cls, query: dict, find_column: Union[dict, list] = None,
|
|
89
|
+
sort_list: List[List[Any]] = None, to_model: bool = False):
|
|
90
|
+
if find_column:
|
|
91
|
+
if isinstance(find_column, list):
|
|
92
|
+
find_column = {_: 1 for _ in find_column}
|
|
93
|
+
pool = DsHelper(cls.__connect_name__).pool
|
|
94
|
+
database_name, collection_name = cls.__route_key__.split(".")
|
|
95
|
+
collection = pool[database_name][collection_name]
|
|
96
|
+
res = collection.find(query, find_column)
|
|
97
|
+
if sort_list:
|
|
98
|
+
res = res.sort(sort_list)
|
|
99
|
+
if res and to_model:
|
|
100
|
+
res = [dict2model(_, cls) for _ in res]
|
|
101
|
+
else:
|
|
102
|
+
res = list(res)
|
|
103
|
+
return res
|
|
104
|
+
|
|
105
|
+
def insert_one(self):
|
|
106
|
+
document = dict()
|
|
107
|
+
for k, v in vars(self).items():
|
|
108
|
+
if not k.startswith("__"):
|
|
109
|
+
document.update({k: v})
|
|
110
|
+
pool = DsHelper(self.__connect_name__).pool
|
|
111
|
+
database_name, collection_name = self.__route_key__.split(".")
|
|
112
|
+
collection = pool[database_name][collection_name]
|
|
113
|
+
return collection.insert_one(document=document)
|
|
114
|
+
|
|
115
|
+
@classmethod
|
|
116
|
+
def insert_many(cls, data: List[dict]):
|
|
117
|
+
pool = DsHelper(cls.__connect_name__).pool
|
|
118
|
+
database_name, collection_name = cls.__route_key__.split(".")
|
|
119
|
+
collection = pool[database_name][collection_name]
|
|
120
|
+
return collection.insert_many(documents=data)
|
|
121
|
+
|
|
122
|
+
@classmethod
|
|
123
|
+
def delete_one(cls, query):
|
|
124
|
+
pool = DsHelper(cls.__connect_name__).pool
|
|
125
|
+
database_name, collection_name = cls.__route_key__.split(".")
|
|
126
|
+
collection = pool[database_name][collection_name]
|
|
127
|
+
return collection.delete_one(query)
|
|
128
|
+
|
|
129
|
+
@classmethod
|
|
130
|
+
def delete_many(cls, query):
|
|
131
|
+
pool = DsHelper(cls.__connect_name__).pool
|
|
132
|
+
database_name, collection_name = cls.__route_key__.split(".")
|
|
133
|
+
collection = pool[database_name][collection_name]
|
|
134
|
+
return collection.delete_many(query)
|
|
135
|
+
|
|
136
|
+
@classmethod
|
|
137
|
+
def update_one(cls, query, update, upsert=False, **kwargs):
|
|
138
|
+
pool = DsHelper(cls.__connect_name__).pool
|
|
139
|
+
database_name, collection_name = cls.__route_key__.split(".")
|
|
140
|
+
collection = pool[database_name][collection_name]
|
|
141
|
+
return collection.update_one(filter=query, update=update, upsert=upsert, **kwargs)
|
|
142
|
+
|
|
143
|
+
@classmethod
|
|
144
|
+
def update_many(cls, query, update, upsert=False, **kwargs):
|
|
145
|
+
pool = DsHelper(cls.__connect_name__).pool
|
|
146
|
+
database_name, collection_name = cls.__route_key__.split(".")
|
|
147
|
+
collection = pool[database_name][collection_name]
|
|
148
|
+
return collection.update_many(filter=query, update=update, upsert=upsert, **kwargs)
|
|
149
|
+
|
|
150
|
+
@classmethod
|
|
151
|
+
def aggregate(cls, pipeline, **kwargs):
|
|
152
|
+
pool = DsHelper(cls.__connect_name__).pool
|
|
153
|
+
database_name, collection_name = cls.__route_key__.split(".")
|
|
154
|
+
collection = pool[database_name][collection_name]
|
|
155
|
+
return collection.aggregate(pipeline=pipeline, **kwargs)
|
|
156
|
+
|
|
157
|
+
@classmethod
|
|
158
|
+
def bulk_write(cls, data: list, ordered: bool = True, **kwargs):
|
|
159
|
+
pool = DsHelper(cls.__connect_name__).pool
|
|
160
|
+
database_name, collection_name = cls.__route_key__.split(".")
|
|
161
|
+
collection = pool[database_name][collection_name]
|
|
162
|
+
return collection.bulk_write(requests=data, ordered=ordered, **kwargs)
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def dict2model(data: dict, model):
|
|
166
|
+
if issubclass(model, MongoBaseModel):
|
|
167
|
+
return model(**data)
|
|
168
|
+
else:
|
|
169
|
+
raise Exception(f"{model.__class__.__name__} is not subclass of BaseModel")
|
lesscode_database/version.py
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
__version__ = "0.0.
|
|
1
|
+
__version__ = "0.0.9"
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
lesscode_database/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
lesscode_database/connect_pool.py,sha256=_RdmtKmacohFP2FwEet_4HY05Vr5zGlwj8lkFnfmK64,44572
|
|
3
|
+
lesscode_database/connection_info.py,sha256=HMlA-hA0LE7jGUS8crZAgrBMB-o6B9mxrWYEywgqXHg,1307
|
|
4
|
+
lesscode_database/db_options.py,sha256=Z8Iy3pqDYW5YVVfvoFSsAYecK9LfiwRasYc3fzqIBt8,1928
|
|
5
|
+
lesscode_database/db_request.py,sha256=siZGXIdPuTlY4NwVLnU8098bVM_6jgQzSzeO4BzkDrA,4832
|
|
6
|
+
lesscode_database/ds_helper.py,sha256=W3VrTe_l0mGX0uEbN7UI6NlQbruLpr2WXgO7AexzQ-k,4845
|
|
7
|
+
lesscode_database/dynamic_import_package.py,sha256=J8hgGRHe6KrprOgOq-xbKHVAYHSjUBcNyTaSPvBmvIk,164
|
|
8
|
+
lesscode_database/mongo_base_model.py,sha256=cKHNmi5GJBGRxCtqsggEcrzIZK7__Z_4cVaIHxmqZsc,6406
|
|
9
|
+
lesscode_database/version.py,sha256=46Yjk3fz9o8aTN8E95McnzpJcjGzVJmHmQqUZ5mXzfc,22
|
|
10
|
+
lesscode_database-0.0.9.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
|
|
11
|
+
lesscode_database-0.0.9.dist-info/METADATA,sha256=v1FcYg9dFAyZ4-U_t34STUYQWhF4QpEabH0aQRfk1jg,11692
|
|
12
|
+
lesscode_database-0.0.9.dist-info/WHEEL,sha256=yQN5g4mg4AybRjkgi-9yy4iQEFibGQmlz78Pik5Or-A,92
|
|
13
|
+
lesscode_database-0.0.9.dist-info/top_level.txt,sha256=h6cg13be6kkDfNaX9nWeDcfducTb5bKG5iYRO2JPmAM,18
|
|
14
|
+
lesscode_database-0.0.9.dist-info/RECORD,,
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
lesscode_database/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
-
lesscode_database/connect_pool.py,sha256=LtUoBzZoMpg3Pm_jqhiY3MdyZFeZnlM5lVJyTs5P298,41144
|
|
3
|
-
lesscode_database/connection_info.py,sha256=HMlA-hA0LE7jGUS8crZAgrBMB-o6B9mxrWYEywgqXHg,1307
|
|
4
|
-
lesscode_database/db_options.py,sha256=Z8Iy3pqDYW5YVVfvoFSsAYecK9LfiwRasYc3fzqIBt8,1928
|
|
5
|
-
lesscode_database/db_request.py,sha256=siZGXIdPuTlY4NwVLnU8098bVM_6jgQzSzeO4BzkDrA,4832
|
|
6
|
-
lesscode_database/ds_helper.py,sha256=W3VrTe_l0mGX0uEbN7UI6NlQbruLpr2WXgO7AexzQ-k,4845
|
|
7
|
-
lesscode_database/dynamic_import_package.py,sha256=J8hgGRHe6KrprOgOq-xbKHVAYHSjUBcNyTaSPvBmvIk,164
|
|
8
|
-
lesscode_database/version.py,sha256=R9xOYoYrWKcfO5zvTeGC3m_eDNOvxMd8CocQs2tLufo,22
|
|
9
|
-
lesscode_database-0.0.7.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
|
|
10
|
-
lesscode_database-0.0.7.dist-info/METADATA,sha256=VVM9UbLSs-oRjEnyKwtaP4YCSiMwXVgrUd_4vNnifk0,11692
|
|
11
|
-
lesscode_database-0.0.7.dist-info/WHEEL,sha256=pkctZYzUS4AYVn6dJ-7367OJZivF2e8RA9b_ZBjif18,92
|
|
12
|
-
lesscode_database-0.0.7.dist-info/top_level.txt,sha256=h6cg13be6kkDfNaX9nWeDcfducTb5bKG5iYRO2JPmAM,18
|
|
13
|
-
lesscode_database-0.0.7.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|