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 #include "fetch_result.h"
17 #include "media_log.h"
18
19 using namespace std;
20
21 namespace {
22 const int ARG1 = 0;
23 const int ARG2 = 1;
24 const int ARG3 = 2;
25 }
26
27 namespace OHOS {
28 namespace Media {
FetchResult(const shared_ptr<OHOS::NativeRdb::AbsSharedResultSet> & resultset)29 FetchResult::FetchResult(const shared_ptr<OHOS::NativeRdb::AbsSharedResultSet>& resultset)
30 {
31 count_ = 0;
32 if (resultset != nullptr) {
33 resultset->GetRowCount(count_);
34 }
35 isContain_ = count_ > 0;
36 isClosed_ = false;
37 resultset_ = resultset;
38 networkId_ = "";
39 }
40
41 // empty constructor napi
FetchResult()42 FetchResult::FetchResult()
43 : isContain_(false), isClosed_(false), count_(0), resultset_(nullptr) {}
44
~FetchResult()45 FetchResult::~FetchResult() {}
46
Close()47 void FetchResult::Close()
48 {
49 isClosed_ = true;
50 }
51
IsContain()52 bool FetchResult::IsContain()
53 {
54 return isContain_;
55 }
56
GetCount()57 int32_t FetchResult::GetCount()
58 {
59 return count_;
60 }
61
IsClosed()62 bool FetchResult::IsClosed()
63 {
64 return isClosed_;
65 }
66
GetObjectAtPosition(int32_t index)67 unique_ptr<FileAsset> FetchResult::GetObjectAtPosition(int32_t index)
68 {
69 if ((index < 0) || (index > (count_ - 1)) || (resultset_ == nullptr)) {
70 MEDIA_ERR_LOG("index not proper or rs is null");
71 return nullptr;
72 }
73
74 if (resultset_->GoToRow(index) != 0) {
75 MEDIA_ERR_LOG("failed to go to row at index pos");
76 return nullptr;
77 }
78
79 return GetObject();
80 }
81
GetFirstObject()82 unique_ptr<FileAsset> FetchResult::GetFirstObject()
83 {
84 if ((resultset_ == nullptr) || (resultset_->GoToFirstRow() != 0)) {
85 MEDIA_ERR_LOG("resultset is null|first row failed");
86 return nullptr;
87 }
88
89 return GetObject();
90 }
91
GetNextObject()92 unique_ptr<FileAsset> FetchResult::GetNextObject()
93 {
94 if ((resultset_ == nullptr) || (resultset_->GoToNextRow() != 0)) {
95 MEDIA_ERR_LOG("resultset is null|go to next row failed");
96 return nullptr;
97 }
98
99 return GetObject();
100 }
101
GetLastObject()102 unique_ptr<FileAsset> FetchResult::GetLastObject()
103 {
104 if ((resultset_ == nullptr) || (resultset_->GoToLastRow() != 0)) {
105 MEDIA_ERR_LOG("resultset is null|go to last row failed");
106 return nullptr;
107 }
108
109 return GetObject();
110 }
111
IsAtLastRow()112 bool FetchResult::IsAtLastRow()
113 {
114 if (resultset_ == nullptr) {
115 MEDIA_ERR_LOG("resultset null");
116 return false;
117 }
118
119 bool retVal = false;
120 resultset_->IsAtLastRow(retVal);
121 return retVal;
122 }
123
ReturnDefaultOnError(string errMsg,ResultSetDataType dataType)124 variant<int32_t, int64_t, string> ReturnDefaultOnError(string errMsg, ResultSetDataType dataType)
125 {
126 MEDIA_ERR_LOG("%{public}s", errMsg.c_str());
127 if ((dataType) == TYPE_STRING)
128 return "";
129 else
130 return 0;
131 }
132
GetRowValFromColumnn(string columnName,ResultSetDataType dataType)133 variant<int32_t, int64_t, string> FetchResult::GetRowValFromColumnn(string columnName, ResultSetDataType dataType)
134 {
135 int index;
136 variant<int32_t, int64_t, string> cellValue;
137 int integerVal = 0;
138 string stringVal = "";
139 int64_t longVal = 0;
140 int status;
141
142 if (resultset_ == nullptr) {
143 ReturnDefaultOnError("Resultset is null", dataType);
144 }
145
146 status = resultset_->GetColumnIndex(columnName, index);
147 if (status != NativeRdb::E_OK) {
148 ReturnDefaultOnError("failed to obtain the index", dataType);
149 }
150
151 switch (dataType) {
152 case TYPE_STRING:
153 status = resultset_->GetString(index, stringVal);
154 if (status != NativeRdb::E_OK) {
155 ReturnDefaultOnError("failed to obtain string value from resultset", dataType);
156 }
157 cellValue = stringVal;
158 break;
159 case TYPE_INT32:
160 status = resultset_->GetInt(index, integerVal);
161 if (status != NativeRdb::E_OK) {
162 ReturnDefaultOnError("failed to obtain int value from resultset", dataType);
163 }
164 cellValue = integerVal;
165 break;
166 case TYPE_INT64:
167 status = resultset_->GetLong(index, longVal);
168 if (status != NativeRdb::E_OK) {
169 ReturnDefaultOnError("failed to obtain long value from resultset", dataType);
170 }
171
172 cellValue = longVal;
173 break;
174 default:
175 MEDIA_ERR_LOG("not match dataType %{public}d!!!!!", dataType);
176 break;
177 }
178
179 return cellValue;
180 }
181
GetFileMediaTypeUri(MediaType mediaType,const string & networkId)182 static string GetFileMediaTypeUri(MediaType mediaType, const string& networkId)
183 {
184 string uri = MEDIALIBRARY_DATA_ABILITY_PREFIX + networkId + MEDIALIBRARY_DATA_URI_IDENTIFIER;
185 switch (mediaType) {
186 case MEDIA_TYPE_AUDIO:
187 return uri + MEDIALIBRARY_TYPE_AUDIO_URI;
188 break;
189 case MEDIA_TYPE_VIDEO:
190 return uri + MEDIALIBRARY_TYPE_VIDEO_URI;
191 break;
192 case MEDIA_TYPE_IMAGE:
193 return uri + MEDIALIBRARY_TYPE_IMAGE_URI;
194 break;
195 case MEDIA_TYPE_FILE:
196 default:
197 return uri + MEDIALIBRARY_TYPE_FILE_URI;
198 break;
199 }
200 }
201
GetObject()202 unique_ptr<FileAsset> FetchResult::GetObject()
203 {
204 unique_ptr<FileAsset> fileAsset = make_unique<FileAsset>();
205
206 fileAsset->SetId(get<ARG1>(GetRowValFromColumnn(MEDIA_DATA_DB_ID, TYPE_INT32)));
207
208 fileAsset->SetMediaType(static_cast<Media::MediaType>(get<ARG1>(
209 GetRowValFromColumnn(MEDIA_DATA_DB_MEDIA_TYPE, TYPE_INT32))));
210
211 fileAsset->SetDisplayName(get<ARG3>(GetRowValFromColumnn(MEDIA_DATA_DB_NAME, TYPE_STRING)));
212
213 fileAsset->SetRelativePath(get<ARG3>(GetRowValFromColumnn(MEDIA_DATA_DB_RELATIVE_PATH, TYPE_STRING)));
214
215 fileAsset->SetParent(get<ARG1>(GetRowValFromColumnn(MEDIA_DATA_DB_PARENT_ID, TYPE_INT32)));
216
217 fileAsset->SetSize(get<ARG2>(GetRowValFromColumnn(MEDIA_DATA_DB_SIZE, TYPE_INT64)));
218
219 fileAsset->SetDateAdded(get<ARG2>(GetRowValFromColumnn(MEDIA_DATA_DB_DATE_ADDED, TYPE_INT64)));
220
221 fileAsset->SetDateModified(get<ARG2>(GetRowValFromColumnn(MEDIA_DATA_DB_DATE_MODIFIED, TYPE_INT64)));
222
223 fileAsset->SetDateTaken(get<ARG2>(GetRowValFromColumnn(MEDIA_DATA_DB_DATE_TAKEN, TYPE_INT64)));
224
225 fileAsset->SetPath(get<ARG3>(GetRowValFromColumnn(MEDIA_DATA_DB_FILE_PATH, TYPE_STRING)));
226
227 fileAsset->SetMimeType(get<ARG3>(GetRowValFromColumnn(MEDIA_DATA_DB_MIME_TYPE, TYPE_STRING)));
228
229 fileAsset->SetTitle(get<ARG3>(GetRowValFromColumnn(MEDIA_DATA_DB_TITLE, TYPE_STRING)));
230
231 fileAsset->SetArtist(get<ARG3>(GetRowValFromColumnn(MEDIA_DATA_DB_ARTIST, TYPE_STRING)));
232
233 fileAsset->SetAlbum(get<ARG3>(GetRowValFromColumnn(MEDIA_DATA_DB_ALBUM, TYPE_STRING)));
234
235 fileAsset->SetWidth(get<ARG1>(GetRowValFromColumnn(MEDIA_DATA_DB_WIDTH, TYPE_INT32)));
236
237 fileAsset->SetHeight(get<ARG1>(GetRowValFromColumnn(MEDIA_DATA_DB_HEIGHT, TYPE_INT32)));
238
239 fileAsset->SetDuration(get<ARG1>(GetRowValFromColumnn(MEDIA_DATA_DB_DURATION, TYPE_INT32)));
240
241 fileAsset->SetOrientation(get<ARG1>(GetRowValFromColumnn(MEDIA_DATA_DB_ORIENTATION, TYPE_INT32)));
242
243 fileAsset->SetAlbumId(get<ARG1>(GetRowValFromColumnn(MEDIA_DATA_DB_BUCKET_ID, TYPE_INT32)));
244
245 fileAsset->SetAlbumName(get<ARG3>(GetRowValFromColumnn(MEDIA_DATA_DB_BUCKET_NAME, TYPE_STRING)));
246
247 fileAsset->SetTimePending(get<ARG2>(GetRowValFromColumnn(MEDIA_DATA_DB_TIME_PENDING, TYPE_INT64)));
248
249 fileAsset->SetPending((get<ARG1>(GetRowValFromColumnn(MEDIA_DATA_DB_IS_PENDING, TYPE_INT32)) != 0));
250
251 fileAsset->SetFavorite((get<ARG1>(GetRowValFromColumnn(MEDIA_DATA_DB_IS_FAV, TYPE_INT32)) != 0));
252
253 fileAsset->SetDateTrashed(get<ARG2>(GetRowValFromColumnn(MEDIA_DATA_DB_DATE_TRASHED, TYPE_INT64)));
254
255 fileAsset->SetSelfId(get<ARG3>(GetRowValFromColumnn(MEDIA_DATA_DB_SELF_ID, TYPE_STRING)));
256
257 fileAsset->SetUri(GetFileMediaTypeUri(fileAsset->GetMediaType(), networkId_)
258 + "/" + to_string(fileAsset->GetId()));
259
260 return fileAsset;
261 }
262 } // namespace Media
263 } // namespace OHOS
264