def get_table_names(self, table_type: str) -> TableNamesDto:
        try:
            url_withaction = self.__url + "metadata_query/get_table_names"

            if table_type is not None:
                url_withaction += "?table_type=" + table_type

            result = self.__aceQLHttpApi.call_with_get_url(url_withaction)

            result_analyzer = ResultAnalyzer(
                result, self.__aceQLHttpApi.get_http_status_code())
            if not result_analyzer.is_status_ok():
                raise Error(result_analyzer.get_error_message(),
                            result_analyzer.get_error_type(), None, None,
                            self.__aceQLHttpApi.get_http_status_code())

            if AceQLMetadataApi.__debug:
                print(result)

            table_names_dto_schema = marshmallow_dataclass.class_schema(
                TableNamesDto)
            table_names_dto: TableNamesDto = table_names_dto_schema().loads(
                result)

            if AceQLMetadataApi.__debug:
                print(table_names_dto)

            return table_names_dto

        except Exception as e:
            if isinstance(e, Error):
                raise
            else:
                raise Error(str(e), 0, e, None,
                            self.__aceQLHttpApi.get_http_status_code())
    def get_database_info(self) -> DatabaseInfoDto:

        try:
            url_withaction = self.__url + "get_database_info"
            result = self.__aceQLHttpApi.call_with_get_url(url_withaction)

            result_analyzer = ResultAnalyzer(
                result, self.__aceQLHttpApi.get_http_status_code())
            if not result_analyzer.is_status_ok():
                raise Error(result_analyzer.get_error_message(),
                            result_analyzer.get_error_type(), None, None,
                            self.__aceQLHttpApi.get_http_status_code())

            if AceQLMetadataApi.__debug:
                print(result)

            holder_database_info_dto_schema = marshmallow_dataclass.class_schema(
                DatabaseInfoDto)
            database_info_dto: DatabaseInfoDto = holder_database_info_dto_schema(
            ).loads(result)

            if AceQLMetadataApi.__debug:
                print(database_info_dto)

            print(database_info_dto)
            return database_info_dto

        except Exception as e:
            if isinstance(e, Error):
                raise
            else:
                raise Error(str(e), 0, e, None,
                            self.__aceQLHttpApi.get_http_status_code())
    def get_db_metadata(self) -> JdbcDatabaseMetaDataDto:
        try:
            url_withaction = self.__url + "metadata_query/get_db_metadata"
            result = self.__aceQLHttpApi.call_with_get_url(url_withaction)

            result_analyzer = ResultAnalyzer(
                result, self.__aceQLHttpApi.get_http_status_code())
            if not result_analyzer.is_status_ok():
                raise Error(result_analyzer.get_error_message(),
                            result_analyzer.get_error_type(), None, None,
                            self.__aceQLHttpApi.get_http_status_code())

            if AceQLMetadataApi.__debug:
                print(result)

            holder_jdbc_database_meta_data_schema = marshmallow_dataclass.class_schema(
                JdbcDatabaseMetaDataDto)
            jdbc_database_meta_data_holder: JdbcDatabaseMetaDataDto = holder_jdbc_database_meta_data_schema(
            ).loads(result)

            if AceQLMetadataApi.__debug:
                print(jdbc_database_meta_data_holder)

            return jdbc_database_meta_data_holder

        except Exception as e:
            if isinstance(e, Error):
                raise
            else:
                raise Error(str(e), 0, e, None,
                            self.__aceQLHttpApi.get_http_status_code())
    def get_blob_length(self, blob_id: str) -> int:
        """ Gets the blob length. """
        try:

            if blob_id is None:
                raise TypeError("blob_id is null!")

            action = "get_blob_length"

            dict_params: dict = {"blob_id": blob_id}

            url_withaction = self._url + action

            AceQLDebug.debug("urlWithaction: " + url_withaction)
            AceQLDebug.debug("dictParams   : " + str(dict_params))

            # r = requests.post('http://httpbin.org/post', data = {'key':'value'})

            if self.__timeout is None:
                response = requests.post(url_withaction,
                                         headers=self.__headers,
                                         data=dict_params,
                                         proxies=self.__proxies,
                                         auth=self.__auth)
            else:
                response = requests.post(url_withaction,
                                         headers=self.__headers,
                                         data=dict_params,
                                         proxies=self.__proxies,
                                         auth=self.__auth,
                                         timeout=self.__timeout)

            self.__http_status_code = response.status_code
            result = response.text

            AceQLDebug.debug("result: " + result)

            result_analyzer = ResultAnalyzer(result, self.__http_status_code)
            if not result_analyzer.is_status_ok():
                raise Error(result_analyzer.get_error_message(),
                            result_analyzer.get_error_type(), None, None,
                            self.__http_status_code)

            length_str = result_analyzer.get_value("length")
            AceQLDebug.debug("result: " + length_str + ":")
            return int(length_str)

        except Exception as e:
            if isinstance(e, Error):
                raise
            else:
                raise Error(str(e), 0, e, None, self.__http_status_code)
    def get_blob_stream(self, blob_id: str):
        """ returns a BLOB stream as a Requests response """
        try:

            if blob_id is None:
                raise TypeError("blob_id is null!")

            the_url = self._url + "/blob_download?blob_id=" + blob_id

            if self.__timeout is None:
                response = requests.get(the_url,
                                        headers=self.__headers,
                                        proxies=self.__proxies,
                                        auth=self.__auth)
            else:
                response = requests.get(the_url,
                                        headers=self.__headers,
                                        proxies=self.__proxies,
                                        auth=self.__auth,
                                        timeout=self.__timeout)

            self.__http_status_code = response.status_code

            return response

        except Exception as e:
            if isinstance(e, Error):
                raise
            else:
                raise Error(str(e), 0, e, None, self.__http_status_code)
    def get_blob_stream(self, blob_id: str) -> Request:
        """ returns a BLOB stream as a Requests response """
        try:

            if blob_id is None:
                raise TypeError("blob_id is null!")

            the_url = self.__url + "/blob_download?blob_id=" + blob_id

            if self.__aceQLHttpApi.get_timeout() is None:
                response: Request = requests.get(the_url, headers=self.__aceQLHttpApi.get_headers(),
                                                 proxies=self.__aceQLHttpApi.get_proxies(),
                                                 auth=self.__aceQLHttpApi.get_auth())
            else:
                response: Request = requests.get(the_url,  headers=self.__aceQLHttpApi.get_headers(),
                                                 proxies=self.__aceQLHttpApi.get_proxies(),
                                                 auth=self.__aceQLHttpApi.get_auth(),
                                                 timeout=self.__aceQLHttpApi.get_timeout())

            self.__aceQLHttpApi.set_http_status_code(response.status_code)
            return response

        except Exception as e:
            if isinstance(e, Error):
                raise
            else:
                raise Error(str(e), 0, e, None, self.__aceQLHttpApi.get_http_status_code())
    def call_api_no_result(self, command_name: str, command_option: str):
        if command_name is None:
            raise TypeError("command_name is null!")

        try:
            result = self.call_with_get_action(command_name, command_option)

            result_analyzer = ResultAnalyzer(result, self.__http_status_code)
            if not result_analyzer.is_status_ok():
                raise Error(result_analyzer.get_error_message(),
                            result_analyzer.get_error_type(), None, None,
                            self.__http_status_code)

        except Exception as e:
            if isinstance(e, Error):
                raise
            else:
                raise Error(str(e), 0, e, None, self.__http_status_code)
    def db_schema_download(self, file_format: str, table_name: str):
        """ returns a schema stream as a Requests response """
        try:
            if file_format is None:
                raise TypeError("format is null!")

            the_url = self.__url + "/metadata_query/db_schema_download"

            dict_params = {"format": file_format}

            if table_name is not None:
                table_name = table_name.lower()
                dict_params["table_name"] = table_name

            if self.__aceQLHttpApi.get_timeout() is None:
                response: Request = requests.post(
                    the_url,
                    headers=self.__aceQLHttpApi.get_headers(),
                    data=dict_params,
                    proxies=self.__aceQLHttpApi.get_proxies(),
                    auth=self.__aceQLHttpApi.get_auth())
            else:
                response: Request = requests.post(
                    the_url,
                    headers=self.__aceQLHttpApi.get_headers(),
                    data=dict_params,
                    proxies=self.__aceQLHttpApi.get_proxies(),
                    auth=self.__aceQLHttpApi.get_auth(),
                    timeout=self.__aceQLHttpApi.get_timeout())

            self.__aceQLHttpApi.set_http_status_code(response.status_code)
            return response

        except Exception as e:
            if isinstance(e, Error):
                raise
            else:
                raise Error(str(e), 0, e, None,
                            self.__aceQLHttpApi.get_http_status_code())
