lidb 1.0.3__py3-none-any.whl → 1.0.5__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.

Potentially problematic release.


This version of lidb might be problematic. Click here for more details.

lidb/__init__.py CHANGED
@@ -15,6 +15,8 @@ from .database import (
15
15
  put,
16
16
  has,
17
17
  tb_path,
18
+ read_mysql,
19
+ read_ck,
18
20
  )
19
21
 
20
- __version__ = "1.0.3"
22
+ __version__ = "1.0.5"
lidb/database.py CHANGED
@@ -10,7 +10,8 @@ Email: yundi.xxii@outlook.com
10
10
  import re
11
11
  from pathlib import Path
12
12
  from .parse import extract_table_names_from_sql
13
- from .init import DB_PATH, logger
13
+ from .init import DB_PATH, logger, get_settings
14
+ import urllib
14
15
 
15
16
  # ======================== 本地数据库 catdb ========================
16
17
  def tb_path(tb_name: str) -> Path:
@@ -110,3 +111,66 @@ def sql(query: str, abs_path: bool = False):
110
111
  new_query = pattern.sub(lambda m: convertor[m.group(0)], query)
111
112
  return pl.sql(new_query)
112
113
 
114
+ def read_mysql(query: str, db_conf: str = "DATABASES.mysql"):
115
+ """
116
+ 从MySQL数据库中读取数据。
117
+ Parameters
118
+ ----------
119
+ query: str
120
+ 查询语句
121
+ db_conf: str
122
+ 对应的配置 $DB_PATH/conf/settings.toml
123
+ Returns
124
+ -------
125
+ polars.DataFrame
126
+ """
127
+ import polars as pl
128
+ try:
129
+ db_setting = get_settings().get(db_conf, {})
130
+ required_keys = ['user', 'password', 'url']
131
+ missing_keys = [key for key in required_keys if key not in db_setting]
132
+ if missing_keys:
133
+ raise KeyError(f"Missing required keys in database config: {missing_keys}")
134
+
135
+ user = urllib.parse.quote_plus(db_setting['user'])
136
+ password = urllib.parse.quote_plus(db_setting['password'])
137
+ uri = f"mysql://{user}:{password}@{db_setting['url']}"
138
+ return pl.read_database_uri(query, uri)
139
+
140
+ except KeyError as e:
141
+ raise RuntimeError("Database configuration error: missing required fields.") from e
142
+ except Exception as e:
143
+ raise RuntimeError(f"Failed to execute MySQL query: {e}") from e
144
+
145
+
146
+ def read_ck(query: str, db_conf: str = "DATABASES.ck"):
147
+ """
148
+ 从Clickhouse集群读取数据。
149
+ Parameters
150
+ ----------
151
+ query: str
152
+ 查询语句
153
+ db_conf: str
154
+ 对应的配置 $DB_PATH/conf/settings.toml
155
+ Returns
156
+ -------
157
+ polars.DataFrame
158
+ """
159
+ import clickhouse_df
160
+ try:
161
+ db_setting = get_settings().get(db_conf, {})
162
+ required_keys = ['user', 'password', 'urls']
163
+ missing_keys = [key for key in required_keys if key not in db_setting]
164
+ if missing_keys:
165
+ raise KeyError(f"Missing required keys in database config: {missing_keys}")
166
+
167
+ user = urllib.parse.quote_plus(db_setting['user'])
168
+ password = urllib.parse.quote_plus(db_setting['password'])
169
+
170
+ with clickhouse_df.connect(db_setting['urls'], user=user, password=password):
171
+ return clickhouse_df.to_polars(query)
172
+
173
+ except KeyError as e:
174
+ raise RuntimeError("Database configuration error: missing required fields.") from e
175
+ except Exception as e:
176
+ raise RuntimeError(f"Failed to execute ClickHouse query: {e}") from e
@@ -1,9 +1,11 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: lidb
3
- Version: 1.0.3
3
+ Version: 1.0.5
4
4
  Requires-Python: >=3.12
5
5
  Description-Content-Type: text/markdown
6
6
  Requires-Dist: dynaconf>=3.2.11
7
7
  Requires-Dist: polars>=1.31.0
8
8
  Requires-Dist: sqlparse>=0.5.3
9
9
  Requires-Dist: logair>=1.0.0
10
+ Requires-Dist: clickhouse-df>=0.1.5
11
+ Requires-Dist: connectorx>=0.4.3
@@ -0,0 +1,8 @@
1
+ lidb/__init__.py,sha256=bkXm3SpSwrgGZ6UdTGhT2NN8-a0k_lFo08mIL6W7KLw,306
2
+ lidb/database.py,sha256=U1h80jgmkRfgQW8sdhb7B3ISvQJUvPefLurWMsbzqa0,5372
3
+ lidb/init.py,sha256=S6Yud6bHEmbaCVV7yXZWsQhe0YIsw9D_Yy17kiDpsGQ,1159
4
+ lidb/parse.py,sha256=N1BBZoUhvLj58biZfEhFs4cGsqaZqsanx27bAp_P7Oo,2236
5
+ lidb-1.0.5.dist-info/METADATA,sha256=IXmwvqe87vY_UfJWXD2kI09MA1OLD8C8Ecq-1F8FbJ8,303
6
+ lidb-1.0.5.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
7
+ lidb-1.0.5.dist-info/top_level.txt,sha256=NgXJNwt6ld6oLXtW1vOPaEh-VO5R0JEX_KmGIJR4ueE,5
8
+ lidb-1.0.5.dist-info/RECORD,,
@@ -1,8 +0,0 @@
1
- lidb/__init__.py,sha256=GZLAOnJ1tu9y_9t5VNXeSbg3_txKg6xkktPBsEh3_ek,277
2
- lidb/database.py,sha256=OktJZCPVIaBUYENGMeWNB8NPOy1R01kwWtkfuUzP07E,3147
3
- lidb/init.py,sha256=S6Yud6bHEmbaCVV7yXZWsQhe0YIsw9D_Yy17kiDpsGQ,1159
4
- lidb/parse.py,sha256=N1BBZoUhvLj58biZfEhFs4cGsqaZqsanx27bAp_P7Oo,2236
5
- lidb-1.0.3.dist-info/METADATA,sha256=ZatuH9dkJ4iGgyXpCizaRz_ShWiS1CxzDESEb5jNNqY,234
6
- lidb-1.0.3.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
7
- lidb-1.0.3.dist-info/top_level.txt,sha256=NgXJNwt6ld6oLXtW1vOPaEh-VO5R0JEX_KmGIJR4ueE,5
8
- lidb-1.0.3.dist-info/RECORD,,
File without changes