1 /*
2  * Copyright (c) 2023 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 "firmware_component_operator.h"
17 
18 #include <memory>
19 
20 namespace OHOS {
21 namespace UpdateEngine {
UpdateProgressByUrl(const std::string & url,UpgradeStatus status,int32_t progress)22 bool FirmwareComponentOperator::UpdateProgressByUrl(const std::string &url, UpgradeStatus status, int32_t progress)
23 {
24     NativeRdb::ValuesBucket values;
25     values.PutInt(COLUMN_COMPONENT_STATUS, CAST_INT(status));
26     values.PutInt(COLUMN_COMPONENT_PROGRESS, progress);
27 
28     OHOS::NativeRdb::RdbPredicates predicates(GetTableName());
29     predicates.EqualTo(COLUMN_COMPONENT_DOWNLOAD_URL, url);
30     return TableBaseOperator::Update(values, predicates);
31 }
32 
UpdateUrlByVersionId(const std::string & versionId,const std::string & url)33 bool FirmwareComponentOperator::UpdateUrlByVersionId(const std::string &versionId, const std::string &url)
34 {
35     NativeRdb::ValuesBucket values;
36     values.PutString(COLUMN_COMPONENT_DOWNLOAD_URL, url);
37 
38     OHOS::NativeRdb::RdbPredicates predicates(GetTableName());
39     predicates.EqualTo(COLUMN_COMPONENT_VERSION_ID, versionId);
40     return TableBaseOperator::Update(values, predicates);
41 }
42 
QueryByVersionId(const std::string & versionId,FirmwareComponent & component)43 bool FirmwareComponentOperator::QueryByVersionId(const std::string &versionId, FirmwareComponent &component)
44 {
45     std::vector<FirmwareComponent> components;
46     OHOS::NativeRdb::RdbPredicates predicates(GetTableName());
47     predicates.EqualTo(COLUMN_COMPONENT_VERSION_ID, versionId);
48     if (Query(components, predicates) && !components.empty()) {
49         component = components[0];
50         return true;
51     }
52     return false;
53 }
54 
QueryByUrl(const std::string & url,FirmwareComponent & component)55 bool FirmwareComponentOperator::QueryByUrl(const std::string &url, FirmwareComponent &component)
56 {
57     std::vector<FirmwareComponent> components;
58     OHOS::NativeRdb::RdbPredicates predicates(GetTableName());
59     predicates.EqualTo(COLUMN_COMPONENT_DOWNLOAD_URL, url);
60     if (Query(components, predicates) && !components.empty()) {
61         component = components[0];
62         return true;
63     }
64     return false;
65 }
66 } // namespace UpdateEngine
67 } // namespace OHOS
68