• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "product_info_dao.h"
17 
18 #include "distributed_device_profile_constants.h"
19 #include "distributed_device_profile_errors.h"
20 #include "distributed_device_profile_log.h"
21 #include "dp_services_constants.h"
22 #include "profile_data_rdb_adapter.h"
23 
24 namespace OHOS {
25 namespace DistributedDeviceProfile {
26 IMPLEMENT_SINGLE_INSTANCE(ProductInfoDao);
27 namespace {
28     const std::string TAG = "ProductInfoDao";
29 }
30 
Init()31 int32_t ProductInfoDao::Init()
32 {
33     std::lock_guard<std::mutex> lock(rdbMutex_);
34     int32_t ret = RET_INIT;
35     if (!ProfileDataRdbAdapter::GetInstance().IsInit()) {
36         ret = ProfileDataRdbAdapter::GetInstance().Init();
37         if (ret != DP_SUCCESS) {
38             HILOGE("ProfileDataRdbAdapter Init failed");
39             return DP_INIT_DB_FAILED;
40         }
41     }
42     CreateTable();
43     CreateIndex();
44     HILOGI("end!");
45     return DP_SUCCESS;
46 }
47 
UnInit()48 int32_t ProductInfoDao::UnInit()
49 {
50     std::lock_guard<std::mutex> lock(rdbMutex_);
51     if (!ProfileDataRdbAdapter::GetInstance().IsInit()) {
52         HILOGE("ProfileDataRdbAdapter is UnInit");
53         return DP_SUCCESS;
54     }
55     int32_t ret = ProfileDataRdbAdapter::GetInstance().UnInit();
56     if (ret != DP_SUCCESS) {
57         HILOGE("ProfileDataRdbAdapter UnInit failed");
58         return DP_UNINIT_FAIL;
59     }
60     HILOGI("end!");
61     return DP_SUCCESS;
62 }
63 
PutProductInfo(const ProductInfo & productInfo)64 int32_t ProductInfoDao::PutProductInfo(const ProductInfo& productInfo)
65 {
66     ValuesBucket values;
67     ProductInfoToEntries(productInfo, values);
68     int64_t rowId = ROWID_INIT;
69     int32_t ret = RET_INIT;
70     {
71         std::lock_guard<std::mutex> lock(rdbMutex_);
72         ret = ProfileDataRdbAdapter::GetInstance().Put(rowId, PRODUCT_INFO_TABLE, values);
73         if (ret != DP_SUCCESS) {
74             HILOGE("%{public}s insert failed", PRODUCT_INFO_TABLE.c_str());
75             return DP_PUT_PRODUCT_INFO_FAIL;
76         }
77     }
78     HILOGI("end!");
79     return DP_SUCCESS;
80 }
81 
GetProductInfos(const std::vector<std::string> & productIds,std::vector<ProductInfo> & productInfos)82 int32_t ProductInfoDao::GetProductInfos(const std::vector<std::string>& productIds,
83     std::vector<ProductInfo>& productInfos)
84 {
85     std::string sql;
86     std::vector<ValueObject> condition;
87     if (!CreateQuerySqlAndCondition(productIds, sql, condition)) {
88         HILOGE("invalid params: productIds.size=%{public}zu", productIds.size());
89         return DP_INVALID_PARAMS;
90     }
91     std::shared_ptr<ResultSet> resultSet = ProfileDataRdbAdapter::GetInstance().Get(sql, condition);
92     if (resultSet == nullptr) {
93         HILOGE("resultSet is nullptr");
94         return DP_GET_RESULTSET_FAIL;
95     }
96     int32_t rowCount = ROWCOUNT_INIT;
97     resultSet->GetRowCount(rowCount);
98     if (rowCount == 0) {
99         HILOGE("by condition not find data");
100         resultSet->Close();
101         return DP_NOT_FIND_DATA;
102     }
103     while (resultSet->GoToNextRow() == DP_SUCCESS) {
104         ProductInfo productInfo;
105         ConvertToProductInfo(resultSet, productInfo);
106         productInfos.emplace_back(productInfo);
107     }
108     resultSet->Close();
109     if (productInfos.empty()) {
110         return DP_NOT_FIND_DATA;
111     }
112     HILOGI("end!");
113     return DP_SUCCESS;
114 }
115 
DeleteProductInfo(const ProductInfo & productInfo)116 int32_t ProductInfoDao::DeleteProductInfo(const ProductInfo& productInfo)
117 {
118     {
119         std::lock_guard<std::mutex> lock(rdbMutex_);
120         int32_t deleteRows = DELETEROWS_INIT;
121         int32_t ret = ProfileDataRdbAdapter::GetInstance().Delete(deleteRows, PRODUCT_INFO_TABLE,
122             PRODUCT_ID + " = ?", std::vector<ValueObject>{ ValueObject(productInfo.GetProductId()) });
123         if (ret != DP_SUCCESS) {
124             HILOGE("delete %{public}s data failed", PRODUCT_INFO_TABLE.c_str());
125             return DP_DEL_PRODUCT_INFO_FAIL;
126         }
127     }
128     HILOGI("end!");
129     return DP_SUCCESS;
130 }
131 
UpdateProductInfo(const ProductInfo & productInfo)132 int32_t ProductInfoDao::UpdateProductInfo(const ProductInfo& productInfo)
133 {
134     ValuesBucket values;
135     ProductInfoToEntries(productInfo, values);
136     int32_t changeRowCnt = CHANGEROWCNT_INIT;
137     {
138         std::lock_guard<std::mutex> lock(rdbMutex_);
139         int32_t ret = ProfileDataRdbAdapter::GetInstance().Update(changeRowCnt, PRODUCT_INFO_TABLE, values,
140             PRODUCT_ID + " = ?", std::vector<ValueObject>{ ValueObject(productInfo.GetProductId()) });
141         if (ret != DP_SUCCESS) {
142             HILOGE("Update %{public}s table failed", PRODUCT_INFO_TABLE.c_str());
143             return DP_UPDATE_PRODUCT_INFO_FAIL;
144         }
145     }
146     HILOGI("end!");
147     return DP_SUCCESS;
148 }
149 
CreateTable()150 int32_t ProductInfoDao::CreateTable()
151 {
152     int32_t ret = ProfileDataRdbAdapter::GetInstance().CreateTable(CREATE_PRODUCT_INFO_TABLE_SQL);
153     if (ret != DP_SUCCESS) {
154         HILOGE("%{public}s create failed", PRODUCT_INFO_TABLE.c_str());
155         return DP_CREATE_TABLE_FAIL;
156     }
157     return DP_SUCCESS;
158 }
159 
CreateIndex()160 int32_t ProductInfoDao::CreateIndex()
161 {
162     return DP_SUCCESS;
163 }
164 
CreateQuerySqlAndCondition(const std::vector<std::string> & productIds,std::string & sql,std::vector<ValueObject> & condition)165 bool ProductInfoDao::CreateQuerySqlAndCondition(const std::vector<std::string>& productIds,
166     std::string& sql, std::vector<ValueObject>& condition)
167 {
168     sql = SELECT_PRODUCT_INFO_TABLE;
169     bool flag = false;
170     if (!productIds.empty()) {
171         sql += PRODUCT_ID + " IN(";
172         for (const auto& prodId : productIds) {
173             sql += "?,";
174             condition.emplace_back(ValueObject(prodId));
175         }
176         sql.erase(sql.end() - 1, sql.end());
177         sql += ")";
178         flag = true;
179     }
180     return flag;
181 }
182 
ProductInfoToEntries(const ProductInfo & productInfo,ValuesBucket & values)183 int32_t ProductInfoDao::ProductInfoToEntries(const ProductInfo& productInfo, ValuesBucket& values)
184 {
185     values.PutString(PRODUCT_ID, productInfo.GetProductId());
186     values.PutString(DEVICE_MODEL, productInfo.GetModel());
187     values.PutString(PRODUCT_NAME, productInfo.GetProductName());
188     values.PutString(PRODUCT_SHORT_NAME, productInfo.GetProductShortName());
189     values.PutString(IMAGE_VERSION, productInfo.GetImageVersion());
190     return DP_SUCCESS;
191 }
192 
ConvertToProductInfo(std::shared_ptr<ResultSet> resultSet,ProductInfo & productInfo)193 int32_t ProductInfoDao::ConvertToProductInfo(std::shared_ptr<ResultSet> resultSet, ProductInfo& productInfo)
194 {
195     if (resultSet == nullptr) {
196         HILOGE("resultSet is nullptr");
197         return DP_GET_RESULTSET_FAIL;
198     }
199     RowEntity rowEntity;
200     if (resultSet->GetRow(rowEntity) != DP_SUCCESS) {
201         HILOGE("get resultSet failed");
202         return DP_GET_RESULTSET_FAIL;
203     }
204     productInfo.SetProductId(rowEntity.Get(PRODUCT_ID));
205     productInfo.SetModel(rowEntity.Get(DEVICE_MODEL));
206     productInfo.SetProductName(rowEntity.Get(PRODUCT_NAME));
207     productInfo.SetProductShortName(rowEntity.Get(PRODUCT_SHORT_NAME));
208     productInfo.SetImageVersion(rowEntity.Get(IMAGE_VERSION));
209     return DP_SUCCESS;
210 }
211 } // namespace DistributedDeviceProfile
212 } // namespace OHOS
213