1 /*
2  * Copyright (c) 2022 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 "bms_param.h"
17 
18 #include "app_log_wrapper.h"
19 
20 namespace OHOS {
21 namespace AppExecFwk {
22 namespace {
23 const std::string BMS_PARAM_TABLE_NAME = "bms_param";
24 }
BmsParam()25 BmsParam::BmsParam()
26 {
27     APP_LOGD("BmsParam instance is created");
28     BmsRdbConfig bmsRdbConfig;
29     bmsRdbConfig.dbName = ServiceConstants::BUNDLE_RDB_NAME;
30     bmsRdbConfig.tableName = BMS_PARAM_TABLE_NAME;
31     rdbDataManager_ = std::make_shared<RdbDataManager>(bmsRdbConfig);
32     rdbDataManager_->CreateTable();
33 }
34 
~BmsParam()35 BmsParam::~BmsParam()
36 {
37     APP_LOGD("BmsParam instance is destroyed");
38 }
39 
GetBmsParam(const std::string & key,std::string & value)40 bool BmsParam::GetBmsParam(const std::string &key, std::string &value)
41 {
42     if (key.empty()) {
43         APP_LOGE("key is empty");
44         return false;
45     }
46 
47     if (rdbDataManager_ == nullptr) {
48         APP_LOGE("rdbDataManager is null");
49         return false;
50     }
51 
52     return rdbDataManager_->QueryData(key, value);
53 }
54 
SaveBmsParam(const std::string & paramKeyInfo,const std::string & paramValueInfo)55 bool BmsParam::SaveBmsParam(const std::string &paramKeyInfo, const std::string &paramValueInfo)
56 {
57     if (paramKeyInfo.empty() || paramValueInfo.empty()) {
58         APP_LOGE("key or value is empty");
59         return false;
60     }
61 
62     if (rdbDataManager_ == nullptr) {
63         APP_LOGE("rdbDataManager is null");
64         return false;
65     }
66 
67     return rdbDataManager_->InsertData(paramKeyInfo, paramValueInfo);
68 }
69 
DeleteBmsParam(const std::string & key)70 bool BmsParam::DeleteBmsParam(const std::string &key)
71 {
72     if (key.empty()) {
73         APP_LOGE("key is empty");
74         return false;
75     }
76 
77     if (rdbDataManager_ == nullptr) {
78         APP_LOGE("rdbDataManager is null");
79         return false;
80     }
81 
82     return rdbDataManager_->DeleteData(key);
83 }
84 
85 }  // namespace AppExecFwk
86 }  // namespace OHOS