• 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(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     ret = ret && GetValue(node, GET_NAME(subscriberId), subscriberId);
63     ret = ret && GetValue(node, GET_NAME(userId), userId);
64     ret = ret && GetValue(node, GET_NAME(templat), tpl);
65     return ret;
66 }
67 
TemplateRootNode(const std::string & uri,const std::string & bundleName,const int64_t subscriberId,const int32_t userId,const Template & tpl)68 TemplateRootNode::TemplateRootNode(const std::string &uri, const std::string &bundleName, const int64_t subscriberId,
69     const int32_t userId, const Template &tpl)
70     : uri(uri), bundleName(bundleName), subscriberId(subscriberId), userId(userId), tpl(tpl)
71 {
72 }
73 
HasVersion() const74 bool TemplateData::HasVersion() const
75 {
76     return false;
77 }
78 
GetValue() const79 std::string TemplateData::GetValue() const
80 {
81     return DistributedData::Serializable::Marshall(value);
82 }
83 
TemplateData(const std::string & uri,const std::string & bundleName,int64_t subscriberId,int32_t userId,const Template & tpl)84 TemplateData::TemplateData(
85     const std::string &uri, const std::string &bundleName, int64_t subscriberId, int32_t userId, const Template &tpl)
86     :KvData(Id(GenId(uri, bundleName, subscriberId), userId)), value(uri, bundleName, subscriberId, userId, tpl)
87 {
88 }
89 
GetVersion() const90 int TemplateData::GetVersion() const
91 {
92     return 0;
93 }
94 
GenId(const std::string & uri,const std::string & bundleName,int64_t subscriberId)95 std::string TemplateData::GenId(const std::string &uri, const std::string &bundleName, int64_t subscriberId)
96 {
97     return uri + "_" + std::to_string(subscriberId) + "_" + bundleName;
98 }
99 
Query(const std::string & filter,Template & aTemplate)100 int32_t TemplateData::Query(const std::string &filter, Template &aTemplate)
101 {
102     auto delegate = KvDBDelegate::GetInstance();
103     if (delegate == nullptr) {
104         ZLOGE("db open failed");
105         return E_ERROR;
106     }
107     std::string queryResult;
108     int32_t status = delegate->Get(KvDBDelegate::TEMPLATE_TABLE, filter, "{}", queryResult);
109     if (status != E_OK) {
110         ZLOGE("db Get failed, %{public}s %{public}d", filter.c_str(), status);
111         return status;
112     }
113     TemplateRootNode data;
114     if (!DistributedData::Serializable::Unmarshall(queryResult, data)) {
115         ZLOGE("Unmarshall failed, %{private}s", queryResult.c_str());
116         return E_ERROR;
117     }
118     aTemplate = data.ToTemplate();
119     return E_OK;
120 }
121 
Delete(const std::string & bundleName,const int32_t userId)122 bool TemplateData::Delete(const std::string &bundleName, const int32_t userId)
123 {
124     auto delegate = KvDBDelegate::GetInstance();
125     if (delegate == nullptr) {
126         ZLOGE("db open failed");
127         return false;
128     }
129     auto status = delegate->Delete(KvDBDelegate::TEMPLATE_TABLE,
130         "{\"bundleName\":\"" + bundleName + "\", \"userId\": " + std::to_string(userId) + "}");
131     if (status != E_OK) {
132         ZLOGE("db DeleteById failed, %{public}d", status);
133         return false;
134     }
135     return true;
136 }
137 
Add(const std::string & uri,const int32_t userId,const std::string & bundleName,const int64_t subscriberId,const Template & aTemplate)138 bool TemplateData::Add(const std::string &uri, const int32_t userId, const std::string &bundleName,
139     const int64_t subscriberId, const Template &aTemplate)
140 {
141     auto delegate = KvDBDelegate::GetInstance();
142     if (delegate == nullptr) {
143         ZLOGE("db open failed");
144         return false;
145     }
146     TemplateData data(uri, bundleName, subscriberId, userId, aTemplate);
147     auto status = delegate->Upsert(KvDBDelegate::TEMPLATE_TABLE, data);
148     if (status != E_OK) {
149         ZLOGE("db Upsert failed, %{public}d", status);
150         return false;
151     }
152     return true;
153 }
154 
Delete(const std::string & uri,const int32_t userId,const std::string & bundleName,const int64_t subscriberId)155 bool TemplateData::Delete(
156     const std::string &uri, const int32_t userId, const std::string &bundleName, const int64_t subscriberId)
157 {
158     auto delegate = KvDBDelegate::GetInstance();
159     if (delegate == nullptr) {
160         ZLOGE("db open failed");
161         return false;
162     }
163     auto status = delegate->Delete(KvDBDelegate::TEMPLATE_TABLE,
164         static_cast<std::string>(Id(TemplateData::GenId(uri, bundleName, subscriberId), userId)));
165     if (status != E_OK) {
166         ZLOGE("db DeleteById failed, %{public}d", status);
167         return false;
168     }
169     return true;
170 }
171 
ToTemplate() const172 Template TemplateRootNode::ToTemplate() const
173 {
174     return tpl.ToTemplate();
175 }
176 
PredicatesNode(const std::string & key,const std::string & selectSql)177 PredicatesNode::PredicatesNode(const std::string &key, const std::string &selectSql) : key(key), selectSql(selectSql)
178 {
179 }
Marshal(DistributedData::Serializable::json & node) const180 bool PredicatesNode::Marshal(DistributedData::Serializable::json &node) const
181 {
182     bool ret = SetValue(node[GET_NAME(key)], key);
183     ret = ret && SetValue(node[GET_NAME(selectSql)], selectSql);
184     return ret;
185 }
Unmarshal(const DistributedData::Serializable::json & node)186 bool PredicatesNode::Unmarshal(const DistributedData::Serializable::json &node)
187 {
188     bool ret = GetValue(node, GET_NAME(key), key);
189     ret = ret && GetValue(node, GET_NAME(selectSql), selectSql);
190     return ret;
191 }
192 } // namespace OHOS::DataShare