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 #define EXPORT __attribute__ ((visibility ("default")))
34 /**
35  * @brief Class for returning the data cursor to application.
36  *
37  * @since 1.0
38  * @version 1.0
39  */
40 template <class T>
41 class FetchResult {
42 public:
43     EXPORT explicit FetchResult(const std::shared_ptr<DataShare::DataShareResultSet> &resultset);
44     EXPORT FetchResult();
45     EXPORT virtual ~FetchResult();
46 
47     EXPORT void Close();
48     EXPORT int32_t GetCount();
49     EXPORT bool IsAtLastRow();
50     EXPORT void SetInfo(unique_ptr<FetchResult<T>> &fetch);
51     EXPORT void SetNetworkId(const string &networkId);
52     EXPORT void SetResultNapiType(const ResultNapiType napiType);
53     EXPORT void SetFetchResType(const FetchResType resType);
54     EXPORT void SetHiddenOnly(const bool hiddenOnly);
55     EXPORT void SetLocationOnly(const bool locationOnly);
56 
57     EXPORT std::string GetNetworkId();
58     EXPORT ResultNapiType GetResultNapiType();
59     EXPORT std::shared_ptr<DataShare::DataShareResultSet> &GetDataShareResultSet();
60     EXPORT FetchResType GetFetchResType();
61     EXPORT bool GetHiddenOnly();
62     EXPORT bool GetLocationOnly();
63 
64     EXPORT std::unique_ptr<T> GetObjectAtPosition(int32_t index);
65     EXPORT std::unique_ptr<T> GetFirstObject();
66     EXPORT std::unique_ptr<T> GetObjectFromRdb(std::shared_ptr<NativeRdb::ResultSet> &resultSet, int idx);
67     EXPORT std::unique_ptr<T> GetNextObject();
68     EXPORT std::unique_ptr<T> GetLastObject();
69     EXPORT std::unique_ptr<T> GetObject();
70 
71 private:
72     EXPORT std::unique_ptr<T> GetObject(std::shared_ptr<NativeRdb::ResultSet> &resultSet);
73     EXPORT std::variant<int32_t, int64_t, std::string, double> GetRowValFromColumn(std::string columnName,
74         ResultSetDataType dataType, std::shared_ptr<NativeRdb::ResultSet> &resultSet);
75     std::variant<int32_t, int64_t, std::string, double> GetValByIndex(int32_t index, ResultSetDataType dataType,
76         std::shared_ptr<NativeRdb::ResultSet> &resultSet);
77 
78     void SetFileAsset(FileAsset *fileAsset, std::shared_ptr<NativeRdb::ResultSet> &resultSet);
79     void SetAlbumAsset(AlbumAsset* albumData, std::shared_ptr<NativeRdb::ResultSet> &resultSet);
80     void SetPhotoAlbum(PhotoAlbum* photoAlbumData, std::shared_ptr<NativeRdb::ResultSet> &resultSet);
81     void SetSmartAlbumAsset(SmartAlbumAsset* smartAlbumData, std::shared_ptr<NativeRdb::ResultSet> &resultSet);
82     void SetAssetUri(FileAsset *fileAsset);
83 
84     void GetObjectFromResultSet(FileAsset *asset, shared_ptr<NativeRdb::ResultSet> &resultSet);
85     void GetObjectFromResultSet(AlbumAsset *asset, shared_ptr<NativeRdb::ResultSet> &resultSet);
86     void GetObjectFromResultSet(PhotoAlbum *asset, shared_ptr<NativeRdb::ResultSet> &resultSet);
87     void GetObjectFromResultSet(SmartAlbumAsset *asset, shared_ptr<NativeRdb::ResultSet> &resultSet);
88 
89     std::string networkId_;
90     ResultNapiType resultNapiType_;
91     std::shared_ptr<DataShare::DataShareResultSet> resultset_ = nullptr;
92     FetchResType fetchResType_;
93     bool hiddenOnly_ = false;
94     bool locationOnly_ = false;
95 };
96 } // namespace Media
97 } // namespace OHOS
98 
99 #endif  // INTERFACES_INNERKITS_NATIVE_INCLUDE_FETCH_RESULT_H_