def _get_element_result(self, exec_query_resp): """ Get the response from rasserver for query which doesn't return array which can be converted to Numpy ndarray e.g: select 1 + 2, select {1, 2, 3} :return: array of results if server returns multiple elements or just 1 result if only 1 element """ rpcstatus = 0 band_types = get_type_structure_from_string( exec_query_resp.type_structure) result_arr = ResultArray(band_types['type'], is_object=False) while rpcstatus == 0: elemresp = rassrvr_get_next_element( self.transaction.database.stub, self.transaction.database.connection.session.clientId) rpcstatus = elemresp.status if rpcstatus == 2: raise Exception( "getNextElement - no transfer or empty element") # e.g: select 2 + 3, select complex(3, 5) result = convert_binary_data_stream(band_types, elemresp.data) result_arr.add_data(result) return result_arr
def _get_collection_result(self, exec_query_resp): """ Parse the string binary containing data (encoded, unencoded) from rasql to objects which later can be translated to numpy array. Or returns the list of rasdaman collections in RASBASE. :return: ResultArray object """ spatial_domain = get_spatial_domain_from_type_structure( exec_query_resp.type_structure) band_types = get_type_structure_from_string( exec_query_resp.type_structure) # concatenate all the binary strings to one string to be decoded as Numpy ndarray buffer data_list = self.__get_array_result_mdd() data = b''.join(data_list) data_type = band_types["type"] # NOTE: unencoded result array is nD+ numpy array. If number of bands > 1 then the number of dimensions + 1 number_of_bands = 1 if data_type == "struct": data_type = band_types["sub_type"]["types"][0] number_of_bands = len(band_types["sub_type"]["types"]) res_array = ResultArray(data_type, spatial_domain, number_of_bands) res_array.add_data(data) return res_array
def _get_list_collection(self): """ Return the list of collection names from RASBASE for query (select c from RAS_COLLECTIONNAMES as c) :return: list of string collection names """ result_array = ResultArray("string") for r in self.__get_array_result_mdd(): result_array.add_data(encoded_bytes_to_str(r)) return result_array
def execute_read(self): """ Executes the query with read permission and returns back a result :return: the resulting array returned by the query :rtype: result object according to query """ exec_query_resp = rassrvr_execute_query( self.transaction.database.stub, self.transaction.database.connection.session.clientId, self.query_str) if exec_query_resp.status == 4 or exec_query_resp.status == 5: error_message = ExceptionFactories.create_error_message( exec_query_resp.err_no, exec_query_resp.line_no, exec_query_resp.col_no, exec_query_resp.token) raise Exception( "Error executing query '{}', error message '{}'".format( self.query_str, error_message)) elif exec_query_resp.status == 0: tmp_query = self.query_str.upper() # e.g: query: select c from RAS_COLLECTIONNAMES as c if "RAS_COLLECTIONNAMES" in tmp_query or \ "RAS_STRUCT_TYPES" in tmp_query or \ "RAS_MARRAY_TYPES" in tmp_query or \ "RAS_SET_TYPES" in tmp_query or \ "DBINFO" in tmp_query: return self._get_list_collection() # e.g: query: select c + 1 from test_mr as c, select encode(c, "png") from test_mr as c return self._get_collection_result(exec_query_resp) elif exec_query_resp.status == 1: # e.g: query: select complex(35, 56), select sdom(c) from test_mr as c, select {1, 2, 3} return self._get_element_result(exec_query_resp) elif exec_query_resp.status == 2: # Query can return empty collection (e.g: select c from c where all_cells( c > 20 )) which returns empty return ResultArray("string") # empty array else: raise Exception("Unknown status code: " + str(exec_query_resp.status) + " returned by ExecuteQuery")