Exemple #9
0
 def treat_result(self, filename: str):
     file_out = None
     if self.__aceQLHttpApi.is_gzip_result():
         file_out = filename[0:len(filename) - 4] + ".ungzipped.txt"
         FileUtil.decompress(filename, file_out)
         if AceQLDebugParms.DELETE_FILES:
             CursorUtil.remove_file_safe(filename)
     else:
         file_out = filename
     AceQLDebug.debug("Before StreamResultAnalyzer")
     result_analyzer = StreamResultAnalyzer(
         file_out, self.__aceQLHttpApi.get_http_status_code())
     if not result_analyzer.is_status_ok():
         if AceQLDebugParms.DELETE_FILES:
             CursorUtil.remove_file_safe(filename)
         raise Error(result_analyzer.get_error_message(),
                     result_analyzer.get_error_type(), None, None,
                     self.__aceQLHttpApi.get_http_status_code())
     row_counter = RowCounter(file_out)
     row_count = row_counter.count()
     result_set_info = ResultSetInfo(file_out, row_count)
     AceQLDebug.debug("Before resultSetInfo")
     return result_set_info
    def __init__(self,
                 *,
                 url: str,
                 username: str,
                 password: str,
                 database: str,
                 connection_options: ConnectionOptions = None):

        if url is None:
            raise TypeError("url is null!")
        if database is None:
            raise TypeError("database is null!")
        if username is None:
            raise TypeError("username is null!")

        session_id: str = None
        proxies: dict = None
        auth: ProxyAuth = None
        gzip_result: bool = True
        timeout = None
        request_headers: dict = {}

        if connection_options is not None:
            if connection_options.session_id is not None:
                session_id = connection_options.session_id
                password = None
            proxies = connection_options.proxies
            auth = connection_options.auth
            gzip_result = connection_options.gzip_result
            timeout = connection_options.timeout
            request_headers = connection_options.request_headers

        self.__url = url
        self.__database = database
        self.__username = username
        self.__password = password
        self.__proxies = proxies
        self.__auth = auth
        self.__gzip_result = gzip_result
        self.__timeout = timeout
        self.__headers = request_headers
        self.__http_status_code = requests.codes.ok

        # Other self for other methods
        self.__pretty_printing = True
        #self.__temp_length = 0  ==> Done in AceQLBlobUploadApi
        #self.__total_length = 0 ==> ==> Done in AceQLBlobUploadApi
        self.__progress_indicator = None

        # url = c + "/database/" + database + "/username/" \
        #       + username + "/connect" + "?password="******"/session/" + session_id + "/get_connection"

                result = self.call_with_get_url(the_url)

                result_analyzer = ResultAnalyzer(result,
                                                 self.__http_status_code)
                if not result_analyzer.is_status_ok():
                    raise Error(result_analyzer.get_error_message(),
                                result_analyzer.get_error_type(), None, None,
                                self.__http_status_code)

                connection_id = result_analyzer.get_value("connection_id")
                self.__url = url + "/session/" + session_id + "/connection/" + connection_id + "/"

            else:
                url = url + "/database/" + database + "/username/" \
                      + username + "/login"

                dict_params = {
                    "password": password,
                    "client_version": str(VersionValues.VERSION)
                }

                result = self.call_with_post_url(url, dict_params)

                result_analyzer = ResultAnalyzer(result,
                                                 self.__http_status_code)
                if not result_analyzer.is_status_ok():
                    raise Error(result_analyzer.get_error_message(),
                                result_analyzer.get_error_type(), None, None,
                                self.__http_status_code)

                session_id = result_analyzer.get_value("session_id")
                connection_id = result_analyzer.get_value("connection_id")
                self.__url = url + "/session/" + session_id + "/connection/" + connection_id + "/"

                user_login_store.set_session_id(session_id)
        except Exception as e:
            if isinstance(e, Error):
                raise
            else:
                raise Error(str(e), 0, e, None, self.__http_status_code)
    def execute_update(self, sql: str, is_prepared_statement: bool,
                       statement_parameters: dict):
        """Calls /execute_update API"""

        try:
            action = "execute_update"
            AceQLExecutionUtil.check_values(is_prepared_statement, sql)
            dict_params: dict = {"sql": sql}

            AceQLExecutionUtil.set_is_prepared_statement(
                dict_params, is_prepared_statement)
            url_withaction = self.__url + action

            AceQLDebug.debug("url_withaction: " + url_withaction)
            AceQLDebug.debug("dict_params 1: " + str(dict_params))

            if statement_parameters is not None:
                if not isinstance(statement_parameters, dict):
                    raise TypeError(
                        "statement_parameters is not a dictionary!")

                dict_params.update(statement_parameters)

            AceQLDebug.debug("dictParams 2: " + str(dict_params))

            # r = requests.post('http://httpbin.org/post', data = {'key':'value'})
            # print("Before update request")

            if self.__aceQLHttpApi.get_timeout() is None:
                AceQLDebug.debug("UPDATE HERE 1")
                response: Request = requests.post(
                    url_withaction,
                    headers=self.__aceQLHttpApi.get_headers(),
                    data=dict_params,
                    proxies=self.__aceQLHttpApi.get_proxies(),
                    auth=self.__aceQLHttpApi.get_auth())
            else:
                AceQLDebug.debug("UPDATE HERE 2")
                response: Request = requests.post(
                    url_withaction,
                    headers=self.__aceQLHttpApi.get_headers(),
                    data=dict_params,
                    proxies=self.__aceQLHttpApi.get_proxies(),
                    auth=self.__aceQLHttpApi.get_auth(),
                    timeout=self.__aceQLHttpApi.get_timeout())

            self.__aceQLHttpApi.set_http_status_code(response.status_code)
            result = response.text

            # print("self.__http_status_code: " + str(self.__http_status_code ))
            # print("result                 : " + str(result))
            AceQLDebug.debug("result: " + result)

            result_analyzer = ResultAnalyzer(
                result, self.__aceQLHttpApi.get_http_status_code())
            if not result_analyzer.is_status_ok():
                raise Error(result_analyzer.get_error_message(),
                            result_analyzer.get_error_type(), None, None,
                            self.__aceQLHttpApi.get_http_status_code())

            row_count = result_analyzer.get_int_value("row_count")
            return row_count

        except Exception as e:
            if isinstance(e, Error):
                raise
            else:
                raise Error(str(e), 0, e, None,
                            self.__aceQLHttpApi.get_http_status_code())
    def execute_batch(self, sql: str, batch_file_parameters: str):

        try:
            action = "prepared_statement_execute_batch"
            AceQLExecutionUtil.check_values(True, sql)
            url_withaction = self.__url + action

            AceQLDebug.debug("url_withaction: " + url_withaction)

            blob_id: str = os.path.basename(batch_file_parameters)
            length: int = os.path.getsize(batch_file_parameters)

            with open(batch_file_parameters, "rb") as fd:
                self.__aceQLHttpApi.blob_upload(blob_id, fd, length)

            dict_params: dict = {"sql": sql, "blob_id": blob_id}
            AceQLDebug.debug("dict_params: " + str(dict_params))

            # r = requests.post('http://httpbin.org/post', data = {'key':'value'})
            # print("Before update request")

            # if self.__timeout is None:
            #     response: Request = requests.post(url_withaction, headers=self.__headers, data=dict_params,
            #                                       proxies=self.__proxies, auth=self.__auth)
            # else:
            #     response: Request = requests.post(url_withaction, headers=self.__headers, data=dict_params,
            #                                       proxies=self.__proxies, auth=self.__auth,
            #                                       timeout=self.__timeout)

            if self.__aceQLHttpApi.get_timeout() is None:
                response: Request = requests.post(url_withaction, headers=self.__aceQLHttpApi.get_headers(), data=dict_params,
                                                  proxies=self.__aceQLHttpApi.get_proxies(),
                                                  auth=self.__aceQLHttpApi.get_auth())
            else:
                response: Request = requests.post(url_withaction, headers=self.__aceQLHttpApi.get_headers(), data=dict_params,
                                                  proxies=self.__aceQLHttpApi.get_proxies(),
                                                  auth=self.__aceQLHttpApi.get_auth(),
                                                  timeout=self.__aceQLHttpApi.get_timeout())

            self.__aceQLHttpApi.set_http_status_code(response.status_code)
            result = response.text

            # print("self.__http_status_code: " + str(self.__http_status_code ))
            # print("result                 : " + str(result))
            AceQLDebug.debug("result: " + result)

            result_analyzer = ResultAnalyzer(result, self.__aceQLHttpApi.get_http_status_code())
            if not result_analyzer.is_status_ok():
                raise Error(result_analyzer.get_error_message(),
                            result_analyzer.get_error_type(), None, None, self.__aceQLHttpApi.get_http_status_code())

            update_counts_array_dto_schema = marshmallow_dataclass.class_schema(UpdateCountsArrayDto)
            update_counts_array_dto_back: UpdateCountsArrayDto = update_counts_array_dto_schema().loads(
                result)

            update_counts_array: List[int] = update_counts_array_dto_back.updateCountsArray
            return update_counts_array
        except Exception as e:
            if isinstance(e, Error):
                raise
            else:
                raise Error(str(e), 0, e, None, self.__aceQLHttpApi.get_http_status_code())
