diff --git a/data_share/frameworks/native/common/include/datashare_abs_result_set.h b/data_share/frameworks/native/common/include/datashare_abs_result_set.h index f89b547322cb6a501d39901484bbfb16cde9de17..755f294a5492c5e63e8a300e914a601c481a37e4 100644 --- a/data_share/frameworks/native/common/include/datashare_abs_result_set.h +++ b/data_share/frameworks/native/common/include/datashare_abs_result_set.h @@ -16,6 +16,7 @@ #ifndef DATASHARE_ABS_RESULT_SET_H #define DATASHARE_ABS_RESULT_SET_H +#include #include #include "basic/result_set.h" @@ -56,8 +57,10 @@ protected: static const int INIT_POS = -1; // Current row position int rowPos_; + int count_; // Indicates whether the result set is closed bool isClosed_; + std::map indexCache_; }; } // namespace DataShare } // namespace OHOS diff --git a/data_share/frameworks/native/common/include/datashare_operation.h b/data_share/frameworks/native/common/include/datashare_operation.h deleted file mode 100644 index 1aa5d5700393c19ab33ca30b801c5b989b9758f6..0000000000000000000000000000000000000000 --- a/data_share/frameworks/native/common/include/datashare_operation.h +++ /dev/null @@ -1,174 +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. - */ - -#ifndef DATASHARE_OPERATION_H -#define DATASHARE_OPERATION_H - -#include -#include -#include "datashare_operation_builder.h" -#include "datashare_predicates.h" -#include "datashare_values_bucket.h" -#include "uri.h" -#include "parcel.h" - -using Uri = OHOS::Uri; - -namespace OHOS { -namespace DataShare { -class DataShareOperationBuilder; -class DataShareOperation final : public Parcelable, public std::enable_shared_from_this { -public: - ~DataShareOperation(); - - DataShareOperation( - const std::shared_ptr &dataShareOperation, const std::shared_ptr &withUri); - explicit DataShareOperation(Parcel &in); - explicit DataShareOperation(const std::shared_ptr &builder); - DataShareOperation(); - /** - * @brief Creates an operation for inserting data. - * @param uri Indicates the path of data to operate. - * @return Returns an insert DataShareOperationBuilder object. - */ - static std::shared_ptr NewInsertBuilder(const std::shared_ptr &uri); - /** - * @brief Creates an operation for updating data. - * @param uri Indicates the path of data to operate. - * @return Returns an update DataShareOperationBuilder object. - */ - static std::shared_ptr NewUpdateBuilder(const std::shared_ptr &uri); - /** - * @brief Creates an operation for deleting data. - * @param uri Indicates the path of data to operate. - * @return Returns an delete DataShareOperationBuilder object. - */ - static std::shared_ptr NewDeleteBuilder(const std::shared_ptr &uri); - /** - * @brief Creates an operation for asserting data. - * @param uri Indicates the path of data to operate. - * @return Returns an assert DataShareOperationBuilder object. - */ - static std::shared_ptr NewAssertBuilder(const std::shared_ptr &uri); - /** - * @brief Obtains the value of the type attribute included in this DataShareOperation. - * @return Returns the type included in this DataShareOperation. - */ - int GetType() const; - /** - * @brief Obtains the value of the uri attribute included in this DataShareOperation. - * @return Returns the uri included in this DataShareOperation. - */ - std::shared_ptr GetUri() const; - /** - * @brief Obtains the value of the databaseValuesBucket attribute included in this DataShareOperation. - * @return Returns the databaseValuesBucket included in this DataShareOperation. - */ - std::shared_ptr GetValuesBucket() const; - /** - * @brief Obtains the value of the expectedCount attribute included in this DataShareOperation. - * @return Returns the expectedCount included in this DataShareOperation. - */ - int GetExpectedCount() const; - /** - * @brief Obtains the value of the dataBasePredicates attribute included in this DataShareOperation. - * @return Returns the dataBasePredicates included in this DataShareOperation. - */ - std::shared_ptr GetDataSharePredicates() const; - /** - * @brief Obtains the value of the valuesBucketReferences attribute included in this DataShareOperation. - * @return Returns the valuesBucketReferences included in this DataShareOperation. - */ - std::shared_ptr GetValuesBucketReferences() const; - /** - * @brief Obtains the value of the dataSharePredicatesBackReferences attribute included in this - * DataShareOperation. - * @return Returns the dataSharePredicatesBackReferences included in this DataShareOperation. - */ - std::map GetDataSharePredicatesBackReferences() const; - /** - * @brief Checks whether an insert operation is created. - * @return Returns true if it is an insert operation; returns false otherwise. - */ - bool IsInsertOperation() const; - /** - * @brief Checks whether an delete operation is created. - * @return Returns true if it is an delete operation; returns false otherwise. - */ - bool IsDeleteOperation() const; - /** - * @brief Checks whether an update operation is created. - * @return Returns true if it is an update operation; returns false otherwise. - */ - bool IsUpdateOperation() const; - /** - * @brief Checks whether an assert operation is created. - * @return Returns true if it is an assert operation; returns false otherwise. - */ - bool IsAssertOperation() const; - /** - * @brief Checks whether an operation can be interrupted. - * @return Returns true if the operation can be interrupted; returns false otherwise. - */ - bool IsInterruptionAllowed() const; - - bool operator==(const DataShareOperation &other) const; - DataShareOperation &operator=(const DataShareOperation &other); - bool Marshalling(Parcel &out) const; - static DataShareOperation *Unmarshalling(Parcel &in); - - /** - * @brief Creates a DataShareOperation instance based on the given Parcel object - * @param in Indicates the Parcel object. - * @return Returns the DataShareOperation object. - */ - static std::shared_ptr CreateFromParcel(Parcel &in); - -public: - static constexpr int TYPE_INSERT = 1; - static constexpr int TYPE_UPDATE = 2; - static constexpr int TYPE_DELETE = 3; - static constexpr int TYPE_ASSERT = 4; - -private: - void PutMap(Parcel &in); - bool ReadFromParcel(Parcel &in); - bool Marshalling(Parcel &out, const std::shared_ptr &uri) const; - bool ReadFromParcel(Parcel &in, std::shared_ptr &uri); - bool Marshalling(Parcel &out, const std::shared_ptr &valuesBucket) const; - bool ReadFromParcel(Parcel &in, std::shared_ptr &valuesBucket); - bool Marshalling(Parcel &out, const std::shared_ptr &dataSharePredicates) const; - bool ReadFromParcel(Parcel &in, std::shared_ptr &dataSharePredicates); - bool Marshalling(Parcel &out, const std::map &dataSharePredicatesBackReferences) const; - bool ReadFromParcel(Parcel &in, std::map &dataSharePredicatesBackReferences); - -private: - // no object in parcel - static constexpr int VALUE_NULL = 0; - // object exist in parcel - static constexpr int VALUE_OBJECT = 1; - static constexpr int REFERENCE_THRESHOLD = 3 * 1024 * 1024; - int type_ = -1; - int expectedCount_ = 0; - bool interrupted_ = false; - std::shared_ptr uri_ = nullptr; - std::shared_ptr valuesBucket_ = nullptr; - std::shared_ptr dataSharePredicates_ = nullptr; - std::shared_ptr valuesBucketReferences_ = nullptr; - std::map dataSharePredicatesBackReferences_; -}; -} // namespace DataShare -} // namespace OHOS -#endif // DATASHARE_OPERATION_H \ No newline at end of file diff --git a/data_share/frameworks/native/common/include/datashare_operation_builder.h b/data_share/frameworks/native/common/include/datashare_operation_builder.h deleted file mode 100644 index 63948de73fbe598e6d4fa6f307ad72f1aac3e0b9..0000000000000000000000000000000000000000 --- a/data_share/frameworks/native/common/include/datashare_operation_builder.h +++ /dev/null @@ -1,98 +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. - */ - -#ifndef DATASHARE_OPERATION_BUILDER_H -#define DATASHARE_OPERATION_BUILDER_H - -#include -#include -#include "datashare_predicates.h" -#include "datashare_values_bucket.h" -#include "uri.h" -#include "parcel.h" - -using Uri = OHOS::Uri; -namespace OHOS { -namespace DataShare { -class DataShareOperation; -class DataShareOperationBuilder final : public std::enable_shared_from_this { - friend class DataShareOperation; - -public: - DataShareOperationBuilder(const int type, const std::shared_ptr &uri); - ~DataShareOperationBuilder(); - /** - * @brief Creates a DataShareOperation object. - * @return Returns the DataShareOperation object. - */ - std::shared_ptr Build(); - /** - * @brief Sets the data records to be inserted or updated. - * @param values Indicates the data values to be set. - * @return Returns a DataShareOperationBuilder object containing the given values parameter. - */ - std::shared_ptr WithValuesBucket(std::shared_ptr &values); - /** - * @brief Sets filter criteria used for deleting updating or assert query data. - * @param predicates Indicates the filter criteria to set. If this parameter is null, all data records will be - * operated by default. - * @return Returns an object containing the given filter criteria. - */ - std::shared_ptr WithPredicates(std::shared_ptr &predicates); - /** - * @brief Sets the expected number of rows to update ,delete or assert query. - * @param count Indicates the expected number of rows to update or delete. - * @return Returns a DataShareOperationBuilder object containing the given count parameter. - */ - std::shared_ptr WithExpectedCount(int count); - /** - * @brief Adds a back reference to be used as a filter criterion in withPredicates(DataSharePredicates). - * @param requestArgIndex Indicates the index referencing the predicate parameter whose value is to be replaced. - * @param previousResult Indicates the index referencing the historical DataShareResult used to replace the value - * of the specified predicate parameter. - * @return Returns a DataShareOperationBuilder object containing the given requestArgIndex and previousResult - * parameters. - */ - std::shared_ptr WithPredicatesBackReference(int requestArgIndex, int previousResult); - /** - * @brief Adds a back reference to be used in withValuesBucket(DataShareValuesBucket). - * @param backReferences Indicates the DataShareValuesBucket object containing a set of key-value pairs. - * In each pair, the key specifies the value to be updated and the value specifies. - * In each pair, the replace the specified value. This parameter cannot be null. - * @return Returns a DataShareOperationBuilder object containing the given backReferences parameter. - */ - std::shared_ptr WithValueBackReferences( - std::shared_ptr &backReferences); - /** - * @brief Sets an interrupt flag bit for a batch operation, which can be insert, update, delete, or assert. - * @param interrupted Specifies whether a batch operation can be interrupted. The value true indicates that the - * operation can be interrupted, and false indicates the opposite. - * @return Returns a DataShareOperationBuilder object containing the given interrupted parameter. - */ - std::shared_ptr WithInterruptionAllowed(bool interrupted); - -private: - int type_; - int expectedCount_; - bool interrupted_; - std::shared_ptr uri_; - std::shared_ptr valuesBucket_; - std::shared_ptr dataSharePredicates_; - std::shared_ptr valuesBucketReferences_; - std::map dataSharePredicatesBackReferences_; -}; -} // namespace DataShare -} // namespace OHOS -#endif // DATASHARE_OPERATION_BUILDER_H \ No newline at end of file diff --git a/data_share/frameworks/native/common/include/datashare_result.h b/data_share/frameworks/native/common/include/datashare_result.h deleted file mode 100644 index 3490e2d50afe7e7b3c852cc9e8f70b72db074153..0000000000000000000000000000000000000000 --- a/data_share/frameworks/native/common/include/datashare_result.h +++ /dev/null @@ -1,106 +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. - */ - -#ifndef DATASHARE_RESULT_H -#define DATASHARE_RESULT_H - -#include "parcel.h" -#include "uri.h" - -using Uri = OHOS::Uri; - -namespace OHOS { -namespace DataShare { -class DataShareResult final : public Parcelable { -public: - /** - * @brief A constructor used to create a DataShareResult instance - * with the input parameter count specified. - */ - explicit DataShareResult(int count); - - /** - * @brief A constructor used to create a DataShareResult instance - * with the input parameter uri specified - */ - explicit DataShareResult(const Uri &uri); - - /** - * @brief A constructor used to create a DataShareResult instance - * with a Parcel object specified. - */ - explicit DataShareResult(Parcel &parcel); - - /** - * @brief A constructor used to create a DataShareResult instance - * with input parameters uri, count, and failure specified. - */ - DataShareResult(const Uri &uri, int count); - - ~DataShareResult(); - - /** - * @brief Obtains the Uri object corresponding to the operation. - * @return Obtains the Uri object corresponding to the operation. - */ - Uri GetUri(); - - /** - * @brief Obtains the number of rows affected by the operation. - * @return Returns the number of rows affected by the operation. - */ - int GetCount(); - - /** - * @brief Prints out a string containing the class object information. - * @return Returns object information. - */ - std::string ToString(); - - /** - * @brief Marshals a DataShareResult object into a Parcel. - * @param parcel Indicates the Parcel object for marshalling. - * @return Returns true if the marshalling is successful; returns false otherwise. - */ - virtual bool Marshalling(Parcel &parcel) const; - - /** - * @brief Unmarshals a DataShareResult object from a Parcel. - * @param parcel Indicates the Parcel object for unmarshalling. - * @return Returns true if the unmarshalling is successful; returns false otherwise. - */ - static DataShareResult *Unmarshalling(Parcel &parcel); - - /** - * @brief Creates a DataShareResult instance based on the given Parcel object. - * Used to transfer DataShareResult object using Parcel. - * @param parcel Indicates the Parcel object. - * @return Returns the DataShareResult object. - */ - static DataShareResult *CreateFromParcel(Parcel &parcel); - -private: - Uri uri_; - int count_; - - bool ReadFromParcel(Parcel &parcel); - // no object in parcel - static constexpr int VALUE_NULL = -1; - // object exist in parcel - static constexpr int VALUE_OBJECT = 1; -}; -} // namespace DataShare -} // namespace OHOS -#endif // DATASHARE_RESULT_H diff --git a/data_share/frameworks/native/common/include/ishared_result_set_proxy.h b/data_share/frameworks/native/common/include/ishared_result_set_proxy.h index 52e4dd098517b7062245c426bbe91da9e17db456..a979fd217bb2098855bc07faf37fc2f12f87c80f 100644 --- a/data_share/frameworks/native/common/include/ishared_result_set_proxy.h +++ b/data_share/frameworks/native/common/include/ishared_result_set_proxy.h @@ -32,6 +32,7 @@ public: bool OnGo(int startRowIndex, int targetRowIndex, int *cachedIndex = nullptr) override; int Close() override; private: + void ReportEventToSuspendManager(const std::string &uriString) const; std::mutex mutex_; static BrokerDelegator delegator_; std::vector columnNames_; diff --git a/data_share/frameworks/native/common/include/shared_block.h b/data_share/frameworks/native/common/include/shared_block.h index 9a79b15656ef9c031e3f6eed1e87ee836fa9ec6c..1230171c00e05fb17989e153451dc6f3fb6f7953 100644 --- a/data_share/frameworks/native/common/include/shared_block.h +++ b/data_share/frameworks/native/common/include/shared_block.h @@ -293,7 +293,7 @@ private: */ uint32_t Alloc(size_t size, bool aligned = false); - uint32_t *GetRowOffset(uint32_t row); + inline uint32_t *GetRowOffset(uint32_t row); uint32_t *AllocRowOffset(); @@ -304,7 +304,16 @@ private: uint32_t OffsetFromPtr(void *ptr); - void *OffsetToPtr(uint32_t offset, uint32_t bufferSize = 0); + inline void *OffsetToPtr(uint32_t offset, uint32_t bufferSize = 0) + { + if (offset >= mSize) { + return nullptr; + } + if (offset + bufferSize > mSize) { + return nullptr; + } + return static_cast(mData) + offset; + } /** * Convert utf8 string to utf16. diff --git a/data_share/frameworks/native/common/src/datashare_abs_result_set.cpp b/data_share/frameworks/native/common/src/datashare_abs_result_set.cpp index 71f531f016a85657a8c4bf674cd914cebd118789..d006905e9597ab2e253c02f1a0040eed2e49a67b 100644 --- a/data_share/frameworks/native/common/src/datashare_abs_result_set.cpp +++ b/data_share/frameworks/native/common/src/datashare_abs_result_set.cpp @@ -24,7 +24,7 @@ namespace OHOS { namespace DataShare { -DataShareAbsResultSet::DataShareAbsResultSet() : rowPos_(INIT_POS), isClosed_(false) +DataShareAbsResultSet::DataShareAbsResultSet() : rowPos_(INIT_POS), count_(-1), isClosed_(false) {} DataShareAbsResultSet::~DataShareAbsResultSet() {} @@ -176,18 +176,25 @@ int DataShareAbsResultSet::IsEnded(bool &result) int DataShareAbsResultSet::GetColumnCount(int &count) { - std::vector columnNames; - int ret = GetAllColumnNames(columnNames); - if (ret != E_OK) { - LOG_ERROR("return GetAllColumnNames ret is wrong!"); - return ret; + if (count_ == -1) { + std::vector columnNames; + int ret = GetAllColumnNames(columnNames); + if (ret != E_OK) { + LOG_ERROR("return GetAllColumnNames ret is wrong!"); + return ret; + } + count_ = static_cast(columnNames.size()); } - count = static_cast(columnNames.size()); + count = count_; return E_OK; } int DataShareAbsResultSet::GetColumnIndex(const std::string &columnName, int &columnIndex) { + if (indexCache_.find(columnName) != indexCache_.end()) { + columnIndex = indexCache_[columnName]; + return E_OK; + } auto periodIndex = columnName.rfind('.'); std::string columnNameLower = columnName; if (periodIndex != std::string::npos) { @@ -206,6 +213,7 @@ int DataShareAbsResultSet::GetColumnIndex(const std::string &columnName, int &co std::string lowerName = name; transform(name.begin(), name.end(), lowerName.begin(), ::tolower); if (lowerName == columnNameLower) { + indexCache_[columnName] = columnIndex; return E_OK; } columnIndex++; diff --git a/data_share/frameworks/native/common/src/datashare_operation.cpp b/data_share/frameworks/native/common/src/datashare_operation.cpp deleted file mode 100644 index 83cbdcb28d12bd167d7e6a1687f962edd6243fd6..0000000000000000000000000000000000000000 --- a/data_share/frameworks/native/common/src/datashare_operation.cpp +++ /dev/null @@ -1,494 +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. - */ - -#include "datashare_operation.h" -#include "datashare_log.h" -#include "itypes_utils.h" - -namespace OHOS { -namespace DataShare { -DataShareOperation::DataShareOperation( - const std::shared_ptr &dataShareOperation, const std::shared_ptr &withUri) -{ - uri_ = withUri; - if (dataShareOperation != nullptr) { - type_ = dataShareOperation->type_; - valuesBucket_ = dataShareOperation->valuesBucket_; - expectedCount_ = dataShareOperation->expectedCount_; - dataSharePredicates_ = dataShareOperation->dataSharePredicates_; - valuesBucketReferences_ = dataShareOperation->valuesBucketReferences_; - dataSharePredicatesBackReferences_ = dataShareOperation->dataSharePredicatesBackReferences_; - interrupted_ = dataShareOperation->interrupted_; - } else { - type_ = 0; - expectedCount_ = 0; - valuesBucket_ = std::make_shared(); - dataSharePredicates_ = std::make_shared(); - valuesBucketReferences_ = std::make_shared(); - dataSharePredicatesBackReferences_.clear(); - interrupted_ = false; - } -} -DataShareOperation::DataShareOperation(Parcel &in) -{ - ReadFromParcel(in); -} -DataShareOperation::DataShareOperation(const std::shared_ptr &builder) -{ - if (builder != nullptr) { - type_ = builder->type_; - uri_ = builder->uri_; - valuesBucket_ = builder->valuesBucket_; - expectedCount_ = builder->expectedCount_; - dataSharePredicates_ = builder->dataSharePredicates_; - valuesBucketReferences_ = builder->valuesBucketReferences_; - dataSharePredicatesBackReferences_ = builder->dataSharePredicatesBackReferences_; - interrupted_ = builder->interrupted_; - } -} - -DataShareOperation::DataShareOperation() -{ - type_ = 0; - uri_ = nullptr; - expectedCount_ = 0; - valuesBucket_ = std::make_shared(); - dataSharePredicates_ = std::make_shared(); - valuesBucketReferences_ = std::make_shared(); - dataSharePredicatesBackReferences_.clear(); - interrupted_ = false; -} - -DataShareOperation::~DataShareOperation() -{ - dataSharePredicatesBackReferences_.clear(); -} - -bool DataShareOperation::operator==(const DataShareOperation &other) const -{ - if (type_ != other.type_) { - return false; - } - if ((uri_ != nullptr) && (other.uri_ != nullptr) && (uri_->ToString() != other.uri_->ToString())) { - return false; - } - if (expectedCount_ != other.expectedCount_) { - return false; - } - if (valuesBucket_ != other.valuesBucket_) { - return false; - } - if (dataSharePredicates_ != other.dataSharePredicates_) { - return false; - } - if (valuesBucketReferences_ != other.valuesBucketReferences_) { - return false; - } - size_t backReferencesCount = dataSharePredicatesBackReferences_.size(); - size_t otherBackReferencesCount = other.dataSharePredicatesBackReferences_.size(); - if (backReferencesCount != otherBackReferencesCount) { - return false; - } - - std::map::const_iterator it = dataSharePredicatesBackReferences_.begin(); - while (it != dataSharePredicatesBackReferences_.end()) { - std::map::const_iterator otherIt = other.dataSharePredicatesBackReferences_.find(it->first); - if (otherIt != other.dataSharePredicatesBackReferences_.end()) { - if (otherIt->second != it->second) { - return false; - } - } else { - return false; - } - ++it; - } - - if (interrupted_ != other.interrupted_) { - return false; - } - return true; -} - -DataShareOperation &DataShareOperation::operator=(const DataShareOperation &other) -{ - if (this != &other) { - type_ = other.type_; - uri_ = other.uri_; - expectedCount_ = other.expectedCount_; - valuesBucket_ = other.valuesBucket_; - dataSharePredicates_ = other.dataSharePredicates_; - valuesBucketReferences_ = other.valuesBucketReferences_; - dataSharePredicatesBackReferences_ = other.dataSharePredicatesBackReferences_; - interrupted_ = other.interrupted_; - } - return *this; -} - -std::shared_ptr DataShareOperation::NewInsertBuilder(const std::shared_ptr &uri) -{ - LOG_DEBUG("Start"); - if (uri == nullptr) { - LOG_ERROR("uri is nullptr"); - return nullptr; - } - std::shared_ptr builder = - std::make_shared(TYPE_INSERT, uri); - return builder; -} - -std::shared_ptr DataShareOperation::NewUpdateBuilder(const std::shared_ptr &uri) -{ - LOG_DEBUG("Start"); - if (uri == nullptr) { - LOG_ERROR("uri is nullptr"); - return nullptr; - } - std::shared_ptr builder = - std::make_shared(TYPE_UPDATE, uri); - return builder; -} - -std::shared_ptr DataShareOperation::NewDeleteBuilder(const std::shared_ptr &uri) -{ - LOG_DEBUG("Start"); - if (uri == nullptr) { - LOG_ERROR("uri is nullptr"); - return nullptr; - } - std::shared_ptr builder = - std::make_shared(TYPE_DELETE, uri); - return builder; -} - -std::shared_ptr DataShareOperation::NewAssertBuilder(const std::shared_ptr &uri) -{ - LOG_DEBUG("Start"); - if (uri == nullptr) { - LOG_ERROR("uri is nullptr"); - return nullptr; - } - std::shared_ptr builder = - std::make_shared(TYPE_ASSERT, uri); - return builder; -} - -int DataShareOperation::GetType() const -{ - return type_; -} - -std::shared_ptr DataShareOperation::GetUri() const -{ - return uri_; -} - -std::shared_ptr DataShareOperation::GetValuesBucket() const -{ - return valuesBucket_; -} - -int DataShareOperation::GetExpectedCount() const -{ - return expectedCount_; -} - -std::shared_ptr DataShareOperation::GetDataSharePredicates() const -{ - return dataSharePredicates_; -} - -std::shared_ptr DataShareOperation::GetValuesBucketReferences() const -{ - return valuesBucketReferences_; -} -std::map DataShareOperation::GetDataSharePredicatesBackReferences() const -{ - return dataSharePredicatesBackReferences_; -} -bool DataShareOperation::IsInsertOperation() const -{ - LOG_DEBUG("type_ == TYPE_INSERT : %{public}d", type_ == TYPE_INSERT); - return type_ == TYPE_INSERT; -} -bool DataShareOperation::IsUpdateOperation() const -{ - LOG_DEBUG("type_ == TYPE_INSERT : %{public}d", type_ == TYPE_UPDATE); - return type_ == TYPE_UPDATE; -} -bool DataShareOperation::IsDeleteOperation() const -{ - LOG_DEBUG("type_ == TYPE_INSERT : %{public}d", type_ == TYPE_DELETE); - return type_ == TYPE_DELETE; -} -bool DataShareOperation::IsAssertOperation() const -{ - LOG_DEBUG("type_ == TYPE_INSERT : %{public}d", type_ == TYPE_ASSERT); - return type_ == TYPE_ASSERT; -} -bool DataShareOperation::IsInterruptionAllowed() const -{ - LOG_DEBUG("interrupted_ : %{public}d", interrupted_); - return interrupted_; -} -bool DataShareOperation::Marshalling(Parcel &out) const -{ - LOG_DEBUG("Start"); - if (!out.WriteInt32(type_)) { - LOG_ERROR("Write type_ error"); - return false; - } - if (!out.WriteInt32(expectedCount_)) { - LOG_ERROR("Write expectedCount_ error"); - return false; - } - - if (!out.WriteBool(interrupted_)) { - LOG_ERROR("Write interrupted_ error"); - return false; - } - - if (!Marshalling(out, uri_) || !Marshalling(out, valuesBucket_) || !Marshalling(out, dataSharePredicates_) || - !Marshalling(out, valuesBucketReferences_) || !Marshalling(out, dataSharePredicatesBackReferences_)) { - return false; - } - - LOG_DEBUG("End successfully"); - return true; -} -DataShareOperation *DataShareOperation::Unmarshalling(Parcel &in) -{ - LOG_DEBUG("Start"); - DataShareOperation *dataShareOperation = new (std::nothrow) DataShareOperation(); - if (dataShareOperation != nullptr && !dataShareOperation->ReadFromParcel(in)) { - LOG_ERROR("Read dataShareOperation error"); - delete dataShareOperation; - dataShareOperation = nullptr; - } - LOG_DEBUG("End"); - return dataShareOperation; -} -bool DataShareOperation::ReadFromParcel(Parcel &in) -{ - LOG_DEBUG("Start"); - if (!in.ReadInt32(type_)) { - LOG_ERROR("Read type_ error"); - return false; - } - if (!in.ReadInt32(expectedCount_)) { - LOG_ERROR("Read expectedCount_ error"); - return false; - } - interrupted_ = in.ReadBool(); - if (!ReadFromParcel(in, uri_) || !ReadFromParcel(in, valuesBucket_) || !ReadFromParcel(in, dataSharePredicates_) || - !ReadFromParcel(in, valuesBucketReferences_) || !ReadFromParcel(in, dataSharePredicatesBackReferences_)) { - return false; - } - return true; -} -std::shared_ptr DataShareOperation::CreateFromParcel(Parcel &in) -{ - LOG_DEBUG("Start"); - std::shared_ptr operation = std::make_shared(in); - return operation; -} -void DataShareOperation::PutMap(Parcel &in) -{ - LOG_DEBUG("Start"); - int count = in.ReadInt32(); - if (count > 0 && count < REFERENCE_THRESHOLD) { - for (int i = 0; i < count; ++i) { - dataSharePredicatesBackReferences_.insert(std::make_pair(in.ReadInt32(), in.ReadInt32())); - } - return; - } - LOG_WARN("count <= 0 or count >= REFERENCE_THRESHOLD"); -} - -bool DataShareOperation::Marshalling(Parcel &out, const std::shared_ptr &uri) const -{ - if (uri != nullptr) { - if (!out.WriteInt32(VALUE_OBJECT)) { - LOG_ERROR("Write VALUE_OBJECT error"); - return false; - } - - if (!out.WriteParcelable(uri.get())) { - LOG_ERROR("Write uri error"); - return false; - } - } else { - if (!out.WriteInt32(VALUE_NULL)) { - LOG_ERROR("Write VALUE_NULL error"); - return false; - } - } - return true; -} - -bool DataShareOperation::ReadFromParcel(Parcel &in, std::shared_ptr &uri) -{ - int isEmpty = VALUE_NULL; - if (!in.ReadInt32(isEmpty)) { - LOG_ERROR("Read isEmpty error"); - return false; - } - if (isEmpty == VALUE_OBJECT) { - uri.reset(in.ReadParcelable()); - } else { - uri.reset(); - } - return true; -} - -bool DataShareOperation::Marshalling(Parcel &out, const std::shared_ptr &valuesBucket) const -{ - if (valuesBucket != nullptr) { - if (!out.WriteInt32(VALUE_OBJECT)) { - LOG_ERROR("Write VALUE_OBJECT error"); - return false; - } - - if (!ITypesUtils::Marshalling(*valuesBucket, out)) { - LOG_ERROR("Write valuesBucket error"); - return false; - } - } else { - if (!out.WriteInt32(VALUE_NULL)) { - LOG_ERROR("Write VALUE_NULL error"); - return false; - } - } - return true; -} - -bool DataShareOperation::ReadFromParcel(Parcel &in, std::shared_ptr &valuesBucket) -{ - int isEmpty = VALUE_NULL; - if (!in.ReadInt32(isEmpty)) { - LOG_ERROR("Read isEmpty error"); - return false; - } - if (isEmpty == VALUE_OBJECT) { - DataShareValuesBucket vb; - if (!ITypesUtils::Unmarshalling(in, vb)) { - return false; - } - valuesBucket.reset(&vb); - } else { - valuesBucket.reset(); - } - return true; -} - -bool DataShareOperation::Marshalling(Parcel &out, const std::shared_ptr &dataSharePredicates) const -{ - if (dataSharePredicates != nullptr) { - if (!out.WriteInt32(VALUE_OBJECT)) { - LOG_ERROR("Write VALUE_OBJECT error"); - return false; - } - if (!ITypesUtils::Marshalling(*dataSharePredicates, out)) { - LOG_ERROR("Write dataSharePredicates error"); - return false; - } - } else { - if (!out.WriteInt32(VALUE_NULL)) { - LOG_ERROR("Write VALUE_NULL error"); - return false; - } - } - return true; -} - -bool DataShareOperation::ReadFromParcel(Parcel &in, std::shared_ptr &dataSharePredicates) -{ - int isEmpty = VALUE_NULL; - if (!in.ReadInt32(isEmpty)) { - LOG_ERROR("Read isEmpty error"); - return false; - } - if (isEmpty == VALUE_OBJECT) { - DataSharePredicates tmpPredicates; - if (!ITypesUtils::Unmarshalling(in, tmpPredicates)) { - return false; - } - dataSharePredicates.reset(&tmpPredicates); - } else { - dataSharePredicates.reset(); - } - return true; -} - -bool DataShareOperation::Marshalling(Parcel &out, const std::map &dataSharePredicatesBackReferences) const -{ - int referenceSize = 0; - if (!dataSharePredicatesBackReferences.empty()) { - referenceSize = (int)dataSharePredicatesBackReferences.size(); - if (!out.WriteInt32(referenceSize)) { - LOG_ERROR("Write referenceSize error"); - return false; - } - if (referenceSize >= REFERENCE_THRESHOLD) { - LOG_INFO("referenceSize >= REFERENCE_THRESHOLD"); - return true; - } - for (auto &it : dataSharePredicatesBackReferences) { - if (!out.WriteInt32(it.first)) { - LOG_ERROR("Write first error"); - return false; - } - if (!out.WriteInt32(it.second)) { - LOG_ERROR("Write second error"); - return false; - } - } - } else { - LOG_DEBUG("dataSharePredicatesBackReferences is empty"); - if (!out.WriteInt32(referenceSize)) { - LOG_ERROR("Write referenceSize error"); - return false; - } - } - return true; -} - -bool DataShareOperation::ReadFromParcel(Parcel &in, std::map &dataSharePredicatesBackReferences) -{ - int referenceSize = 0; - if (!in.ReadInt32(referenceSize)) { - LOG_ERROR("Read referenceSize error"); - return false; - } - if (referenceSize >= REFERENCE_THRESHOLD) { - LOG_INFO("referenceSize >= REFERENCE_THRESHOLD"); - return true; - } - for (int i = 0; i < REFERENCE_THRESHOLD && i < referenceSize; ++i) { - int first = 0; - int second = 0; - if (!in.ReadInt32(first)) { - LOG_ERROR("Read first error"); - return false; - } - if (!in.ReadInt32(second)) { - LOG_ERROR("Read second error"); - return false; - } - dataSharePredicatesBackReferences.insert(std::make_pair(first, second)); - } - return true; -} -} // namespace DataShare -} // namespace OHOS \ No newline at end of file diff --git a/data_share/frameworks/native/common/src/datashare_operation_builder.cpp b/data_share/frameworks/native/common/src/datashare_operation_builder.cpp deleted file mode 100644 index 6b019602081183cc520ffefab2fafba0576cade7..0000000000000000000000000000000000000000 --- a/data_share/frameworks/native/common/src/datashare_operation_builder.cpp +++ /dev/null @@ -1,134 +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. - */ - -#include "datashare_operation_builder.h" -#include "datashare_log.h" -#include "datashare_operation.h" -#include "datashare_value_object.h" -#include "datashare_values_bucket.h" -namespace OHOS::DataShare { class DataSharePredicates; } - -namespace OHOS { -namespace DataShare { -DataShareOperationBuilder::DataShareOperationBuilder(const int type, const std::shared_ptr &uri) -{ - type_ = type; - uri_ = uri; - expectedCount_ = 0; - interrupted_ = false; - valuesBucket_ = nullptr; - dataSharePredicates_ = nullptr; - valuesBucketReferences_ = nullptr; - dataSharePredicatesBackReferences_.clear(); -} -DataShareOperationBuilder::~DataShareOperationBuilder() -{ - dataSharePredicatesBackReferences_.clear(); -} - -std::shared_ptr DataShareOperationBuilder::Build() -{ - LOG_DEBUG("Start"); - if (type_ != DataShareOperation::TYPE_UPDATE || (valuesBucket_ != nullptr && !valuesBucket_->IsEmpty())) { - std::shared_ptr operation = std::make_shared(shared_from_this()); - LOG_DEBUG("return operation"); - return operation; - } - LOG_ERROR("return nullptr"); - return nullptr; -} -std::shared_ptr DataShareOperationBuilder::WithValuesBucket( - std::shared_ptr &values) -{ - LOG_DEBUG("Start"); - if (type_ != DataShareOperation::TYPE_INSERT && type_ != DataShareOperation::TYPE_UPDATE && - type_ != DataShareOperation::TYPE_ASSERT) { - LOG_ERROR("Only inserts, updates can have values, type=%{public}d", type_); - return nullptr; - } - - valuesBucket_ = std::make_shared(values->valuesMap); - LOG_DEBUG("End successfully"); - return shared_from_this(); -} - -std::shared_ptr DataShareOperationBuilder::WithPredicates( - std::shared_ptr &predicates) -{ - LOG_DEBUG("Start"); - if (type_ != DataShareOperation::TYPE_DELETE && type_ != DataShareOperation::TYPE_UPDATE && - type_ != DataShareOperation::TYPE_ASSERT) { - LOG_ERROR("Only deletes and updates can have selections, type=%{public}d", type_); - return nullptr; - } - dataSharePredicates_ = predicates; - LOG_DEBUG("End successfully"); - return shared_from_this(); -} -std::shared_ptr DataShareOperationBuilder::WithExpectedCount(int count) -{ - LOG_DEBUG("Start"); - LOG_INFO("expectedCount:%{public}d", count); - if (type_ != DataShareOperation::TYPE_UPDATE && type_ != DataShareOperation::TYPE_DELETE && - type_ != DataShareOperation::TYPE_ASSERT) { - LOG_ERROR("Only updates, deletes can have expected counts, type=%{public}d", type_); - return nullptr; - } - expectedCount_ = count; - LOG_DEBUG("End successfully"); - return shared_from_this(); -} -std::shared_ptr DataShareOperationBuilder::WithPredicatesBackReference( - int requestArgIndex, int previousResult) -{ - LOG_DEBUG("Start"); - LOG_INFO("requestArgIndex=%{public}d, previousResult=%{public}d", requestArgIndex, previousResult); - if (type_ != DataShareOperation::TYPE_UPDATE && type_ != DataShareOperation::TYPE_DELETE && - type_ != DataShareOperation::TYPE_ASSERT) { - LOG_ERROR("Only updates, deletes, and asserts can have select back-references, type=%{public}d", type_); - return nullptr; - } - dataSharePredicatesBackReferences_.insert(std::make_pair(requestArgIndex, previousResult)); - LOG_DEBUG("End successfully"); - return shared_from_this(); -} -std::shared_ptr DataShareOperationBuilder::WithValueBackReferences( - std::shared_ptr &backReferences) -{ - LOG_DEBUG("Start"); - if (type_ != DataShareOperation::TYPE_INSERT && type_ != DataShareOperation::TYPE_UPDATE && - type_ != DataShareOperation::TYPE_ASSERT) { - LOG_ERROR("Only inserts, updates, and asserts can have value back-references, type=%{public}d", type_); - return nullptr; - } - valuesBucketReferences_ = backReferences; - LOG_DEBUG("End successfully"); - return shared_from_this(); -} -std::shared_ptr DataShareOperationBuilder::WithInterruptionAllowed(bool interrupted) -{ - LOG_DEBUG("Start"); - LOG_INFO("interrupted=%{public}d", interrupted); - if (type_ != DataShareOperation::TYPE_INSERT && type_ != DataShareOperation::TYPE_UPDATE && - type_ != DataShareOperation::TYPE_ASSERT && type_ != DataShareOperation::TYPE_DELETE) { - LOG_ERROR("Only inserts, updates, delete, and asserts can have value back-references, type=%{public}d", type_); - return nullptr; - } - interrupted_ = interrupted; - LOG_DEBUG("End successfully"); - return shared_from_this(); -} -} // namespace DataShare -} // namespace OHOS diff --git a/data_share/frameworks/native/common/src/datashare_result.cpp b/data_share/frameworks/native/common/src/datashare_result.cpp deleted file mode 100644 index 8a5b56612fe3369398a677720335be3b0f3224b9..0000000000000000000000000000000000000000 --- a/data_share/frameworks/native/common/src/datashare_result.cpp +++ /dev/null @@ -1,166 +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. - */ - -#include "datashare_result.h" - -#include "datashare_log.h" - -namespace OHOS { -namespace DataShare { -/** - * @brief A constructor used to create a DataShareResult instance - * with the input parameter count specified. - */ -DataShareResult::DataShareResult(int count) : uri_("") -{ - count_ = count; -} - -/** - * @brief A constructor used to create a DataShareResult instance - * with a Parcel object specified. - */ -DataShareResult::DataShareResult(Parcel &parcel) : uri_(""), count_(0) -{ - ReadFromParcel(parcel); -} - -/** - * @brief A constructor used to create a DataShareResult instance - * with the input parameter uri specified - */ -DataShareResult::DataShareResult(const Uri &uri) : uri_(uri.ToString()), count_(0) -{} - -/** - * @brief A constructor used to create a DataShareResult instance - * with input parameters uri, count, and failure specified. - */ -DataShareResult::DataShareResult(const Uri &uri, int count) : uri_(uri.ToString()) -{ - count_ = count; -} - -DataShareResult::~DataShareResult() -{} - -/** - * @brief Obtains the Uri object corresponding to the operation. - * @return Obtains the Uri object corresponding to the operation. - */ -Uri DataShareResult::GetUri() -{ - return uri_; -} - -/** - * @brief Obtains the number of rows affected by the operation. - * @return Returns the number of rows affected by the operation. - */ -int DataShareResult::GetCount() -{ - return count_; -} - -/** - * @brief Creates a DataShareResult instance based on the given Parcel object. - * Used to transfer DataShareResult object using Parcel. - * @param parcel Indicates the Parcel object. - * @return Returns the DataShareResult object. - */ -DataShareResult *DataShareResult::CreateFromParcel(Parcel &parcel) -{ - DataShareResult *dataShareResult = new (std::nothrow) DataShareResult(parcel); - if (dataShareResult == nullptr) { - LOG_ERROR("dataShareResult is nullptr"); - } - return dataShareResult; -} - -/** - * @brief Prints out a string containing the class object information. - * @return Returns object information. - */ -std::string DataShareResult::ToString() -{ - std::string stringBuilder = "DataShareResult("; - stringBuilder.append("uri=").append(uri_.ToString()).append(" "); - stringBuilder.append("count=").append(std::to_string(count_)).append(" "); - stringBuilder.erase(stringBuilder.length() - 1, 1); - stringBuilder.append(")"); - return stringBuilder; -} - -/** - * @brief Marshals a DataShareResult object into a Parcel. - * @param parcel Indicates the Parcel object for marshalling. - * @return Returns true if the marshalling is successful; returns false otherwise. - */ -bool DataShareResult::Marshalling(Parcel &parcel) const -{ - // uri_ - if (uri_.ToString().empty()) { - return parcel.WriteInt32(VALUE_NULL) && parcel.WriteInt32(count_); - } - - return parcel.WriteInt32(VALUE_OBJECT) && parcel.WriteParcelable(&uri_) && parcel.WriteInt32(count_); -} - -/** - * @brief Unmarshals a DataShareResult object from a Parcel. - * @param parcel Indicates the Parcel object for unmarshalling. - * @return Returns true if the unmarshalling is successful; returns false otherwise. - */ -DataShareResult *DataShareResult::Unmarshalling(Parcel &parcel) -{ - DataShareResult *dataShareResult = new (std::nothrow) DataShareResult(0); - if (dataShareResult != nullptr) { - if (!dataShareResult->ReadFromParcel(parcel)) { - delete dataShareResult; - dataShareResult = nullptr; - } - } - - return dataShareResult; -} - -bool DataShareResult::ReadFromParcel(Parcel &parcel) -{ - // uri_ - int32_t empty = VALUE_NULL; - if (!parcel.ReadInt32(empty)) { - return false; - } - - if (empty == VALUE_OBJECT) { - auto uri = parcel.ReadParcelable(); - if (uri != nullptr) { - uri_ = *uri; - delete uri; - uri = nullptr; - } else { - return false; - } - } - - // count_ - if (!parcel.ReadInt32(count_)) { - return false; - } - - return true; -} -} // namespace DataShare -} // namespace OHOS diff --git a/data_share/frameworks/native/common/src/datashare_result_set.cpp b/data_share/frameworks/native/common/src/datashare_result_set.cpp index 10d00eb59c2e0477901be5b67f12497b43cf3636..50c2ee308361a8ddad1f927216f9c35b13019eb2 100644 --- a/data_share/frameworks/native/common/src/datashare_result_set.cpp +++ b/data_share/frameworks/native/common/src/datashare_result_set.cpp @@ -53,11 +53,12 @@ DataShareResultSet::DataShareResultSet(std::shared_ptr &bridge) DataShareResultSet::~DataShareResultSet() { - ClosedBlock(); + Close(); } int DataShareResultSet::GetAllColumnNames(std::vector &columnNames) { + DISTRIBUTED_DATA_HITRACE("DataShareResultSet::GetAllColumnNames"); if (bridge_ == nullptr) { LOG_ERROR("bridge_ is null!"); return E_ERROR; @@ -76,6 +77,7 @@ int DataShareResultSet::GetRowCount(int &count) bool DataShareResultSet::OnGo(int startRowIndex, int targetRowIndex, int *cachedIndex) { + DISTRIBUTED_DATA_HITRACE("DataShareResultSet::OnGo"); if (bridge_ == nullptr || blockWriter_ == nullptr || sharedBlock_ == nullptr) { LOG_ERROR("bridge_ or blockWriter_ or sharedBlock_ is null!"); return false; @@ -109,6 +111,7 @@ AppDataFwk::SharedBlock *DataShareResultSet::GetBlock() const int DataShareResultSet::GetDataType(int columnIndex, DataType &dataType) { + DISTRIBUTED_DATA_HITRACE("DataShareResultSet::GetDataType"); int rowCount = 0; GetRowCount(rowCount); AppDataFwk::SharedBlock::CellUnit *cellUnit = @@ -163,7 +166,7 @@ int DataShareResultSet::GoToRow(int position) int DataShareResultSet::GetBlob(int columnIndex, std::vector &value) { - DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__)); + DISTRIBUTED_DATA_HITRACE("DataShareResultSet::GetBlob"); int errorCode = CheckState(columnIndex); if (errorCode != E_OK) { return errorCode; @@ -202,9 +205,9 @@ int DataShareResultSet::GetBlob(int columnIndex, std::vector &value) int DataShareResultSet::GetString(int columnIndex, std::string &value) { - int errorCode = CheckState(columnIndex); - if (errorCode != E_OK) { - return errorCode; + if (sharedBlock_ == nullptr) { + LOG_ERROR("sharedBlock is null!"); + return E_ERROR; } AppDataFwk::SharedBlock::CellUnit *cellUnit = sharedBlock_->GetCellUnit(rowPos_ - startRowPos_, columnIndex); if (!cellUnit) { @@ -214,13 +217,10 @@ int DataShareResultSet::GetString(int columnIndex, std::string &value) int type = cellUnit->type; if (type == AppDataFwk::SharedBlock::CELL_UNIT_TYPE_STRING) { size_t sizeIncludingNull; - const char *tempValue = sharedBlock_->GetCellUnitValueString(cellUnit, &sizeIncludingNull); - if ((sizeIncludingNull <= 1) || (tempValue == nullptr)) { - value = ""; - return E_ERROR; - } - value = tempValue; + value = std::string(sharedBlock_->GetCellUnitValueString(cellUnit, &sizeIncludingNull)); return E_OK; + } else if (type == AppDataFwk::SharedBlock::CELL_UNIT_TYPE_NULL) { + return E_ERROR; } else if (type == AppDataFwk::SharedBlock::CELL_UNIT_TYPE_INTEGER) { int64_t tempValue = cellUnit->cell.longValue; value = std::to_string(tempValue); @@ -232,8 +232,6 @@ int DataShareResultSet::GetString(int columnIndex, std::string &value) value = os.str(); } return E_OK; - } else if (type == AppDataFwk::SharedBlock::CELL_UNIT_TYPE_NULL) { - return E_ERROR; } else if (type == AppDataFwk::SharedBlock::CELL_UNIT_TYPE_BLOB) { return E_ERROR; } else { @@ -244,6 +242,10 @@ int DataShareResultSet::GetString(int columnIndex, std::string &value) int DataShareResultSet::GetInt(int columnIndex, int &value) { + if (sharedBlock_ == nullptr) { + LOG_ERROR("sharedBlock is null!"); + return E_ERROR; + } AppDataFwk::SharedBlock::CellUnit *cellUnit = sharedBlock_->GetCellUnit(rowPos_ - startRowPos_, columnIndex); if (!cellUnit) { LOG_ERROR("cellUnit is null!"); @@ -255,9 +257,9 @@ int DataShareResultSet::GetInt(int columnIndex, int &value) int DataShareResultSet::GetLong(int columnIndex, int64_t &value) { - int errorCode = CheckState(columnIndex); - if (errorCode != E_OK) { - return errorCode; + if (sharedBlock_ == nullptr) { + LOG_ERROR("sharedBlock is null!"); + return E_ERROR; } AppDataFwk::SharedBlock::CellUnit *cellUnit = sharedBlock_->GetCellUnit(rowPos_ - startRowPos_, columnIndex); if (!cellUnit) { @@ -266,7 +268,6 @@ int DataShareResultSet::GetLong(int columnIndex, int64_t &value) } int type = cellUnit->type; - if (type == AppDataFwk::SharedBlock::CELL_UNIT_TYPE_INTEGER) { value = cellUnit->cell.longValue; return E_OK; @@ -292,6 +293,7 @@ int DataShareResultSet::GetLong(int columnIndex, int64_t &value) int DataShareResultSet::GetDouble(int columnIndex, double &value) { + DISTRIBUTED_DATA_HITRACE("DataShareResultSet::GetDouble"); int errorCode = CheckState(columnIndex); if (errorCode != E_OK) { return errorCode; @@ -328,6 +330,7 @@ int DataShareResultSet::GetDouble(int columnIndex, double &value) int DataShareResultSet::IsColumnNull(int columnIndex, bool &isNull) { + DISTRIBUTED_DATA_HITRACE("DataShareResultSet::IsColumnNull"); int errorCode = CheckState(columnIndex); if (errorCode != E_OK) { return errorCode; @@ -350,6 +353,7 @@ int DataShareResultSet::Close() DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__)); DataShareAbsResultSet::Close(); ClosedBlock(); + bridge_ = nullptr; return E_OK; } @@ -402,9 +406,7 @@ int DataShareResultSet::CheckState(int columnIndex) if (columnIndex >= cnt || columnIndex < 0) { return E_INVALID_COLUMN_INDEX; } - int rowCnt = 0; - GetRowCount(rowCnt); - if (rowPos_ < 0 || rowPos_ >= rowCnt) { + if (rowPos_ == INITIAL_POS) { return E_INVALID_STATEMENT; } return E_OK; @@ -430,5 +432,12 @@ bool DataShareResultSet::Unmarshalling(MessageParcel &parcel) } return true; } + +void DataShareResultSet::SetUri(const std::string &uri) +{ + if (!uri.empty()) { + uri_ = uri; + } +} } // namespace DataShare } // namespace OHOS \ No newline at end of file diff --git a/data_share/frameworks/native/common/src/ishared_result_set_proxy.cpp b/data_share/frameworks/native/common/src/ishared_result_set_proxy.cpp index 058c68dbdee4c8d925face5ccdef81d6eeac771d..f2a4299aee248373fa208acf9fdac9d9f0aae57e 100644 --- a/data_share/frameworks/native/common/src/ishared_result_set_proxy.cpp +++ b/data_share/frameworks/native/common/src/ishared_result_set_proxy.cpp @@ -19,6 +19,9 @@ #include "datashare_errno.h" #include "datashare_log.h" #include "iremote_proxy.h" +#ifdef EFFICIENCY_MANAGER_ENABLE +#include "suspend_manager_client.h" +#endif namespace OHOS::DataShare { std::function(MessageParcel &parcel)> ISharedResultSet::consumerCreator_ = @@ -52,6 +55,7 @@ int ISharedResultSetProxy::GetAllColumnNames(std::vector &columnNam request.WriteInterfaceToken(GetDescriptor()); MessageParcel reply; MessageOption msgOption; + ReportEventToSuspendManager(uri_); int errCode = Remote()->SendRequest(FUNC_GET_ALL_COLUMN_NAMES, request, reply, msgOption); if (errCode != 0) { LOG_ERROR("IPC Error %{public}x", errCode); @@ -80,6 +84,7 @@ int ISharedResultSetProxy::GetRowCount(int &count) request.WriteInterfaceToken(GetDescriptor()); MessageParcel reply; MessageOption msgOption; + ReportEventToSuspendManager(uri_); int errCode = Remote()->SendRequest(FUNC_GET_ROW_COUNT, request, reply, msgOption); if (errCode != 0) { LOG_ERROR("IPC Error %{public}x", errCode); @@ -104,6 +109,7 @@ bool ISharedResultSetProxy::OnGo(int oldRowIndex, int newRowIndex, int *cachedIn request.WriteInt32(newRowIndex); MessageParcel reply; MessageOption msgOption; + ReportEventToSuspendManager(uri_); int errCode = Remote()->SendRequest(FUNC_ON_GO, request, reply, msgOption); if (errCode != 0) { LOG_ERROR("IPC Error %{public}x", errCode); @@ -126,6 +132,7 @@ int ISharedResultSetProxy::Close() request.WriteInterfaceToken(GetDescriptor()); MessageParcel reply; MessageOption msgOption; + ReportEventToSuspendManager(uri_); int errCode = Remote()->SendRequest(FUNC_CLOSE, request, reply, msgOption); if (errCode != 0) { LOG_ERROR("IPC Error %{public}x", errCode); @@ -133,4 +140,13 @@ int ISharedResultSetProxy::Close() } return reply.ReadInt32(); } + +void ISharedResultSetProxy::ReportEventToSuspendManager(const std::string &uriString) const +{ +#ifdef EFFICIENCY_MANAGER_ENABLE + OHOS::SuspendManager::AppInfo appInfo(-1, -1, uriString, "", "THAW_BY_DATASHARE_EXTENSION_CALLED"); + appInfo.SetIsExtension(true); + OHOS::SuspendManager::SuspendManagerClient::GetInstance().ThawOneAppByAppInfo(appInfo); +#endif +} } // namespace OHOS::DataShare \ No newline at end of file diff --git a/data_share/frameworks/native/common/src/shared_block.cpp b/data_share/frameworks/native/common/src/shared_block.cpp index b5e16c12a4e3950650e2ea69c189310a41836c97..0028401e414ef4cbb942ca5a19e802ce20871895 100644 --- a/data_share/frameworks/native/common/src/shared_block.cpp +++ b/data_share/frameworks/native/common/src/shared_block.cpp @@ -64,7 +64,6 @@ bool SharedBlock::Init() int SharedBlock::CreateSharedBlock(const std::string &name, size_t size, sptr ashmem, SharedBlock *&outSharedBlock) { - LOG_DEBUG("CreateSharedBlock into."); outSharedBlock = new SharedBlock(name, ashmem, size, false); if (outSharedBlock == nullptr) { LOG_ERROR("CreateSharedBlock: new SharedBlock error."); @@ -257,7 +256,7 @@ uint32_t SharedBlock::Alloc(size_t size, bool aligned) return offset; } -uint32_t *SharedBlock::GetRowOffset(uint32_t row) +inline uint32_t *SharedBlock::GetRowOffset(uint32_t row) { uint32_t rowPos = row; @@ -453,19 +452,6 @@ size_t SharedBlock::SetRawData(const void *rawData, size_t size) return SHARED_BLOCK_OK; } -void *SharedBlock::OffsetToPtr(uint32_t offset, uint32_t bufferSize) -{ - if (offset >= mSize) { - LOG_ERROR("Offset %{public}" PRIu32 " out of bounds, max value %{public}zu", offset, mSize); - return nullptr; - } - if (offset + bufferSize > mSize) { - LOG_ERROR("End offset %{public}" PRIu32 " out of bounds, max value %{public}zu", offset + bufferSize, mSize); - return nullptr; - } - return static_cast(mData) + offset; -} - uint32_t SharedBlock::OffsetFromPtr(void *ptr) { return static_cast(ptr) - static_cast(mData); diff --git a/data_share/frameworks/native/consumer/include/datashare_connection.h b/data_share/frameworks/native/consumer/include/datashare_connection.h index 02dd75812b6a351a6dfc9f5c83963400ba945cd7..bc9f4e1b91d161fe88be4aa456d1fb4f15165e55 100644 --- a/data_share/frameworks/native/consumer/include/datashare_connection.h +++ b/data_share/frameworks/native/consumer/include/datashare_connection.h @@ -80,6 +80,7 @@ private: void DisconnectDataShareExtAbility(); void SetDataShareProxy(sptr proxy); bool IsExtAbilityConnected(); + void ReportEventToSuspendManager(const std::string &uriString) const; std::mutex mutex_; std::shared_ptr dataShareProxy_; diff --git a/data_share/frameworks/native/consumer/src/datashare_connection.cpp b/data_share/frameworks/native/consumer/src/datashare_connection.cpp index 1fd7eb3fb3081d5ac04ac0a3f39bb0c13d23ac20..17e80b486fc3b4630608fab62a5674e9371fde13 100644 --- a/data_share/frameworks/native/consumer/src/datashare_connection.cpp +++ b/data_share/frameworks/native/consumer/src/datashare_connection.cpp @@ -18,6 +18,9 @@ #include "ability_manager_client.h" #include "datashare_proxy.h" #include "datashare_log.h" +#ifdef EFFICIENCY_MANAGER_ENABLE +#include "suspend_manager_client.h" +#endif namespace OHOS { namespace DataShare { @@ -41,7 +44,7 @@ void DataShareConnection::OnAbilityConnectDone( return; } std::unique_lock lock(condition_.mutex); - SetDataShareProxy(iface_cast(remoteObject)); + SetDataShareProxy(new (std::nothrow) DataShareProxy(remoteObject)); condition_.condition.notify_all(); } @@ -72,6 +75,7 @@ void DataShareConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName */ bool DataShareConnection::ConnectDataShareExtAbility(const Uri &uri, const sptr token) { + ReportEventToSuspendManager(uri.ToString()); if (dataShareProxy_ != nullptr) { return true; } @@ -157,5 +161,14 @@ bool DataShareConnection::IsConnected() { return dataShareProxy_ != nullptr; } + +void DataShareConnection::ReportEventToSuspendManager(const std::string &uriString) const +{ +#ifdef EFFICIENCY_MANAGER_ENABLE + OHOS::SuspendManager::AppInfo appInfo(-1, -1, uriString, "", "THAW_BY_DATASHARE_EXTENSION_CALLED"); + appInfo.SetIsExtension(true); + OHOS::SuspendManager::SuspendManagerClient::GetInstance().ThawOneAppByAppInfo(appInfo); +#endif +} } // namespace DataShare } // namespace OHOS \ No newline at end of file diff --git a/data_share/frameworks/native/consumer/src/datashare_helper.cpp b/data_share/frameworks/native/consumer/src/datashare_helper.cpp index 39cae15a5e216b6ab182a6a9f3960e992415e320..c4ad97b561835e7e0340106ed95143577a59652a 100644 --- a/data_share/frameworks/native/consumer/src/datashare_helper.cpp +++ b/data_share/frameworks/native/consumer/src/datashare_helper.cpp @@ -340,6 +340,11 @@ std::shared_ptr DataShareHelper::Query( if (proxy != nullptr) { resultset = proxy->Query(uri, predicates, columns); } + + if (resultset) { + resultset->SetUri(uri_.ToString()); + } + return resultset; } diff --git a/data_share/frameworks/native/consumer/src/datashare_proxy.cpp b/data_share/frameworks/native/consumer/src/datashare_proxy.cpp index 93625c25597fe18481d58786aabc10f9e5ba298a..8f9ddcc466538cc02540bcea9b47a505ba6bfa23 100644 --- a/data_share/frameworks/native/consumer/src/datashare_proxy.cpp +++ b/data_share/frameworks/native/consumer/src/datashare_proxy.cpp @@ -249,7 +249,6 @@ int DataShareProxy::Delete(const Uri &uri, const DataSharePredicates &predicates std::shared_ptr DataShareProxy::Query(const Uri &uri, const DataSharePredicates &predicates, std::vector &columns) { - LOG_INFO("begin."); MessageParcel data; if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) { LOG_ERROR("WriteInterfaceToken failed"); @@ -278,7 +277,6 @@ std::shared_ptr DataShareProxy::Query(const Uri &uri, LOG_ERROR("Query fail to SendRequest. err: %{public}d", err); return nullptr; } - LOG_INFO("end successfully."); return ISharedResultSet::ReadFromParcel(reply); } diff --git a/data_share/frameworks/native/proxy/include/data_share_manager.h b/data_share/frameworks/native/proxy/include/data_share_manager.h deleted file mode 100644 index ab4494eaf4265dbbde055335f3a58f63226c4c42..0000000000000000000000000000000000000000 --- a/data_share/frameworks/native/proxy/include/data_share_manager.h +++ /dev/null @@ -1,35 +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. - */ - -#ifndef DATASHARESERVICE_DATA_SHARE_MANAGER_H -#define DATASHARESERVICE_DATA_SHARE_MANAGER_H - -#include -#include -#include - -#include "idata_share_service.h" -#include "data_share_types.h" -#include "uri.h" -#include "iremote_broker.h" -#include "iremote_proxy.h" -namespace OHOS::DataShare { -class DataShareManager { -public: - static std::shared_ptr GetDataShareService(); -}; - -} // namespace OHOS::DataShare -#endif diff --git a/data_share/frameworks/native/proxy/src/data_share_service_proxy.cpp b/data_share/frameworks/native/proxy/src/data_share_service_proxy.cpp index a1062a34fe3869a207a1498ddb939b87c77cdf4a..8c0efb12d88d7ab492ba554c5ea378910ff74ebc 100644 --- a/data_share/frameworks/native/proxy/src/data_share_service_proxy.cpp +++ b/data_share/frameworks/native/proxy/src/data_share_service_proxy.cpp @@ -27,9 +27,9 @@ DataShareServiceProxy::DataShareServiceProxy(const sptr &object) LOG_INFO("Construct complete."); } -int DataShareServiceProxy::Insert(const Uri &uri, const DataShareValuesBucket &value) +int32_t DataShareServiceProxy::Insert(const Uri &uri, const DataShareValuesBucket &value) { - std::string uriStr = uri.ToString(); + const std::string &uriStr = uri.ToString(); MessageParcel data; if (!data.WriteInterfaceToken(IDataShareService::GetDescriptor())) { LOG_ERROR("Write descriptor failed!"); @@ -53,7 +53,7 @@ int DataShareServiceProxy::Insert(const Uri &uri, const DataShareValuesBucket &v int32_t DataShareServiceProxy::Update(const Uri &uri, const DataSharePredicates &predicate, const DataShareValuesBucket &valuesBucket) { - std::string uriStr = uri.ToString(); + const std::string &uriStr = uri.ToString(); MessageParcel data; if (!data.WriteInterfaceToken(IDataShareService::GetDescriptor())) { LOG_ERROR("Write descriptor failed!"); @@ -74,9 +74,9 @@ int32_t DataShareServiceProxy::Update(const Uri &uri, return reply.ReadInt32(); } - int DataShareServiceProxy::Delete(const Uri &uri, const DataSharePredicates &predicate) +int32_t DataShareServiceProxy::Delete(const Uri &uri, const DataSharePredicates &predicate) { - std::string uriStr = uri.ToString(); + const std::string &uriStr = uri.ToString(); MessageParcel data; if (!data.WriteInterfaceToken(IDataShareService::GetDescriptor())) { LOG_ERROR("Write descriptor failed!"); @@ -100,7 +100,7 @@ int32_t DataShareServiceProxy::Update(const Uri &uri, std::shared_ptr DataShareServiceProxy::Query( const Uri &uri, const DataSharePredicates &predicates, std::vector &columns) { - std::string uriStr = uri.ToString(); + const std::string &uriStr = uri.ToString(); MessageParcel data; if (!data.WriteInterfaceToken(IDataShareService::GetDescriptor())) { LOG_ERROR("WriteInterfaceToken failed!"); diff --git a/data_share/interfaces/inner_api/consumer/include/datashare_result_set.h b/data_share/interfaces/inner_api/consumer/include/datashare_result_set.h index bdc3765d24a368922eb9775c6952e6e58cf24351..f3fe488d341b739d650d94f04d0db8cb0ab54949 100644 --- a/data_share/interfaces/inner_api/consumer/include/datashare_result_set.h +++ b/data_share/interfaces/inner_api/consumer/include/datashare_result_set.h @@ -52,6 +52,7 @@ public: virtual void SetBlock(AppDataFwk::SharedBlock *block); int Close() override; bool HasBlock() const; + void SetUri(const std::string &uri); protected: int CheckState(int columnIndex); @@ -63,6 +64,8 @@ protected: bool Unmarshalling(MessageParcel &parcel); bool Marshalling(MessageParcel &parcel); + std::string uri_ = ""; + private: static int blockId_; // The actual position of the first row of data in the shareblock diff --git a/data_share/test/native/unittest/mediadatashare_test/src/mediadatashare_unit_test.cpp b/data_share/test/native/unittest/mediadatashare_test/src/mediadatashare_unit_test.cpp index e35778460fb96aacc68f14a9e932ab13c2e55f62..b18ba63b8da5f833b248e390b387214d7ad2fd5a 100644 --- a/data_share/test/native/unittest/mediadatashare_test/src/mediadatashare_unit_test.cpp +++ b/data_share/test/native/unittest/mediadatashare_test/src/mediadatashare_unit_test.cpp @@ -540,15 +540,15 @@ HWTEST_F(MediaDataShareUnitTest, MediaDataShare_Predicates_Test_019, TestSize.Le LOG_INFO("MediaDataShare_Predicates_Test_019, End"); } -// HWTEST_F(MediaDataShareUnitTest, MediaDataShare_Predicates_Test_020, TestSize.Level0) -// { -// LOG_INFO("MediaDataShare_Predicates_Test_020::Start"); -// DataShare::DataSharePredicates predicates; -// predicates.SetSettingMode(DataShare::SettingMode::PREDICATES_METHOD); -// DataShare::SettingMode setting = predicates.GetSettingMode(); -// EXPECT_EQ(setting, DataShare::SettingMode::PREDICATES_METHOD); -// LOG_INFO("MediaDataShare_Predicates_Test_020, End"); -// } +HWTEST_F(MediaDataShareUnitTest, MediaDataShare_Predicates_Test_020, TestSize.Level0) +{ + LOG_INFO("MediaDataShare_Predicates_Test_020::Start"); + DataShare::DataSharePredicates predicates; + predicates.SetSettingMode(DataShare::SettingMode::PREDICATES_METHOD); + int16_t setting = predicates.GetSettingMode(); + EXPECT_EQ(setting, DataShare::SettingMode::PREDICATES_METHOD); + LOG_INFO("MediaDataShare_Predicates_Test_020, End"); +} HWTEST_F(MediaDataShareUnitTest, MediaDataShare_Predicates_Test_021, TestSize.Level0) { @@ -886,17 +886,17 @@ HWTEST_F(MediaDataShareUnitTest, MediaDataShare_ResultSet_Test_003, TestSize.Lev Uri uri(MEDIALIBRARY_DATA_URI); auto resultSet = helper->Query(uri, predicates, columns); AppDataFwk::SharedBlock *block = nullptr; - if (resultSet != nullptr) { - bool hasBlock = resultSet->HasBlock(); - EXPECT_EQ(hasBlock, true); - block = resultSet->GetBlock(); - EXPECT_NE(block, nullptr); - - resultSet->SetBlock(block); - EXPECT_EQ(block, resultSet->GetBlock()); - resultSet->FillBlock(0, block); - EXPECT_EQ(block, resultSet->GetBlock()); - } + ASSERT_TRUE(resultSet != nullptr); + + bool hasBlock = resultSet->HasBlock(); + EXPECT_EQ(hasBlock, true); + block = resultSet->GetBlock(); + EXPECT_NE(block, nullptr); + + resultSet->SetBlock(block); + EXPECT_EQ(block, resultSet->GetBlock()); + resultSet->FillBlock(0, block); + EXPECT_EQ(block, resultSet->GetBlock()); LOG_INFO("MediaDataShare_ResultSet_Test_003, End"); } @@ -948,6 +948,41 @@ HWTEST_F(MediaDataShareUnitTest, MediaDataShare_ResultSet_Test_005, TestSize.Lev LOG_INFO("MediaDataShare_ResultSet_Test_005, End"); } +HWTEST_F(MediaDataShareUnitTest, MediaDataShare_ResultSet_Test_006, TestSize.Level0) +{ + LOG_INFO("MediaDataShare_ResultSet_Test_006::Start"); + std::shared_ptr helper = g_mediaDataShareHelper; + ASSERT_TRUE(helper != nullptr); + Uri uri(MEDIALIBRARY_DATA_URI); + DataShare::DataShareValuesBucket valuesBucket; + int value = 1112; + valuesBucket.Put(MEDIA_DATA_DB_PARENT_ID, value); + int retVal = helper->Insert(uri, valuesBucket); + EXPECT_EQ((retVal > 0), true); + + DataShare::DataSharePredicates predicates; + predicates.EqualTo(MEDIA_DATA_DB_PARENT_ID, value); + vector columns; + auto resultSet = helper->Query(uri, predicates, columns); + int columnIndex = 0; + int result = 0; + ASSERT_TRUE(resultSet != nullptr); + resultSet->GoToFirstRow(); + resultSet->GetColumnIndex(MEDIA_DATA_DB_PARENT_ID, columnIndex); + DataShare::DataType dt; + resultSet->GetDataType(0, dt); + EXPECT_EQ(dt, DataShare::DataType::TYPE_INTEGER); + resultSet->GetInt(columnIndex, result); + EXPECT_EQ(result, value); + + DataShare::DataSharePredicates deletePredicates; + std::string selections = MEDIA_DATA_DB_PARENT_ID + " = 1112"; + deletePredicates.SetWhereClause(selections); + retVal = helper->Delete(uri, deletePredicates); + EXPECT_EQ((retVal >= 0), true); + LOG_INFO("MediaDataShare_ResultSet_Test_006, End"); +} + HWTEST_F(MediaDataShareUnitTest, Creator_ContextNull_Test_001, TestSize.Level0) { LOG_INFO("Creator_ContextNull_Test_001::Start"); diff --git a/preferences/frameworks/js/common/include/js_utils.h b/preferences/frameworks/js/common/include/js_utils.h index 49907b1b677f17868ccc12be100a41925133f6ee..67d57b8ed441785a0b418751fa80be0dd6a5772f 100644 --- a/preferences/frameworks/js/common/include/js_utils.h +++ b/preferences/frameworks/js/common/include/js_utils.h @@ -52,6 +52,8 @@ public: static int32_t Convert2JSBoolArr(napi_env env, std::vector value, napi_value &output); static int32_t Convert2JSDoubleArr(napi_env env, std::vector value, napi_value &output); + static napi_value Convert2JSValue(napi_env env, int32_t value); + static bool Equals(napi_env env, napi_value value, napi_ref copy); }; } // namespace PreferencesJsKit diff --git a/preferences/frameworks/js/common/src/js_utils.cpp b/preferences/frameworks/js/common/src/js_utils.cpp index 738a60bc23f6576f169ac5d5f2258eccdc23b28c..35d6031d22e689d9ed166e14f8d0b1e6111789f3 100644 --- a/preferences/frameworks/js/common/src/js_utils.cpp +++ b/preferences/frameworks/js/common/src/js_utils.cpp @@ -249,5 +249,15 @@ int32_t JSUtils::Convert2JSStringArr(napi_env env, std::vector valu } return OK; } + +napi_value JSUtils::Convert2JSValue(napi_env env, int32_t value) +{ + napi_value jsValue; + napi_status status = napi_create_int32(env, value, &jsValue); + if (status != napi_ok) { + return nullptr; + } + return jsValue; +} } // namespace PreferencesJsKit } // namespace OHOS diff --git a/preferences/frameworks/js/napi/src/napi_preferences_helper.cpp b/preferences/frameworks/js/napi/src/napi_preferences_helper.cpp index e89435c8bf7f0fb68452ce8f99584f7a93e2e67e..ab42c330de9512d30ea285cb40c607b954be78fb 100644 --- a/preferences/frameworks/js/napi/src/napi_preferences_helper.cpp +++ b/preferences/frameworks/js/napi/src/napi_preferences_helper.cpp @@ -22,6 +22,7 @@ #include "js_utils.h" #include "napi_preferences.h" #include "napi_preferences_error.h" +#include "preferences.h" #include "preferences_errno.h" #include "securec.h" @@ -172,6 +173,8 @@ napi_value InitPreferencesHelper(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("getPreferences", GetPreferences), DECLARE_NAPI_FUNCTION("deletePreferences", DeletePreferences), DECLARE_NAPI_FUNCTION("removePreferencesFromCache", RemovePreferencesFromCache), + DECLARE_NAPI_PROPERTY("MAX_KEY_LENGTH", JSUtils::Convert2JSValue(env, Preferences::MAX_KEY_LENGTH)), + DECLARE_NAPI_PROPERTY("MAX_VALUE_LENGTH", JSUtils::Convert2JSValue(env, Preferences::MAX_VALUE_LENGTH)), }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(properties) / sizeof(*properties), properties)); return exports; diff --git a/preferences/frameworks/js/napi/src/napi_storage_helper.cpp b/preferences/frameworks/js/napi/src/napi_storage_helper.cpp index 0f2762cef3237a809d04d18475f7d3b117d9dbdb..4a8326c733f33d55fcf621842e43b7a689dbcb44 100644 --- a/preferences/frameworks/js/napi/src/napi_storage_helper.cpp +++ b/preferences/frameworks/js/napi/src/napi_storage_helper.cpp @@ -1,4 +1,4 @@ -/* +/* * Copyright (c) 2021 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. @@ -18,8 +18,10 @@ #include #include "js_logger.h" +#include "js_utils.h" #include "napi_async_proxy.h" #include "napi_storage.h" +#include "preferences.h" #include "preferences_errno.h" #include "securec.h" @@ -192,6 +194,8 @@ napi_value InitPreferenceHelper(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("deleteStorageSync", DeleteStorageSync), DECLARE_NAPI_FUNCTION("removeStorageFromCache", RemoveStorageFromCache), DECLARE_NAPI_FUNCTION("removeStorageFromCacheSync", RemoveStorageFromCacheSync), + DECLARE_NAPI_PROPERTY("MAX_KEY_LENGTH", JSUtils::Convert2JSValue(env, Preferences::MAX_KEY_LENGTH)), + DECLARE_NAPI_PROPERTY("MAX_VALUE_LENGTH", JSUtils::Convert2JSValue(env, Preferences::MAX_VALUE_LENGTH)), }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(properties) / sizeof(*properties), properties)); return exports; diff --git a/relational_store/frameworks/js/napi/rdb/src/napi_rdb_predicates.cpp b/relational_store/frameworks/js/napi/rdb/src/napi_rdb_predicates.cpp index 63776f5d9c4d778a654726eacacde81afd24abdc..2a9c4a8a86afd1928bc658d30ea782c2559fd087 100644 --- a/relational_store/frameworks/js/napi/rdb/src/napi_rdb_predicates.cpp +++ b/relational_store/frameworks/js/napi/rdb/src/napi_rdb_predicates.cpp @@ -90,6 +90,14 @@ void RdbPredicatesProxy::Init(napi_env env, napi_value exports) NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, exports, "RdbPredicates", cons)); SetGlobalNamedProperty(env, "RdbPredicatesConstructor", cons); + + napi_value consV9; + NAPI_CALL_RETURN_VOID(env, napi_define_class(env, "RdbPredicatesV9", NAPI_AUTO_LENGTH, NewV9, nullptr, + sizeof(descriptors) / sizeof(napi_property_descriptor), descriptors, &consV9)); + NAPI_CALL_RETURN_VOID(env, napi_create_reference(env, consV9, 1, &constructor_)); + NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, exports, "RdbPredicatesV9", consV9)); + + SetGlobalNamedProperty(env, "RdbPredicatesConstructorV9", consV9); LOG_DEBUG("RdbPredicatesProxy::Init end"); } diff --git a/relational_store/frameworks/js/napi/rdb/src/napi_rdb_store.cpp b/relational_store/frameworks/js/napi/rdb/src/napi_rdb_store.cpp index fd6e3040943df7c1c8ef56e2d881576e5f00fa4f..6fcd75d16572010e2105300cd32727668b9a7b49 100644 --- a/relational_store/frameworks/js/napi/rdb/src/napi_rdb_store.cpp +++ b/relational_store/frameworks/js/napi/rdb/src/napi_rdb_store.cpp @@ -208,6 +208,10 @@ void RdbStoreProxy::Init(napi_env env, napi_value exports) sizeof(descriptors) / sizeof(napi_property_descriptor), descriptors, &cons)); NAPI_CALL_RETURN_VOID(env, napi_create_reference(env, cons, 1, &constructor_)); + NAPI_CALL_RETURN_VOID(env, napi_define_class(env, "RdbStoreV9", NAPI_AUTO_LENGTH, InitializeV9, nullptr, + sizeof(descriptors) / sizeof(napi_property_descriptor), descriptors, &cons)); + NAPI_CALL_RETURN_VOID(env, napi_create_reference(env, cons, 1, &constructorV9_)); + LOG_DEBUG("Init RdbStoreProxy end"); } diff --git a/relational_store/frameworks/js/napi/rdb/src/napi_rdb_store_helper.cpp b/relational_store/frameworks/js/napi/rdb/src/napi_rdb_store_helper.cpp index fadaae0607c0650cb86c1d384c94c31b28e2b5df..659fa675b99895174fbc8b7bf95a0ab6f2f9d0a6 100644 --- a/relational_store/frameworks/js/napi/rdb/src/napi_rdb_store_helper.cpp +++ b/relational_store/frameworks/js/napi/rdb/src/napi_rdb_store_helper.cpp @@ -560,6 +560,8 @@ napi_value InitRdbHelper(napi_env env, napi_value exports) napi_property_descriptor properties[] = { DECLARE_NAPI_FUNCTION("getRdbStore", GetRdbStore), DECLARE_NAPI_FUNCTION("deleteRdbStore", DeleteRdbStore), + DECLARE_NAPI_FUNCTION("getRdbStoreV9", GetRdbStoreV9), + DECLARE_NAPI_FUNCTION("deleteRdbStoreV9", DeleteRdbStoreV9), }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(properties) / sizeof(*properties), properties)); LOG_INFO("RdbJsKit::InitRdbHelper end"); diff --git a/relational_store/frameworks/js/napi/rdb/src/napi_result_set.cpp b/relational_store/frameworks/js/napi/rdb/src/napi_result_set.cpp index 3fc1b2a20bbad6d4e608f10c3d4e88064c9a55f7..da35fe50982203c320c16e1d46aedd255dec6393 100644 --- a/relational_store/frameworks/js/napi/rdb/src/napi_result_set.cpp +++ b/relational_store/frameworks/js/napi/rdb/src/napi_result_set.cpp @@ -146,6 +146,13 @@ napi_value ResultSetProxy::GetConstructor(napi_env env, int version) DECLARE_NAPI_GETTER("isAtLastRow", IsAtLastRow), }; + if (version > APIVERSION_V8) { + NAPI_CALL(env, napi_define_class(env, "ResultSetV9", NAPI_AUTO_LENGTH, InitializeV9, nullptr, + sizeof(clzDes) / sizeof(napi_property_descriptor), clzDes, &cons)); + NAPI_CALL(env, napi_create_reference(env, cons, 1, &ctorRefV9_)); + return cons; + } + NAPI_CALL(env, napi_define_class(env, "ResultSet", NAPI_AUTO_LENGTH, Initialize, nullptr, sizeof(clzDes) / sizeof(napi_property_descriptor), clzDes, &cons)); NAPI_CALL(env, napi_create_reference(env, cons, 1, &ctorRef_)); diff --git a/relational_store/frameworks/js/napi/relationalstore/src/napi_rdb_const_properties.cpp b/relational_store/frameworks/js/napi/relationalstore/src/napi_rdb_const_properties.cpp index 8b9695480704d04b7ebd5ce2a547b905e8b7be1d..5981093890100e37da484e0682c61dc3fbbcf54b 100644 --- a/relational_store/frameworks/js/napi/relationalstore/src/napi_rdb_const_properties.cpp +++ b/relational_store/frameworks/js/napi/relationalstore/src/napi_rdb_const_properties.cpp @@ -14,6 +14,7 @@ */ #include "napi_rdb_const_properties.h" +#include "rdb_common.h" #include "rdb_store.h" #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) #include "rdb_store_config.h" @@ -79,6 +80,7 @@ static napi_value ExportSecurityLevel(napi_env env) napi_object_freeze(env, securityLevel); return securityLevel; } +#endif static napi_value ExportOpenStatus(napi_env env) { @@ -89,7 +91,6 @@ static napi_value ExportOpenStatus(napi_env env) napi_object_freeze(env, openStatus); return openStatus; } -#endif static napi_value ExportConflictResolution(napi_env env) { diff --git a/relational_store/frameworks/native/appdatafwk/src/shared_block.cpp b/relational_store/frameworks/native/appdatafwk/src/shared_block.cpp index f06be8a1f1da2991d16e2b2d936c35ff1337e9dd..b7c7b5d0a0cf47b576e5a80b9b5222fb3d2f4afe 100644 --- a/relational_store/frameworks/native/appdatafwk/src/shared_block.cpp +++ b/relational_store/frameworks/native/appdatafwk/src/shared_block.cpp @@ -64,7 +64,6 @@ bool SharedBlock::Init() int SharedBlock::CreateSharedBlock(const std::string &name, size_t size, sptr ashmem, SharedBlock *&outSharedBlock) { - LOG_DEBUG("CreateSharedBlock into."); outSharedBlock = new SharedBlock(name, ashmem, size, false); if (outSharedBlock == nullptr) { LOG_ERROR("CreateSharedBlock: new SharedBlock error."); diff --git a/relational_store/frameworks/native/rdb/src/rdb_helper.cpp b/relational_store/frameworks/native/rdb/src/rdb_helper.cpp index 20303b7dfffc72e4da34b560c084a23283320ebc..60eb13b33f61c6b8afd5b55fa26bc2f467c38870 100644 --- a/relational_store/frameworks/native/rdb/src/rdb_helper.cpp +++ b/relational_store/frameworks/native/rdb/src/rdb_helper.cpp @@ -16,6 +16,7 @@ #include "rdb_helper.h" #include "logger.h" +#include "rdb_common.h" #include "rdb_errno.h" #include "rdb_store_impl.h" #include "rdb_trace.h" @@ -29,90 +30,25 @@ namespace OHOS { namespace NativeRdb { -std::shared_ptr RdbHelper::GetRdbStore( - const RdbStoreConfig &config, int version, RdbOpenCallback &openCallback, int &errCode) +void RdbHelper::InitSecurityManager(const RdbStoreConfig &config) { - DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__)); - bool isDbFileExists = access(config.GetPath().c_str(), F_OK) == 0; - - SqliteGlobalConfig::InitSqliteGlobalConfig(); - #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) if (config.IsEncrypt()) { RdbSecurityManager::GetInstance().Init(config.GetBundleName(), config.GetPath()); } #endif - - std::shared_ptr rdbStore = RdbStoreManager::GetInstance().GetRdbStore(config, errCode); - if (rdbStore == nullptr) { - LOG_ERROR("RdbHelper GetRdbStore fail to open RdbStore"); - return nullptr; - } - -#if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) - errCode = SecurityPolicy::SetSecurityLabel(config); - if (errCode != E_OK) { - LOG_ERROR("RdbHelper set security label fail."); - return nullptr; - } -#endif - - if (isDbFileExists) { - rdbStore->SetStatus(static_cast(OpenStatus::ON_OPEN)); - } else { - rdbStore->SetStatus(static_cast(OpenStatus::ON_CREATE)); - } - - if (version == -1) { - return rdbStore; - } - - errCode = ProcessOpenCallback(*rdbStore, config, version, openCallback); - if (errCode != E_OK) { - LOG_ERROR("RdbHelper GetRdbStore ProcessOpenCallback fail"); - return nullptr; - } - - return rdbStore; } -int RdbHelper::ProcessOpenCallback( - RdbStore &rdbStore, const RdbStoreConfig &config, int version, RdbOpenCallback &openCallback) +std::shared_ptr RdbHelper::GetRdbStore( + const RdbStoreConfig &config, int version, RdbOpenCallback &openCallback, int &errCode) { DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__)); - int currentVersion; - int errCode = rdbStore.GetVersion(currentVersion); - if (errCode != E_OK) { - return errCode; - } - - if (version == currentVersion) { - return openCallback.OnOpen(rdbStore); - } - - if (config.IsReadOnly()) { - LOG_ERROR("RdbHelper ProcessOpenCallback Can't upgrade read-only store"); - return E_CANNOT_UPDATE_READONLY; - } - - if (currentVersion == 0) { - errCode = openCallback.OnCreate(rdbStore); - } else if (version > currentVersion) { - errCode = openCallback.OnUpgrade(rdbStore, currentVersion, version); - } else { - errCode = openCallback.OnDowngrade(rdbStore, currentVersion, version); - } - - if (errCode == E_OK) { - errCode = rdbStore.SetVersion(version); - } - - if (errCode != E_OK) { - LOG_ERROR("RdbHelper ProcessOpenCallback set new version failed."); - return errCode; - } + SqliteGlobalConfig::InitSqliteGlobalConfig(); + InitSecurityManager(config); + std::shared_ptr rdbStore = + RdbStoreManager::GetInstance().GetRdbStore(config, errCode, version, openCallback); - return openCallback.OnOpen(rdbStore); + return rdbStore; } void RdbHelper::ClearCache() @@ -120,7 +56,6 @@ void RdbHelper::ClearCache() RdbStoreManager::GetInstance().Clear(); } - static void DeleteRdbKeyFiles(const std::string &dbFileName) { #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) @@ -177,6 +112,7 @@ int RdbHelper::DeleteRdbStore(const std::string &dbFileName) return errCode; } + RdbStoreNode::RdbStoreNode(const std::shared_ptr &rdbStore) : rdbStore_(rdbStore) {} RdbStoreNode &RdbStoreNode::operator=(const std::shared_ptr &store) @@ -208,10 +144,15 @@ RdbStoreManager::RdbStoreManager() { timer_ = std::make_shared("RdbStoreCloser"); timer_->Setup(); + // 30000 ms + ms_ = 30000; } -std::shared_ptr RdbStoreManager::GetRdbStore(const RdbStoreConfig &config, int &errCode) +std::shared_ptr RdbStoreManager::GetRdbStore(const RdbStoreConfig &config, + int &errCode, int version, RdbOpenCallback &openCallback) { + bool isDbFileExists = access(config.GetPath().c_str(), F_OK) == 0; + std::shared_ptr rdbStore; std::string path = config.GetPath(); std::lock_guard lock(mutex_); @@ -226,7 +167,28 @@ std::shared_ptr RdbStoreManager::GetRdbStore(const RdbStoreConfig &con } else { RestartTimer(path, *storeCache_[path]); rdbStore = storeCache_[path]->rdbStore_; + return rdbStore; + } + + if (SetSecurityLabel(config) != E_OK) { + storeCache_.erase(path); + LOG_ERROR("RdbHelper set security label fail."); + return nullptr; } + + if (isDbFileExists) { + rdbStore->SetStatus(static_cast(OpenStatus::ON_OPEN)); + } else { + rdbStore->SetStatus(static_cast(OpenStatus::ON_CREATE)); + } + + errCode = ProcessOpenCallback(*rdbStore, config, version, openCallback); + if (errCode != E_OK) { + storeCache_.erase(path); + LOG_ERROR("RdbHelper GetRdbStore ProcessOpenCallback fail"); + return nullptr; + } + return rdbStore; } @@ -235,7 +197,7 @@ void RdbStoreManager::RestartTimer(const std::string &path, RdbStoreNode &node) if (timer_ != nullptr) { timer_->Unregister(node.timerId_); // after 30000ms, auto close. - node.timerId_ = timer_->Register(std::bind(RdbStoreManager::AutoClose, path, this), 30000, true); + node.timerId_ = timer_->Register(std::bind(RdbStoreManager::AutoClose, path, this), ms_, true); } } @@ -268,5 +230,62 @@ void RdbStoreManager::Clear() iter = storeCache_.erase(iter); } } + + +int RdbStoreManager::ProcessOpenCallback( + RdbStore &rdbStore, const RdbStoreConfig &config, int version, RdbOpenCallback &openCallback) +{ + DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__)); + int errCode = E_OK; + if (version == -1) { + return errCode; + } + + int currentVersion; + errCode = rdbStore.GetVersion(currentVersion); + if (errCode != E_OK) { + return errCode; + } + + if (version == currentVersion) { + return openCallback.OnOpen(rdbStore); + } + + if (config.IsReadOnly()) { + LOG_ERROR("RdbHelper ProcessOpenCallback Can't upgrade read-only store"); + return E_CANNOT_UPDATE_READONLY; + } + + if (currentVersion == 0) { + errCode = openCallback.OnCreate(rdbStore); + } else if (version > currentVersion) { + errCode = openCallback.OnUpgrade(rdbStore, currentVersion, version); + } else { + errCode = openCallback.OnDowngrade(rdbStore, currentVersion, version); + } + + if (errCode == E_OK) { + errCode = rdbStore.SetVersion(version); + } + + if (errCode != E_OK) { + LOG_ERROR("RdbHelper ProcessOpenCallback set new version failed."); + return errCode; + } + + return openCallback.OnOpen(rdbStore); +} +int RdbStoreManager::SetSecurityLabel(const RdbStoreConfig &config) +{ + int errCode = E_OK; +#if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) + errCode = SecurityPolicy::SetSecurityLabel(config); +#endif + return errCode; +} +void RdbStoreManager::SetReleaseTime(int ms) +{ + ms_ = ms; +} } // namespace NativeRdb } // namespace OHOS diff --git a/relational_store/frameworks/native/rdb/src/rdb_store_impl.cpp b/relational_store/frameworks/native/rdb/src/rdb_store_impl.cpp index 4bec2ff573fb5e23d870aaeba4b716a5824467be..77aedbc08f30751bdd4575dedbbfa0557194a3dc 100644 --- a/relational_store/frameworks/native/rdb/src/rdb_store_impl.cpp +++ b/relational_store/frameworks/native/rdb/src/rdb_store_impl.cpp @@ -563,7 +563,6 @@ int RdbStoreImpl::BeginExecuteSql(const std::string &sql, SqliteConnection **con return E_OK; } bool RdbStoreImpl::IsHoldingConnection() - { return connectionPool != nullptr; } diff --git a/relational_store/frameworks/native/rdb/src/sqlite_connection.cpp b/relational_store/frameworks/native/rdb/src/sqlite_connection.cpp index 7c879007af930a9ab63f88e9024abaf0f0fefc10..7b0ce1a188e837bd974b8efb0b9ec9204bb6a5b0 100644 --- a/relational_store/frameworks/native/rdb/src/sqlite_connection.cpp +++ b/relational_store/frameworks/native/rdb/src/sqlite_connection.cpp @@ -691,7 +691,7 @@ int SqliteConnection::ExecuteForSharedBlock(int &rowNum, std::string sql, const SqliteConnectionS connection(this->dbHandle, this->openFlags, this->filePath); int errCode = PrepareAndBind(sql, bindArgs); if (errCode != E_OK) { - LOG_ERROR("ExecuteForSharedBlock:sharedBlock is null. SqliteConnectionS connection"); + LOG_ERROR("PrepareAndBind sql and bindArgs error = %{public}d ", errCode); return errCode; } diff --git a/relational_store/frameworks/native/rdb/src/sqlite_connection_pool.cpp b/relational_store/frameworks/native/rdb/src/sqlite_connection_pool.cpp index b8d4ebdcf143ee684d248f24f1facb36ba5ba518..7927497e01da1693d684f5cab9ae35f5e173e52c 100644 --- a/relational_store/frameworks/native/rdb/src/sqlite_connection_pool.cpp +++ b/relational_store/frameworks/native/rdb/src/sqlite_connection_pool.cpp @@ -156,7 +156,6 @@ void SqliteConnectionPool::ReleaseWriteConnection() */ SqliteConnection *SqliteConnectionPool::AcquireReadConnection() { - LOG_DEBUG("idleReadConnectionCount:%{public}d", idleReadConnectionCount); std::unique_lock lock(readMutex); readCondition.wait(lock, [&] { return idleReadConnectionCount > 0; }); SqliteConnection *connection = readConnections.back(); @@ -171,7 +170,6 @@ SqliteConnection *SqliteConnectionPool::AcquireReadConnection() */ void SqliteConnectionPool::ReleaseReadConnection(SqliteConnection *connection) { - LOG_DEBUG("idleReadConnectionCount:%{public}d", idleReadConnectionCount); { std::unique_lock lock(readMutex); readConnections.push_back(connection); diff --git a/relational_store/frameworks/native/rdb/src/sqlite_sql_builder.cpp b/relational_store/frameworks/native/rdb/src/sqlite_sql_builder.cpp index 59958f1835ecb5979751bbd8e6eb37892d3e7836..9490a193c85e26838548ca4bbeaef6a7fa849c26 100644 --- a/relational_store/frameworks/native/rdb/src/sqlite_sql_builder.cpp +++ b/relational_store/frameworks/native/rdb/src/sqlite_sql_builder.cpp @@ -400,7 +400,6 @@ std::string SqliteSqlBuilder::NormalizeAlias(const std::string &source, int &err std::smatch result; bool columnMatcher = std::regex_match(strTrimed, result, pattern); if (!columnMatcher) { - LOG_DEBUG("NormalizeAlias no match %{public}s", strTrimed.c_str()); return Normalize(strTrimed, errorCode); } std::string words = StringUtils::Trim(result[1]); diff --git a/relational_store/frameworks/native/rdb/src/sqlite_statement.cpp b/relational_store/frameworks/native/rdb/src/sqlite_statement.cpp index c65740be9fe37801062b84703eb6ea08351c4492..ee454238cf15e4a802307fbf5ab3b5181b81327b 100644 --- a/relational_store/frameworks/native/rdb/src/sqlite_statement.cpp +++ b/relational_store/frameworks/native/rdb/src/sqlite_statement.cpp @@ -285,26 +285,34 @@ int SqliteStatement::GetColumnString(int index, std::string &value) const } int type = sqlite3_column_type(stmtHandle, index); - if (type == SQLITE_TEXT) { - auto val = reinterpret_cast(sqlite3_column_text(stmtHandle, index)); - value = (val == nullptr) ? "" : std::string(val, sqlite3_column_bytes(stmtHandle, index)); - } else if (type == SQLITE_INTEGER) { - int64_t val = sqlite3_column_int64(stmtHandle, index); - value = std::to_string(val); - } else if (type == SQLITE_FLOAT) { - double val = sqlite3_column_double(stmtHandle, index); - std::ostringstream os; - if (os << std::setprecision(SET_DATA_PRECISION) << val) - value = os.str(); - } else if (type == SQLITE_NULL) { - value = ""; - return E_OK; - } else if (type == SQLITE_BLOB) { - return E_INVALID_COLUMN_TYPE; - } else { - return E_ERROR; + switch (type) { + case SQLITE_TEXT: { + auto val = reinterpret_cast(sqlite3_column_text(stmtHandle, index)); + value = (val == nullptr) ? "" : std::string(val, sqlite3_column_bytes(stmtHandle, index)); + break; + } + case SQLITE_INTEGER: { + int64_t val = sqlite3_column_int64(stmtHandle, index); + value = std::to_string(val); + break; + } + case SQLITE_FLOAT: { + double val = sqlite3_column_double(stmtHandle, index); + std::ostringstream os; + if (os << std::setprecision(SET_DATA_PRECISION) << val) + value = os.str(); + break; + } + case SQLITE_NULL: { + value = ""; + return E_OK; + } + case SQLITE_BLOB: { + return E_INVALID_COLUMN_TYPE; + } + default: + return E_ERROR; } - return E_OK; } diff --git a/relational_store/frameworks/native/rdb/src/step_result_set.cpp b/relational_store/frameworks/native/rdb/src/step_result_set.cpp index aa51f5cd742140e6a39cf95ed1e416ef2710b2a2..c664866738e33e099c8b90ef34856e4aa66cd887 100644 --- a/relational_store/frameworks/native/rdb/src/step_result_set.cpp +++ b/relational_store/frameworks/native/rdb/src/step_result_set.cpp @@ -332,7 +332,6 @@ int StepResultSet::GetBlob(int columnIndex, std::vector &blob) int StepResultSet::GetString(int columnIndex, std::string &value) { - DISTRIBUTED_DATA_HITRACE("StepResultSet::GetString"); if (rowPos_ == INIT_POS) { return E_STEP_RESULT_QUERY_NOT_EXECUTED; } diff --git a/relational_store/frameworks/native/rdb_data_share_adapter/src/rdb_result_set_bridge.cpp b/relational_store/frameworks/native/rdb_data_share_adapter/src/rdb_result_set_bridge.cpp index e231288cb68369840cb382ea1d13d57b96de492a..1c3665f3bb56bc2c6c47ef4623c5f9878f792afa 100644 --- a/relational_store/frameworks/native/rdb_data_share_adapter/src/rdb_result_set_bridge.cpp +++ b/relational_store/frameworks/native/rdb_data_share_adapter/src/rdb_result_set_bridge.cpp @@ -111,7 +111,6 @@ void RdbResultSetBridge::WriteColumn( int columnCount, const std::vector &columnTypes, Writer &writer, int row) { for (int i = 0; i < columnCount; i++) { - LOG_DEBUG("Write data of row: %{public}d, column: %{public}d", row, i); switch (columnTypes[i]) { case ColumnType::TYPE_INTEGER: int64_t value; diff --git a/data_share/frameworks/native/proxy/include/data_share_types.h b/relational_store/interfaces/inner_api/rdb/include/rdb_common.h similarity index 57% rename from data_share/frameworks/native/proxy/include/data_share_types.h rename to relational_store/interfaces/inner_api/rdb/include/rdb_common.h index a511fc3ba42f652fa106f3e1b869872d9f22bfa9..7926c12888aa74ea1d38cd92de97e55402498bba 100644 --- a/data_share/frameworks/native/proxy/include/data_share_types.h +++ b/relational_store/interfaces/inner_api/rdb/include/rdb_common.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2023 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 @@ -13,19 +13,23 @@ * limitations under the License. */ -#ifndef DISTRIBUTEDDATAFWK_DATA_SHARE_TYPES_H -#define DISTRIBUTEDDATAFWK_DATA_SHARE_TYPES_H +#ifndef NATIVE_RDB_RDB_COMMON_ENUM_H +#define NATIVE_RDB_RDB_COMMON_ENUM_H -#include -#include -#include -#include +namespace OHOS::NativeRdb { +enum class OpenStatus { + ON_CREATE, + ON_OPEN, +}; -namespace OHOS::DataShare { -enum class DataShareStatus { - DATA_SHARE_ERROR = -1, - DATA_SHARE_OK = 0, +enum class ConflictResolution { + ON_CONFLICT_NONE = 0, + ON_CONFLICT_ROLLBACK, + ON_CONFLICT_ABORT, + ON_CONFLICT_FAIL, + ON_CONFLICT_IGNORE, + ON_CONFLICT_REPLACE, }; +} -} // namespace OHOS::DataShare -#endif +#endif // NATIVE_RDB_RDB_COMMON_ENUM_H diff --git a/relational_store/interfaces/inner_api/rdb/include/rdb_helper.h b/relational_store/interfaces/inner_api/rdb/include/rdb_helper.h index e76ef5f47d4d04775ef3bb8cc8290a3bddf37119..ff190a3103e9c2a388a1226848b07a91766f5dd5 100644 --- a/relational_store/interfaces/inner_api/rdb/include/rdb_helper.h +++ b/relational_store/interfaces/inner_api/rdb/include/rdb_helper.h @@ -40,16 +40,23 @@ public: static RdbStoreManager &GetInstance(); RdbStoreManager(); virtual ~RdbStoreManager(); - std::shared_ptr GetRdbStore(const RdbStoreConfig &config, int &errCode); + std::shared_ptr GetRdbStore(const RdbStoreConfig &config, + int &errCode, int version, RdbOpenCallback &openCallback); void Remove(const std::string &path); void Clear(); + int SetSecurityLabel(const RdbStoreConfig &config); + void SetReleaseTime(int ms); private: + int ProcessOpenCallback(RdbStore &rdbStore, + const RdbStoreConfig &config, int version, RdbOpenCallback &openCallback); void RestartTimer(const std::string &path, RdbStoreNode &node); static void AutoClose(const std::string &path, RdbStoreManager *manager); std::mutex mutex_; std::shared_ptr timer_; std::map> storeCache_; + // ms_ : [10*1000 ~ 10*60*1000] + int ms_; }; class RdbHelper final { @@ -60,8 +67,7 @@ public: static void ClearCache(); private: - static int ProcessOpenCallback( - RdbStore &rdbStore, const RdbStoreConfig &config, int version, RdbOpenCallback &openCallback); + static void InitSecurityManager(const RdbStoreConfig &config); }; } // namespace NativeRdb } // namespace OHOS diff --git a/relational_store/interfaces/inner_api/rdb/include/rdb_store.h b/relational_store/interfaces/inner_api/rdb/include/rdb_store.h index 71cbec859792e0c47fd0f1f1fb59d5d87bb84de6..620feff55bccf89d5fb1d0d0fc7f10acde293e63 100644 --- a/relational_store/interfaces/inner_api/rdb/include/rdb_store.h +++ b/relational_store/interfaces/inner_api/rdb/include/rdb_store.h @@ -26,17 +26,9 @@ #include "value_object.h" #include "values_bucket.h" #include "rdb_types.h" +#include "rdb_common.h" namespace OHOS::NativeRdb { -enum class ConflictResolution { - ON_CONFLICT_NONE = 0, - ON_CONFLICT_ROLLBACK, - ON_CONFLICT_ABORT, - ON_CONFLICT_FAIL, - ON_CONFLICT_IGNORE, - ON_CONFLICT_REPLACE, -}; - class RdbStore { public: using SyncOption = DistributedRdb::SyncOption; diff --git a/relational_store/interfaces/inner_api/rdb/include/rdb_store_config.h b/relational_store/interfaces/inner_api/rdb/include/rdb_store_config.h index cddb915c9039720df78983fab314d74c7881741b..468ba4c7d359dee63ba9c9a97aae396f1f6f3776 100644 --- a/relational_store/interfaces/inner_api/rdb/include/rdb_store_config.h +++ b/relational_store/interfaces/inner_api/rdb/include/rdb_store_config.h @@ -57,11 +57,6 @@ enum class SecurityLevel : int32_t { LAST }; -enum class OpenStatus { - ON_CREATE, - ON_OPEN, -}; - static constexpr int DB_PAGE_SIZE = 4096; /* default page size : 4k */ static constexpr int DB_JOURNAL_SIZE = 1048576; /* default file size : 1M */ static constexpr char DB_DEFAULT_JOURNAL_MODE[] = "WAL"; diff --git a/relational_store/interfaces/inner_api/rdb/mock/include/rdb_helper.h b/relational_store/interfaces/inner_api/rdb/mock/include/rdb_helper.h index 19422eb67c16a56daf90b4284d107ce31c33a817..bc87a5b1a74f3f823a2f5dc4cfe6961a3e51a834 100644 --- a/relational_store/interfaces/inner_api/rdb/mock/include/rdb_helper.h +++ b/relational_store/interfaces/inner_api/rdb/mock/include/rdb_helper.h @@ -27,10 +27,6 @@ namespace OHOS { namespace NativeRdb { -enum class RdbStatus { - ON_CREATE = 0, - ON_OPEN = 1, -}; struct RdbStoreNode { RdbStoreNode(const std::shared_ptr &rdbStore); @@ -45,16 +41,23 @@ public: static RdbStoreManager &GetInstance(); RdbStoreManager(); virtual ~RdbStoreManager(); - std::shared_ptr GetRdbStore(const RdbStoreConfig &config, int &errCode); + std::shared_ptr GetRdbStore(const RdbStoreConfig &config, + int &errCode, int version, RdbOpenCallback &openCallback); void Remove(const std::string &path); void Clear(); + int SetSecurityLabel(const RdbStoreConfig &config); + void SetReleaseTime(int ms); private: + int ProcessOpenCallback(RdbStore &rdbStore, + const RdbStoreConfig &config, int version, RdbOpenCallback &openCallback); void RestartTimer(const std::string &path, RdbStoreNode &node); static void AutoClose(const std::string &path, RdbStoreManager *manager); std::mutex mutex_; std::shared_ptr timer_; std::map> storeCache_; + // ms_ : [10*1000 ~ 10*60*1000] + int ms_; }; class RdbHelper final { @@ -65,8 +68,7 @@ public: static void ClearCache(); private: - static int ProcessOpenCallback( - RdbStore &rdbStore, const RdbStoreConfig &config, int version, RdbOpenCallback &openCallback); + static void InitSecurityManager(const RdbStoreConfig &config); }; } // namespace NativeRdb } // namespace OHOS diff --git a/relational_store/interfaces/inner_api/rdb/mock/include/rdb_store.h b/relational_store/interfaces/inner_api/rdb/mock/include/rdb_store.h index 5e10884d0d8f5ca82ef285065487c3ef462d040a..c6368f8be0fadcfa4d4fd4694f1c9610e7b92dc4 100644 --- a/relational_store/interfaces/inner_api/rdb/mock/include/rdb_store.h +++ b/relational_store/interfaces/inner_api/rdb/mock/include/rdb_store.h @@ -24,17 +24,9 @@ #include "result_set.h" #include "value_object.h" #include "values_bucket.h" +#include "rdb_common.h" namespace OHOS::NativeRdb { -enum class ConflictResolution { - ON_CONFLICT_NONE = 0, - ON_CONFLICT_ROLLBACK, - ON_CONFLICT_ABORT, - ON_CONFLICT_FAIL, - ON_CONFLICT_IGNORE, - ON_CONFLICT_REPLACE, -}; - class RdbStore { public: virtual ~RdbStore() {} diff --git a/relational_store/interfaces/inner_api/rdb/mock/include/rdb_store_config.h b/relational_store/interfaces/inner_api/rdb/mock/include/rdb_store_config.h index 945ff9f0a123779309170f118a5604080fb3be70..ec005068805ddbde39f5e43f45f99574bb21fe4d 100644 --- a/relational_store/interfaces/inner_api/rdb/mock/include/rdb_store_config.h +++ b/relational_store/interfaces/inner_api/rdb/mock/include/rdb_store_config.h @@ -56,11 +56,6 @@ enum class SecurityLevel : int32_t { LAST }; -enum class OpenStatus { - ON_CREATE, - ON_OPEN, -}; - static constexpr int DB_PAGE_SIZE = 4096; /* default page size : 4k */ static constexpr int DB_JOURNAL_SIZE = 1048576; /* default file size : 1M */ static constexpr char DB_DEFAULT_JOURNAL_MODE[] = "delete";