1 /*
2 * Copyright (c) 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 #include "patch_data_storage_rdb.h"
17
18 #include "app_log_tag_wrapper.h"
19
20 namespace OHOS {
21 namespace AppExecFwk {
22 namespace {
23 constexpr const char* BUNDLE_RDB_TABLE_NAME = "install_patch_bundle";
24 } // namespace
25
PatchDataStorageRdb()26 PatchDataStorageRdb::PatchDataStorageRdb()
27 {
28 APP_LOGI("PatchDataStorageRdb instance is create");
29 BmsRdbConfig bmsRdbConfig;
30 bmsRdbConfig.dbName = ServiceConstants::BUNDLE_RDB_NAME;
31 bmsRdbConfig.tableName = BUNDLE_RDB_TABLE_NAME;
32 rdbDataManager_ = std::make_shared<RdbDataManager>(bmsRdbConfig);
33 rdbDataManager_->CreateTable();
34 }
35
~PatchDataStorageRdb()36 PatchDataStorageRdb::~PatchDataStorageRdb()
37 {
38 APP_LOGI("PatchDataStorageRdb instance is destroyed");
39 }
40
GetStoragePatchInfo(const std::string & bundleName,InnerPatchInfo & info)41 bool PatchDataStorageRdb::GetStoragePatchInfo(const std::string &bundleName, InnerPatchInfo &info)
42 {
43 if (rdbDataManager_ == nullptr) {
44 APP_LOGE("rdbDataManager is null");
45 return false;
46 }
47 if (bundleName.empty()) {
48 APP_LOGE("bundleName is empty");
49 return false;
50 }
51 std::string value;
52 if (!rdbDataManager_->QueryData(bundleName, value) || value.empty()) {
53 return false;
54 }
55 if (!info.FromJson(value)) {
56 APP_LOGE("Error bundleName: %{public}s", bundleName.c_str());
57 rdbDataManager_->DeleteData(bundleName);
58 return false;
59 }
60 APP_LOGI("get patchInfo success, bundleName: %{public}s", bundleName.c_str());
61 return true;
62 }
63
SaveStoragePatchInfo(const std::string & bundleName,const InnerPatchInfo & InnerPatchInfo)64 bool PatchDataStorageRdb::SaveStoragePatchInfo(const std::string &bundleName, const InnerPatchInfo &InnerPatchInfo)
65 {
66 if (rdbDataManager_ == nullptr) {
67 APP_LOGE("rdbDataManager is null");
68 return false;
69 }
70 if (bundleName.empty()) {
71 APP_LOGE("bundleName is empty");
72 return false;
73 }
74 std::string value = InnerPatchInfo.ToString();
75 if (value.empty()) {
76 APP_LOGE("value is empty");
77 return false;
78 }
79 APP_LOGI("insertData, key: %{public}s, value: %{public}s", bundleName.c_str(), value.c_str());
80 return rdbDataManager_->InsertData(bundleName, value);
81 }
82
DeleteStoragePatchInfo(const std::string & bundleName)83 bool PatchDataStorageRdb::DeleteStoragePatchInfo(const std::string &bundleName)
84 {
85 if (rdbDataManager_ == nullptr) {
86 APP_LOGE("rdbDataManager is null");
87 return false;
88 }
89 if (bundleName.empty()) {
90 APP_LOGE("bundleName is empty");
91 return false;
92 }
93 APP_LOGI("deleteData, key: %{public}s", bundleName.c_str());
94 return rdbDataManager_->DeleteData(bundleName);
95 }
96 } // namespace AppExecFwk
97 } // namespace OHOS
98