1 /*
2 * Copyright (C) 2023-2025 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 BACKUP_DATABASE_UTILS_H
17 #define BACKUP_DATABASE_UTILS_H
18
19 #include <string>
20 #include <sstream>
21 #include <vector>
22 #include <type_traits>
23
24 #include "backup_const.h"
25 #include "rdb_helper.h"
26 #include "result_set.h"
27 #include "medialibrary_errno.h"
28 #include "medialibrary_rdb_utils.h"
29 #include "result_set_utils.h"
30
31 namespace OHOS {
32 namespace Media {
33 using FileIdPair = std::pair<int32_t, int32_t>;
34 using TagPairOpt = std::pair<std::optional<std::string>, std::optional<std::string>>;
35 constexpr int32_t TOTAL_TBL_FACE_ANALYSED = 2;
36 class BackupDatabaseUtils {
37 public:
38 static int32_t InitDb(std::shared_ptr<NativeRdb::RdbStore> &rdbStore, const std::string &dbName,
39 const std::string &dbPath, const std::string &bundleName, bool isMediaLibary,
40 int32_t area = DEFAULT_AREA_VERSION);
41 static int32_t InitReadOnlyRdb(std::shared_ptr<NativeRdb::RdbStore> &rdbStore, const std::string &dbName,
42 const std::string &dbPath, const std::string &bundleName);
43 static int32_t QueryInt(std::shared_ptr<NativeRdb::RdbStore> rdbStore, const std::string &sql,
44 const std::string &column, const std::vector<NativeRdb::ValueObject> &args = {});
45 static int32_t Update(std::shared_ptr<NativeRdb::RdbStore> &rdbStore, int32_t &changeRows,
46 NativeRdb::ValuesBucket &valuesBucket, std::unique_ptr<NativeRdb::AbsRdbPredicates> &predicates);
47 static int32_t Delete(NativeRdb::AbsRdbPredicates &predicates, int32_t &changeRows,
48 std::shared_ptr<NativeRdb::RdbStore> &rdbStore);
49 static int32_t InitGarbageAlbum(std::shared_ptr<NativeRdb::RdbStore> rdbStore, std::set<std::string> &cacheSet,
50 std::unordered_map<std::string, std::string> &nickMap);
51 static void QueryGalleryDuplicateDataCount(std::shared_ptr<NativeRdb::RdbStore> galleryRdb, int32_t &count,
52 int32_t &total);
53 static std::shared_ptr<NativeRdb::ResultSet> GetQueryResultSet(const std::shared_ptr<NativeRdb::RdbStore> &rdbStore,
54 const std::string &querySql, const std::vector<std::string> &sqlArgs = {});
55 static std::unordered_map<std::string, std::string> GetColumnInfoMap(
56 const std::shared_ptr<NativeRdb::RdbStore> &rdbStore, const std::string &tableName);
57 static void UpdateUniqueNumber(const std::shared_ptr<NativeRdb::RdbStore> &rdbStore, int32_t number,
58 const std::string &type);
59 static int32_t QueryUniqueNumber(const std::shared_ptr<NativeRdb::RdbStore> &rdbStore, const std::string &type);
60 static std::string GarbleInfoName(const std::string &infoName);
61 static void UpdateSelection(std::string &selection, const std::string &selectionToAdd, bool needWrap = false);
62 static void UpdateSdWhereClause(std::string &querySql, bool shouldIncludeSd);
63 static bool QueryThumbImage(NativeRdb::RdbStore &rdbStore,
64 const std::string &keyValue, std::vector<uint8_t> &blob);
65 static int32_t GetBlob(const std::string &columnName, std::shared_ptr<NativeRdb::ResultSet> resultSet,
66 std::vector<uint8_t> &blobVal);
67 static uint32_t GetUint32ValFromBytes(const std::vector<uint8_t> &bytes, size_t start);
68 static void UpdateAnalysisTotalStatus(std::shared_ptr<NativeRdb::RdbStore> rdbStore);
69 static void UpdateAnalysisFaceTagStatus(std::shared_ptr<NativeRdb::RdbStore> rdbStore);
70 static bool SetTagIdNew(PortraitAlbumInfo &portraitAlbumInfo,
71 std::unordered_map<std::string, std::string> &tagIdMap);
72 static bool SetFileIdNew(FaceInfo &faceInfo, const std::unordered_map<std::string, FileInfo> &fileInfoMap);
73 static bool SetTagIdNew(FaceInfo &faceInfo, const std::unordered_map<std::string, std::string> &tagIdMap);
74 static bool SetAlbumIdNew(FaceInfo &faceInfo, const std::unordered_map<std::string, int32_t> &albumIdMap);
75 static void PrintErrorLog(const std::string &errorLog, int64_t start);
76 static float GetLandmarksScale(int32_t width, int32_t height);
77 static bool IsLandmarkValid(const FaceInfo &faceInfo, float landmarkX, float landmarkY);
78 static bool IsValInBound(float val, float minVal, float maxVal);
79 static std::vector<std::pair<std::string, std::string>> GetColumnInfoPairs(
80 const std::shared_ptr<NativeRdb::RdbStore> &rdbStore, const std::string &tableName);
81 static std::vector<std::string> GetCommonColumnInfos(std::shared_ptr<NativeRdb::RdbStore> mediaRdb,
82 std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb, std::string tableName);
83 static std::vector<std::string> filterColumns(const std::vector<std::string>& allColumns,
84 const std::vector<std::string>& excludedColumns);
85 static std::vector<FileIdPair> CollectFileIdPairs(const std::vector<FileInfo>& fileInfos);
86 static std::pair<std::vector<int32_t>, std::vector<int32_t>> UnzipFileIdPairs(const std::vector<FileIdPair>& pairs);
87 static void UpdateAnalysisPhotoMapStatus(std::shared_ptr<NativeRdb::RdbStore> rdbStore);
88 static std::vector<std::string> SplitString(const std::string& str, char delimiter);
89 static void PrintQuerySql(const std::string& querySql);
90 static int64_t QueryLong(std::shared_ptr<NativeRdb::RdbStore> rdbStore, const std::string &sql,
91 const std::string &columnName, const std::vector<NativeRdb::ValueObject> &args = {});
92 static int64_t QueryMaxId(std::shared_ptr<NativeRdb::RdbStore> rdbStore,
93 const std::string& tableName, const std::string& idColumnName);
94 static int ExecuteSQL(std::shared_ptr<NativeRdb::RdbStore> rdbStore, const std::string& sql,
95 const std::vector<NativeRdb::ValueObject> &args = {});
96 static bool DeleteDuplicatePortraitAlbum(int64_t maxAlbumId, const std::vector<std::string> &albumNames,
97 const std::vector<std::string> &tagIds, std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb);
98 static void UpdateAnalysisTotalTblStatus(std::shared_ptr<NativeRdb::RdbStore> rdbStore,
99 const std::vector<FileIdPair>& fileIdPair);
100 static void UpdateFaceAnalysisTblStatus(std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb);
101 static void DeleteExistingImageFaceData(std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb,
102 const std::vector<FileIdPair>& fileIdPair);
103 static std::vector<TagPairOpt> QueryTagInfo(std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb);
104 static void ParseFaceTagResultSet(const std::shared_ptr<NativeRdb::ResultSet>& resultSet,
105 TagPairOpt& tagPair);
106 static void UpdateGroupTagColumn(const std::vector<TagPairOpt>& updatedPairs,
107 std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb);
108 static void UpdateFaceGroupTagsUnion(std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb);
109 static void UpdateFaceGroupTagOfGallery(std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb);
110 static void UpdateTagPairs(std::vector<TagPairOpt>& updatedPairs, const std::string& newGroupTag,
111 const std::vector<std::string>& tagIds);
112 static void UpdateGroupTags(std::vector<TagPairOpt>& updatedPairs,
113 const std::unordered_map<std::string, std::vector<std::string>>& groupTagMap);
114 static void UpdateAssociateFileId(std::shared_ptr<NativeRdb::RdbStore> rdbStore,
115 const std::vector<FileInfo> &fileInfos);
116 static void BatchUpdatePhotosToLocal(std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb,
117 const std::vector<std::string> &inColumn);
118 static int32_t BatchInsert(std::shared_ptr<NativeRdb::RdbStore> rdbStore, const std::string &tableName,
119 std::vector<NativeRdb::ValuesBucket> &value, int64_t &rowNum);
120 static std::string CheckDbIntegrity(std::shared_ptr<NativeRdb::RdbStore> rdbStore, int32_t sceneCode,
121 const std::string &dbTag = "");
122 static int32_t QueryLocalNoAstcCount(std::shared_ptr<NativeRdb::RdbStore> rdbStore);
123 static int32_t QueryReadyAstcCount(std::shared_ptr<NativeRdb::RdbStore> rdbStore);
124 static std::unordered_map<int32_t, int32_t> QueryMediaTypeCount(
125 const std::shared_ptr<NativeRdb::RdbStore>& rdbStore, const std::string& querySql);
126 static std::shared_ptr<NativeRdb::ResultSet> QuerySql(std::shared_ptr<NativeRdb::RdbStore> rdbStore,
127 const std::string &querySql, const std::vector<NativeRdb::ValueObject> ¶ms);
128 template <typename T>
129 static std::string JoinValues(const std::vector<T>& values, std::string_view delimiter);
130 template <typename T>
131 static std::string JoinSQLValues(const std::vector<T>& values, std::string_view delimiter);
132 template <typename T>
133 static std::vector<T> LeftJoinValues(std::vector<T>& values,
134 std::string_view delimiter);
135 template <typename T>
136 struct always_false : std::false_type {};
137 template <typename T>
138 static std::optional<T> GetOptionalValue(const std::shared_ptr<NativeRdb::ResultSet> &resultSet,
139 const std::string &columnName);
140 static void UpdateBurstPhotos(const std::shared_ptr<NativeRdb::RdbStore> &rdbStore);
141 static std::vector<int32_t> QueryIntVec(std::shared_ptr<NativeRdb::RdbStore> rdbStore,
142 const std::string& sql, const std::string& columnName);
143 static void UpdateAnalysisTotalTblNoFaceStatus(std::shared_ptr<NativeRdb::RdbStore> newRdbStore,
144 std::shared_ptr<NativeRdb::RdbStore> oldRdbStore, const std::vector<FileIdPair>& fileIdPair);
145
146 private:
147 static std::string CloudSyncTriggerFunc(const std::vector<std::string> &args);
148 static std::string IsCallerSelfFunc(const std::vector<std::string> &args);
149 static std::string PhotoAlbumNotifyFunc(const std::vector<std::string>& args);
150 static std::string BeginGenerateHighlightThumbnail(const std::vector<std::string>& args);
151 static const ssize_t WAL_LIMIT_SIZE = 1024 * 1024 * 1024;
152 };
153
154 class RdbCallback : public NativeRdb::RdbOpenCallback {
155 public:
OnCreate(NativeRdb::RdbStore & rdb)156 virtual int32_t OnCreate(NativeRdb::RdbStore &rdb) override
157 {
158 return 0;
159 }
160
OnUpgrade(NativeRdb::RdbStore & rdb,int32_t oldVersion,int32_t newVersion)161 virtual int32_t OnUpgrade(NativeRdb::RdbStore &rdb, int32_t oldVersion,
162 int32_t newVersion) override
163 {
164 return 0;
165 }
166 };
167
168 template <typename T>
JoinSQLValues(const std::vector<T> & values,std::string_view delimiter)169 std::string BackupDatabaseUtils::JoinSQLValues(const std::vector<T>& values, std::string_view delimiter)
170 {
171 std::stringstream ss;
172 bool first = true;
173 for (const auto& value : values) {
174 if (!first) {
175 ss << delimiter;
176 }
177 first = false;
178 if constexpr (std::is_same_v<T, std::string>) {
179 ss << "'" << value << "'";
180 } else {
181 ss << std::to_string(value);
182 }
183 }
184 return ss.str();
185 }
186
187 template <typename T>
JoinValues(const std::vector<T> & values,std::string_view delimiter)188 std::string BackupDatabaseUtils::JoinValues(const std::vector<T>& values, std::string_view delimiter)
189 {
190 std::stringstream ss;
191 bool first = true;
192 for (const auto& value : values) {
193 if (!first) {
194 ss << delimiter;
195 }
196 first = false;
197 if constexpr (std::is_same_v<T, std::string>) {
198 ss << value;
199 } else {
200 ss << std::to_string(value);
201 }
202 }
203 return ss.str();
204 }
205
206 template <typename T>
LeftJoinValues(std::vector<T> & values,std::string_view delimiter)207 std::vector<T> BackupDatabaseUtils::LeftJoinValues(std::vector<T>& values, std::string_view delimiter)
208 {
209 for (auto& value : values) {
210 value.insert(0, delimiter);
211 }
212 return values;
213 }
214
215 template<typename T>
GetOptionalValue(const std::shared_ptr<NativeRdb::ResultSet> & resultSet,const std::string & columnName)216 std::optional<T> BackupDatabaseUtils::GetOptionalValue(const std::shared_ptr<NativeRdb::ResultSet> &resultSet,
217 const std::string &columnName)
218 {
219 int32_t columnIndex;
220 int32_t err = resultSet->GetColumnIndex(columnName, columnIndex);
221 if (err != E_OK) {
222 return std::nullopt;
223 }
224
225 bool isNull = false;
226 int32_t errCode = resultSet->IsColumnNull(columnIndex, isNull);
227 if (errCode || isNull) {
228 return std::nullopt;
229 }
230
231 T value;
232 if constexpr (std::is_same_v<T, int32_t>) {
233 errCode = resultSet->GetInt(columnIndex, value);
234 } else if constexpr (std::is_same_v<T, int64_t>) {
235 errCode = resultSet->GetLong(columnIndex, value);
236 } else if constexpr (std::is_same_v<T, double>) {
237 errCode = resultSet->GetDouble(columnIndex, value);
238 } else if constexpr (std::is_same_v<T, std::string>) {
239 errCode = resultSet->GetString(columnIndex, value);
240 } else if constexpr (std::is_same_v<T, std::vector<uint8_t>>) {
241 errCode = resultSet->GetBlob(columnIndex, value);
242 } else {
243 static_assert(always_false<T>::value, "Unsupported type for GetOptionalValue");
244 }
245
246 return errCode ? std::nullopt : std::optional<T>(value);
247 }
248 } // namespace Media
249 } // namespace OHOS
250
251 #endif // BACKUP_DATABASE_UTILS_H