assets_guardian.core.clients.mysql_client

Classes

MySQLClient(host,Β port,Β user,Β password,Β database)

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: object

MySQL 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.