1 /* 2 * Copyright (C) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef INTERFACES_INNERKITS_NATIVE_INCLUDE_FETCH_RESULT_H_ 17 #define INTERFACES_INNERKITS_NATIVE_INCLUDE_FETCH_RESULT_H_ 18 19 #include <variant> 20 #include "abs_shared_result_set.h" 21 #include "album_asset.h" 22 #include "datashare_result_set.h" 23 #include "file_asset.h" 24 #include "medialibrary_type_const.h" 25 #include "medialibrary_db_const.h" 26 #include "rdb_errno.h" 27 #include "photo_album.h" 28 #include "smart_album_asset.h" 29 #include "userfile_manager_types.h" 30 31 namespace OHOS { 32 namespace Media { 33 enum ResultSetDataType { 34 TYPE_NULL = 0, 35 TYPE_STRING, 36 TYPE_INT32, 37 TYPE_INT64, 38 TYPE_DOUBLE 39 }; 40 41 /** 42 * @brief Class for returning the data cursor to application. 43 * 44 * @since 1.0 45 * @version 1.0 46 */ 47 template <class T> 48 class FetchResult { 49 public: 50 explicit FetchResult(const std::shared_ptr<DataShare::DataShareResultSet> &resultset); 51 FetchResult(); 52 virtual ~FetchResult(); 53 54 void Close(); 55 int32_t GetCount(); 56 bool IsAtLastRow(); 57 void SetInfo(unique_ptr<FetchResult<T>> &fetch); 58 void SetNetworkId(const string &networkId); 59 void SetResultNapiType(const ResultNapiType napiType); 60 void SetFetchResType(const FetchResType resType); 61 std::string GetNetworkId(); 62 ResultNapiType GetResultNapiType(); 63 std::shared_ptr<DataShare::DataShareResultSet> &GetDataShareResultSet(); 64 FetchResType GetFetchResType(); 65 std::unique_ptr<T> GetObjectAtPosition(int32_t index); 66 std::unique_ptr<T> GetFirstObject(); 67 std::unique_ptr<T> GetObjectFromRdb(std::shared_ptr<NativeRdb::ResultSet> &resultSet, int idx); 68 std::unique_ptr<T> GetNextObject(); 69 std::unique_ptr<T> GetLastObject(); 70 std::unique_ptr<T> GetObject(); 71 72 private: 73 std::unique_ptr<T> GetObject(std::shared_ptr<NativeRdb::ResultSet> &resultSet); 74 std::variant<int32_t, int64_t, std::string> GetRowValFromColumn(std::string columnName, 75 ResultSetDataType dataType, std::shared_ptr<NativeRdb::ResultSet> &resultSet); 76 std::variant<int32_t, int64_t, std::string> GetValByIndex(int32_t index, ResultSetDataType dataType, 77 std::shared_ptr<NativeRdb::ResultSet> &resultSet); 78 79 void SetFileAsset(FileAsset *fileAsset, std::shared_ptr<NativeRdb::ResultSet> &resultSet); 80 void SetAlbumAsset(AlbumAsset* albumData, std::shared_ptr<NativeRdb::ResultSet> &resultSet); 81 void SetPhotoAlbum(PhotoAlbum* photoAlbumData, std::shared_ptr<NativeRdb::ResultSet> &resultSet); 82 void SetSmartAlbumAsset(SmartAlbumAsset* smartAlbumData, std::shared_ptr<NativeRdb::ResultSet> &resultSet); 83 void SetAssetUri(FileAsset *fileAsset); 84 85 void GetObjectFromResultSet(FileAsset *asset, shared_ptr<NativeRdb::ResultSet> &resultSet); 86 void GetObjectFromResultSet(AlbumAsset *asset, shared_ptr<NativeRdb::ResultSet> &resultSet); 87 void GetObjectFromResultSet(PhotoAlbum *asset, shared_ptr<NativeRdb::ResultSet> &resultSet); 88 void GetObjectFromResultSet(SmartAlbumAsset *asset, shared_ptr<NativeRdb::ResultSet> &resultSet); 89 90 private: 91 std::string networkId_; 92 ResultNapiType resultNapiType_; 93 std::shared_ptr<DataShare::DataShareResultSet> resultset_ = nullptr; 94 FetchResType fetchResType_; 95 }; 96 } // namespace Media 97 } // namespace OHOS 98 99 #endif // INTERFACES_INNERKITS_NATIVE_INCLUDE_FETCH_RESULT_H_