db-sdk 4.0.12__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.
- db_sdk/__init__.py +19 -0
- db_sdk/config/__init__.py +0 -0
- db_sdk/config/settings.py +0 -0
- db_sdk/connectors/__init__.py +0 -0
- db_sdk/connectors/mongodb.py +15 -0
- db_sdk/connectors/mysql.py +20 -0
- db_sdk/connectors/postgres.py +20 -0
- db_sdk/connectors/redis.py +20 -0
- db_sdk/core/__init__.py +0 -0
- db_sdk/core/base_connector.py +11 -0
- db_sdk/core/exceptions.py +18 -0
- db_sdk/core/factory.py +30 -0
- db_sdk/test_connection.py +15 -0
- db_sdk-4.0.12.dist-info/METADATA +17 -0
- db_sdk-4.0.12.dist-info/RECORD +17 -0
- db_sdk-4.0.12.dist-info/WHEEL +5 -0
- db_sdk-4.0.12.dist-info/top_level.txt +1 -0
db_sdk/__init__.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import os
|
|
2
|
+
from db_sdk.core.exceptions import ConfigurationError
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def load_config_from_env():
|
|
6
|
+
db_type = os.getenv("DB_TYPE")
|
|
7
|
+
|
|
8
|
+
if not db_type:
|
|
9
|
+
raise ConfigurationError("DB_TYPE environment variable not set.")
|
|
10
|
+
|
|
11
|
+
return {
|
|
12
|
+
"db_type": db_type,
|
|
13
|
+
"host": os.getenv("DB_HOST"),
|
|
14
|
+
"user": os.getenv("DB_USER"),
|
|
15
|
+
"password": os.getenv("DB_PASSWORD"),
|
|
16
|
+
"database": os.getenv("DB_NAME"),
|
|
17
|
+
"port": os.getenv("DB_PORT"),
|
|
18
|
+
"uri": os.getenv("DB_URI"),
|
|
19
|
+
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from pymongo import MongoClient
|
|
2
|
+
from ..core.base_connector import BaseConnector
|
|
3
|
+
from ..core.exceptions import DatabaseConnectionError
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class MongoConnector(BaseConnector):
|
|
7
|
+
|
|
8
|
+
def connect(self):
|
|
9
|
+
try:
|
|
10
|
+
uri = self.config.get("uri")
|
|
11
|
+
client = MongoClient(uri)
|
|
12
|
+
return client
|
|
13
|
+
|
|
14
|
+
except Exception as e:
|
|
15
|
+
raise DatabaseConnectionError(f"MongoDB connection failed: {e}")
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import mysql.connector
|
|
2
|
+
from ..core.base_connector import BaseConnector
|
|
3
|
+
from ..core.exceptions import DatabaseConnectionError
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class MySQLConnector(BaseConnector):
|
|
7
|
+
|
|
8
|
+
def connect(self):
|
|
9
|
+
try:
|
|
10
|
+
connection = mysql.connector.connect(
|
|
11
|
+
host=self.config.get("host"),
|
|
12
|
+
user=self.config.get("user"),
|
|
13
|
+
password=self.config.get("password"),
|
|
14
|
+
database=self.config.get("database"),
|
|
15
|
+
port=self.config.get("port", 3306),
|
|
16
|
+
)
|
|
17
|
+
return connection
|
|
18
|
+
|
|
19
|
+
except mysql.connector.Error as e:
|
|
20
|
+
raise DatabaseConnectionError(f"MySQL connection failed: {e}")
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import psycopg2
|
|
2
|
+
from ..core.base_connector import BaseConnector
|
|
3
|
+
from ..core.exceptions import DatabaseConnectionError
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class PostgresConnector(BaseConnector):
|
|
7
|
+
|
|
8
|
+
def connect(self):
|
|
9
|
+
try:
|
|
10
|
+
connection = psycopg2.connect(
|
|
11
|
+
host=self.config.get("host"),
|
|
12
|
+
user=self.config.get("user"),
|
|
13
|
+
password=self.config.get("password"),
|
|
14
|
+
dbname=self.config.get("database"),
|
|
15
|
+
port=self.config.get("port", 5432),
|
|
16
|
+
)
|
|
17
|
+
return connection
|
|
18
|
+
|
|
19
|
+
except psycopg2.Error as e:
|
|
20
|
+
raise DatabaseConnectionError(f"PostgreSQL connection failed: {e}")
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import redis
|
|
2
|
+
from ..core.base_connector import BaseConnector
|
|
3
|
+
from ..core.exceptions import DatabaseConnectionError
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class RedisConnector(BaseConnector):
|
|
7
|
+
|
|
8
|
+
def connect(self):
|
|
9
|
+
try:
|
|
10
|
+
connection = redis.Redis(
|
|
11
|
+
host=self.config.get("host"),
|
|
12
|
+
port=self.config.get("port", 6379),
|
|
13
|
+
password=self.config.get("password"),
|
|
14
|
+
decode_responses=True,
|
|
15
|
+
)
|
|
16
|
+
connection.ping() # Test connection
|
|
17
|
+
return connection
|
|
18
|
+
|
|
19
|
+
except redis.RedisError as e:
|
|
20
|
+
raise DatabaseConnectionError(f"Redis connection failed: {e}")
|
db_sdk/core/__init__.py
ADDED
|
File without changes
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
class BaseConnector:
|
|
2
|
+
"""
|
|
3
|
+
Base connector class.
|
|
4
|
+
All database connectors must inherit from this.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
def __init__(self, config: dict):
|
|
8
|
+
self.config = config
|
|
9
|
+
|
|
10
|
+
def connect(self):
|
|
11
|
+
raise NotImplementedError("Connect method must be implemented by subclass.")
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
class UniversalDBException(Exception):
|
|
2
|
+
"""Base exception for Universal DB SDK."""
|
|
3
|
+
pass
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class UnsupportedDatabaseError(UniversalDBException):
|
|
7
|
+
"""Raised when unsupported database type is provided."""
|
|
8
|
+
pass
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class DatabaseConnectionError(UniversalDBException):
|
|
12
|
+
"""Raised when connection fails."""
|
|
13
|
+
pass
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class ConfigurationError(UniversalDBException):
|
|
17
|
+
"""Raised when configuration is invalid."""
|
|
18
|
+
pass
|
db_sdk/core/factory.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
from db_sdk.connectors.mysql import MySQLConnector
|
|
2
|
+
from db_sdk.connectors.postgres import PostgresConnector
|
|
3
|
+
from db_sdk.connectors.mongodb import MongoConnector
|
|
4
|
+
from db_sdk.connectors.redis import RedisConnector
|
|
5
|
+
# from db_sdk.core.exceptions import UnsupportedDatabaseError
|
|
6
|
+
from .exceptions import UnsupportedDatabaseError
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class ConnectionFactory:
|
|
10
|
+
|
|
11
|
+
CONNECTOR_MAPPING = {
|
|
12
|
+
"mysql": MySQLConnector,
|
|
13
|
+
"postgres": PostgresConnector,
|
|
14
|
+
"mongodb": MongoConnector,
|
|
15
|
+
"redis": RedisConnector,
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
@staticmethod
|
|
19
|
+
def create_connection(db_type: str, config: dict):
|
|
20
|
+
db_type = db_type.lower()
|
|
21
|
+
|
|
22
|
+
if db_type not in ConnectionFactory.CONNECTOR_MAPPING:
|
|
23
|
+
raise UnsupportedDatabaseError(
|
|
24
|
+
f"Unsupported database type: {db_type}"
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
connector_class = ConnectionFactory.CONNECTOR_MAPPING[db_type]
|
|
28
|
+
connector = connector_class(config)
|
|
29
|
+
|
|
30
|
+
return connector.connect()
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from db_sdk.core.factory import ConnectionFactory
|
|
2
|
+
|
|
3
|
+
config = {
|
|
4
|
+
"host": "192.168.137.9",
|
|
5
|
+
"user": "pwf",
|
|
6
|
+
"password": "JqYVmy5uPTzG",
|
|
7
|
+
"database": "pwf",
|
|
8
|
+
"port": 3306
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
try:
|
|
12
|
+
connection = ConnectionFactory.create_connection("mysql", config)
|
|
13
|
+
print("✅ Database connected successfully!")
|
|
14
|
+
except Exception as e:
|
|
15
|
+
print("❌ Connection failed:", e)
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: db_sdk
|
|
3
|
+
Version: 4.0.12
|
|
4
|
+
Summary: A simple database connection SDK
|
|
5
|
+
Author: Neha
|
|
6
|
+
Requires-Python: >=3.8
|
|
7
|
+
Requires-Dist: pymongo>=4.0.0
|
|
8
|
+
Requires-Dist: mysql-connector-python>=8.0
|
|
9
|
+
Requires-Dist: psycopg2-binary>=2.9.0
|
|
10
|
+
Requires-Dist: redis>=4.0.0
|
|
11
|
+
Requires-Dist: pytest>=7.0.0
|
|
12
|
+
Requires-Dist: pytest-cov>=4.0.0
|
|
13
|
+
Requires-Dist: mongomock>=4.1.2
|
|
14
|
+
Dynamic: author
|
|
15
|
+
Dynamic: requires-dist
|
|
16
|
+
Dynamic: requires-python
|
|
17
|
+
Dynamic: summary
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
db_sdk/__init__.py,sha256=8oYWCzSeU8jAHKIdO03T92VMmBHjgjLc7UZENoxrJR4,509
|
|
2
|
+
db_sdk/test_connection.py,sha256=iEnbWKy5JE9PQx_Ltyrd-B46295uGzzCH-EyDRhqEDc,371
|
|
3
|
+
db_sdk/config/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
db_sdk/config/settings.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
5
|
+
db_sdk/connectors/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
6
|
+
db_sdk/connectors/mongodb.py,sha256=0qp1pZme06h3U0NNzt_Q8ffBV1v6aLxuY2SFzTjUW3Y,423
|
|
7
|
+
db_sdk/connectors/mysql.py,sha256=M8UkfrfIbCW4PwytQYuisi4QvxHx49BrfbOHYyOuQx8,665
|
|
8
|
+
db_sdk/connectors/postgres.py,sha256=0XXKbHdQJWPX95pfaU14Jn7eEnJVjOeovYA55w0alb0,650
|
|
9
|
+
db_sdk/connectors/redis.py,sha256=WwVqKvJkk9DPwQg0dPy5SJmQh4T23_fELGy0uApsyoE,626
|
|
10
|
+
db_sdk/core/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
11
|
+
db_sdk/core/base_connector.py,sha256=IoOpxqUPgNDsOK2om755qF0Mz-zrQIplXBngGeMQsLk,291
|
|
12
|
+
db_sdk/core/exceptions.py,sha256=R9q4_FkxaZhyf24Cwmg8pfs0dh5sFoXf_Ih88bdr2kA,431
|
|
13
|
+
db_sdk/core/factory.py,sha256=hrydt3J3S2YrYgoGo1iBjG_E8dFvvbX_QwhkbdSrFpo,960
|
|
14
|
+
db_sdk-4.0.12.dist-info/METADATA,sha256=zurWB8bG8HdzH1zCXoV6KaDwNoShma4rvguuy1kdQ7k,443
|
|
15
|
+
db_sdk-4.0.12.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
16
|
+
db_sdk-4.0.12.dist-info/top_level.txt,sha256=sWeCT6R-hslk8L6onXix41smZ45S8k6LmYDErKvC8xA,7
|
|
17
|
+
db_sdk-4.0.12.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
db_sdk
|