|
1 | | -from MSSQLConnector import MSSQLConnector |
2 | | -from MariaDBConnector import MariaDBConnector |
3 | | -from OracleConnector import OracleConnector |
4 | | -from PostgresConnector import PostgresConnector |
| 1 | +from DBConnectorFactory import DBConnectorFactory |
5 | 2 | from SQLYExecutionError import SQLYExecutionError |
6 | | -from SQLYUtils import SQLYUtils |
7 | | -from SQLiteConnector import SQLiteConnector |
8 | 3 |
|
9 | 4 |
|
10 | 5 | class DatabaseConnector: |
11 | 6 | """ |
12 | | - DatabaseConnector is a utility class that provides a method to obtain a specific |
13 | | - database connector instance based on the database type. |
14 | | -
|
15 | | - Methods: |
16 | | - get_connector(db_type: str, connection: Any) -> Any: |
17 | | - Returns an instance of the appropriate database connector class based on |
18 | | - the provided database type. |
19 | | -
|
20 | | - Parameters: |
21 | | - db_type (str): The type of the database (e.g., 'sqlite', 'mariadb', 'postgres', 'oracle', 'mssql'). |
22 | | - connection (Any): The connection object or connection string required to establish a connection to the database. |
23 | | -
|
24 | | - Returns: |
25 | | - An instance of the corresponding database connector class. |
26 | | -
|
27 | | - Raises: |
28 | | - SQLYExecutionError: If the provided database type is not supported. |
| 7 | + DatabaseConnector is a utility class that provides methods to connect to |
| 8 | + and execute queries against different types of databases. |
29 | 9 | """ |
30 | | - @staticmethod |
31 | | - def get_connector(db_type, connection): |
| 10 | + def __init__(self, db_type, connection): |
32 | 11 | """ |
33 | | - Returns a database connector instance based on the specified database type. |
| 12 | + Initializes the DatabaseConnector with the database type and connection information. |
34 | 13 |
|
35 | 14 | Args: |
36 | 15 | db_type (str): The type of the database (e.g., "sqlite", "mariadb", "postgres", "oracle", "mssql"). |
37 | 16 | connection: The connection object or parameters required to establish the database connection. |
| 17 | + """ |
| 18 | + self.db_type = db_type |
| 19 | + self.connection = connection |
| 20 | + self.connector = None |
38 | 21 |
|
39 | | - Returns: |
40 | | - An instance of the corresponding database connector class. |
| 22 | + def _ensure_connector(self): |
| 23 | + """ |
| 24 | + Ensures that a connector instance exists, creating it if necessary. |
41 | 25 |
|
42 | | - Raises: |
43 | | - SQLYExecutionError: If the specified database type is not supported. |
| 26 | + Returns: |
| 27 | + The database connector instance. |
44 | 28 | """ |
45 | | - connectors = { |
46 | | - "sqlite": SQLiteConnector, |
47 | | - "mariadb": MariaDBConnector, |
48 | | - "postgres": PostgresConnector, |
49 | | - "oracle": OracleConnector, |
50 | | - "mssql": MSSQLConnector, |
51 | | - } |
52 | | - if db_type not in connectors: |
53 | | - raise SQLYExecutionError("Unsupported database type: " + db_type) |
54 | | - return connectors[db_type](connection) |
| 29 | + if self.connector is None: |
| 30 | + self.connector = DBConnectorFactory.create_connector(self.db_type, self.connection) |
| 31 | + return self.connector |
55 | 32 |
|
56 | | - def execute_query(self, query: dict, db_type: str, connection: Any): |
| 33 | + def execute_query(self, query: dict, db_type=None, connection=None): |
57 | 34 | """ |
58 | 35 | Executes a SQLY query against a specified database. |
59 | 36 |
|
60 | 37 | Args: |
61 | 38 | query (dict): The query dictionary" |
62 | | - db_type (str): The type of the database (e.g., "sqlite", "mariadb", "postgres", "oracle", "mssql"). |
63 | | - connection: The connection object or parameters required to establish the database connection. |
| 39 | + db_type (str, optional): The type of the database. Defaults to the instance's db_type. |
| 40 | + connection (optional): The connection object or parameters. Defaults to the instance's connection. |
64 | 41 |
|
65 | 42 | Returns: |
66 | 43 | The result of the executed query. |
67 | 44 |
|
68 | 45 | Raises: |
69 | 46 | SQLYExecutionError: If the query is invalid or an error occurs during execution. |
70 | 47 | """ |
71 | | - db_connector = DatabaseConnector.get_connector(db_type, connection) |
72 | | - sql, params = SQLYUtils.translate_to_sql(query) |
73 | | - return db_connector.execute(sql, params) |
| 48 | + # Use provided values or fall back to instance attributes |
| 49 | + db_type = db_type or self.db_type |
| 50 | + connection = connection or self.connection |
| 51 | + |
| 52 | + if db_type != self.db_type or connection != self.connection: |
| 53 | + # If parameters differ from instance attributes, create a new connector |
| 54 | + connector = DBConnectorFactory.create_connector(db_type, connection) |
| 55 | + else: |
| 56 | + connector = self._ensure_connector() |
| 57 | + |
| 58 | + # Execute the query using the appropriate connector |
| 59 | + return connector.execute_query(query) |
0 commit comments