1 /*
2 * Copyright (C) 2023 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 void CheckDbIntegrity(std::shared_ptr<NativeRdb::RdbStore> rdbStore, int32_t sceneCode,
114 const std::string &dbTag = "");
115
116 template <typename T>
117 static std::string JoinValues(const std::vector<T>& values, std::string_view delimiter);
118 template <typename T>
119 static std::string JoinSQLValues(const std::vector<T>& values, std::string_view delimiter);
120
121 template <typename T>
122 struct always_false : std::false_type {};
123 template <typename T>
124 static std::optional<T> GetOptionalValue(const std::shared_ptr<NativeRdb::ResultSet> &resultSet,
125 const std::string &columnName);
126 static int32_t BatchInsert(std::shared_ptr<NativeRdb::RdbStore> rdbStore, const std::string &tableName,
127 std::vector<NativeRdb::ValuesBucket> &value, int64_t &rowNum);
128
129 private:
130 static std::string CloudSyncTriggerFunc(const std::vector<std::string> &args);
131 static std::string IsCallerSelfFunc(const std::vector<std::string> &args);
132 };
133
134 class RdbCallback : public NativeRdb::RdbOpenCallback {
135 public:
OnCreate(NativeRdb::RdbStore & rdb)136 virtual int32_t OnCreate(NativeRdb::RdbStore &rdb) override
137 {
138 return 0;
139 }
140
OnUpgrade(NativeRdb::RdbStore & rdb,int32_t oldVersion,int32_t newVersion)141 virtual int32_t OnUpgrade(NativeRdb::RdbStore &rdb, int32_t oldVersion,
142 int32_t newVersion) override
143 {
144 return 0;
145 }
146 };
147
148 template <typename T>
JoinSQLValues(const std::vector<T> & values,std::string_view delimiter)149 std::string BackupDatabaseUtils::JoinSQLValues(const std::vector<T>& values, std::string_view delimiter)
150 {
151 std::stringstream ss;
152 bool first = true;
153 for (const auto& value : values) {
154 if (!first) {
155 ss << delimiter;
156 }
157 first = false;
158 if constexpr (std::is_same_v<T, std::string>) {
159 ss << "'" << value << "'";
160 } else {
161 ss << std::to_string(value);
162 }
163 }
164 return ss.str();
165 }
166
167 template <typename T>
JoinValues(const std::vector<T> & values,std::string_view delimiter)168 std::string BackupDatabaseUtils::JoinValues(const std::vector<T>& values, std::string_view delimiter)
169 {
170 std::stringstream ss;
171 bool first = true;
172 for (const auto& value : values) {
173 if (!first) {
174 ss << delimiter;
175 }
176 first = false;
177 if constexpr (std::is_same_v<T, std::string>) {
178 ss << value;
179 } else {
180 ss << std::to_string(value);
181 }
182 }
183 return ss.str();
184 }
185
186 template<typename T>
GetOptionalValue(const std::shared_ptr<NativeRdb::ResultSet> & resultSet,const std::string & columnName)187 std::optional<T> BackupDatabaseUtils::GetOptionalValue(const std::shared_ptr<NativeRdb::ResultSet> &resultSet,
188 const std::string &columnName)
189 {
190 int32_t columnIndex;
191 int32_t err = resultSet->GetColumnIndex(columnName, columnIndex);
192 if (err != E_OK) {
193 return std::nullopt;
194 }
195
196 bool isNull = false;
197 int32_t errCode = resultSet->IsColumnNull(columnIndex, isNull);
198 if (errCode || isNull) {
199 return std::nullopt;
200 }
201
202 T value;
203 if constexpr (std::is_same_v<T, int32_t>) {
204 errCode = resultSet->GetInt(columnIndex, value);
205 } else if constexpr (std::is_same_v<T, int64_t>) {
206 errCode = resultSet->GetLong(columnIndex, value);
207 } else if constexpr (std::is_same_v<T, double>) {
208 errCode = resultSet->GetDouble(columnIndex, value);
209 } else if constexpr (std::is_same_v<T, std::string>) {
210 errCode = resultSet->GetString(columnIndex, value);
211 } else {
212 static_assert(always_false<T>::value, "Unsupported type for GetOptionalValue");
213 }
214
215 return errCode ? std::nullopt : std::optional<T>(value);
216 }
217 } // namespace Media
218 } // namespace OHOS
219
220 #endif // BACKUP_DATABASE_UTILS_H