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 class BackupDatabaseUtils {
36 public:
37 static int32_t InitDb(std::shared_ptr<NativeRdb::RdbStore> &rdbStore, const std::string &dbName,
38 const std::string &dbPath, const std::string &bundleName, bool isMediaLibary,
39 int32_t area = DEFAULT_AREA_VERSION);
40 static int32_t InitReadOnlyRdb(std::shared_ptr<NativeRdb::RdbStore> &rdbStore, const std::string &dbName,
41 const std::string &dbPath, const std::string &bundleName);
42 static int32_t QueryInt(std::shared_ptr<NativeRdb::RdbStore> rdbStore, const std::string &sql,
43 const std::string &column, const std::vector<NativeRdb::ValueObject> &args = {});
44 static int32_t Update(std::shared_ptr<NativeRdb::RdbStore> &rdbStore, int32_t &changeRows,
45 NativeRdb::ValuesBucket &valuesBucket, std::unique_ptr<NativeRdb::AbsRdbPredicates> &predicates);
46 static int32_t Delete(NativeRdb::AbsRdbPredicates &predicates, int32_t &changeRows,
47 std::shared_ptr<NativeRdb::RdbStore> &rdbStore);
48 static int32_t InitGarbageAlbum(std::shared_ptr<NativeRdb::RdbStore> rdbStore, std::set<std::string> &cacheSet,
49 std::unordered_map<std::string, std::string> &nickMap);
50 static int32_t QueryGalleryCloneCount(std::shared_ptr<NativeRdb::RdbStore> rdbStore);
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 bool DeleteDuplicatePortraitAlbum(const std::vector<std::string> &albumNames,
91 const std::vector<std::string> tagIds, std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb);
92 static int ExecuteSQL(std::shared_ptr<NativeRdb::RdbStore> rdbStore, const std::string& sql,
93 const std::vector<NativeRdb::ValueObject> &args = {});
94 static void UpdateAnalysisTotalTblStatus(std::shared_ptr<NativeRdb::RdbStore> rdbStore,
95 const std::vector<FileIdPair>& fileIdPair);
96 static void UpdateFaceAnalysisTblStatus(std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb);
97 static void DeleteExistingImageFaceData(std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb,
98 const std::vector<FileIdPair>& fileIdPair);
99 static std::vector<TagPairOpt> QueryTagInfo(std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb);
100 static void ParseFaceTagResultSet(const std::shared_ptr<NativeRdb::ResultSet>& resultSet,
101 TagPairOpt& tagPair);
102 static void UpdateGroupTagColumn(const std::vector<TagPairOpt>& updatedPairs,
103 std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb);
104 static void UpdateFaceGroupTagsUnion(std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb);
105 static void UpdateFaceGroupTagOfDualFrame(std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb);
106 static void UpdateTagPairs(std::vector<TagPairOpt>& updatedPairs, const std::string& newGroupTag,
107 const std::vector<std::string>& tagIds);
108 static void UpdateGroupTags(std::vector<TagPairOpt>& updatedPairs,
109 const std::unordered_map<std::string, std::vector<std::string>>& groupTagMap);
110 static void UpdateAssociateFileId(std::shared_ptr<NativeRdb::RdbStore> rdbStore,
111 const std::vector<FileInfo> &fileInfos);
112 static void BatchUpdatePhotosToLocal(std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb,
113 const std::vector<std::string> &inColumn);
114 static int32_t BatchInsert(std::shared_ptr<NativeRdb::RdbStore> rdbStore, const std::string &tableName,
115 std::vector<NativeRdb::ValuesBucket> &value, int64_t &rowNum);
116 static std::string CheckDbIntegrity(std::shared_ptr<NativeRdb::RdbStore> rdbStore, int32_t sceneCode,
117 const std::string &dbTag = "");
118 static int32_t QueryLocalNoAstcCount(std::shared_ptr<NativeRdb::RdbStore> rdbStore);
119 static int32_t QueryReadyAstcCount(std::shared_ptr<NativeRdb::RdbStore> rdbStore);
120 static std::unordered_map<int32_t, int32_t> QueryMediaTypeCount(
121 const std::shared_ptr<NativeRdb::RdbStore>& rdbStore, const std::string& querySql);
122 static std::shared_ptr<NativeRdb::ResultSet> QuerySql(std::shared_ptr<NativeRdb::RdbStore> rdbStore,
123 const std::string &querySql, const std::vector<NativeRdb::ValueObject> ¶ms);
124 template <typename T>
125 static std::string JoinValues(const std::vector<T>& values, std::string_view delimiter);
126 template <typename T>
127 static std::string JoinSQLValues(const std::vector<T>& values, std::string_view delimiter);
128 template <typename T>
129 static std::vector<T> LeftJoinValues(std::vector<T>& values,
130 std::string_view delimiter);
131 template <typename T>
132 struct always_false : std::false_type {};
133 template <typename T>
134 static std::optional<T> GetOptionalValue(const std::shared_ptr<NativeRdb::ResultSet> &resultSet,
135 const std::string &columnName);
136 static void UpdateBurstPhotos(const std::shared_ptr<NativeRdb::RdbStore> &rdbStore);
137
138 private:
139 static std::string CloudSyncTriggerFunc(const std::vector<std::string> &args);
140 static std::string IsCallerSelfFunc(const std::vector<std::string> &args);
141 static std::string PhotoAlbumNotifyFunc(const std::vector<std::string>& args);
142 static std::string BeginGenerateHighlightThumbnail(const std::vector<std::string>& args);
143 };
144
145 class RdbCallback : public NativeRdb::RdbOpenCallback {
146 public:
OnCreate(NativeRdb::RdbStore & rdb)147 virtual int32_t OnCreate(NativeRdb::RdbStore &rdb) override
148 {
149 return 0;
150 }
151
OnUpgrade(NativeRdb::RdbStore & rdb,int32_t oldVersion,int32_t newVersion)152 virtual int32_t OnUpgrade(NativeRdb::RdbStore &rdb, int32_t oldVersion,
153 int32_t newVersion) override
154 {
155 return 0;
156 }
157 };
158
159 template <typename T>
JoinSQLValues(const std::vector<T> & values,std::string_view delimiter)160 std::string BackupDatabaseUtils::JoinSQLValues(const std::vector<T>& values, std::string_view delimiter)
161 {
162 std::stringstream ss;
163 bool first = true;
164 for (const auto& value : values) {
165 if (!first) {
166 ss << delimiter;
167 }
168 first = false;
169 if constexpr (std::is_same_v<T, std::string>) {
170 ss << "'" << value << "'";
171 } else {
172 ss << std::to_string(value);
173 }
174 }
175 return ss.str();
176 }
177
178 template <typename T>
JoinValues(const std::vector<T> & values,std::string_view delimiter)179 std::string BackupDatabaseUtils::JoinValues(const std::vector<T>& values, std::string_view delimiter)
180 {
181 std::stringstream ss;
182 bool first = true;
183 for (const auto& value : values) {
184 if (!first) {
185 ss << delimiter;
186 }
187 first = false;
188 if constexpr (std::is_same_v<T, std::string>) {
189 ss << value;
190 } else {
191 ss << std::to_string(value);
192 }
193 }
194 return ss.str();
195 }
196
197 template <typename T>
LeftJoinValues(std::vector<T> & values,std::string_view delimiter)198 std::vector<T> BackupDatabaseUtils::LeftJoinValues(std::vector<T>& values, std::string_view delimiter)
199 {
200 for (auto& value : values) {
201 value.insert(0, delimiter);
202 }
203 return values;
204 }
205
206 template<typename T>
GetOptionalValue(const std::shared_ptr<NativeRdb::ResultSet> & resultSet,const std::string & columnName)207 std::optional<T> BackupDatabaseUtils::GetOptionalValue(const std::shared_ptr<NativeRdb::ResultSet> &resultSet,
208 const std::string &columnName)
209 {
210 int32_t columnIndex;
211 int32_t err = resultSet->GetColumnIndex(columnName, columnIndex);
212 if (err != E_OK) {
213 return std::nullopt;
214 }
215
216 bool isNull = false;
217 int32_t errCode = resultSet->IsColumnNull(columnIndex, isNull);
218 if (errCode || isNull) {
219 return std::nullopt;
220 }
221
222 T value;
223 if constexpr (std::is_same_v<T, int32_t>) {
224 errCode = resultSet->GetInt(columnIndex, value);
225 } else if constexpr (std::is_same_v<T, int64_t>) {
226 errCode = resultSet->GetLong(columnIndex, value);
227 } else if constexpr (std::is_same_v<T, double>) {
228 errCode = resultSet->GetDouble(columnIndex, value);
229 } else if constexpr (std::is_same_v<T, std::string>) {
230 errCode = resultSet->GetString(columnIndex, value);
231 } else if constexpr (std::is_same_v<T, std::vector<uint8_t>>) {
232 errCode = resultSet->GetBlob(columnIndex, value);
233 } else {
234 static_assert(always_false<T>::value, "Unsupported type for GetOptionalValue");
235 }
236
237 return errCode ? std::nullopt : std::optional<T>(value);
238 }
239 } // namespace Media
240 } // namespace OHOS
241
242 #endif // BACKUP_DATABASE_UTILS_H