Skip to content

Commit ceb21e7

Browse files
committed
fix: DuckDB 등도 매칭 가능하도록 자동탐색 추가
1 parent 8b755d6 commit ceb21e7

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

utils/databases/factory.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"""
88

99
import importlib
10+
import inspect
1011
import os
1112
from typing import Optional
1213

@@ -25,18 +26,24 @@ class DatabaseFactory:
2526
@staticmethod
2627
def get_connector(db_type: Optional[str] = None, config: Optional[DBConfig] = None):
2728
"""
28-
주어진 DB 타입에 맞는 Connector 인스턴스를 반환합니다.
29+
주어진 DB 타입에 해당하는 Connector 인스턴스를 반환합니다.
30+
31+
지정된 DB 타입에 맞는 커넥터 모듈을 동적으로 로드하고,
32+
해당 모듈 내의 Connector 클래스를 탐색하여 인스턴스를 생성합니다.
33+
DB 타입이 지정되지 않은 경우 환경 변수(DB_TYPE)에서 자동으로 가져옵니다.
2934
3035
Args:
31-
db_type (Optional[str]): DB 타입 문자열. (예: 'postgres', 'mysql', 'trino')
32-
config (Optional[DBConfig]): DB 연결 설정 객체.
33-
지정되지 않은 경우 환경변수에서 자동 로드합니다.
36+
db_type (Optional[str]): 데이터베이스 타입 문자열 (예: 'postgres', 'mysql', 'trino').
37+
config (Optional[DBConfig]): 데이터베이스 연결 설정 객체.
38+
지정되지 않은 경우 환경 변수에서 자동으로 로드됩니다.
3439
3540
Returns:
36-
BaseConnector: 지정된 DB 타입에 맞는 Connector 인스턴스.
41+
BaseConnector: 주어진 DB 타입에 해당하는 Connector 인스턴스.
3742
3843
Raises:
39-
ValueError: 지원되지 않는 DB 타입이거나 모듈을 로드할 수 없는 경우.
44+
ValueError: DB_TYPE이 지정되지 않았거나,
45+
지원되지 않는 DB 타입이거나,
46+
모듈 또는 Connector 클래스를 찾을 수 없는 경우.
4047
"""
4148
if not db_type:
4249
db_type = os.getenv("DB_TYPE")
@@ -50,10 +57,17 @@ def get_connector(db_type: Optional[str] = None, config: Optional[DBConfig] = No
5057
try:
5158
module_name = f"utils.databases.connector.{db_type}_connector"
5259
module = importlib.import_module(module_name)
53-
connector_class = getattr(module, f"{db_type.capitalize()}Connector")
60+
61+
connector_class = None
62+
for name, cls in inspect.getmembers(module, inspect.isclass):
63+
if name.lower() == f"{db_type}connector":
64+
connector_class = cls
65+
break
66+
if connector_class is None:
67+
raise ValueError(f"No matching Connector class found for {db_type}")
5468
except (ImportError, AttributeError) as e:
5569
logger.error(
56-
"지원되지 않는 DB 타입이거나 모듈을 로드할 수 없습니다: {%s}",
70+
"지원되지 않는 DB 타입이거나 모듈을 로드할 수 없습니다: %s",
5771
db_type,
5872
)
5973
raise ValueError(f"Unsupported DB type: {db_type}") from e

0 commit comments

Comments
 (0)