1 /*
2  * Copyright (C) 2024 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 #ifndef OHOS_MEDIA_BACKUP_DATABASE_HELPER
16 #define OHOS_MEDIA_BACKUP_DATABASE_HELPER
17 
18 #include <string>
19 
20 #include "backup_const.h"
21 #include "rdb_store.h"
22 
23 namespace OHOS::Media {
24 class BackupDatabaseHelper {
25 public:
26     enum DbType {
27         DEFAULT = 0,
28         EXTERNAL,
29         PHOTO_CACHE,
30         VIDEO_CACHE,
31         PHOTO_SD_CACHE,
32         VIDEO_SD_CACHE,
33     };
34 
35     struct DbInfo {
36         std::string name;
37         std::string path;
38         std::shared_ptr<NativeRdb::RdbStore> rdbStore {nullptr};
39         DbInfo() = default;
DbInfoDbInfo40         DbInfo(const std::string &name, const std::string &path) : name(name), path(path) {}
DbInfoDbInfo41         DbInfo(std::shared_ptr<NativeRdb::RdbStore> rdbStore) : rdbStore(rdbStore) {}
42     };
43 
44     struct FileQueryInfo {
45         int32_t dbType {DbType::DEFAULT};
46         std::string tableName;
47         std::string columnName;
48         std::string path;
49         FileQueryInfo() = default;
FileQueryInfoFileQueryInfo50         FileQueryInfo(int32_t dbType, const std::string &tableName, const std::string &columnName,
51             const std::string &path) : dbType(dbType), tableName(tableName), columnName(columnName), path(path) {}
52     };
53 
54 public:
55     void Init(int32_t sceneCode, bool shouldIncludeSd, const std::string &prefix);
56     void InitDb(int32_t dbType, const std::string &prefix);
57     void InitDb(const std::vector<int32_t> &dbTypeList, const std::string &prefix);
58     void AddDb(int32_t dbType, std::shared_ptr<NativeRdb::RdbStore> rdbStore);
59     void IsFileExist(int32_t sceneCode, const FileInfo &fileInfo, int32_t &dbType, int32_t &dbStatus,
60         int32_t &fileStatus);
61 
62 private:
63     bool HasDb(int32_t dbType);
64     void GetFileQueryInfo(int32_t sceneCode, const FileInfo &fileInfo, FileQueryInfo &fileQueryInfo);
65 
66     std::unordered_map<int32_t, DbInfo> dbInfoMap_;
67 
68     const std::unordered_map<int32_t, DbInfo> DB_INFO_MAP = {
69         { DbType::PHOTO_CACHE, DbInfo("photo_Cache.db", "/storage/emulated/0/photo_Cache.db") },
70         { DbType::VIDEO_CACHE, DbInfo("video_Cache.db", "/storage/emulated/0/video_Cache.db") },
71         { DbType::PHOTO_SD_CACHE, DbInfo("photo_sd_Cache.db", "/photo_sd_Cache.db") },
72         { DbType::VIDEO_SD_CACHE, DbInfo("video_sd_Cache.db", "/video_sd_Cache.db") },
73     };
74 };
75 } // namespace OHOS::Media
76 #endif // OHOS_MEDIA_BACKUP_DATABASE_HELPER