From 0a9bce1b526bbcd2ba0de103ac9a9b093682e81e Mon Sep 17 00:00:00 2001 From: yangliu Date: Fri, 20 May 2022 20:16:34 +0800 Subject: [PATCH 1/3] add code to branch Signed-off-by: yangliu --- bundle.json | 3 +- ...t_set.h => kvstore_datashare_result_set.h} | 105 ++-- .../include/kvstore_predicates.h | 79 ++- .../distributeddatafwk/src/kv_utils.cpp | 107 ++++ .../src/kvdbstore_datashare_native.cpp | 59 -- ...t.cpp => kvstore_datashare_result_set.cpp} | 230 ++++---- .../src/kvstore_predicates.cpp | 539 +++++++++++++++++- .../distributeddata/include/js_util.h | 3 + interfaces/innerkits/distributeddata/BUILD.gn | 12 +- ...vdbstore_datashare_native.h => kv_utils.h} | 89 +-- 10 files changed, 891 insertions(+), 335 deletions(-) rename frameworks/innerkitsimpl/distributeddatafwk/include/{kvdbstore_result_set.h => kvstore_datashare_result_set.h} (48%) create mode 100644 frameworks/innerkitsimpl/distributeddatafwk/src/kv_utils.cpp delete mode 100644 frameworks/innerkitsimpl/distributeddatafwk/src/kvdbstore_datashare_native.cpp rename frameworks/innerkitsimpl/distributeddatafwk/src/{kvdbstore_result_set.cpp => kvstore_datashare_result_set.cpp} (43%) rename interfaces/innerkits/distributeddata/include/{kvdbstore_datashare_native.h => kv_utils.h} (42%) diff --git a/bundle.json b/bundle.json index 6983df085..9ed5012f5 100644 --- a/bundle.json +++ b/bundle.json @@ -109,8 +109,7 @@ "visibility.h", "data_query.h", "device_status_change_listener.h", - "store_errno.h", - "kvdbstore_datashare_native.h" + "store_errno.h" ], "header_base": "//foundation/distributeddatamgr/distributeddatamgr/interfaces/innerkits/distributeddata/include" } diff --git a/frameworks/innerkitsimpl/distributeddatafwk/include/kvdbstore_result_set.h b/frameworks/innerkitsimpl/distributeddatafwk/include/kvstore_datashare_result_set.h similarity index 48% rename from frameworks/innerkitsimpl/distributeddatafwk/include/kvdbstore_result_set.h rename to frameworks/innerkitsimpl/distributeddatafwk/include/kvstore_datashare_result_set.h index be5e7c58d..d898eeef3 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/include/kvdbstore_result_set.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/include/kvstore_datashare_result_set.h @@ -1,54 +1,51 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef KVDBSTORE_RESULT_SET_H -#define KVDBSTORE_RESULT_SET_H - -#include "datashare_abstract_result_set.h" -#include "kvstore_result_set.h" -#include "single_kvstore.h" -#include "datashare_errno.h" - -namespace OHOS { -namespace DistributedKv { -using namespace DataShare; -class KvDBStoreResultSet : public DataShareAbstractResultSet -{ -public: - KvDBStoreResultSet(std::shared_ptr kvResultSet); - - ~KvDBStoreResultSet() = default; - - int GetRowCount(int &count) override; - - int GetAllColumnOrKeyName(std::vector &columnOrKeyNames) override; - - bool OnGo(int oldRowIndex, int newRowIndex, const std::shared_ptr &writer) override; - -private: - int Count(); - - bool FillBlock(int startRowIndex, const std::shared_ptr &writer); - - static constexpr int INVALID_COUNT = -1; - - int resultRowCount {INVALID_COUNT}; - - std::shared_ptr kvResultSet_; -}; -} // namespace DistributedKv -} // namespace OHOS -#endif // KVDBSTORE_RESULT_SET_H - +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef KVSTORE_DATASHARE_RESULT_SET_H +#define KVSTORE_DATASHARE_RESULT_SET_H + +#include "kvstore_result_set.h" +#include "single_kvstore.h" +#include "result_set_bridge.h" +#include "datashare_errno.h" + +namespace OHOS { +namespace DistributedKv { +class KvStoreDataShareResultSet : public DataShare::ResultSetBridge { +public: + KvStoreDataShareResultSet(std::shared_ptr kvResultSet); + + ~KvStoreDataShareResultSet() = default; + + int GetRowCount(int32_t &count) override; + + int GetAllColumnNames(std::vector &columnsName) override; + + bool OnGo(int32_t start, int32_t target, DataShare::ResultSetBridge::Writer &writer) override; + +private: + int Count(); + + bool FillBlock(int startRowIndex, DataShare::ResultSetBridge::Writer &writer); + + static constexpr int32_t INVALID_COUNT = -1; + + int32_t resultRowCount {INVALID_COUNT}; + + std::shared_ptr kvResultSet_; +}; +} // namespace DistributedKv +} // namespace OHOS +#endif // KVSTORE_DATASHARE_RESULT_SET_H diff --git a/frameworks/innerkitsimpl/distributeddatafwk/include/kvstore_predicates.h b/frameworks/innerkitsimpl/distributeddatafwk/include/kvstore_predicates.h index a70f3593c..aff65f659 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/include/kvstore_predicates.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/include/kvstore_predicates.h @@ -24,27 +24,68 @@ namespace OHOS { namespace DistributedKv { -using namespace DataShare; - -class KvStorePredicate -{ +class KvStorePredicates { public: - KvStorePredicate(const DataSharePredicates &predicates); - - ~KvStorePredicate() = default; - - Status ToQuery(DataQuery &query); - + struct Context { + int intValue; + int64_t longValue; + double doubleValue; + bool boolValue; + std::string field; + std::string stringValue; + std::vector intList; + std::vector longList; + std::vector doubleList; + std::vector stringList; + DataShare::DataSharePredicatesObjectType innerType; + }; + KvStorePredicates() = default; + ~KvStorePredicates() = default; + Status ToQuery(const DataShare::DataSharePredicates &predicates, DataQuery &query); + Status GetKeys(const DataShare::DataSharePredicates &predicates, std::vector &keys); private: - Status InKeys(const OperationItem &oper, DataQuery &query); - - Status KeyPrefix(const OperationItem &oper, DataQuery &query); - - using QueryHandler = Status (KvStorePredicate::*)(const OperationItem &, DataQuery &); - - static const std::map HANDLERS; - - DataSharePredicates predicates_; + Status EqualTo(const DataShare::OperationItem &oper, DataQuery &query); + Status NotEqualTo(const DataShare::OperationItem &oper, DataQuery &query); + Status GreaterThan(const DataShare::OperationItem &oper, DataQuery &query); + Status LessThan(const DataShare::OperationItem &oper, DataQuery &query); + Status GreaterThanOrEqualTo(const DataShare::OperationItem &oper, DataQuery &query); + Status LessThanOrEqualTo(const DataShare::OperationItem &oper, DataQuery &query); + Status And(const DataShare::OperationItem &oper, DataQuery &query); + Status Or(const DataShare::OperationItem &oper, DataQuery &query); + Status IsNull(const DataShare::OperationItem &oper, DataQuery &query); + Status IsNotNull(const DataShare::OperationItem &oper, DataQuery &query); + Status In(const DataShare::OperationItem &oper, DataQuery &query); + Status NotIn(const DataShare::OperationItem &oper, DataQuery &query); + Status Like(const DataShare::OperationItem &oper, DataQuery &query); + Status Unlike(const DataShare::OperationItem &oper, DataQuery &query); + Status OrderByAsc(const DataShare::OperationItem &oper, DataQuery &query); + Status OrderByDesc(const DataShare::OperationItem &oper, DataQuery &query); + Status Limit(const DataShare::OperationItem &oper, DataQuery &query); + Status InKeys(const DataShare::OperationItem &oper, DataQuery &query); + Status KeyPrefix(const DataShare::OperationItem &oper, DataQuery &query); + Status GetContext(const DataShare::OperationItem &oper, Context &context); + using QueryHandler = Status (KvStorePredicates::*)(const DataShare::OperationItem &, DataQuery &); + static constexpr QueryHandler HANDLERS[DataShare::LAST_TYPE] = { + [DataShare::EQUAL_TO] = &KvStorePredicates::EqualTo, + [DataShare::NOT_EQUAL_TO] = &KvStorePredicates::NotEqualTo, + [DataShare::GREATER_THAN] = &KvStorePredicates::GreaterThan, + [DataShare::LESS_THAN] = &KvStorePredicates::LessThan, + [DataShare::GREATER_THAN_OR_EQUAL_TO] = &KvStorePredicates::GreaterThanOrEqualTo, + [DataShare::LESS_THAN_OR_EQUAL_TO] = &KvStorePredicates::LessThanOrEqualTo, + [DataShare::AND] = &KvStorePredicates::And, + [DataShare::OR] = &KvStorePredicates::Or, + [DataShare::IS_NULL] = &KvStorePredicates::IsNull, + [DataShare::IS_NOT_NULL] = &KvStorePredicates::IsNotNull, + [DataShare::NOT_IN] = &KvStorePredicates::NotIn, + [DataShare::LIKE] = &KvStorePredicates::Like, + [DataShare::UNLIKE] = &KvStorePredicates::Unlike, + [DataShare::ORDER_BY_ASC] = &KvStorePredicates::OrderByAsc, + [DataShare::ORDER_BY_DESC] = &KvStorePredicates::OrderByDesc, + [DataShare::LIMIT] = &KvStorePredicates::Limit, + [DataShare::IN_KEY] = &KvStorePredicates::InKeys, + [DataShare::KEY_PREFIX] = &KvStorePredicates::KeyPrefix, + [DataShare::IN] = &KvStorePredicates::In, + }; }; }// namespace DistributedKv }//namespace diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kv_utils.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/kv_utils.cpp new file mode 100644 index 000000000..5af3432b5 --- /dev/null +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kv_utils.cpp @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define LOG_TAG "KvUtils" + +#include "kv_utils.h" +#include "kvstore_datashare_result_set.h" +#include "kvstore_predicates.h" +#include "log_print.h" +#include "data_query.h" +// #include "kvstore_values_bucket.h" + +namespace OHOS { +namespace DistributedKv { +using namespace DataShare; +const std::string KvUtils::KEY = "key"; +const std::string KvUtils::VALUE = "value"; +std::shared_ptr KvUtils::ToResultSetBridge( + std::shared_ptr resultSet) +{ + if (resultSet == nullptr) { + ZLOGE("param error, kvResultSet nullptr"); + return nullptr; + } + return std::make_shared(resultSet); +} + +Status KvUtils::ToQuery(const DataSharePredicates &predicates, DataQuery &query) +{ + + auto kvPredicates = std::make_shared(); + Status status = kvPredicates->ToQuery(predicates, query); + if (status != Status::SUCCESS) { + ZLOGE("ToQuery failed: %{public}d", status); + return status; + } + return status; +} + +std::vector KvUtils::ToEntries(const std::vector &valueBuckets) +{ + + std::vector entries; + for (const auto &val : valueBuckets) { + Entry entry = ToEntry(val); + entries.push_back(entry); + } + return entries; +} + +Entry KvUtils::ToEntry(const DataShareValuesBucket &valueBucket) +{ + std::map valuesMap; + valueBucket.GetAll(valuesMap); + if (valuesMap.empty()) { + ZLOGE("valuesMap is null"); + return {}; + } + Entry entry; + Status status = ToEntryData(valuesMap, KEY, entry.key); + if (status != Status::SUCCESS) { + ZLOGE("GetEntry key failed: %{public}d", status); + return {}; + } + status = ToEntryData(valuesMap, VALUE, entry.value); + if (status != Status::SUCCESS) { + ZLOGE("GetEntry value failed: %{public}d", status); + return {}; + } + return entry; +} + +Status KvUtils::ToEntryData(const std::map &valuesMap, const std::string field, Blob &kv) +{ + auto it = valuesMap.find(field); + if (it == valuesMap.end()) { + ZLOGE("field is not find!"); + return Status::ERROR; + } + DataShareValueObjectType type = it->second.GetType(); + if (type != DataShareValueObjectType::TYPE_BLOB) { + ZLOGE("key type is not TYPE_BLOB"); + return Status::ERROR; + } + std::vector data; + int status = it->second.GetBlob(data); + if (status != Status::SUCCESS) { + ZLOGE("GetBlob failed: %{public}d", status); + return Status::ERROR; + } + kv = data; + return Status::SUCCESS; +} +} // namespace DistributedKv +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvdbstore_datashare_native.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/kvdbstore_datashare_native.cpp deleted file mode 100644 index fc875b7e4..000000000 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvdbstore_datashare_native.cpp +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#define LOG_TAG "kvDBDataShareNative" - - -#include "kvdbstore_result_set.h" -#include "kvstore_predicates.h" -#include "kvdbstore_datashare_native.h" -#include "log_print.h" -#include "data_query.h" - -namespace OHOS { -namespace DistributedKv { - -kvDBDataShareNative::kvDBDataShareNative(std::shared_ptr kvStoreClient) - :kvStoreClient_(kvStoreClient) {}; - -std::shared_ptr kvDBDataShareNative::GetDataShareResult(const DataSharePredicates &predicate) -{ - if(kvStoreClient_ == nullptr) - { - ZLOGE("kvStoreClient_ nullptr"); - return nullptr; - } - - DataQuery query; - auto kvPredicates = std::make_shared(predicate); - Status status = kvPredicates->ToQuery(query); - if(status != Status::SUCCESS) - { - ZLOGE("ToQuery failed: %{public}d", status); - return nullptr; - } - - std::shared_ptr resultSet; - status = kvStoreClient_->GetResultSetWithQuery(query, resultSet); - if (status != Status::SUCCESS) - { - ZLOGE("GetResultSetWithQuery failed: %{public}d", status); - return nullptr; - } - - return std::make_shared(resultSet); -} -} // namespace DistributedKv -} // namespace OHOS diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvdbstore_result_set.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_datashare_result_set.cpp similarity index 43% rename from frameworks/innerkitsimpl/distributeddatafwk/src/kvdbstore_result_set.cpp rename to frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_datashare_result_set.cpp index 8d3d46ebd..0730e00f6 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvdbstore_result_set.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_datashare_result_set.cpp @@ -1,123 +1,107 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#define LOG_TAG "KvDBStoreResultSet" - -#include "constant.h" -#include "log_print.h" -#include "kvdbstore_result_set.h" - -namespace OHOS { -namespace DistributedKv { -KvDBStoreResultSet::KvDBStoreResultSet(std::shared_ptr kvResultSet) - :kvResultSet_(kvResultSet) {}; - -int KvDBStoreResultSet::GetRowCount(int &count) -{ - count = Count(); - return count == INVALID_COUNT ? E_ERROR : E_OK; -} - -int KvDBStoreResultSet::GetAllColumnOrKeyName(std::vector &columnOrKeyNames) -{ - columnOrKeyNames.clear(); - columnOrKeyNames.emplace_back("key", "value"); - return E_OK; -} - -bool KvDBStoreResultSet::FillBlock(int startRowIndex, const std::shared_ptr &writer) -{ - if (kvResultSet_ == nullptr) - { - ZLOGE("kvResultSet_ nullptr"); - return false; - } - - bool isMoved = kvResultSet_->MoveToPosition(startRowIndex); - if (!isMoved) - { - ZLOGE("MoveToPosition failed"); - return false; - } - - Entry entry; - Status status = kvResultSet_->GetEntry(entry); - if (status != Status::SUCCESS) - { - ZLOGE("GetEntry failed %{public}d", status); - return false; - } - - int clearStatus = writer->Clear(); - if(clearStatus != E_OK){ - ZLOGE("clear writer failed: %{public}d", clearStatus); - return false; - } - - if ((entry.key.Size() + entry.value.Size()) > (writer->Size() - writer->GetUsedBytes())) - { - ZLOGE("not enought memory; entry size: %{public}d, writer size: %{public}d", - entry.key.Size() + entry.value.Size(), writer->Size() - writer->GetUsedBytes()); - return false; - } - // uint8_t - int keyStatus = writer->WriteBlob((uint32_t)startRowIndex, 0, &entry.key.Data(), entry.key.Size()); - if(keyStatus != E_OK) - { - ZLOGE("WriteBlob key error: %{public}d", keyStatus); - return false; - } - - int valueStatus = writer->WriteBlob((uint32_t)startRowIndex, 1, &entry.value.Data(), entry.value.Size()); - if(valueStatus != E_OK) - { - ZLOGE("WriteBlob value error: %{public}d", valueStatus); - return false; - } - return true; -} - -int KvDBStoreResultSet::Count() -{ - if (kvResultSet_ == nullptr) - { - ZLOGE("kvResultSet_ nullptr"); - return INVALID_COUNT; - } - if (resultRowCount != INVALID_COUNT) - { - return resultRowCount; - } - int count = kvResultSet_->GetCount(); - if (count < 0) - { - ZLOGE("kvResultSet count invalid: %{public}d", count); - return INVALID_COUNT; - } - resultRowCount = count; - return count; -} - -bool KvDBStoreResultSet::OnGo(int oldRowIndex, int newRowIndex, const std::shared_ptr &writer) -{ - if (writer == nullptr || newRowIndex >= Count()) - { - ZLOGE("invalid writer: {public} or newRowIndex: %{public}d", newRowIndex); - return false; - } - return FillBlock(newRowIndex, writer); -} -} // namespace DistributedKv -} // namespace OHOS +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define LOG_TAG "KvStoreDataShareResultSet" + +#include "constant.h" +#include "log_print.h" +#include "kvstore_datashare_result_set.h" + +namespace OHOS { +namespace DistributedKv { +using namespace DataShare; +KvStoreDataShareResultSet::KvStoreDataShareResultSet(std::shared_ptr kvResultSet) + :kvResultSet_(kvResultSet) {}; + +int KvStoreDataShareResultSet::GetRowCount(int32_t &count) +{ + count = Count(); + return count == INVALID_COUNT ? E_ERROR : E_OK; +} + +int KvStoreDataShareResultSet::GetAllColumnNames(std::vector &columnsName) +{ + columnsName = { "key", "value" }; + return E_OK; +} +bool KvStoreDataShareResultSet::FillBlock(int pos, ResultSetBridge::Writer &writer) +{ + if (kvResultSet_ == nullptr) { + ZLOGE("kvResultSet_ nullptr"); + return false; + } + bool isMoved = kvResultSet_->MoveToPosition(pos); + if (!isMoved) { + ZLOGE("MoveToPosition failed"); + return false; + } + Entry entry; + Status status = kvResultSet_->GetEntry(entry); + if (status != Status::SUCCESS) { + ZLOGE("GetEntry failed %{public}d", status); + return false; + } + int statusAlloc = writer.AllocRow(); + if (statusAlloc != E_OK) { + ZLOGE("SharedBlock is full: %{public}d", statusAlloc); + return false; + } + int keyStatus = writer.Write(0, (uint8_t *)&entry.key.Data(), entry.key.Size()); + if (keyStatus != E_OK) { + ZLOGE("WriteBlob key error: %{public}d", keyStatus); + return false; + } + int valueStatus = writer.Write(1, (uint8_t *)&entry.value.Data(), entry.value.Size()); + if (valueStatus != E_OK) { + ZLOGE("WriteBlob value error: %{public}d", valueStatus); + return false; + } + return true; +} + +int KvStoreDataShareResultSet::Count() +{ + if (kvResultSet_ == nullptr) { + ZLOGE("kvResultSet_ nullptr"); + return INVALID_COUNT; + } + if (resultRowCount != INVALID_COUNT) { + return resultRowCount; + } + int count = kvResultSet_->GetCount(); + if (count < 0) { + ZLOGE("kvResultSet count invalid: %{public}d", count); + return INVALID_COUNT; + } + resultRowCount = count; + return count; +} +bool KvStoreDataShareResultSet::OnGo(int32_t start, int32_t target, ResultSetBridge::Writer &writer) +{ + if ((start < 0) || (target < 0) || (start > target) || (target >= Count())) { + ZLOGE("nowRowIndex out of line: %{public}d", target); + return false; + } + for (int pos = start; pos <= target; pos++) { + bool ret = FillBlock(pos, writer); + if (!ret) { + ZLOGE("nowRowIndex out of line: %{public}d", target); + return ret; + } + } + return true; +} +} // namespace DistributedKv +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_predicates.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_predicates.cpp index e28b0ad24..76538315a 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_predicates.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_predicates.cpp @@ -13,7 +13,7 @@ * limitations under the License. */ -#define LOG_TAG "KvStorePredicate" +#define LOG_TAG "KvStorePredicates" #include "kvstore_predicates.h" #include "log_print.h" @@ -21,57 +21,538 @@ namespace OHOS { namespace DistributedKv { - -const std::map KvStorePredicate::HANDLERS = { - {IN_KEY, &KvStorePredicate::InKeys}, - {KEY_PREFIX, &KvStorePredicate::KeyPrefix} -}; +using namespace DataShare; +constexpr KvStorePredicates::QueryHandler KvStorePredicates::HANDLERS[LAST_TYPE]; -KvStorePredicate::KvStorePredicate(const DataSharePredicates &predicates) - :predicates_(predicates){}; +Status KvStorePredicates::GetContext(const OperationItem &oper, Context &context) +{ + int status = oper.para1.GetString(context.field); + if (status != E_OK) { + ZLOGE("Get context.field failed: %{public}d", status); + return Status::ERROR; + } + + auto innerType = oper.para2.GetType(); + switch (innerType) { + case DataSharePredicatesObjectType::TYPE_INT: { + status = oper.para2.GetInt(context.intValue); + break; + } + case DataSharePredicatesObjectType::TYPE_LONG: { + status = oper.para2.GetLong(context.longValue); + break; + } + case DataSharePredicatesObjectType::TYPE_DOUBLE: { + status = oper.para2.GetDouble(context.doubleValue); + break; + } + case DataSharePredicatesObjectType::TYPE_BOOL: { + status = oper.para2.GetBool(context.boolValue); + break; + } + case DataSharePredicatesObjectType::TYPE_STRING: { + status = oper.para2.GetString(context.stringValue); + break; + } + case DataSharePredicatesObjectType::TYPE_INT_VECTOR: { + status = oper.para2.GetIntVector(context.intList); + break; + } + case DataSharePredicatesObjectType::TYPE_LONG_VECTOR: { + status = oper.para2.GetLongVector(context.longList); + break; + } + case DataSharePredicatesObjectType::TYPE_DOUBLE_VECTOR: { + status = oper.para2.GetDoubleVector(context.doubleList); + break; + } + case DataSharePredicatesObjectType::TYPE_STRING_VECTOR: { + status = oper.para2.GetStringVector(context.stringList); + break; + } + default: + status = Status::ERROR; + break; + } + + if(status != Status::SUCCESS) { + ZLOGE("GetType failed: %{public}d", status); + return Status::ERROR; + } + return Status::SUCCESS; +} -Status KvStorePredicate::ToQuery(DataQuery &query) +Status KvStorePredicates::ToQuery(const DataSharePredicates &predicates, DataQuery &query) { - std::list operationList = predicates_.GetOperationList(); - for(const auto &oper : operationList) - { - auto it = HANDLERS.find(oper.operation); - if(it == HANDLERS.end()) - { - ZLOGE("This feature is not supported"); + std::list operationList = predicates.GetOperationList(); + if (operationList.empty()) { + ZLOGE("ToQuery operationList is null"); + return Status::INVALID_ARGUMENT; + } + + for (const auto &oper : operationList) { + if (oper.operation < 0 || oper.operation >= LAST_TYPE) { + ZLOGE("operation param error"); return Status::NOT_SUPPORT; } - Status status = (this->*(it->second))(oper, query); - if(status != Status::SUCCESS){ - ZLOGE("ToQuery called failed"); + Status status = (this->*HANDLERS[oper.operation])(oper, query); + if (status != Status::SUCCESS) { + ZLOGE("ToQuery called failed: %{public}d", status); return status; } } - return Status::SUCCESS; + return Status::SUCCESS; } -Status KvStorePredicate::InKeys(const OperationItem &oper, DataQuery &query) +Status KvStorePredicates::GetKeys(const DataSharePredicates &predicates, std::vector &keys) +{ + std::list operationList = predicates.GetOperationList(); + if (operationList.empty()) { + ZLOGE("operationList is null"); + return Status::ERROR; + } + + std::vector myKeys; + for(const auto &oper : operationList) + { + if (oper.operation != IN_KEY) { + ZLOGE("find operation failed"); + return Status::NOT_SUPPORT; + } + std::vector val; + int status = oper.para1.GetStringVector(val); + if (status != E_OK) { + ZLOGE("GetStringVector failed: %{public}d", status); + return Status::ERROR; + } + myKeys.insert(myKeys.end(), val.begin(), val.end()); + } + for (const auto &it : myKeys) + { + keys.push_back(it.c_str()); + } + return Status::SUCCESS; +} + +Status KvStorePredicates::InKeys(const OperationItem &oper, DataQuery &query) { std::vector keys; int status = oper.para1.GetStringVector(keys); - if(status != E_OK){ - ZLOGE("GetStringVector failed"); + if (status != E_OK) { + ZLOGE("GetStringVector failed: %{public}d", status); return Status::ERROR; } query.InKeys(keys); return Status::SUCCESS; } -Status KvStorePredicate::KeyPrefix(const OperationItem &oper, DataQuery &query) +Status KvStorePredicates::KeyPrefix(const OperationItem &oper, DataQuery &query) { std::string prefix; int status = oper.para1.GetString(prefix); - if(status != E_OK){ - ZLOGE("KeyPrefix failed"); + if (status != E_OK) { + ZLOGE("KeyPrefix failed: %{public}d", status); return Status::ERROR; } query.KeyPrefix(prefix); return Status::SUCCESS; -} -}// namespace DistributedKv -}// namespace OHOS \ No newline at end of file +} + +Status KvStorePredicates::EqualTo(const OperationItem &oper, DataQuery &query) +{ + Context context; + Status status = GetContext(oper, context); + if (status != Status::SUCCESS) { + ZLOGE("EqualTo GetContext called failed: %{public}d", status); + return status; + } + + switch (context.innerType) { + case DataSharePredicatesObjectType::TYPE_INT: { + query.EqualTo(context.field, context.intValue); + break; + } + case DataSharePredicatesObjectType::TYPE_LONG: { + query.EqualTo(context.field, context.longValue); + break; + } + case DataSharePredicatesObjectType::TYPE_DOUBLE: { + query.EqualTo(context.field, context.doubleValue); + break; + } + case DataSharePredicatesObjectType::TYPE_BOOL: { + query.EqualTo(context.field, context.boolValue); + break; + } + case DataSharePredicatesObjectType::TYPE_STRING: { + query.EqualTo(context.field, context.stringValue); + break; + } + default: + status = Status::ERROR; + ZLOGE("EqualTo failed: %{public}d", status); + break; + } + return status; +} + +Status KvStorePredicates::NotEqualTo(const OperationItem &oper, DataQuery &query) +{ + Context context; + Status status = GetContext(oper, context); + if (status != Status::SUCCESS) { + ZLOGE("NotEqualTo GetContext called failed: %{public}d", status); + return status; + } + + switch (context.innerType) { + case DataSharePredicatesObjectType::TYPE_INT: { + query.NotEqualTo(context.field, context.intValue); + break; + } + case DataSharePredicatesObjectType::TYPE_LONG: { + query.NotEqualTo(context.field, context.longValue); + break; + } + case DataSharePredicatesObjectType::TYPE_DOUBLE: { + query.NotEqualTo(context.field, context.doubleValue); + break; + } + case DataSharePredicatesObjectType::TYPE_BOOL: { + query.NotEqualTo(context.field, context.boolValue); + break; + } + case DataSharePredicatesObjectType::TYPE_STRING: { + query.NotEqualTo(context.field, context.stringValue); + break; + } + default: + status = Status::ERROR; + ZLOGE("NotEqualTo failed: %{public}d", status); + break; + } + return status; +} + +Status KvStorePredicates::GreaterThan(const OperationItem &oper, DataQuery &query) +{ + Context context; + Status status = GetContext(oper, context); + if (status != Status::SUCCESS) { + ZLOGE("GreaterThan GetContext called failed: %{public}d", status); + return status; + } + + switch (context.innerType) { + case DataSharePredicatesObjectType::TYPE_INT: { + query.GreaterThan(context.field, context.intValue); + break; + } + case DataSharePredicatesObjectType::TYPE_LONG: { + query.GreaterThan(context.field, context.longValue); + break; + } + case DataSharePredicatesObjectType::TYPE_DOUBLE: { + query.GreaterThan(context.field, context.doubleValue); + break; + } + case DataSharePredicatesObjectType::TYPE_STRING: { + query.GreaterThan(context.field, context.stringValue); + break; + } + default: + status = Status::ERROR; + ZLOGE("GreaterThan failed: %{public}d", status); + break; + } + return status; +} + +Status KvStorePredicates::LessThan(const OperationItem &oper, DataQuery &query) +{ + Context context; + Status status = GetContext(oper, context); + if (status != Status::SUCCESS) { + ZLOGE("LessThan GetContext called failed: %{public}d", status); + return status; + } + + switch (context.innerType) { + case DataSharePredicatesObjectType::TYPE_INT: { + query.LessThan(context.field, context.intValue); + break; + } + case DataSharePredicatesObjectType::TYPE_LONG: { + query.LessThan(context.field, context.longValue); + break; + } + case DataSharePredicatesObjectType::TYPE_DOUBLE: { + query.LessThan(context.field, context.doubleValue); + break; + } + case DataSharePredicatesObjectType::TYPE_STRING: { + query.LessThan(context.field, context.stringValue); + break; + } + default: + status = Status::ERROR; + ZLOGE("LessThan failed: %{public}d", status); + break; + } + return status; +} + +Status KvStorePredicates::GreaterThanOrEqualTo(const OperationItem &oper, DataQuery &query) +{ + Context context; + Status status = GetContext(oper, context); + if (status != Status::SUCCESS) { + ZLOGE("GreaterThanOrEqualTo GetContext called failed: %{public}d", status); + return status; + } + + switch (context.innerType) { + case DataSharePredicatesObjectType::TYPE_INT: { + query.GreaterThanOrEqualTo(context.field, context.intValue); + break; + } + case DataSharePredicatesObjectType::TYPE_LONG: { + query.GreaterThanOrEqualTo(context.field, context.longValue); + break; + } + case DataSharePredicatesObjectType::TYPE_DOUBLE: { + query.GreaterThanOrEqualTo(context.field, context.doubleValue); + break; + } + case DataSharePredicatesObjectType::TYPE_STRING: { + query.GreaterThanOrEqualTo(context.field, context.stringValue); + break; + } + default: + status = Status::ERROR; + ZLOGE("GreaterThanOrEqualTo failed: %{public}d", status); + break; + } + return status; +} + +Status KvStorePredicates::LessThanOrEqualTo(const OperationItem &oper, DataQuery &query) +{ + Context context; + Status status = GetContext(oper, context); + if (status != Status::SUCCESS) { + ZLOGE("LessThanOrEqualTo GetContext called failed: %{public}d", status); + return status; + } + + switch (context.innerType) { + case DataSharePredicatesObjectType::TYPE_INT: { + query.LessThanOrEqualTo(context.field, context.intValue); + break; + } + case DataSharePredicatesObjectType::TYPE_LONG: { + query.LessThanOrEqualTo(context.field, context.longValue); + break; + } + case DataSharePredicatesObjectType::TYPE_DOUBLE: { + query.LessThanOrEqualTo(context.field, context.doubleValue); + break; + } + case DataSharePredicatesObjectType::TYPE_STRING: { + query.LessThanOrEqualTo(context.field, context.stringValue); + break; + } + default: + status = Status::ERROR; + ZLOGE("LessThanOrEqualTo failed: %{public}d", status); + break; + } + return status; +} + +Status KvStorePredicates::And(const OperationItem &oper, DataQuery &query) +{ + query.And(); + return Status::SUCCESS; +} + +Status KvStorePredicates::Or(const OperationItem &oper, DataQuery &query) +{ + query.Or(); + return Status::SUCCESS; +} + +Status KvStorePredicates::IsNull(const OperationItem &oper, DataQuery &query) +{ + std::string field; + int status = oper.para1.GetString(field); + if (status != E_OK) { + ZLOGE("IsNull failed: %{public}d", status); + return Status::ERROR; + } + query.IsNull(field); + return Status::SUCCESS; +} + +Status KvStorePredicates::IsNotNull(const OperationItem &oper, DataQuery &query) +{ + std::string field; + int status = oper.para1.GetString(field); + if (status != E_OK) { + ZLOGE("IsNotNull failed: %{public}d", status); + return Status::ERROR; + } + query.IsNotNull(field); + return Status::SUCCESS; +} + +Status KvStorePredicates::In(const OperationItem &oper, DataQuery &query) +{ + Context context; + Status status = GetContext(oper, context); + if (status != Status::SUCCESS) { + ZLOGE("In GetContext called failed: %{public}d", status); + return status; + } + + switch (context.innerType) { + case DataSharePredicatesObjectType::TYPE_INT_VECTOR: { + query.In(context.field, context.intList); + break; + } + case DataSharePredicatesObjectType::TYPE_LONG_VECTOR: { + query.In(context.field, context.longList); + break; + } + case DataSharePredicatesObjectType::TYPE_DOUBLE_VECTOR: { + query.In(context.field, context.doubleList); + break; + } + case DataSharePredicatesObjectType::TYPE_STRING_VECTOR: { + query.In(context.field, context.stringList); + break; + } + default: + status = Status::ERROR; + ZLOGE("In failed: %{public}d", status); + break; + } + return status; +} + +Status KvStorePredicates::NotIn(const OperationItem &oper, DataQuery &query) +{ + Context context; + Status status = GetContext(oper, context); + if (status != Status::SUCCESS) { + ZLOGE("NotIn GetContext called failed: %{public}d", status); + return status; + } + + switch (context.innerType) { + case DataSharePredicatesObjectType::TYPE_INT_VECTOR: { + query.NotIn(context.field, context.intList); + break; + } + case DataSharePredicatesObjectType::TYPE_LONG_VECTOR: { + query.NotIn(context.field, context.longList); + break; + } + case DataSharePredicatesObjectType::TYPE_DOUBLE_VECTOR: { + query.NotIn(context.field, context.doubleList); + break; + } + case DataSharePredicatesObjectType::TYPE_STRING_VECTOR: { + query.NotIn(context.field, context.stringList); + break; + } + default: + status = Status::ERROR; + ZLOGE("NotIn failed: %{public}d", status); + break; + } + return status; +} + +Status KvStorePredicates::Like(const OperationItem &oper, DataQuery &query) +{ + std::string field; + int status = oper.para1.GetString(field); + if (status != E_OK) { + ZLOGE("Like field failed: %{public}d", status); + return Status::ERROR; + } + + std::string value; + status = oper.para2.GetString(value); + if (status != E_OK) { + ZLOGE("Like value failed: %{public}d", status); + return Status::ERROR; + } + query.Like(field, value); + return Status::SUCCESS; +} + +Status KvStorePredicates::Unlike(const OperationItem &oper, DataQuery &query) +{ + std::string field; + int status = oper.para1.GetString(field); + if (status != E_OK) { + ZLOGE("Unlike failed: %{public}d", status); + return Status::ERROR; + } + + std::string value; + status = oper.para2.GetString(value); + if (status != E_OK) { + ZLOGE("Unlike value failed: %{public}d", status); + return Status::ERROR; + } + query.Unlike(field, value); + return Status::SUCCESS; +} + +Status KvStorePredicates::OrderByAsc(const OperationItem &oper, DataQuery &query) +{ + std::string field; + int status = oper.para1.GetString(field); + if (status != E_OK) { + ZLOGE("OrderByAsc failed: %{public}d", status); + return Status::ERROR; + } + query.OrderByAsc(field); + return Status::SUCCESS; +} + +Status KvStorePredicates::OrderByDesc(const OperationItem &oper, DataQuery &query) +{ + std::string field; + int status = oper.para1.GetString(field); + if (status != E_OK) { + ZLOGE("OrderByDesc failed: %{public}d", status); + return Status::ERROR; + } + query.OrderByDesc(field); + return Status::SUCCESS; +} + +Status KvStorePredicates::Limit(const OperationItem &oper, DataQuery &query) +{ + int number; + int status = oper.para1.GetInt(number); + if (status != E_OK) { + ZLOGE("Limit number failed: %{public}d", status); + return Status::ERROR; + } + + int offset; + status = oper.para2.GetInt(offset); + if (status != E_OK) { + ZLOGE("Limit offset failed: %{public}d", offset); + return Status::ERROR; + } + query.Limit(number, offset); + return Status::SUCCESS; +} +} // namespace DistributedKv +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/jskitsimpl/distributeddata/include/js_util.h b/frameworks/jskitsimpl/distributeddata/include/js_util.h index 1c4489424..4fe2c3d48 100644 --- a/frameworks/jskitsimpl/distributeddata/include/js_util.h +++ b/frameworks/jskitsimpl/distributeddata/include/js_util.h @@ -22,6 +22,8 @@ #include "napi/native_api.h" #include "napi/native_common.h" #include "napi/native_node_api.h" +#include "datashare_predicates.h" +#include "datashare_values_bucket.h" namespace OHOS::DistributedData { class JSUtil final { @@ -137,6 +139,7 @@ public: static napi_status GetValue(napi_env env, napi_value in, JsSchema*& out); + /* napi_get_named_property wrapper */ template static inline napi_status GetNamedProperty(napi_env env, napi_value in, const std::string& prop, T& value) diff --git a/interfaces/innerkits/distributeddata/BUILD.gn b/interfaces/innerkits/distributeddata/BUILD.gn index 9e9050639..100b1fa45 100644 --- a/interfaces/innerkits/distributeddata/BUILD.gn +++ b/interfaces/innerkits/distributeddata/BUILD.gn @@ -33,7 +33,7 @@ config("distributeddatafwk_config") { "//foundation/distributeddatamgr/distributeddatamgr/interfaces/innerkits/app_distributeddata/include", "//foundation/distributeddatamgr/appdatamgr/interfaces/inner_api/native/data_share/provider/include", "//foundation/distributeddatamgr/appdatamgr/interfaces/inner_api/native/data_share/common/include", - "//foundation/distributeddatamgr/appdatamgr/frameworks/native/data_share/common/include" + "//foundation/distributeddatamgr/appdatamgr/frameworks/native/data_share/common/include", ] } @@ -66,17 +66,17 @@ ohos_shared_library("distributeddata_inner") { "../../../frameworks/innerkitsimpl/distributeddatafwk/src/itypes_util.cpp", "../../../frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_client.cpp", "../../../frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_client_death_observer.cpp", + "../../../frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_datashare_result_set.cpp", "../../../frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_death_recipient_impl.cpp", "../../../frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_observer_client.cpp", + "../../../frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_predicates.cpp", "../../../frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_resultset_client.cpp", "../../../frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_service_death_notifier.cpp", "../../../frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_snapshot_client.cpp", "../../../frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_sync_callback_client.cpp", "../../../frameworks/innerkitsimpl/distributeddatafwk/src/single_kvstore_client.cpp", "../../../frameworks/innerkitsimpl/distributeddatafwk/src/sync_observer.cpp", - "../../../frameworks/innerkitsimpl/distributeddatafwk/src/kvdbstore_datashare_native.cpp", - "../../../frameworks/innerkitsimpl/distributeddatafwk/src/kvdbstore_result_set.cpp", - "../../../frameworks/innerkitsimpl/distributeddatafwk/src/kvstore_predicates.cpp", + "../../../frameworks/innerkitsimpl/distributeddatafwk/src/kv_utils.cpp", "include/types.h", ] @@ -92,12 +92,12 @@ ohos_shared_library("distributeddata_inner") { configs = [ ":distributeddatafwk_config" ] deps = [ + "//foundation/distributeddatamgr/appdatamgr/frameworks/native/data_share/common:datashare_common", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter:distributeddata_adapter", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/utils:distributeddata_utils_static", "//utils/native/base:utils", - "//foundation/distributeddatamgr/appdatamgr/frameworks/native/data_share/common:datashare_common" - # "//foundation/distributeddatamgr/appdatamgr/interfaces/inner_api/native/data_share:datashare_common" ] + external_deps = [ "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", diff --git a/interfaces/innerkits/distributeddata/include/kvdbstore_datashare_native.h b/interfaces/innerkits/distributeddata/include/kv_utils.h similarity index 42% rename from interfaces/innerkits/distributeddata/include/kvdbstore_datashare_native.h rename to interfaces/innerkits/distributeddata/include/kv_utils.h index 64177e676..95db371b3 100644 --- a/interfaces/innerkits/distributeddata/include/kvdbstore_datashare_native.h +++ b/interfaces/innerkits/distributeddata/include/kv_utils.h @@ -1,43 +1,46 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef KVDBSTORE_DATASHARE_NATIVE_H -#define KVDBSTORE_DATASHARE_NATIVE_H - -#include "types.h" -#include "single_kvstore.h" -#include "datashare_predicates.h" -#include "data_query.h" -#include "datashare_abstract_result_set.h" - -namespace OHOS { -namespace DistributedKv { -using namespace DataShare; -class kvDBDataShareNative { - -public: - kvDBDataShareNative(std::shared_ptr kvStoreClient); - - ~kvDBDataShareNative() = default; - - std::shared_ptr GetDataShareResult(const DataSharePredicates &predicate); - -private: - std::shared_ptr kvStoreClient_ = nullptr; - -}; -} // namespace DistributedKv -} // namespace OHOS -#endif // KVDBSTORE_DATASHARE_NATIVE_H \ No newline at end of file +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef KV_UTILS_H +#define KV_UTILS_H + +#include "types.h" +#include "datashare_predicates.h" +#include "data_query.h" +#include "datashare_values_bucket.h" +#include "kvstore_result_set.h" +#include "result_set_bridge.h" + +namespace OHOS { +namespace DistributedKv { +class KvUtils { +public: + static std::shared_ptr ToResultSetBridge(std::shared_ptr resultSet); + static Status ToQuery(const DataShare::DataSharePredicates &predicates, DataQuery &query); + static Entry ToEntry(const DataShare::DataShareValuesBucket &valueBucket); + static std::vector ToEntries(const std::vector &Buckets); +private: + KvUtils(KvUtils &&) = delete; + KvUtils(const KvUtils &) = delete; + KvUtils &operator=(KvUtils &&) = delete; + KvUtils &operator=(const KvUtils &) = delete; + ~KvUtils() = delete; + static Status ToEntryData(const std::map &valuesMap, const std::string field, Blob &kv); + static const std::string KEY; + static const std::string VALUE; +}; +} // namespace DistributedKv +} // namespace OHOS +#endif // KV_UTILS_H \ No newline at end of file -- Gitee From 2cb1de17262ddf86ef91a529d85e2da19081d0d0 Mon Sep 17 00:00:00 2001 From: yangliu Date: Fri, 20 May 2022 20:43:02 +0800 Subject: [PATCH 2/3] modify code Signed-off-by: yangliu --- frameworks/jskitsimpl/distributeddata/include/js_util.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/frameworks/jskitsimpl/distributeddata/include/js_util.h b/frameworks/jskitsimpl/distributeddata/include/js_util.h index 4fe2c3d48..781aa1163 100644 --- a/frameworks/jskitsimpl/distributeddata/include/js_util.h +++ b/frameworks/jskitsimpl/distributeddata/include/js_util.h @@ -22,8 +22,6 @@ #include "napi/native_api.h" #include "napi/native_common.h" #include "napi/native_node_api.h" -#include "datashare_predicates.h" -#include "datashare_values_bucket.h" namespace OHOS::DistributedData { class JSUtil final { -- Gitee From b1b5d4b7b3c15850ab4b35c98a8c55d9e4102440 Mon Sep 17 00:00:00 2001 From: yangliu Date: Sat, 21 May 2022 00:23:42 +0800 Subject: [PATCH 3/3] update Signed-off-by: yangliu --- .../include/kvstore_predicates.h | 1 - .../distributeddatafwk/src/data_query.cpp | 24 ++++++++++++------- .../distributeddata/src/js_query.cpp | 19 ++++++++------- .../distributeddata/include/data_query.h | 16 ++++++------- 4 files changed, 35 insertions(+), 25 deletions(-) diff --git a/frameworks/innerkitsimpl/distributeddatafwk/include/kvstore_predicates.h b/frameworks/innerkitsimpl/distributeddatafwk/include/kvstore_predicates.h index aff65f659..5f5fbdeda 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/include/kvstore_predicates.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/include/kvstore_predicates.h @@ -13,7 +13,6 @@ * limitations under the License. */ - #ifndef KVSTORE_PREDICATE_H #define KVSTORE_PREDICATE_H diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/data_query.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/data_query.cpp index 22dd33299..3cdd03cc3 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/data_query.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/data_query.cpp @@ -336,8 +336,9 @@ DataQuery& DataQuery::IsNotNull(const std::string &field) return *this; } -DataQuery& DataQuery::InInt(const std::string &field, const std::vector &valueList) +DataQuery& DataQuery::In(const std::string &field, const std::vector &valueList) { + ZLOGD("DataQuery::In int"); std::string myField = field; if (ValidateField(myField)) { AppendCommonList(IN, TYPE_INTEGER, myField, valueList); @@ -345,8 +346,9 @@ DataQuery& DataQuery::InInt(const std::string &field, const std::vector &va return *this; } -DataQuery& DataQuery::InLong(const std::string &field, const std::vector &valueList) +DataQuery& DataQuery::In(const std::string &field, const std::vector &valueList) { + ZLOGD("DataQuery::In int64_t"); std::string myField = field; if (ValidateField(myField)) { AppendCommonList(IN, TYPE_LONG, myField, valueList); @@ -354,8 +356,9 @@ DataQuery& DataQuery::InLong(const std::string &field, const std::vector &valueList) +DataQuery& DataQuery::In(const std::string &field, const std::vector &valueList) { + ZLOGD("DataQuery::In double"); std::string myField = field; if (ValidateField(myField)) { AppendCommonList(IN, TYPE_DOUBLE, myField, valueList); @@ -363,8 +366,9 @@ DataQuery& DataQuery::InDouble(const std::string &field, const std::vector &valueList) +DataQuery& DataQuery::In(const std::string &field, const std::vector &valueList) { + ZLOGD("DataQuery::In string"); std::string myField = field; std::vector myValueList(valueList); if (ValidateField(myField)) { @@ -373,8 +377,9 @@ DataQuery& DataQuery::InString(const std::string &field, const std::vector &valueList) +DataQuery& DataQuery::NotIn(const std::string &field, const std::vector &valueList) { + ZLOGD("DataQuery::NotIn int"); std::string myField = field; if (ValidateField(myField)) { AppendCommonList(NOT_IN, TYPE_INTEGER, myField, valueList); @@ -382,8 +387,9 @@ DataQuery& DataQuery::NotInInt(const std::string &field, const std::vector return *this; } -DataQuery& DataQuery::NotInLong(const std::string &field, const std::vector &valueList) +DataQuery& DataQuery::NotIn(const std::string &field, const std::vector &valueList) { + ZLOGD("DataQuery::NotIn int64_t"); std::string myField = field; if (ValidateField(myField)) { AppendCommonList(NOT_IN, TYPE_LONG, myField, valueList); @@ -391,8 +397,9 @@ DataQuery& DataQuery::NotInLong(const std::string &field, const std::vector &valueList) +DataQuery& DataQuery::NotIn(const std::string &field, const std::vector &valueList) { + ZLOGD("DataQuery::NotIn double"); std::string myField = field; if (ValidateField(myField)) { AppendCommonList(NOT_IN, TYPE_DOUBLE, myField, valueList); @@ -400,8 +407,9 @@ DataQuery& DataQuery::NotInDouble(const std::string &field, const std::vector &valueList) +DataQuery& DataQuery::NotIn(const std::string &field, const std::vector &valueList) { + ZLOGD("DataQuery::NotIn string"); std::string myField = field; std::vector myValueList(valueList); if (ValidateField(myField)) { diff --git a/frameworks/jskitsimpl/distributeddata/src/js_query.cpp b/frameworks/jskitsimpl/distributeddata/src/js_query.cpp index 2abc634ad..feda04777 100644 --- a/frameworks/jskitsimpl/distributeddata/src/js_query.cpp +++ b/frameworks/jskitsimpl/distributeddata/src/js_query.cpp @@ -345,23 +345,25 @@ struct NumbersContext : public ContextBase { napi_value JsQuery::InNumber(napi_env env, napi_callback_info info) { + ZLOGD("Query::InNumber()"); auto ctxt = std::make_shared(); ctxt->GetNumberSync(env, info); NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& query = reinterpret_cast(ctxt->native)->query_; if (ctxt->innerType == NumberType::NUMBER_INT) { - query.InInt(ctxt->field, ctxt->intList); + query.In(ctxt->field, ctxt->intList); } else if (ctxt->innerType == NumberType::NUMBER_LONG) { - query.InLong(ctxt->field, ctxt->longList); + query.In(ctxt->field, ctxt->longList); } else if (ctxt->innerType == NumberType::NUMBER_DOUBLE) { - query.InDouble(ctxt->field, ctxt->doubleList); + query.In(ctxt->field, ctxt->doubleList); } return ctxt->self; } napi_value JsQuery::InString(napi_env env, napi_callback_info info) { + ZLOGD("Query::InString()"); struct StringsContext : public ContextBase { std::string field; std::vector valueList; @@ -379,23 +381,24 @@ napi_value JsQuery::InString(napi_env env, napi_callback_info info) NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& query = reinterpret_cast(ctxt->native)->query_; - query.InString(ctxt->field, ctxt->valueList); + query.In(ctxt->field, ctxt->valueList); return ctxt->self; } napi_value JsQuery::NotInNumber(napi_env env, napi_callback_info info) { + ZLOGD("Query::NotInNumber()"); auto ctxt = std::make_shared(); ctxt->GetNumberSync(env, info); NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& query = reinterpret_cast(ctxt->native)->query_; if (ctxt->innerType == NumberType::NUMBER_INT) { - query.NotInInt(ctxt->field, ctxt->intList); + query.NotIn(ctxt->field, ctxt->intList); } else if (ctxt->innerType == NumberType::NUMBER_LONG) { - query.NotInLong(ctxt->field, ctxt->longList); + query.NotIn(ctxt->field, ctxt->longList); } else if (ctxt->innerType == NumberType::NUMBER_DOUBLE) { - query.NotInDouble(ctxt->field, ctxt->doubleList); + query.NotIn(ctxt->field, ctxt->doubleList); } return ctxt->self; } @@ -420,7 +423,7 @@ napi_value JsQuery::NotInString(napi_env env, napi_callback_info info) NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!"); auto& query = reinterpret_cast(ctxt->native)->query_; - query.NotInString(ctxt->field, ctxt->valueList); + query.NotIn(ctxt->field, ctxt->valueList); return ctxt->self; } diff --git a/interfaces/innerkits/distributeddata/include/data_query.h b/interfaces/innerkits/distributeddata/include/data_query.h index 5a96f05c6..7eda964b0 100644 --- a/interfaces/innerkits/distributeddata/include/data_query.h +++ b/interfaces/innerkits/distributeddata/include/data_query.h @@ -262,7 +262,7 @@ public: // value: the field value list. // Return: // This Query. - API_EXPORT DataQuery &InInt(const std::string &field, const std::vector &valueList); + API_EXPORT DataQuery &In(const std::string &field, const std::vector &valueList); // In long value list. // Parameters: @@ -270,7 +270,7 @@ public: // value: the field value list. // Return: // This Query. - API_EXPORT DataQuery &InLong(const std::string &field, const std::vector &valueList); + API_EXPORT DataQuery &In(const std::string &field, const std::vector &valueList); // In Double value list. // Parameters: @@ -278,7 +278,7 @@ public: // value: the field value list. // Return: // This Query. - API_EXPORT DataQuery &InDouble(const std::string &field, const std::vector &valueList); + API_EXPORT DataQuery &In(const std::string &field, const std::vector &valueList); // In String value list. // Parameters: @@ -286,7 +286,7 @@ public: // value: the field value list. // Return: // This Query. - API_EXPORT DataQuery &InString(const std::string &field, const std::vector &valueList); + API_EXPORT DataQuery &In(const std::string &field, const std::vector &valueList); // Not in int value list. // Parameters: @@ -294,7 +294,7 @@ public: // value: the field value list. // Return: // This Query. - API_EXPORT DataQuery &NotInInt(const std::string &field, const std::vector &valueList); + API_EXPORT DataQuery &NotIn(const std::string &field, const std::vector &valueList); // Not in long value list. // Parameters: @@ -302,7 +302,7 @@ public: // value: the field value list. // Return: // This Query. - API_EXPORT DataQuery &NotInLong(const std::string &field, const std::vector &valueList); + API_EXPORT DataQuery &NotIn(const std::string &field, const std::vector &valueList); // Not in Double value list. // Parameters: @@ -310,7 +310,7 @@ public: // value: the field value list. // Return: // This Query. - API_EXPORT DataQuery &NotInDouble(const std::string &field, const std::vector &valueList); + API_EXPORT DataQuery &NotIn(const std::string &field, const std::vector &valueList); // Not in String value list. // Parameters: @@ -318,7 +318,7 @@ public: // value: the field value list. // Return: // This Query. - API_EXPORT DataQuery &NotInString(const std::string &field, const std::vector &valueList); + API_EXPORT DataQuery &NotIn(const std::string &field, const std::vector &valueList); // Like String value. // Parameters: -- Gitee