assets_guardian.core.clients.mysql_clientο
Classes
|
MySQL database connector with resilience management. |
- class assets_guardian.core.clients.mysql_client.MySQLClient(host: str, port: int, user: str, password: str, database: str, max_retries: int = 5)[source]ο
Bases:
objectMySQL database connector with resilience management.
This class wraps mysql-connector-python to provide a robust interface for interacting with a MySQL database. It includes automatic reconnection logic with exponential backoff to handle network instability.
- Variables:
host (str) β The host of the MySQL server.
port (int) β The connection port (default: 3306).
user (str) β The username for the client connection.
password (str) β The password associated with the user.
database (str) β The target database name.
max_retries (int) β Maximum number of connection attempts before giving up.
Initializes the connector settings.
- Parameters:
host β MySQL server address.
port β Server port.
user β Connection username.
password β Connection password.
database β Target database name.
max_retries β Number of retry attempts for the connection.
- close() None[source]ο
Properly closes the active MySQL connection.
Resets the internal connection attribute after closure.
- connect() bool[source]ο
Establishes a connection to the MySQL server with a retry strategy.
Attempts to connect to the server using exponential backoff (2^attempt seconds) between each failure.
- Returns:
True if connection succeeded, False after exhausting attempts.
- Return type:
bool
- execute_query(query: str, params: dict[str, Any] | None = None) list[dict[str, Any]] | int | None[source]ο
Executes a SQL query (read or write).
If the query starts with βSELECTβ, returns the results as a list. Otherwise (INSERT, UPDATE, DELETE), commits the transaction and returns the number of affected rows.
- Parameters:
query β The SQL query string.
params β Dictionary of parameters for the query (optional).
- Returns:
List of dictionaries for SELECT queries.
Integer representing affected rows for write queries.
None if connection or SQL error occurs.