Exemple #13
0
    def execute_server_query(self, server_query_executor_class_name: str,
                             parameters: list):
        """Calls /execute_server_query API"""
        try:

            action = "execute_server_query"
            dict_params = {
                "server_query_executor_class_name":
                server_query_executor_class_name
            }
            AceQLDebug.debug("dictParams 1: " + str(dict_params))

            server_query_executor_dto_schema = marshmallow_dataclass.class_schema(
                ServerQueryExecutorDto)
            server_query_executor_dto: ServerQueryExecutorDto = ServerQueryExecutorDtoBuilder.build(
                server_query_executor_class_name, parameters)
            json_string: str = server_query_executor_dto_schema().dumps(
                server_query_executor_dto)

            dict_params["server_query_executor_dto"] = json_string
            url_withaction = self.__url + action

            AceQLDebug.debug("url_withaction: " + url_withaction)
            AceQLDebug.debug("parameters    : " + str(parameters))

            self.update_dict_params(dict_params)
            AceQLDebug.debug("dictParams 2: " + str(dict_params))

            # r = requests.post('http://httpbin.org/post', data = {'key':'value'})

            if self.__aceQLHttpApi.get_timeout() is None:
                AceQLDebug.debug("QUERY HERE 1")
                response: Request = requests.post(
                    url_withaction,
                    headers=self.__aceQLHttpApi.get_headers(),
                    data=dict_params,
                    proxies=self.__aceQLHttpApi.get_proxies(),
                    auth=self.__aceQLHttpApi.get_auth())
            else:
                AceQLDebug.debug("QUERY HERE 2")
                response: Request = requests.post(
                    url_withaction,
                    headers=self.__aceQLHttpApi.get_headers(),
                    data=dict_params,
                    proxies=self.__aceQLHttpApi.get_proxies(),
                    auth=self.__aceQLHttpApi.get_auth(),
                    timeout=self.__aceQLHttpApi.get_timeout())
            AceQLDebug.debug("DONE!")
            self.__aceQLHttpApi.set_http_status_code(response.status_code)

            filename = FileUtil.build_result_set_file()
            AceQLDebug.debug("filename1: " + filename)

            # We dump the JSon stream into user.home/.kawansoft/tmp
            with open(filename, 'wb') as fd:
                for chunk in response.iter_content(chunk_size=2048):
                    fd.write(chunk)

            AceQLDebug.debug("after open filename")
            result_set_info = self.treat_result(filename)
            return result_set_info

        except Exception as e:
            if isinstance(e, Error):
                raise
            else:
                raise Error(str(e), 0, e, None,
                            self.__aceQLHttpApi.get_http_status_code())
