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