clickzetta-dbutils 1.0.7__py3-none-any.whl → 1.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.
@@ -1,5 +1,5 @@
1
1
  from .db_utils import get_active_engine, get_lakehouse_connection, DatabaseConnectionManager, ConnectionConfig, \
2
- DatabaseConnectionError
2
+ DatabaseConnectionError, get_active_lakehouse_engine
3
3
 
4
- __all__ = ["get_active_engine", "get_lakehouse_connection", "DatabaseConnectionManager", "ConnectionConfig",
4
+ __all__ = ["get_active_engine", "get_lakehouse_connection", "get_active_lakehouse_engine", "DatabaseConnectionManager", "ConnectionConfig",
5
5
  "DatabaseConnectionError"]
@@ -38,7 +38,7 @@ class DatabaseConnectionManager:
38
38
  Manages database connections with flexible configuration options.
39
39
  """
40
40
 
41
- def __init__(self, ds_name: str):
41
+ def __init__(self, ds_name: Optional[str] = None):
42
42
  """
43
43
  Initialize a database connection for a specific data source.
44
44
  """
@@ -50,9 +50,10 @@ class DatabaseConnectionManager:
50
50
  self._engine: Optional[Engine] = None
51
51
  self._options = {}
52
52
  self._query: Dict[str, str] = {}
53
+ self._host: Optional[str] = None
53
54
 
54
55
  @classmethod
55
- def _load_connection_configs(cls) -> Dict[str, ConnectionConfig]:
56
+ def load_connection_configs(cls) -> Dict[str, ConnectionConfig]:
56
57
  """
57
58
  Load and cache connection configurations from environment variables.
58
59
 
@@ -61,10 +62,18 @@ class DatabaseConnectionManager:
61
62
  """
62
63
  if not hasattr(DatabaseConnectionManager, '_connection_cache'):
63
64
  # Retrieve and decode connection info from environment variable
64
- conn_info_str = os.environ.get('connectionInfos', '[]')
65
+ conn_info_str = ""
66
+ if name := os.environ.get('EXECUTE_LOG_ID', ''):
67
+ pipe_path = '/tmp/' + name
68
+ try:
69
+ with open(pipe_path, 'r') as f:
70
+ conn_info_str = f.read()
71
+ except FileNotFoundError:
72
+ pass
73
+ # Fall back to environment variable if pipe is not found
74
+ conn_info_str = conn_info_str or os.environ.get('connectionInfos', '[]')
65
75
  if not conn_info_str:
66
- raise DatabaseConnectionError(
67
- "No connection information found in environment variable 'connectionInfos'")
76
+ raise DatabaseConnectionError("No connection information found in env")
68
77
  decoded_info = urllib.parse.unquote(conn_info_str)
69
78
  conn_list = json.loads(decoded_info)
70
79
 
@@ -83,7 +92,7 @@ class DatabaseConnectionManager:
83
92
  """
84
93
  Find connection info by data source name
85
94
  """
86
- connections = self._load_connection_configs()
95
+ connections = self.load_connection_configs()
87
96
 
88
97
  # Validate data source exists
89
98
  if ds_name not in connections:
@@ -95,11 +104,12 @@ class DatabaseConnectionManager:
95
104
  config.query.update(self._query)
96
105
  return config
97
106
 
98
- def get_connection_infos(self):
107
+ @classmethod
108
+ def get_connection_infos(cls) -> Dict[str, ConnectionConfig]:
99
109
  """
100
110
  Get all connection infos
101
111
  """
102
- return self._load_connection_configs()
112
+ return DatabaseConnectionManager.load_connection_configs()
103
113
 
104
114
  def use_workspace(self, workspace: str) -> 'DatabaseConnectionManager':
105
115
  """
@@ -179,6 +189,18 @@ class DatabaseConnectionManager:
179
189
  self._query.update(query)
180
190
  return self
181
191
 
192
+ def use_host(self, host: str) -> 'DatabaseConnectionManager':
193
+ """
194
+ Set host for the connection.
195
+ Args:
196
+ host (str): Host name
197
+ Returns:
198
+ self: For method chaining
199
+ """
200
+ if host:
201
+ self._host = host
202
+ return self
203
+
182
204
  def build(self, *args, **kwargs) -> Engine:
183
205
  """
184
206
  Create SQLAlchemy engine based on data source name and optional schema
@@ -193,7 +215,8 @@ class DatabaseConnectionManager:
193
215
  ds_type = conn_info.dsType
194
216
  options = conn_info.options or {}
195
217
  schema = self._schema or conn_info.schema
196
- host_parts = conn_info.host.split(':')
218
+ host = self._host or conn_info.host
219
+ host_parts = host.split(':')
197
220
  connect_args = {}
198
221
 
199
222
  # Construct connection URL based on data source type
@@ -290,6 +313,7 @@ def get_active_engine(
290
313
  options: Optional[Dict[str, str]] = None,
291
314
  query: Optional[Dict[str, str]] = None,
292
315
  driver: Optional[str] = None,
316
+ host: Optional[str] = None,
293
317
  *args, **kwargs
294
318
  ) -> Engine:
295
319
  """
@@ -303,6 +327,7 @@ def get_active_engine(
303
327
  options (dict, optional): Additional connection options.
304
328
  query (dict, optional): Additional query parameters for SQLAlchemy url.
305
329
  driver (str, optional): Driver name for the connection.
330
+ host (str, optional): Host name for the connection.
306
331
  *args: Additional arguments for SQLAlchemy engine.
307
332
 
308
333
  Returns:
@@ -322,5 +347,66 @@ def get_active_engine(
322
347
  manager.use_query(query)
323
348
  if driver:
324
349
  manager.use_driver(driver)
350
+ if host:
351
+ manager.use_host(host)
325
352
 
326
353
  return manager.build(*args, **kwargs)
354
+
355
+
356
+ def get_active_lakehouse_engine(
357
+ vcluster: Optional[str] = None,
358
+ workspace: Optional[str] = None,
359
+ schema: Optional[str] = None,
360
+ options: Optional[Dict[str, str]] = None,
361
+ query: Optional[Dict[str, str]] = None,
362
+ driver: Optional[str] = None,
363
+ *args, **kwargs
364
+ ) -> Engine:
365
+ """
366
+ Convenience function to create a database engine for lakehouse (ClickZetta) data source.
367
+
368
+ Args:
369
+ vcluster (str, optional): Virtual cluster name for ClickZetta data source. Required.
370
+ workspace (str, optional): Workspace name. Default is 'default'.
371
+ schema (str, optional): Schema name for the connection. Default is 'public'.
372
+ options (dict, optional): Additional connection options.
373
+ query (dict, optional): Additional query parameters for SQLAlchemy url.
374
+ driver (str, optional): Driver name for the connection.
375
+ *args: Additional arguments for SQLAlchemy engine.
376
+ **kwargs: Additional keyword arguments for SQLAlchemy engine.
377
+
378
+ Returns:
379
+ SQLAlchemy Engine instance
380
+
381
+ Raises:
382
+ DatabaseConnectionError: If no lakehouse data source is found in the configuration.
383
+ """
384
+ # Get all connection configurations
385
+ conn_infos = DatabaseConnectionManager.get_connection_infos()
386
+
387
+ # Find the lakehouse (ClickZetta) data source
388
+ lakehouse_ds = None
389
+ for ds_name, conn_info in conn_infos.items():
390
+ if conn_info.dsType == 1: # ClickZetta type
391
+ lakehouse_ds = ds_name
392
+ break
393
+
394
+ if not lakehouse_ds:
395
+ raise DatabaseConnectionError("No lakehouse (ClickZetta) data source found in configuration")
396
+
397
+ # Create engine using the found lakehouse data source
398
+ return get_active_engine(
399
+ ds_name=lakehouse_ds,
400
+ vcluster=vcluster,
401
+ workspace=workspace,
402
+ schema=schema,
403
+ options=options,
404
+ query=query,
405
+ driver=driver,
406
+ *args, **kwargs
407
+ )
408
+
409
+ def clean_connection_cache():
410
+ """Clear connection cache before each test"""
411
+ if hasattr(DatabaseConnectionManager, '_connection_cache'):
412
+ delattr(DatabaseConnectionManager, '_connection_cache')
@@ -1 +1 @@
1
- __version__ = "1.0.7"
1
+ __version__ = "1.0.9"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: clickzetta-dbutils
3
- Version: 1.0.7
3
+ Version: 1.0.9
4
4
  Summary: clickzetta dbutils
5
5
  Author-email: "lin.zhang" <lin.zhang@clickzetta.com>
6
6
  Project-URL: documentation, https://www.yunqi.tech/
@@ -0,0 +1,7 @@
1
+ clickzetta_dbutils/__init__.py,sha256=XSofC7BRKAZbgXkil1HFUiHZztzWWyHcbKNi5tpOjCk,350
2
+ clickzetta_dbutils/db_utils.py,sha256=swBzlWMbwNRcEQiehXeOoWZGibaOkm-JO4pRZdAZxgM,13756
3
+ clickzetta_dbutils/version.py,sha256=lFohcdMk8DViWSfrTasSRt9RPcMXfTvmskn8IZQNlws,21
4
+ clickzetta_dbutils-1.0.9.dist-info/METADATA,sha256=wCpqpipf2S5eaHf0RVEI5CzGUGqZXe6k4N4Ya5AyT48,852
5
+ clickzetta_dbutils-1.0.9.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
6
+ clickzetta_dbutils-1.0.9.dist-info/top_level.txt,sha256=8o5KqMSg9pxnPNejHjMaqZV2vEDvwvsz2GdChZI0N6I,19
7
+ clickzetta_dbutils-1.0.9.dist-info/RECORD,,
@@ -1,7 +0,0 @@
1
- clickzetta_dbutils/__init__.py,sha256=Q_6kas0RvZ0767qlaA_xGESmXxm0dks1YQ8DqCX8LV0,290
2
- clickzetta_dbutils/db_utils.py,sha256=Vv00OcjoBZdC8z1ucEtekEwqPhu95GkgazBdoRBd-bI,10643
3
- clickzetta_dbutils/version.py,sha256=98rTj2jvgnCZhTIeHpbml7e2xuhIqt_9BJNSWkWEQb8,21
4
- clickzetta_dbutils-1.0.7.dist-info/METADATA,sha256=IPehhi4jMgaS7VZfqMhSwTkYkAbnLXHKf708o4537jo,852
5
- clickzetta_dbutils-1.0.7.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
6
- clickzetta_dbutils-1.0.7.dist-info/top_level.txt,sha256=8o5KqMSg9pxnPNejHjMaqZV2vEDvwvsz2GdChZI0N6I,19
7
- clickzetta_dbutils-1.0.7.dist-info/RECORD,,