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 #define MLOG_TAG "SmartAlbum"
16
17 #include "medialibrary_smartalbum_map_db.h"
18 #include "media_file_utils.h"
19 #include "media_log.h"
20 #include "medialibrary_data_manager_utils.h"
21 #include "medialibrary_errno.h"
22 #include "rdb_utils.h"
23
24 using namespace std;
25 using namespace OHOS::DataShare;
26 using namespace OHOS::NativeRdb;
27
28 namespace OHOS {
29 namespace Media {
30 static const std::string SMARTALBUM_MAP_DE_SMARTALBUM_COND = SMARTALBUMMAP_DB_ALBUM_ID + " = ?";
31 static const std::string SMARTALBUM_MAP_DE_ASSETS_COND = SMARTALBUMMAP_DB_CHILD_ASSET_ID + " = ?";
32 static const std::string SMARTALBUM_MAP_DB_COND = SMARTALBUMMAP_DB_ALBUM_ID +
33 " = ? AND " + SMARTALBUMMAP_DB_CHILD_ASSET_ID + " = ?";
InsertSmartAlbumMapInfo(const ValuesBucket & values,const shared_ptr<RdbStore> & rdbStore)34 int64_t MediaLibrarySmartAlbumMapDb::InsertSmartAlbumMapInfo(const ValuesBucket &values,
35 const shared_ptr<RdbStore> &rdbStore)
36 {
37 CHECK_AND_RETURN_RET_LOG(rdbStore != nullptr, E_ALBUM_OPER_ERR, "Invalid RDB store");
38 int64_t outRowId(0);
39 int32_t insertResult = rdbStore->Insert(outRowId, SMARTALBUM_MAP_TABLE, values);
40 CHECK_AND_RETURN_RET_LOG(insertResult == NativeRdb::E_OK, E_ALBUM_OPER_ERR, "Insert failed");
41 return outRowId;
42 }
43
DeleteSmartAlbumMapInfo(const int32_t albumId,const int32_t assetId,const shared_ptr<RdbStore> & rdbStore)44 int32_t MediaLibrarySmartAlbumMapDb::DeleteSmartAlbumMapInfo(const int32_t albumId,
45 const int32_t assetId,
46 const shared_ptr<RdbStore> &rdbStore)
47 {
48 CHECK_AND_RETURN_RET_LOG((rdbStore != nullptr) && (albumId > 0) && (assetId > 0),
49 E_ALBUM_OPER_ERR, "Invalid input");
50 int32_t deletedRows(E_ALBUM_OPER_ERR);
51 vector<string> whereArgs = { std::to_string(albumId), std::to_string(assetId)};
52 int32_t deleteResult = rdbStore->Delete(deletedRows, SMARTALBUM_MAP_TABLE, SMARTALBUM_MAP_DB_COND, whereArgs);
53 CHECK_AND_RETURN_RET_LOG(deleteResult == NativeRdb::E_OK, E_ALBUM_OPER_ERR, "Delete failed");
54 return (deletedRows > 0) ? E_SUCCESS : E_FAIL;
55 }
56
DeleteAllSmartAlbumMapInfo(const int32_t albumId,const shared_ptr<RdbStore> & rdbStore)57 int32_t MediaLibrarySmartAlbumMapDb::DeleteAllSmartAlbumMapInfo(const int32_t albumId,
58 const shared_ptr<RdbStore> &rdbStore)
59 {
60 CHECK_AND_RETURN_RET_LOG((rdbStore != nullptr) && (albumId > 0), E_ALBUM_OPER_ERR, "Invalid input");
61 int32_t deletedRows(E_ALBUM_OPER_ERR);
62 vector<string> whereArgs = { std::to_string(albumId)};
63 int32_t deleteResult = rdbStore->Delete(deletedRows,
64 SMARTALBUM_MAP_TABLE, SMARTALBUM_MAP_DE_SMARTALBUM_COND, whereArgs);
65 CHECK_AND_RETURN_RET_LOG(deleteResult == NativeRdb::E_OK, E_ALBUM_OPER_ERR, "Delete failed");
66 return (deletedRows > 0) ? E_SUCCESS : E_FAIL;
67 }
68
DeleteAllAssetsMapInfo(const int32_t assetId,const shared_ptr<RdbStore> & rdbStore)69 int32_t MediaLibrarySmartAlbumMapDb::DeleteAllAssetsMapInfo(const int32_t assetId,
70 const shared_ptr<RdbStore> &rdbStore)
71 {
72 CHECK_AND_RETURN_RET_LOG((rdbStore != nullptr) && (assetId > 0), E_ALBUM_OPER_ERR, "Invalid input");
73 int32_t deletedRows(E_ALBUM_OPER_ERR);
74 vector<string> whereArgs = { std::to_string(assetId)};
75 int32_t deleteResult = rdbStore->Delete(deletedRows, SMARTALBUM_MAP_TABLE,
76 SMARTALBUM_MAP_DE_ASSETS_COND, whereArgs);
77 CHECK_AND_RETURN_RET_LOG(deleteResult == NativeRdb::E_OK, E_ALBUM_OPER_ERR, "Delete failed");
78 return (deletedRows > 0) ? E_SUCCESS : E_FAIL;
79 }
80
UpdateAssetTrashInfo(const int32_t & assetId,const int64_t & trashDate,const shared_ptr<RdbStore> & rdbStore,string & recyclePath,const string & oldPath)81 int32_t MediaLibrarySmartAlbumMapDb::UpdateAssetTrashInfo(const int32_t &assetId,
82 const int64_t &trashDate,
83 const shared_ptr<RdbStore> &rdbStore,
84 string &recyclePath,
85 const string &oldPath)
86 {
87 vector<string> whereArgs;
88 int32_t changedRows = -1;
89 CHECK_AND_RETURN_RET_LOG(rdbStore != nullptr, E_ALBUM_OPER_ERR, "Invalid input");
90 string strUpdateCondition = MEDIA_DATA_DB_ID + " = " + to_string(assetId);
91 ValuesBucket values;
92 MEDIA_DEBUG_LOG("UpdateAssetTrashInfo isTrash != 0");
93 values.PutString(MEDIA_DATA_DB_FILE_PATH, recyclePath);
94 values.PutString(MEDIA_DATA_DB_RECYCLE_PATH, oldPath);
95 values.PutLong(MEDIA_DATA_DB_DATE_TRASHED, trashDate);
96 values.PutLong(MEDIA_DATA_DB_DATE_MODIFIED, trashDate);
97 values.PutInt(MEDIA_DATA_DB_IS_TRASH, ASSET_ISTRASH);
98 int32_t result = rdbStore->Update(changedRows, MEDIALIBRARY_TABLE, values, strUpdateCondition, whereArgs);
99 if ((result != NativeRdb::E_OK) || (changedRows <= 0)) {
100 MEDIA_ERR_LOG("Update DB failed. Error is %{private}d. Updated count %{private}d", result, changedRows);
101 return E_FAIL;
102 }
103 return E_SUCCESS;
104 }
105
UpdateSameNameInfo(const int32_t & assetId,const string & displayName,const string & path,const shared_ptr<RdbStore> & rdbStore)106 int32_t MediaLibrarySmartAlbumMapDb::UpdateSameNameInfo(const int32_t &assetId,
107 const string &displayName, const string &path, const shared_ptr<RdbStore> &rdbStore)
108 {
109 vector<string> whereArgs;
110 int32_t changedRows = -1;
111 if ((rdbStore != nullptr) && (assetId > 0)) {
112 string strUpdateCondition = MEDIA_DATA_DB_ID + " = " + to_string(assetId);
113 ValuesBucket values;
114 values.PutString(MEDIA_DATA_DB_NAME, displayName);
115 values.PutString(MEDIA_DATA_DB_RECYCLE_PATH, path);
116 values.PutString(MEDIA_DATA_DB_TITLE, MediaLibraryDataManagerUtils::GetFileTitle(displayName));
117 int32_t result = rdbStore->Update(changedRows, MEDIALIBRARY_TABLE, values, strUpdateCondition, whereArgs);
118 if ((result != NativeRdb::E_OK) || (changedRows <= 0)) {
119 MEDIA_ERR_LOG("Update DB failed. Error is %{private}d. Updated count %{private}d", result, changedRows);
120 return E_FAIL;
121 }
122 }
123 return E_SUCCESS;
124 }
125
UpdateParentDirRecycleInfo(const int32_t & assetId,const int32_t & parentId,const string & parentName,const shared_ptr<RdbStore> & rdbStore)126 int32_t MediaLibrarySmartAlbumMapDb::UpdateParentDirRecycleInfo(const int32_t &assetId, const int32_t &parentId,
127 const string &parentName, const shared_ptr<RdbStore> &rdbStore)
128 {
129 vector<string> whereArgs;
130 int32_t changedRows = -1;
131
132 if ((rdbStore != nullptr) && (assetId > 0) && (parentId > 0)) {
133 string strUpdateCondition = MEDIA_DATA_DB_ID + " = " + to_string(assetId);
134 ValuesBucket values;
135 values.PutInt(MEDIA_DATA_DB_PARENT_ID, parentId);
136 if (!parentName.empty()) {
137 values.PutInt(MEDIA_DATA_DB_BUCKET_ID, parentId);
138 values.PutString(MEDIA_DATA_DB_BUCKET_NAME, parentName);
139 }
140 int32_t result = rdbStore->Update(changedRows, MEDIALIBRARY_TABLE, values, strUpdateCondition, whereArgs);
141 if ((result != NativeRdb::E_OK) || (changedRows <= 0)) {
142 MEDIA_ERR_LOG("Update DB failed. Error is %{private}d. Updated count %{private}d", result, changedRows);
143 return E_FAIL;
144 }
145 }
146 return E_SUCCESS;
147 }
148
UpdateChildFileRecycleInfo(const int32_t & assetId,const string & parentName,const shared_ptr<RdbStore> & rdbStore)149 int32_t MediaLibrarySmartAlbumMapDb::UpdateChildFileRecycleInfo(const int32_t &assetId,
150 const string &parentName, const shared_ptr<RdbStore> &rdbStore)
151 {
152 vector<string> whereArgs;
153 int32_t changedRows = -1;
154
155 if ((rdbStore != nullptr) && (assetId > 0)) {
156 string strUpdateCondition = MEDIA_DATA_DB_ID + " = " + to_string(assetId);
157 ValuesBucket values;
158 if (!parentName.empty()) {
159 values.PutString(MEDIA_DATA_DB_BUCKET_NAME, parentName);
160 }
161 int32_t result = rdbStore->Update(changedRows, MEDIALIBRARY_TABLE, values, strUpdateCondition, whereArgs);
162 if ((result != NativeRdb::E_OK) || (changedRows <= 0)) {
163 MEDIA_ERR_LOG("Update DB failed. Error is %{private}d. Updated count %{private}d", result, changedRows);
164 return E_FAIL;
165 }
166 }
167 return E_SUCCESS;
168 }
169
UpdateChildPathInfo(const int32_t & assetId,const string & path,const string & relativePath,const int32_t isTrash,const shared_ptr<RdbStore> & rdbStore)170 int32_t MediaLibrarySmartAlbumMapDb::UpdateChildPathInfo(const int32_t &assetId,
171 const string &path, const string &relativePath, const int32_t isTrash, const shared_ptr<RdbStore> &rdbStore)
172 {
173 vector<string> whereArgs;
174 int32_t changedRows = -1;
175
176 if ((rdbStore != nullptr) && (assetId > 0)) {
177 string strUpdateCondition = MEDIA_DATA_DB_ID + " = " + to_string(assetId);
178 ValuesBucket values;
179 if (isTrash == CHILD_ISTRASH) {
180 values.PutString(MEDIA_DATA_DB_FILE_PATH, path);
181 } else {
182 values.PutString(MEDIA_DATA_DB_RECYCLE_PATH, path);
183 }
184 values.PutString(MEDIA_DATA_DB_RELATIVE_PATH, relativePath);
185 int32_t result = rdbStore->Update(changedRows, MEDIALIBRARY_TABLE, values, strUpdateCondition, whereArgs);
186 if ((result != NativeRdb::E_OK) || (changedRows <= 0)) {
187 MEDIA_ERR_LOG("Update DB failed. Error is %{private}d. Updated count %{private}d", result, changedRows);
188 return E_FAIL;
189 }
190 }
191 return E_SUCCESS;
192 }
193
DeleteTrashInfo(const int32_t & assetId,const shared_ptr<RdbStore> & rdbStore)194 int32_t MediaLibrarySmartAlbumMapDb::DeleteTrashInfo(const int32_t &assetId, const shared_ptr<RdbStore> &rdbStore)
195 {
196 vector<string> whereArgs = {};
197 int32_t deletedRows = E_FAIL;
198 if ((rdbStore != nullptr) && (assetId > 0)) {
199 string strDeleteCondition = MEDIA_DATA_DB_ID + " = " + to_string(assetId);
200 int32_t result = rdbStore->Delete(deletedRows, MEDIALIBRARY_TABLE, strDeleteCondition, whereArgs);
201 if (result != NativeRdb::E_OK) {
202 MEDIA_ERR_LOG("Delete operation failed. Result %{private}d. Deleted %{private}d", result, deletedRows);
203 }
204 }
205
206 return E_SUCCESS;
207 }
208
UpdateFavoriteInfo(const int32_t & assetId,const ValuesBucket & values,const shared_ptr<RdbStore> & rdbStore)209 int32_t MediaLibrarySmartAlbumMapDb::UpdateFavoriteInfo(const int32_t &assetId,
210 const ValuesBucket &values, const shared_ptr<RdbStore> &rdbStore)
211 {
212 vector<string> whereArgs;
213 int32_t changedRows = -1;
214 if ((rdbStore != nullptr) && (assetId > 0)) {
215 string strUpdateCondition = MEDIA_DATA_DB_ID + " = " + to_string(assetId);
216 int32_t result = rdbStore->Update(changedRows, MEDIALIBRARY_TABLE, values, strUpdateCondition, whereArgs);
217 if ((result != E_OK) || (changedRows <= 0)) {
218 MEDIA_ERR_LOG("Update DB failed. Error is %{private}d. Updated count %{private}d", result, changedRows);
219 return E_FAIL;
220 }
221 }
222 return E_SUCCESS;
223 }
224
UpdateRecycleInfo(const int32_t & assetId,const shared_ptr<RdbStore> & rdbStore,const string & realPath)225 int32_t MediaLibrarySmartAlbumMapDb::UpdateRecycleInfo(const int32_t &assetId,
226 const shared_ptr<RdbStore> &rdbStore,
227 const string &realPath)
228 {
229 CHECK_AND_RETURN_RET_LOG(rdbStore != nullptr, E_ALBUM_OPER_ERR, "Invalid input");
230 vector<string> whereArgs;
231 int32_t changedRows = -1;
232 string strUpdateCondition = MEDIA_DATA_DB_ID + " = " + to_string(assetId);
233 ValuesBucket values;
234 values.PutString(MEDIA_DATA_DB_FILE_PATH, realPath);
235 values.PutString(MEDIA_DATA_DB_RECYCLE_PATH, "");
236 values.PutLong(MEDIA_DATA_DB_DATE_MODIFIED, MediaFileUtils::UTCTimeSeconds());
237 values.PutLong(MEDIA_DATA_DB_DATE_TRASHED, 0);
238 values.PutInt(MEDIA_DATA_DB_IS_TRASH, 0);
239 int32_t result = rdbStore->Update(changedRows, MEDIALIBRARY_TABLE, values, strUpdateCondition, whereArgs);
240 if ((result != NativeRdb::E_OK) || (changedRows <= 0)) {
241 MEDIA_ERR_LOG("Update DB failed. Error is %{private}d. Updated count %{private}d", result, changedRows);
242 return E_FAIL;
243 }
244 return E_SUCCESS;
245 }
246
UpdateChildRecycleInfo(const int32_t & assetId,const shared_ptr<RdbStore> & rdbStore,const int64_t & recycleDate)247 int32_t MediaLibrarySmartAlbumMapDb::UpdateChildRecycleInfo(const int32_t &assetId,
248 const shared_ptr<RdbStore> &rdbStore,
249 const int64_t &recycleDate)
250 {
251 CHECK_AND_RETURN_RET_LOG(rdbStore != nullptr, E_ALBUM_OPER_ERR, "Invalid input");
252 vector<string> whereArgs;
253 int32_t changedRows = -1;
254 string strUpdateCondition = MEDIA_DATA_DB_ID + " = " + to_string(assetId);
255 ValuesBucket values;
256 values.PutLong(MEDIA_DATA_DB_DATE_MODIFIED, recycleDate);
257 values.PutLong(MEDIA_DATA_DB_DATE_TRASHED, 0);
258 values.PutInt(MEDIA_DATA_DB_IS_TRASH, 0);
259 int32_t result = rdbStore->Update(changedRows, MEDIALIBRARY_TABLE, values, strUpdateCondition, whereArgs);
260 if ((result != NativeRdb::E_OK) || (changedRows <= 0)) {
261 MEDIA_ERR_LOG("Update DB failed. Error is %{private}d. Updated count %{private}d", result, changedRows);
262 return E_FAIL;
263 }
264 return E_SUCCESS;
265 }
266
UpdateDirTrashInfo(const int32_t & assetId,const int64_t & trashDate,const shared_ptr<RdbStore> & rdbStore,string & recyclePath,const string & oldPath)267 int32_t MediaLibrarySmartAlbumMapDb::UpdateDirTrashInfo(const int32_t &assetId,
268 const int64_t &trashDate,
269 const shared_ptr<RdbStore> &rdbStore,
270 string &recyclePath,
271 const string &oldPath)
272 {
273 CHECK_AND_RETURN_RET_LOG(rdbStore != nullptr, E_ALBUM_OPER_ERR, "Invalid input");
274 vector<string> whereArgs;
275 int32_t changedRows = -1;
276 string strUpdateCondition = MEDIA_DATA_DB_ID + " = " + to_string(assetId);
277 ValuesBucket values;
278 values.PutString(MEDIA_DATA_DB_FILE_PATH, recyclePath);
279 values.PutString(MEDIA_DATA_DB_RECYCLE_PATH, oldPath);
280 values.PutLong(MEDIA_DATA_DB_DATE_MODIFIED, trashDate);
281 values.PutLong(MEDIA_DATA_DB_DATE_TRASHED, trashDate);
282 values.PutInt(MEDIA_DATA_DB_IS_TRASH, DIR_ISTRASH);
283
284 int32_t result = rdbStore->Update(changedRows, MEDIALIBRARY_TABLE, values, strUpdateCondition, whereArgs);
285 if ((result != NativeRdb::E_OK) || (changedRows <= 0)) {
286 MEDIA_ERR_LOG("Update DB failed. Error is %{private}d. Updated count %{private}d", result, changedRows);
287 return E_FAIL;
288 }
289 return E_SUCCESS;
290 }
291
UpdateChildTrashInfo(const int32_t & assetId,const shared_ptr<RdbStore> & rdbStore,const int64_t & trashDate)292 int32_t MediaLibrarySmartAlbumMapDb::UpdateChildTrashInfo(const int32_t &assetId,
293 const shared_ptr<RdbStore> &rdbStore,
294 const int64_t &trashDate)
295 {
296 CHECK_AND_RETURN_RET_LOG(rdbStore != nullptr, E_ALBUM_OPER_ERR, "Invalid input");
297 vector<string> whereArgs;
298 int32_t changedRows = -1;
299 string strUpdateCondition = MEDIA_DATA_DB_ID + " = " + to_string(assetId);
300 ValuesBucket values;
301 values.PutLong(MEDIA_DATA_DB_DATE_TRASHED, trashDate);
302 values.PutLong(MEDIA_DATA_DB_DATE_MODIFIED, trashDate);
303 values.PutInt(MEDIA_DATA_DB_IS_TRASH, CHILD_ISTRASH);
304 int32_t result = rdbStore->Update(changedRows, MEDIALIBRARY_TABLE, values, strUpdateCondition, whereArgs);
305 if ((result != NativeRdb::E_OK) || (changedRows <= 0)) {
306 MEDIA_ERR_LOG("Update DB failed. Error is %{private}d. Updated count %{private}d", result, changedRows);
307 return E_FAIL;
308 }
309 return E_SUCCESS;
310 }
311 } // namespace Media
312 } // namespace OHOS
313