[docs]classTransactional:""" This class provides methods to handle transactional queries. It supports fetching results as JSON or pandas DataFrame, and provides query execution statistics. """def__init__(self,api_key:str):""" Initialize the Transactional object with an API key. :param api_key: API key for authenticating requests. :type api_key: str """self.api_key=api_key
[docs]def__call__(self,query:str,parameters:dict[str,str]={},options:dict[str,str]={})->"Transactional":""" Execute a SQL query using the provided parameters and options. :param query: SQL query template with placeholders for parameters. :type query: str :param parameters: Parameters to format the SQL query. :type parameters: dict[str, str] :param options: Additional options for query execution. :type options: dict[str, str] :return: Returns self for chaining or method chaining. :rtype: Transactional :raises UnauthorizedException: Raises an exception if the API key is invalid. :raises ForbiddenException: Raises an exception if the API key does not have access to the resource. :raises NotFoundException: Raises an exception if the resource is not found. :raises DataSolutionsSDKException: Raises an exception if the query execution results in an error. :raises UnhandledException: Raises an exception if an unhandled exception occurs. """self._status_code=0self.results={}self._stats={}self.json_response={}self._status="error"self.dataframe_data=Nonequery_execution_url=(f"{BASE_URL['base_url']}/{TRANSACTIONAL_ENDPOINTS['query_execution']}")body={"sql":query,"parameters":parameters,"options":options,}try:self.json_response=issue_request(api_key=self.api_key,url=query_execution_url,body=body,method="POST",)self._status=self.json_response["status"]ifself._status=="error":self.error_message=self.json_response["message"]self.error_details=self.json_response.get("details")self.exception=DataSolutionsSDKException(self.error_message)else:self._stats=self.json_response["stats"]self.results=self.json_response["results"]self._status_code=200except(UnauthorizedException,ForbiddenException,NotFoundException)ase:raiseeexceptDataSolutionsSDKExceptionase:self.exception=e.get_exception()self._status_code=e.status_codeexceptExceptionase:self.exception=UnhandledException(details=e,)returnself
[docs]defjson(self)->dict:""" Return the JSON data of the results. :raises Exception: Raises an exception if the query resulted in an error. :return: JSON results of the SQL query. :rtype: dict """ifself._status!="error":returnself.resultselse:raiseself.exception
[docs]defdf(self)->pd.DataFrame:""" Convert query results into a pandas DataFrame. :raises Exception: Raises an exception if the query resulted in an error. :return: DataFrame containing the results of the SQL query. :rtype: pd.DataFrame """ifself._status!="error":ifself.dataframe_dataisNone:self.dataframe_data=pd.DataFrame(self.results)returnself.dataframe_dataelse:raiseself.exception
[docs]defstats(self)->dict:""" Get the statistics of the executed query. :return: Statistics of the query execution. :rtype: dict """ifself._status!="error":returnself._statselse:raiseself.exception
[docs]defwas_successful(self)->bool:""" Determine if the query executed successfully. :return: True if the query was successful, False otherwise. :rtype: bool """ifself._status!="error":returnTruereturnFalse
[docs]defstatus_code(self)->int:""" Return the status code of the query. :return: The status code of the query. :rtype: int """returnself._status_code