Exemple #14
0
    def execute_query(self, sql: str, is_prepared_statement: bool,
                      statement_parameters: dict):
        """Calls /execute_query API"""
        try:

            action = "execute_query"
            AceQLExecutionUtil.check_values(is_prepared_statement, sql)
            dict_params = {"sql": sql}
            AceQLExecutionUtil.set_is_prepared_statement(
                dict_params, is_prepared_statement)

            url_withaction = self.__url + action

            AceQLDebug.debug("url_withaction: " + url_withaction)
            AceQLDebug.debug("dictParams 1: " + str(dict_params))

            if statement_parameters is not None:
                if not isinstance(statement_parameters, dict):
                    raise TypeError("statementParameters is not a dictionary!")

                dict_params.update(statement_parameters)

            self.update_dict_params(dict_params)
            AceQLDebug.debug("dictParams 2: " + str(dict_params))

            # r = requests.post('http://httpbin.org/post', data = {'key':'value'})

            if self.__aceQLHttpApi.get_timeout() is None:
                AceQLDebug.debug("QUERY HERE 1")
                response: Request = requests.post(
                    url_withaction,
                    headers=self.__aceQLHttpApi.get_headers(),
                    data=dict_params,
                    proxies=self.__aceQLHttpApi.get_proxies(),
                    auth=self.__aceQLHttpApi.get_auth())
            else:
                AceQLDebug.debug("QUERY HERE 2")
                response: Request = requests.post(
                    url_withaction,
                    headers=self.__aceQLHttpApi.get_headers(),
                    data=dict_params,
                    proxies=self.__aceQLHttpApi.get_proxies(),
                    auth=self.__aceQLHttpApi.get_auth(),
                    timeout=self.__aceQLHttpApi.get_timeout())
            AceQLDebug.debug("DONE!")
            self.__aceQLHttpApi.set_http_status_code(response.status_code)

            filename = FileUtil.build_result_set_file()
            AceQLDebug.debug("filename1: " + filename)

            # We dump the JSon stream into user.home/.kawansoft/tmp
            with open(filename, 'wb') as fd:
                for chunk in response.iter_content(chunk_size=2048):
                    fd.write(chunk)

            AceQLDebug.debug("after open filename")
            result_set_info = self.treat_result(filename)
            return result_set_info

        except Exception as e:
            if isinstance(e, Error):
                raise
            else:
                raise Error(str(e), 0, e, None,
                            self.__aceQLHttpApi.get_http_status_code())