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 
16 #include "uninstall_data_mgr_storage_rdb.h"
17 
18 #include "app_log_wrapper.h"
19 #include "scope_guard.h"
20 
21 namespace OHOS {
22 namespace AppExecFwk {
23 namespace {
24 const char* const UNINSTALL_BUNDLE_TABLE_NAME = "uninstall_bundle_info";
25 const char* const BUNDLENAME = "BUNDLENAME";
26 const char* const UNINSTALL_BUNDLE_INFO = "UNINSTALL_BUNDLE_INFO";
27 const int8_t KEY_INDEX = 0;
28 const int8_t BUNDLE_INFO_INDEX = 1;
29 }
UninstallDataMgrStorageRdb()30 UninstallDataMgrStorageRdb::UninstallDataMgrStorageRdb()
31 {
32     APP_LOGI("created");
33     BmsRdbConfig bmsRdbConfig;
34     bmsRdbConfig.dbName = ServiceConstants::BUNDLE_RDB_NAME;
35     bmsRdbConfig.tableName = UNINSTALL_BUNDLE_TABLE_NAME;
36     bmsRdbConfig.createTableSql = std::string(
37         "CREATE TABLE IF NOT EXISTS "
38         + std::string{ UNINSTALL_BUNDLE_TABLE_NAME }
39         + "(BUNDLENAME TEXT PRIMARY KEY NOT NULL, "
40         + "UNINSTALL_BUNDLE_INFO TEXT NOT NULL);");
41     rdbDataManager_ = std::make_shared<RdbDataManager>(bmsRdbConfig);
42     rdbDataManager_->CreateTable();
43 }
44 
~UninstallDataMgrStorageRdb()45 UninstallDataMgrStorageRdb::~UninstallDataMgrStorageRdb()
46 {
47     APP_LOGI("~");
48 }
49 
UpdateUninstallBundleInfo(const std::string & bundleName,const UninstallBundleInfo & uninstallbundleInfo)50 bool UninstallDataMgrStorageRdb::UpdateUninstallBundleInfo(const std::string &bundleName,
51     const UninstallBundleInfo &uninstallbundleInfo)
52 {
53     APP_LOGI("update -n %{public}s", bundleName.c_str());
54     if (rdbDataManager_ == nullptr) {
55         APP_LOGE("rdbDataManager is null");
56         return false;
57     }
58     if (bundleName.empty()) {
59         APP_LOGE("param error");
60         return false;
61     }
62     NativeRdb::ValuesBucket valuesBucket;
63     valuesBucket.PutString(BUNDLENAME, bundleName);
64     valuesBucket.PutString(UNINSTALL_BUNDLE_INFO, uninstallbundleInfo.ToString());
65 
66     return rdbDataManager_->InsertData(valuesBucket);
67 }
68 
GetUninstallBundleInfo(const std::string & bundleName,UninstallBundleInfo & uninstallbundleInfo)69 bool UninstallDataMgrStorageRdb::GetUninstallBundleInfo(const std::string &bundleName,
70     UninstallBundleInfo &uninstallbundleInfo)
71 {
72     if (rdbDataManager_ == nullptr) {
73         APP_LOGE("rdbDataManager is null");
74         return false;
75     }
76     if (bundleName.empty()) {
77         APP_LOGE("param error");
78         return false;
79     }
80     NativeRdb::AbsRdbPredicates absRdbPredicates(UNINSTALL_BUNDLE_TABLE_NAME);
81     absRdbPredicates.EqualTo(BUNDLENAME, bundleName);
82     auto absSharedResultSet = rdbDataManager_->QueryData(absRdbPredicates);
83     if (absSharedResultSet == nullptr) {
84         return false;
85     }
86     ScopeGuard stateGuard([absSharedResultSet] { absSharedResultSet->Close(); });
87     auto ret = absSharedResultSet->GoToFirstRow();
88     if (ret != NativeRdb::E_OK) {
89         APP_LOGD("GoToFirstRow failed, ret: %{public}d", ret);
90         return false;
91     }
92     std::string value;
93     ret = absSharedResultSet->GetString(BUNDLE_INFO_INDEX, value);
94     if (ret != NativeRdb::E_OK) {
95         APP_LOGE("GetString failed, ret: %{public}d", ret);
96         return false;
97     }
98     nlohmann::json jsonObject = nlohmann::json::parse(value, nullptr, false);
99     if (jsonObject.is_discarded()) {
100         APP_LOGE("error key");
101         return false;
102     }
103     from_json(jsonObject, uninstallbundleInfo);
104     return true;
105 }
106 
DeleteUninstallBundleInfo(const std::string & bundleName)107 bool UninstallDataMgrStorageRdb::DeleteUninstallBundleInfo(const std::string &bundleName)
108 {
109     if (rdbDataManager_ == nullptr) {
110         APP_LOGE("rdbDataManager is null");
111         return false;
112     }
113     if (bundleName.empty()) {
114         APP_LOGE("param error");
115         return false;
116     }
117     NativeRdb::AbsRdbPredicates absRdbPredicates(UNINSTALL_BUNDLE_TABLE_NAME);
118     absRdbPredicates.EqualTo(BUNDLENAME, bundleName);
119     return rdbDataManager_->DeleteData(absRdbPredicates);
120 }
121 }  // namespace AppExecFwk
122 }  // namespace OHOS
123