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 QueryInt(std::shared_ptr<NativeRdb::RdbStore> rdbStore, const std::string &sql,
41 const std::string &column);
42 static int32_t Update(std::shared_ptr<NativeRdb::RdbStore> &rdbStore, int32_t &changeRows,
43 NativeRdb::ValuesBucket &valuesBucket, std::unique_ptr<NativeRdb::AbsRdbPredicates> &predicates);
44 static int32_t Delete(NativeRdb::AbsRdbPredicates &predicates, int32_t &changeRows,
45 std::shared_ptr<NativeRdb::RdbStore> &rdbStore);
46 static int32_t InitGarbageAlbum(std::shared_ptr<NativeRdb::RdbStore> rdbStore, std::set<std::string> &cacheSet,
47 std::unordered_map<std::string, std::string> &nickMap);
48 static int32_t QueryGalleryCloneCount(std::shared_ptr<NativeRdb::RdbStore> rdbStore);
49 static void QueryGalleryDuplicateDataCount(std::shared_ptr<NativeRdb::RdbStore> galleryRdb, int32_t &count,
50 int32_t &total);
51 static std::shared_ptr<NativeRdb::ResultSet> GetQueryResultSet(const std::shared_ptr<NativeRdb::RdbStore> &rdbStore,
52 const std::string &querySql, const std::vector<std::string> &sqlArgs = {});
53 static std::unordered_map<std::string, std::string> GetColumnInfoMap(
54 const std::shared_ptr<NativeRdb::RdbStore> &rdbStore, const std::string &tableName);
55 static void UpdateUniqueNumber(const std::shared_ptr<NativeRdb::RdbStore> &rdbStore, int32_t number,
56 const std::string &type);
57 static int32_t QueryUniqueNumber(const std::shared_ptr<NativeRdb::RdbStore> &rdbStore, const std::string &type);
58 static std::string GarbleInfoName(const std::string &infoName);
59 static void UpdateSelection(std::string &selection, const std::string &selectionToAdd, bool needWrap = false);
60 static void UpdateSdWhereClause(std::string &querySql, bool shouldIncludeSd);
61 static int32_t GetBlob(const std::string &columnName, std::shared_ptr<NativeRdb::ResultSet> resultSet,
62 std::vector<uint8_t> &blobVal);
63 static std::string GetLandmarksStr(const std::string &columnName, std::shared_ptr<NativeRdb::ResultSet> resultSet);
64 static std::string GetLandmarksStr(const std::vector<uint8_t> &bytes);
65 static uint32_t GetUint32ValFromBytes(const std::vector<uint8_t> &bytes, size_t start);
66 static void UpdateAnalysisTotalStatus(std::shared_ptr<NativeRdb::RdbStore> rdbStore);
67 static void UpdateAnalysisFaceTagStatus(std::shared_ptr<NativeRdb::RdbStore> rdbStore);
68 static bool SetTagIdNew(PortraitAlbumInfo &portraitAlbumInfo,
69 std::unordered_map<std::string, std::string> &tagIdMap);
70 static bool SetLandmarks(FaceInfo &faceInfo, const std::unordered_map<std::string, FileInfo> &fileInfoMap);
71 static bool SetFileIdNew(FaceInfo &faceInfo, const std::unordered_map<std::string, FileInfo> &fileInfoMap);
72 static bool SetTagIdNew(FaceInfo &faceInfo, const std::unordered_map<std::string, std::string> &tagIdMap);
73 static bool SetAlbumIdNew(FaceInfo &faceInfo, const std::unordered_map<std::string, int32_t> &albumIdMap);
74 static void PrintErrorLog(const std::string &errorLog, int64_t start);
75 static float GetLandmarksScale(int32_t width, int32_t height);
76 static bool IsLandmarkValid(const FaceInfo &faceInfo, float landmarkX, float landmarkY);
77 static bool IsValInBound(float val, float minVal, float maxVal);
78 static void UpdateAssociateFileId(std::shared_ptr<NativeRdb::RdbStore> rdbStore,
79 const std::vector<FileInfo> &fileInfos);
80 static std::vector<std::pair<std::string, std::string>> GetColumnInfoPairs(
81 const std::shared_ptr<NativeRdb::RdbStore> &rdbStore, const std::string &tableName);
82 static std::vector<std::string> GetCommonColumnInfos(std::shared_ptr<NativeRdb::RdbStore> mediaRdb,
83 std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb, std::string tableName);
84 static std::vector<std::string> filterColumns(const std::vector<std::string>& allColumns,
85 const std::vector<std::string>& excludedColumns);
86 static std::vector<FileIdPair> CollectFileIdPairs(const std::vector<FileInfo>& fileInfos);
87 static std::pair<std::vector<int32_t>, std::vector<int32_t>> UnzipFileIdPairs(const std::vector<FileIdPair>& pairs);
88 static void UpdateAnalysisPhotoMapStatus(std::shared_ptr<NativeRdb::RdbStore> rdbStore);
89 static std::vector<std::string> SplitString(const std::string& str, char delimiter);
90 static void PrintQuerySql(const std::string& querySql);
91 static bool DeleteDuplicatePortraitAlbum(const std::vector<std::string> &albumNames,
92 const std::vector<std::string> tagIds, std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb);
93 static int ExecuteSQL(std::shared_ptr<NativeRdb::RdbStore> rdbStore, const std::string& sql,
94 const std::vector<NativeRdb::ValueObject> &args = {});
95 static void UpdateAnalysisTotalTblStatus(std::shared_ptr<NativeRdb::RdbStore> rdbStore,
96 const std::vector<FileIdPair>& fileIdPair);
97 static std::string GetFileIdNewFilterClause(std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb,
98 const std::vector<FileIdPair>& fileIdPair);
99 static void UpdateFaceAnalysisTblStatus(std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb);
100 static void DeleteExistingImageFaceData(std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb,
101 const std::vector<FileIdPair>& fileIdPair);
102 static std::vector<TagPairOpt> QueryTagInfo(std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb);
103 static void ParseFaceTagResultSet(const std::shared_ptr<NativeRdb::ResultSet>& resultSet,
104 TagPairOpt& tagPair);
105 static void UpdateGroupTagColumn(const std::vector<TagPairOpt>& updatedPairs,
106 std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb);
107 static void UpdateFaceGroupTagsUnion(std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb);
108 static void UpdateFaceGroupTagOfDualFrame(std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb);
109 static void UpdateTagPairs(std::vector<TagPairOpt>& updatedPairs, const std::string& newGroupTag,
110 const std::vector<std::string>& tagIds);
111 static void UpdateGroupTags(std::vector<TagPairOpt>& updatedPairs,
112 const std::unordered_map<std::string, std::vector<std::string>>& groupTagMap);
113 static int32_t BatchInsert(std::shared_ptr<NativeRdb::RdbStore> rdbStore, const std::string &tableName,
114 std::vector<NativeRdb::ValuesBucket> &value, int64_t &rowNum);
115 static std::string CheckDbIntegrity(std::shared_ptr<NativeRdb::RdbStore> rdbStore, int32_t sceneCode,
116 const std::string &dbTag = "");
117 static std::unordered_map<int32_t, int32_t> QueryMediaTypeCount(
118 const std::shared_ptr<NativeRdb::RdbStore>& rdbStore, const std::string& querySql);
119
120 template <typename T>
121 static std::string JoinValues(const std::vector<T>& values, std::string_view delimiter);
122 template <typename T>
123 static std::string JoinSQLValues(const std::vector<T>& values, std::string_view delimiter);
124
125 template <typename T>
126 struct always_false : std::false_type {};
127 template <typename T>
128 static std::optional<T> GetOptionalValue(const std::shared_ptr<NativeRdb::ResultSet> &resultSet,
129 const std::string &columnName);
130
131 private:
132 static std::string CloudSyncTriggerFunc(const std::vector<std::string> &args);
133 static std::string IsCallerSelfFunc(const std::vector<std::string> &args);
134 static std::string PhotoAlbumNotifyFunc(const std::vector<std::string>& args);
135 };
136
137 class RdbCallback : public NativeRdb::RdbOpenCallback {
138 public:
OnCreate(NativeRdb::RdbStore & rdb)139 virtual int32_t OnCreate(NativeRdb::RdbStore &rdb) override
140 {
141 return 0;
142 }
143
OnUpgrade(NativeRdb::RdbStore & rdb,int32_t oldVersion,int32_t newVersion)144 virtual int32_t OnUpgrade(NativeRdb::RdbStore &rdb, int32_t oldVersion,
145 int32_t newVersion) override
146 {
147 return 0;
148 }
149 };
150
151 template <typename T>
JoinSQLValues(const std::vector<T> & values,std::string_view delimiter)152 std::string BackupDatabaseUtils::JoinSQLValues(const std::vector<T>& values, std::string_view delimiter)
153 {
154 std::stringstream ss;
155 bool first = true;
156 for (const auto& value : values) {
157 if (!first) {
158 ss << delimiter;
159 }
160 first = false;
161 if constexpr (std::is_same_v<T, std::string>) {
162 ss << "'" << value << "'";
163 } else {
164 ss << std::to_string(value);
165 }
166 }
167 return ss.str();
168 }
169
170 template <typename T>
JoinValues(const std::vector<T> & values,std::string_view delimiter)171 std::string BackupDatabaseUtils::JoinValues(const std::vector<T>& values, std::string_view delimiter)
172 {
173 std::stringstream ss;
174 bool first = true;
175 for (const auto& value : values) {
176 if (!first) {
177 ss << delimiter;
178 }
179 first = false;
180 if constexpr (std::is_same_v<T, std::string>) {
181 ss << value;
182 } else {
183 ss << std::to_string(value);
184 }
185 }
186 return ss.str();
187 }
188
189 template<typename T>
GetOptionalValue(const std::shared_ptr<NativeRdb::ResultSet> & resultSet,const std::string & columnName)190 std::optional<T> BackupDatabaseUtils::GetOptionalValue(const std::shared_ptr<NativeRdb::ResultSet> &resultSet,
191 const std::string &columnName)
192 {
193 int32_t columnIndex;
194 int32_t err = resultSet->GetColumnIndex(columnName, columnIndex);
195 if (err != E_OK) {
196 return std::nullopt;
197 }
198
199 bool isNull = false;
200 int32_t errCode = resultSet->IsColumnNull(columnIndex, isNull);
201 if (errCode || isNull) {
202 return std::nullopt;
203 }
204
205 T value;
206 if constexpr (std::is_same_v<T, int32_t>) {
207 errCode = resultSet->GetInt(columnIndex, value);
208 } else if constexpr (std::is_same_v<T, int64_t>) {
209 errCode = resultSet->GetLong(columnIndex, value);
210 } else if constexpr (std::is_same_v<T, double>) {
211 errCode = resultSet->GetDouble(columnIndex, value);
212 } else if constexpr (std::is_same_v<T, std::string>) {
213 errCode = resultSet->GetString(columnIndex, value);
214 } else {
215 static_assert(always_false<T>::value, "Unsupported type for GetOptionalValue");
216 }
217
218 return errCode ? std::nullopt : std::optional<T>(value);
219 }
220 } // namespace Media
221 } // namespace OHOS
222
223 #endif // BACKUP_DATABASE_UTILS_H