Client Reference Architecture
This is an outline of the Python reference archetecture for Mapepire
. The core components of the reference archetecture are the SQLJob
and Query
classes. The SQLJob
class manages the WebSocket connection to the server and provides methods to create and run queries. The Query
class manages the state of the query, sends it to the server, and fetches results.
Core Functions
SQL Job
High-Level Overview
The SQLJob
“class-like” object is designed to manage the client connection to the mapepire-server
. It handles the creation of unique identifiers for queries, manages WebSocket connections for sending and receiving data, and provides methods to create and run SQL queries. The class ensures that queries are executed in a controlled manner, maintaining their state and handling errors appropriately.
Class Definitions and Functions
_get_unique_id(self, prefix: str = "id") -> str
-
Parameters:
- prefix (
str
): A string containing the prefix for the unique identifier.
- prefix (
-
Returns: (
str
) A unique identifier for a query.This function should update a counter for the number of queries executed and return a unique identifier for the query.
_get_channel(self, db2_server: DaemonServer) -> WebSocket
-
Parameters:
- db2_server (
Dict[str, Any]
): A dictionary-like object containing the connection information for the DB2 server. Here is a sample definition of theDaemonServer
class:
- db2_server (
-
Returns: (
WebSocket
) A WebSocket connection to the DB2 server.This function is responsible for setting up a secure WebSocket connection to the
mapepire-server
. It constructs the connection URI, prepares the necessary headers for authentication, configures SSL options, and finally establishes the connection. The function returns the WebSocket object, which can then be used for communication with the server.
send(self, content: Any)
-
Parameters:
- content (
Any
): Any object that can be serialized into a JSON string.
- content (
-
Returns: None
This function is responsible for sending data to the
mapepire-server
over a WebSocket connection.
connect(self, db2_server: DaemonServer) -> Dict[Any, Any]
-
Parameters:
- db2_server (
DaemonServer
): A dictionary-like object containing the connection information for the DB2 server. Here is a sample definition of theDaemonServer
class:
- db2_server (
-
Returns: (
Dict[str, Any]
) A dictionary-like object containing the response from the server.This function is responsible for establishing a connection to the
mapepire-server
. It calls the_get_channel(db2_server)
function to create a WebSocket connection, sends the connection request to the server, and waits for a response. The function returns the response from the server, which contains information about the connection status.
query(self, sql: str, opts: Optional[Union[Dict[str, Any], QueryOptions]] = None)
-
Parameters:
- sql (
str
): A string containing the SQL query to be executed. - opts (
Optional[Union[Dict[str, Any], QueryOptions]]
): A dictionary-like object containing additional options for the query. Here is a sample definition of theQueryOptions
class:
- sql (
-
Returns: (
Query
) AQuery
object representing the query to be executed.This function is responsible for creating a
Query
object that represents the SQL query to be executed. It constructs the query object with the provided SQL statement and any additional options. The function returns theQuery
object, which can then be used to run the query.
query_and_run(self, sql: str, opts: Optional[Union[Dict[str, Any], QueryOptions]] = None)
-
Parameters:
- sql (
str
): A string containing the SQL query to be executed. - opts (
Optional[Union[Dict[str, Any], QueryOptions]]
): A dictionary-like object containing additional options for the query.
- sql (
-
Returns: (
Query
) AQuery
object representing the query to be executed.This function is a convenience method that combines the
query
andrun
functions. It creates aQuery
object with the provided SQL statement and options, then immediately runs the query. The function returns theQuery
object, which can be used to fetch more data or close the query.
close(self)
-
Parameters: None
-
Returns: None
This function is responsible for closing the WebSocket connection to the DB2 server. It sends a close request to the server and waits for a response. Once the connection is closed, the function cleans up any resources associated with the connection.
Query
High-Level Overview
The Query
class-like object is designed to manage and execute SQL queries, handle their states, and fetch additional data. The Query
object inherits an SQLJob
instance and uses it’s WebSocket connection to send and receive data. See the Query Class definition below.
Class Definitions and Functions
run(self, rows_to_fetch: Optional[int] = None) -> Dict[str, Any]
-
Parameters:
- rows_to_fetch (
Optional[int]
): An integer specifying the number of rows to fetch from the query result.
- rows_to_fetch (
-
Returns: (
Dict[str, Any]
) A dictionary-like object containing the response from the server.This function is responsible for executing the query and fetching the initial set of results. It sends the query to the server, waits for a response, and returns the result. If
rows_to_fetch
is specified, the function fetches the specified number of rows from the result set.
fetch_more(self, rows_to_fetch: Optional[int] = None) -> Dict[str, Any]
-
Parameters:
- rows_to_fetch (
Optional[int]
): An integer specifying the number of rows to fetch from the query result.
- rows_to_fetch (
-
Returns: (
Dict[str, Any]
) A dictionary-like object containing the response from the server.This function is responsible for fetching additional rows from the query result. It sends a request to the server to fetch more rows, waits for a response, and returns the result.
Query Objects
A query object is a dictionary-like object that represents a SQL query to be executed. It contains the SQL statement, any additional options, and the unique identifier for the query. The query object is used to manage the state of the query, send it to the server, and fetch results.
Here is a sample definition of a query object:
Here is a sample definition of a query object for a cl command:
here is a sample definition of a query object for fetch more: