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 "first_install_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 FIRST_INSTALL_BUNDLE_TABLE_NAME = "first_install_bundle_info";
25 const char* const BUNDLE_NAME = "BUNDLE_NAME";
26 const char* const USER_ID = "USER_ID";
27
28 const char* const FIRST_INSTALL_BUNDLE_INFO = "FIRST_INSTALL_BUNDLE_INFO";
29 const int8_t BUNDLE_INFO_INDEX = 2;
30 }
FirstInstallDataMgrStorageRdb()31 FirstInstallDataMgrStorageRdb::FirstInstallDataMgrStorageRdb()
32 {
33 APP_LOGI("created");
34 BmsRdbConfig bmsRdbConfig;
35 bmsRdbConfig.dbName = ServiceConstants::BUNDLE_RDB_NAME;
36 bmsRdbConfig.tableName = FIRST_INSTALL_BUNDLE_TABLE_NAME;
37 bmsRdbConfig.createTableSql = std::string(
38 "CREATE TABLE IF NOT EXISTS "
39 + std::string{ FIRST_INSTALL_BUNDLE_TABLE_NAME }
40 + "(BUNDLE_NAME TEXT NOT NULL, "
41 + "USER_ID INTEGER NOT NULL, "
42 + "FIRST_INSTALL_BUNDLE_INFO TEXT NOT NULL, "
43 + "PRIMARY KEY (BUNDLE_NAME, USER_ID));");
44 rdbDataManager_ = std::make_shared<RdbDataManager>(bmsRdbConfig);
45 rdbDataManager_->CreateTable();
46 }
47
~FirstInstallDataMgrStorageRdb()48 FirstInstallDataMgrStorageRdb::~FirstInstallDataMgrStorageRdb()
49 {
50 APP_LOGI("~");
51 }
52
IsExistFirstInstallBundleInfo(const std::string & bundleName,int32_t userId)53 bool FirstInstallDataMgrStorageRdb::IsExistFirstInstallBundleInfo(const std::string &bundleName,
54 int32_t userId)
55 {
56 if (rdbDataManager_ == nullptr) {
57 APP_LOGE("rdbDataManager is null");
58 return false;
59 }
60 if (bundleName.empty()) {
61 APP_LOGE("bundleName is empty");
62 return false;
63 }
64 NativeRdb::AbsRdbPredicates absRdbPredicates(FIRST_INSTALL_BUNDLE_TABLE_NAME);
65 absRdbPredicates.EqualTo(BUNDLE_NAME, bundleName);
66 absRdbPredicates.EqualTo(USER_ID, userId);
67 auto absSharedResultSet = rdbDataManager_->QueryData(absRdbPredicates);
68 if (absSharedResultSet == nullptr) {
69 APP_LOGE("absSharedResultSet is null");
70 return false;
71 }
72 ScopeGuard stateGuard([absSharedResultSet] { absSharedResultSet->Close(); });
73 int32_t count = 0;
74 auto ret = absSharedResultSet->GetRowCount(count);
75 if (ret != NativeRdb::E_OK) {
76 APP_LOGE("GetRowCount failed, ret: %{public}d", ret);
77 return false;
78 }
79 if (count == 0) {
80 APP_LOGD("GetRowCount count is 0");
81 return false;
82 }
83 return true;
84 }
85
AddFirstInstallBundleInfo(const std::string & bundleName,int32_t userId,const FirstInstallBundleInfo & firstInstallBundleInfo)86 bool FirstInstallDataMgrStorageRdb::AddFirstInstallBundleInfo(const std::string &bundleName, int32_t userId,
87 const FirstInstallBundleInfo &firstInstallBundleInfo)
88 {
89 if (rdbDataManager_ == nullptr) {
90 APP_LOGE("rdbDataManager is null");
91 return false;
92 }
93 if (bundleName.empty()) {
94 APP_LOGE("bundleName is empty");
95 return false;
96 }
97 NativeRdb::ValuesBucket valuesBucket;
98 valuesBucket.PutString(BUNDLE_NAME, bundleName);
99 valuesBucket.PutInt(USER_ID, userId);
100 valuesBucket.PutString(FIRST_INSTALL_BUNDLE_INFO, firstInstallBundleInfo.ToString());
101 return rdbDataManager_->InsertData(valuesBucket);
102 }
103
GetFirstInstallBundleInfo(const std::string & bundleName,int32_t userId,FirstInstallBundleInfo & firstInstallBundleInfo)104 bool FirstInstallDataMgrStorageRdb::GetFirstInstallBundleInfo(const std::string &bundleName, int32_t userId,
105 FirstInstallBundleInfo &firstInstallBundleInfo)
106 {
107 if (rdbDataManager_ == nullptr) {
108 APP_LOGE("rdbDataManager is null");
109 return false;
110 }
111 if (bundleName.empty()) {
112 APP_LOGE("bundleName is empty");
113 return false;
114 }
115 NativeRdb::AbsRdbPredicates absRdbPredicates(FIRST_INSTALL_BUNDLE_TABLE_NAME);
116 absRdbPredicates.EqualTo(BUNDLE_NAME, bundleName);
117 absRdbPredicates.EqualTo(USER_ID, userId);
118 auto absSharedResultSet = rdbDataManager_->QueryData(absRdbPredicates);
119 if (absSharedResultSet == nullptr) {
120 return false;
121 }
122 ScopeGuard stateGuard([absSharedResultSet] { absSharedResultSet->Close(); });
123 auto ret = absSharedResultSet->GoToFirstRow();
124 if (ret != NativeRdb::E_OK) {
125 APP_LOGD("GoToFirstRow failed, ret: %{public}d", ret);
126 return false;
127 }
128 std::string value;
129 ret = absSharedResultSet->GetString(BUNDLE_INFO_INDEX, value);
130 if (ret != NativeRdb::E_OK) {
131 APP_LOGE("GetString failed, ret: %{public}d", ret);
132 return false;
133 }
134 nlohmann::json jsonObject = nlohmann::json::parse(value, nullptr, false);
135 if (jsonObject.is_discarded()) {
136 APP_LOGE("error key");
137 return false;
138 }
139 from_json(jsonObject, firstInstallBundleInfo);
140 return true;
141 }
142
DeleteFirstInstallBundleInfo(int32_t userId)143 bool FirstInstallDataMgrStorageRdb::DeleteFirstInstallBundleInfo(int32_t userId)
144 {
145 if (rdbDataManager_ == nullptr) {
146 APP_LOGE("rdbDataManager is null");
147 return false;
148 }
149 NativeRdb::AbsRdbPredicates absRdbPredicates(FIRST_INSTALL_BUNDLE_TABLE_NAME);
150 absRdbPredicates.EqualTo(USER_ID, userId);
151 return rdbDataManager_->DeleteData(absRdbPredicates);
152 }
153 } // namespace AppExecFwk
154 } // namespace OHOS