Merge pull request !632 from Cuiziyuan/cardUpdate
This commit is contained in:
openharmony_ci 2024-11-16 07:26:01 +00:00 committed by Gitee
commit 3f80b84e0e
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
4 changed files with 65 additions and 2 deletions

View File

@ -539,6 +539,12 @@ Template DataShareJSUtils::Convert2Template(napi_env env, napi_value value)
LOG_ERROR("Convert2Template error, value is not object");
return {};
}
std::string update;
if (!UnwrapStringByPropertyName(env, value, "update", update)) {
LOG_INFO("Parameter update undefined");
update = "";
}
napi_value jsPredicates;
auto status = napi_get_named_property(env, value, "predicates", &jsPredicates);
if (status != napi_ok) {
@ -556,7 +562,7 @@ Template DataShareJSUtils::Convert2Template(napi_env env, napi_value value)
LOG_ERROR("Convert scheduler failed");
return {};
}
Template tpl(predicates, scheduler);
Template tpl(update, predicates, scheduler);
return tpl;
}

View File

@ -200,7 +200,12 @@ int DataShareServiceProxy::AddQueryTemplate(const std::string &uri, int64_t subs
LOG_ERROR("Write descriptor failed!");
return DATA_SHARE_ERROR;
}
if (!ITypesUtil::Marshal(data, uri, subscriberId, tpl.predicates_, tpl.scheduler_)) {
std::string updateSqlPrefix = "update";
if (tpl.update_.compare(0, updateSqlPrefix.size(), updateSqlPrefix) != 0) {
LOG_ERROR("Parameter update only support update SQL");
return DATA_SHARE_ERROR;
}
if (!ITypesUtil::Marshal(data, uri, subscriberId, tpl.update_, tpl.predicates_, tpl.scheduler_)) {
LOG_ERROR("Write to message parcel failed!");
return DATA_SHARE_ERROR;
}

View File

@ -42,6 +42,11 @@ struct Template {
Template() = default;
Template(const std::vector<PredicateTemplateNode> &predicates,
const std::string &scheduler) : predicates_(predicates), scheduler_(scheduler) {}
Template(const std::string &update,
const std::vector<PredicateTemplateNode> &predicates,
const std::string &scheduler) : update_(update), predicates_(predicates), scheduler_(scheduler) {}
/** Specifies the update sql of the template. */
std::string update_;
/** Specifies the predicates of the template. {@link #PredicateTemplateNode} */
std::vector<PredicateTemplateNode> predicates_;
/** Specifies the scheduler sql of the template. */

View File

@ -196,6 +196,53 @@ HWTEST_F(ProxyDatasTest, ProxyDatasTest_Template_Test_002, TestSize.Level0)
LOG_INFO("ProxyDatasTest_Template_Test_002::End");
}
HWTEST_F(ProxyDatasTest, ProxyDatasTest_Template_Test_003, TestSize.Level0)
{
LOG_INFO("ProxyDatasTest_Template_Test_003::Start");
auto helper = dataShareHelper;
Uri uri(DATA_SHARE_PROXY_URI);
DataShare::DataShareValuesBucket valuesBucket;
std::string name0 = "name00";
valuesBucket.Put(TBL_NAME0, name0);
helper->Insert(uri, valuesBucket);
PredicateTemplateNode node1("p1", "select name0 as name from TBL00");
std::vector<PredicateTemplateNode> nodes;
nodes.emplace_back(node1);
Template tpl(nodes, "select name0 as name from TBL00");
tpl.update_ = "update TBL00 set name0 = 'updatetest' where name0 = 'name00'";
auto result = helper->AddQueryTemplate(DATA_SHARE_PROXY_URI, SUBSCRIBER_ID, tpl);
EXPECT_EQ(result, 0);
std::vector<std::string> uris;
uris.emplace_back(DATA_SHARE_PROXY_URI);
TemplateId tplId;
tplId.subscriberId_ = SUBSCRIBER_ID;
tplId.bundleName_ = "ohos.datashareproxyclienttest.demo";
std::string data1;
std::vector<OperationResult> results1 =
helper->SubscribeRdbData(uris, tplId, [&data1](const RdbChangeNode &changeNode) {
data1 = changeNode.data_[0];
});
for (auto const &operationResult : results1) {
EXPECT_EQ(operationResult.errCode_, 0);
}
std::vector<OperationResult> results2 = helper->UnsubscribeRdbData(uris, tplId);
EXPECT_EQ(results2.size(), uris.size());
for (auto const &operationResult : results2) {
EXPECT_EQ(operationResult.errCode_, 0);
}
DataShare::DataSharePredicates predicates;
predicates.EqualTo(TBL_NAME0, "updatetest");
std::vector<string> columns;
auto resultSet = helper->Query(uri, predicates, columns);
int queryResult = 0;
resultSet->GetRowCount(queryResult);
EXPECT_EQ(result, 1);
LOG_INFO("ProxyDatasTest_Template_Test_003::End");
}
HWTEST_F(ProxyDatasTest, ProxyDatasTest_Publish_Test_001, TestSize.Level0)
{
LOG_INFO("ProxyDatasTest_Publish_Test_001::Start");