mirror of
https://gitee.com/openharmony/distributeddatamgr_datamgr_service
synced 2024-11-23 06:50:35 +00:00
commit
63a9f8555a
@ -27,6 +27,7 @@
|
||||
#include "metadata/store_meta_data.h"
|
||||
#include "result_set.h"
|
||||
#include "serializable/serializable.h"
|
||||
#include "value_object.h"
|
||||
|
||||
namespace OHOS::DataShare {
|
||||
class DBDelegate {
|
||||
@ -40,6 +41,7 @@ public:
|
||||
virtual std::string Query(
|
||||
const std::string &sql, const std::vector<std::string> &selectionArgs = std::vector<std::string>()) = 0;
|
||||
virtual std::shared_ptr<NativeRdb::ResultSet> QuerySql(const std::string &sql) = 0;
|
||||
virtual std::pair<int, int64_t> UpdateSql(const std::string &sql) = 0;
|
||||
virtual bool IsInvalid() = 0;
|
||||
static void SetExecutorPool(std::shared_ptr<ExecutorPool> executor);
|
||||
static void EraseStoreCache(const int32_t tokenId);
|
||||
|
@ -285,6 +285,22 @@ std::shared_ptr<NativeRdb::ResultSet> RdbDelegate::QuerySql(const std::string &s
|
||||
return resultSet;
|
||||
}
|
||||
|
||||
std::pair<int, int64_t> RdbDelegate::UpdateSql(const std::string &sql)
|
||||
{
|
||||
if (store_ == nullptr) {
|
||||
ZLOGE("store is null");
|
||||
return std::make_pair(E_ERROR, 0);
|
||||
}
|
||||
auto[ret, outValue] = store_->Execute(sql);
|
||||
if (ret != E_OK) {
|
||||
ZLOGE("execute update sql failed, err:%{public}d", ret);
|
||||
return std::make_pair(ret, 0);
|
||||
}
|
||||
int64_t rowCount = 0;
|
||||
outValue.GetLong(rowCount);
|
||||
return std::make_pair(ret, rowCount);
|
||||
}
|
||||
|
||||
bool RdbDelegate::IsInvalid()
|
||||
{
|
||||
return store_ == nullptr;
|
||||
|
@ -38,6 +38,7 @@ public:
|
||||
const int32_t callingPid) override;
|
||||
std::string Query(const std::string &sql, const std::vector<std::string> &selectionArgs) override;
|
||||
std::shared_ptr<NativeRdb::ResultSet> QuerySql(const std::string &sql) override;
|
||||
std::pair<int, int64_t> UpdateSql(const std::string &sql) override;
|
||||
bool IsInvalid() override;
|
||||
std::pair<int64_t, int64_t> InsertEx(const std::string &tableName,
|
||||
const DataShareValuesBucket &valuesBucket) override;
|
||||
|
@ -18,18 +18,20 @@
|
||||
namespace OHOS::DataShare {
|
||||
bool TemplateNode::Marshal(DistributedData::Serializable::json &node) const
|
||||
{
|
||||
bool ret = SetValue(node[GET_NAME(predicates)], predicates);
|
||||
bool ret = SetValue(node[GET_NAME(update)], update);
|
||||
ret = SetValue(node[GET_NAME(predicates)], predicates);
|
||||
ret = ret && SetValue(node[GET_NAME(scheduler)], scheduler);
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool TemplateNode::Unmarshal(const DistributedData::Serializable::json &node)
|
||||
{
|
||||
bool ret = GetValue(node, GET_NAME(predicates), predicates);
|
||||
bool ret = GetValue(node, GET_NAME(update), update);
|
||||
ret = GetValue(node, GET_NAME(predicates), predicates);
|
||||
return ret && GetValue(node, GET_NAME(scheduler), scheduler);
|
||||
}
|
||||
|
||||
TemplateNode::TemplateNode(const Template &tpl) : scheduler(tpl.scheduler_)
|
||||
TemplateNode::TemplateNode(const Template &tpl) : update(tpl.update_), scheduler(tpl.scheduler_)
|
||||
{
|
||||
for (auto &item:tpl.predicates_) {
|
||||
predicates.emplace_back(item.key_, item.selectSql_);
|
||||
@ -42,7 +44,7 @@ Template TemplateNode::ToTemplate() const
|
||||
for (const auto &predicate: predicates) {
|
||||
nodes.emplace_back(predicate.key, predicate.selectSql);
|
||||
}
|
||||
return Template(nodes, scheduler);
|
||||
return Template(update, nodes, scheduler);
|
||||
}
|
||||
|
||||
bool TemplateRootNode::Marshal(DistributedData::Serializable::json &node) const
|
||||
|
@ -36,6 +36,7 @@ struct TemplateNode final: public DistributedData::Serializable {
|
||||
bool Unmarshal(const json &node) override;
|
||||
Template ToTemplate() const;
|
||||
private:
|
||||
std::string update;
|
||||
std::vector<PredicatesNode> predicates;
|
||||
std::string scheduler;
|
||||
};
|
||||
|
@ -117,7 +117,7 @@ int32_t DataShareServiceStub::OnAddTemplate(MessageParcel &data, MessageParcel &
|
||||
std::string uri;
|
||||
int64_t subscriberId;
|
||||
Template tpl;
|
||||
if (!ITypesUtil::Unmarshal(data, uri, subscriberId, tpl.predicates_, tpl.scheduler_)) {
|
||||
if (!ITypesUtil::Unmarshal(data, uri, subscriberId, tpl.update_, tpl.predicates_, tpl.scheduler_)) {
|
||||
ZLOGW("read device list failed.");
|
||||
return -1;
|
||||
}
|
||||
|
@ -343,6 +343,13 @@ int RdbSubscriberManager::Notify(const Key &key, int32_t userId, const std::vect
|
||||
}
|
||||
changeNode.data_.emplace_back("{\"" + predicate.key_ + "\":" + result + "}");
|
||||
}
|
||||
if (!tpl.update_.empty()) {
|
||||
auto [errCode, rowCount] = delegate->UpdateSql(tpl.update_);
|
||||
if (errCode != E_OK) {
|
||||
ZLOGE("Update failed, err:%{public}d, %{public}s, %{public}" PRId64 ", %{public}s",
|
||||
errCode, DistributedData::Anonymous::Change(key.uri).c_str(), key.subscriberId, key.bundleName.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
ZLOGI("emit, valSize: %{public}zu, dataSize:%{public}zu, uri:%{public}s,",
|
||||
val.size(), changeNode.data_.size(), DistributedData::Anonymous::Change(changeNode.uri_).c_str());
|
||||
|
Loading…
Reference in New Issue
Block a user