44
55from pysqly .errors import SQLYExecutionError
66
7- from .mariadb import MariaDBConnector
8- from .mssql import MSSQLConnector
9- from .oracle import OracleConnector
10- from .postgres import PostgresConnector
7+ # Import connectors directly instead of from module to avoid circular dependencies
8+ from .mariadb import MYSQL_AVAILABLE , MariaDBConnector
9+ from .mssql import MSSQL_AVAILABLE , MSSQLConnector
10+ from .oracle import ORACLE_AVAILABLE , OracleConnector
11+ from .postgres import POSTGRES_AVAILABLE , PostgresConnector
1112from .sqlite import SQLiteConnector
1213
1314
@@ -34,15 +35,41 @@ def create_connector(db_type: str, connection: Any) -> Any:
3435 An instance of the corresponding database connector class.
3536
3637 Raises:
37- SQLYExecutionError: If the specified database type is not supported.
38+ SQLYExecutionError: If the specified database type is not supported or
39+ if the required database driver is not installed.
3840 """
41+ # Check if the requested database type is available
42+ if db_type == "mariadb" and not MYSQL_AVAILABLE :
43+ raise SQLYExecutionError (
44+ "MariaDB/MySQL connector is not available. "
45+ "Please install mysql-connector-python package."
46+ )
47+ elif db_type == "postgres" and not POSTGRES_AVAILABLE :
48+ raise SQLYExecutionError (
49+ "PostgreSQL connector is not available. "
50+ "Please install psycopg2 package."
51+ )
52+ elif db_type == "oracle" and not ORACLE_AVAILABLE :
53+ raise SQLYExecutionError (
54+ "Oracle connector is not available. "
55+ "Please install cx_Oracle package."
56+ )
57+ elif db_type == "mssql" and not MSSQL_AVAILABLE :
58+ raise SQLYExecutionError (
59+ "MS SQL Server connector is not available. "
60+ "Please install pyodbc package."
61+ )
62+
63+ # Define the mapping of database types to connector classes
3964 connectors = {
4065 "sqlite" : SQLiteConnector ,
4166 "mariadb" : MariaDBConnector ,
4267 "postgres" : PostgresConnector ,
4368 "oracle" : OracleConnector ,
4469 "mssql" : MSSQLConnector ,
4570 }
71+
4672 if db_type not in connectors :
4773 raise SQLYExecutionError (f"Unsupported database type: { db_type } " )
74+
4875 return connectors [db_type ](connection )
0 commit comments