Skip to content

RDS Gateway

Database operations with connection pooling and batch processing.


RdsServiceGateway

Python
RdsServiceGateway(multithreaded: bool = False, min_pool_size: int = 1, max_pool_size: int = 10)

Provides methods to interact with an RDS database, including querying data and updating the database. Ensures that a single instance is used throughout the application via the Singleton pattern.

Initializes the RdsServiceGateway by establishing a connection or connection pool depending on the multithreading mode.

convert_payload

Python
convert_payload(payload: Tuple[Any, ...]) -> pd.DataFrame | tuple[Any, ...]

Converts elements within a tuple payload to types compatible with psycopg2.

RETURNS DESCRIPTION
Tuple[Any, ...]

A tuple with converted values.

format_sql_query

Python
format_sql_query(query: str, payload: tuple) -> None

Formats and prints the SQL query with the given payload.

get_connection

Python
get_connection() -> Union[psycopg2.extensions.connection, RDSClient]

Retrieves a connection from the connection pool or direct connection based on initialization mode.

RETURNS DESCRIPTION
psycopg2.extensions.connection

A database connection object.

get_cursor

Python
get_cursor() -> psycopg2.extensions.cursor

Returns a new database cursor.

RETURNS DESCRIPTION
psycopg2.extensions.cursor

A new cursor object.

get_data

Python
get_data(query: str, payload: Optional[tuple] = None, fetchall: bool = True, return_dict: bool = True, show_query: bool = False, raise_on_error: bool = False) -> Optional[Any]

Fetch data from the database based on the input query and parameters.

release_connection

Python
release_connection(conn: connection)

Releases a connection back to the pool if multithreaded, otherwise does nothing.

update_database

Python
update_database(query: str, payload: Union[tuple, list[tuple], DataFrame], returning: bool = False, column_order: Optional[List[str]] = None, raise_on_error: bool = True, test_mode: bool = False) -> Optional[List[tuple]]

Updates the database by executing the specified SQL query with the given payload.

Handles different payload formats including a single tuple, a list of tuples, or a DataFrame. Supports optional fetching of return values if returning is True. Commits the transaction by default unless in test mode, where changes are rolled back.

Args: query (str): SQL query to execute. payload (Union[tuple, list[tuple], DataFrame]): Data to be used in the query. Can be a single tuple, a list of tuples, or a DataFrame. returning (bool): If True, fetches and returns results after executing the query. column_order (Optional[List[str]]): Order of columns when payload is a DataFrame. Ensures correct insertion order. raise_on_error (bool): If True, raises exceptions on errors; if False, logs errors without raising. test_mode (bool): If True, rolls back the transaction instead of committing changes to the database.

Returns: Optional[List[tuple]]: List of tuples containing the query results if returning is True, otherwise None.

Raises: ValueError: If column order is missing for DataFrame payloads or if the payload has incompatible data for batch processing. psycopg2.DataError: If no data was committed in batch processing.