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 #define LOG_TAG "TemplateData"
16 #include "template_data.h"
17 #include "log_print.h"
18 namespace OHOS::DataShare {
Marshal(DistributedData::Serializable::json & node) const19 bool TemplateNode::Marshal(DistributedData::Serializable::json &node) const
20 {
21     bool ret = SetValue(node[GET_NAME(predicates)], predicates);
22     ret = ret && SetValue(node[GET_NAME(scheduler)], scheduler);
23     return ret;
24 }
25 
Unmarshal(const DistributedData::Serializable::json & node)26 bool TemplateNode::Unmarshal(const DistributedData::Serializable::json &node)
27 {
28     bool ret = GetValue(node, GET_NAME(predicates), predicates);
29     return ret && GetValue(node, GET_NAME(scheduler), scheduler);
30 }
31 
TemplateNode(const Template & tpl)32 TemplateNode::TemplateNode(const Template &tpl) : scheduler(tpl.scheduler_)
33 {
34     for (auto &item:tpl.predicates_) {
35         predicates.emplace_back(item.key_, item.selectSql_);
36     }
37 }
38 
ToTemplate() const39 Template TemplateNode::ToTemplate() const
40 {
41     std::vector<PredicateTemplateNode> nodes;
42     for (const auto &predicate: predicates) {
43         nodes.emplace_back(predicate.key, predicate.selectSql);
44     }
45     return Template(nodes, scheduler);
46 }
47 
Marshal(DistributedData::Serializable::json & node) const48 bool TemplateRootNode::Marshal(DistributedData::Serializable::json &node) const
49 {
50     bool ret = SetValue(node[GET_NAME(uri)], uri);
51     ret = ret && SetValue(node[GET_NAME(bundleName)], bundleName);
52     ret = ret && SetValue(node[GET_NAME(subscriberId)], subscriberId);
53     ret = ret && SetValue(node[GET_NAME(userId)], userId);
54     ret = ret && SetValue(node[GET_NAME(templat)], tpl);
55     return ret;
56 }
57 
Unmarshal(const DistributedData::Serializable::json & node)58 bool TemplateRootNode::Unmarshal(const DistributedData::Serializable::json &node)
59 {
60     bool ret = GetValue(node, GET_NAME(uri), uri);
61     ret = ret && GetValue(node, GET_NAME(bundleName), bundleName);
62     if (!GetValue(node, GET_NAME(subscriberId), subscriberId)) {
63         int64_t subId;
64         if (GetValue(node, GET_NAME(subscriberId), subId)) {
65             subscriberId = std::to_string(subId);
66         } else {
67             ret = false;
68         }
69     }
70     ret = ret && GetValue(node, GET_NAME(userId), userId);
71     ret = ret && GetValue(node, GET_NAME(templat), tpl);
72     return ret;
73 }
74 
TemplateRootNode(const std::string & uri,const std::string & bundleName,const int64_t subscriberId,const int32_t userId,const Template & tpl)75 TemplateRootNode::TemplateRootNode(const std::string &uri, const std::string &bundleName, const int64_t subscriberId,
76     const int32_t userId, const Template &tpl)
77     : uri(uri), bundleName(bundleName), subscriberId(std::to_string(subscriberId)), userId(userId), tpl(tpl)
78 {
79 }
80 
HasVersion() const81 bool TemplateData::HasVersion() const
82 {
83     return false;
84 }
85 
GetValue() const86 std::string TemplateData::GetValue() const
87 {
88     return DistributedData::Serializable::Marshall(value);
89 }
90 
TemplateData(const std::string & uri,const std::string & bundleName,int64_t subscriberId,int32_t userId,const Template & tpl)91 TemplateData::TemplateData(
92     const std::string &uri, const std::string &bundleName, int64_t subscriberId, int32_t userId, const Template &tpl)
93     :KvData(Id(GenId(uri, bundleName, subscriberId), userId)), value(uri, bundleName, subscriberId, userId, tpl)
94 {
95 }
96 
GetVersion() const97 int TemplateData::GetVersion() const
98 {
99     return 0;
100 }
101 
GenId(const std::string & uri,const std::string & bundleName,int64_t subscriberId)102 std::string TemplateData::GenId(const std::string &uri, const std::string &bundleName, int64_t subscriberId)
103 {
104     return uri + "_" + std::to_string(subscriberId) + "_" + bundleName;
105 }
106 
Query(const std::string & filter,Template & aTemplate)107 int32_t TemplateData::Query(const std::string &filter, Template &aTemplate)
108 {
109     auto delegate = KvDBDelegate::GetInstance();
110     if (delegate == nullptr) {
111         ZLOGE("db open failed");
112         return E_ERROR;
113     }
114     std::string queryResult;
115     int32_t status = delegate->Get(KvDBDelegate::TEMPLATE_TABLE, filter, "{}", queryResult);
116     if (status != E_OK) {
117         ZLOGE("db Get failed, %{public}s %{public}d", filter.c_str(), status);
118         return status;
119     }
120     TemplateRootNode data;
121     if (!DistributedData::Serializable::Unmarshall(queryResult, data)) {
122         ZLOGE("Unmarshall failed, %{private}s", queryResult.c_str());
123         return E_ERROR;
124     }
125     aTemplate = data.ToTemplate();
126     return E_OK;
127 }
128 
Delete(const std::string & bundleName,const int32_t userId)129 bool TemplateData::Delete(const std::string &bundleName, const int32_t userId)
130 {
131     auto delegate = KvDBDelegate::GetInstance();
132     if (delegate == nullptr) {
133         ZLOGE("db open failed");
134         return false;
135     }
136     auto status = delegate->Delete(KvDBDelegate::TEMPLATE_TABLE,
137         "{\"bundleName\":\"" + bundleName + "\", \"userId\": " + std::to_string(userId) + "}");
138     if (status != E_OK) {
139         ZLOGE("db DeleteById failed, %{public}d", status);
140         return false;
141     }
142     delegate->NotifyBackup();
143     return true;
144 }
145 
Add(const std::string & uri,const int32_t userId,const std::string & bundleName,const int64_t subscriberId,const Template & aTemplate)146 bool TemplateData::Add(const std::string &uri, const int32_t userId, const std::string &bundleName,
147     const int64_t subscriberId, const Template &aTemplate)
148 {
149     auto delegate = KvDBDelegate::GetInstance();
150     if (delegate == nullptr) {
151         ZLOGE("db open failed");
152         return false;
153     }
154     TemplateData data(uri, bundleName, subscriberId, userId, aTemplate);
155     auto status = delegate->Upsert(KvDBDelegate::TEMPLATE_TABLE, data);
156     if (status != E_OK) {
157         ZLOGE("db Upsert failed, %{public}d", status);
158         return false;
159     }
160     delegate->NotifyBackup();
161     return true;
162 }
163 
Delete(const std::string & uri,const int32_t userId,const std::string & bundleName,const int64_t subscriberId)164 bool TemplateData::Delete(
165     const std::string &uri, const int32_t userId, const std::string &bundleName, const int64_t subscriberId)
166 {
167     auto delegate = KvDBDelegate::GetInstance();
168     if (delegate == nullptr) {
169         ZLOGE("db open failed");
170         return false;
171     }
172     auto status = delegate->Delete(KvDBDelegate::TEMPLATE_TABLE,
173         static_cast<std::string>(Id(TemplateData::GenId(uri, bundleName, subscriberId), userId)));
174     if (status != E_OK) {
175         ZLOGE("db DeleteById failed, %{public}d", status);
176         return false;
177     }
178     delegate->NotifyBackup();
179     return true;
180 }
181 
ToTemplate() const182 Template TemplateRootNode::ToTemplate() const
183 {
184     return tpl.ToTemplate();
185 }
186 
PredicatesNode(const std::string & key,const std::string & selectSql)187 PredicatesNode::PredicatesNode(const std::string &key, const std::string &selectSql) : key(key), selectSql(selectSql)
188 {
189 }
Marshal(DistributedData::Serializable::json & node) const190 bool PredicatesNode::Marshal(DistributedData::Serializable::json &node) const
191 {
192     bool ret = SetValue(node[GET_NAME(key)], key);
193     ret = ret && SetValue(node[GET_NAME(selectSql)], selectSql);
194     return ret;
195 }
Unmarshal(const DistributedData::Serializable::json & node)196 bool PredicatesNode::Unmarshal(const DistributedData::Serializable::json &node)
197 {
198     bool ret = GetValue(node, GET_NAME(key), key);
199     ret = ret && GetValue(node, GET_NAME(selectSql), selectSql);
200     return ret;
201 }
202 } // namespace OHOS::DataShare