From b349f99f945057c65f0f41d0c83709217cbe7b26 Mon Sep 17 00:00:00 2001 From: houpengtao Date: Wed, 16 Aug 2023 08:55:41 +0800 Subject: [PATCH] use string instead of stringstream Signed-off-by: houpengtao --- .../native/rdb/include/rdb_store_impl.h | 6 +- .../native/rdb/include/security_policy.h | 5 +- .../native/rdb/mock/include/rdb_store_impl.h | 6 +- frameworks/native/rdb/src/rdb_store_impl.cpp | 100 +++++++++--------- frameworks/native/rdb/src/security_policy.cpp | 19 ++-- 5 files changed, 64 insertions(+), 72 deletions(-) diff --git a/frameworks/native/rdb/include/rdb_store_impl.h b/frameworks/native/rdb/include/rdb_store_impl.h index 60ccb319a..3e393d8d2 100644 --- a/frameworks/native/rdb/include/rdb_store_impl.h +++ b/frameworks/native/rdb/include/rdb_store_impl.h @@ -168,8 +168,6 @@ public: private: int InnerOpen(); - int InnerInsert(int64_t &outRowId, const std::string &table, ValuesBucket values, - ConflictResolution conflictResolution); int CheckAttach(const std::string &sql); int BeginExecuteSql(const std::string &sql, SqliteConnection **connection); int FreeTransaction(SqliteConnection *connection, const std::string &sql); @@ -178,13 +176,13 @@ private: int GetDataBasePath(const std::string &databasePath, std::string &backupFilePath); int ExecuteSqlInner(const std::string &sql, const std::vector &bindArgs); int ExecuteGetLongInner(const std::string &sql, const std::vector &bindArgs); - void SetAssetStatusWhileInsert(ValueObject &val); + void SetAssetStatusWhileInsert(const ValueObject &val); void DoCloudSync(const std::string &table); int InnerBackup(const std::string databasePath, const std::vector destEncryptKey = std::vector()); std::map GetModifyTimeByRowId( const std::string &logTable, std::vector &keys); - std::string GetSqlArgs(size_t size); + inline std::string GetSqlArgs(size_t size); Uri GetUri(const std::string &event); int SubscribeLocal(const SubscribeOption& option, RdbStoreObserver *observer); int SubscribeLocalShared(const SubscribeOption& option, RdbStoreObserver *observer); diff --git a/frameworks/native/rdb/include/security_policy.h b/frameworks/native/rdb/include/security_policy.h index df1788c07..0e032057e 100644 --- a/frameworks/native/rdb/include/security_policy.h +++ b/frameworks/native/rdb/include/security_policy.h @@ -25,9 +25,8 @@ public: static int SetSecurityLabel(const RdbStoreConfig &config); private: - static std::string GetSecurityLevelValue(SecurityLevel securityLevel); - static int SetFileSecurityLevel(const std::string &filePath, const std::string &securityLevel); - static std::string GetFileSecurityLevel(const std::string &filePath); + inline static std::string GetSecurityLevelValue(SecurityLevel securityLevel); + inline static std::string GetFileSecurityLevel(const std::string &filePath); }; } // namespace NativeRdb } // namespace OHOS diff --git a/frameworks/native/rdb/mock/include/rdb_store_impl.h b/frameworks/native/rdb/mock/include/rdb_store_impl.h index 0ed65a3b0..af9658551 100644 --- a/frameworks/native/rdb/mock/include/rdb_store_impl.h +++ b/frameworks/native/rdb/mock/include/rdb_store_impl.h @@ -99,8 +99,6 @@ public: private: int InnerOpen(); - int InnerInsert(int64_t &outRowId, const std::string &table, ValuesBucket values, - ConflictResolution conflictResolution); int CheckAttach(const std::string &sql); bool PathToRealPath(const std::string &path, std::string &realPath); std::string ExtractFilePath(const std::string &fileFullName); @@ -111,11 +109,11 @@ private: int GetDataBasePath(const std::string &databasePath, std::string &backupFilePath); int ExecuteSqlInner(const std::string &sql, const std::vector &bindArgs); int ExecuteGetLongInner(const std::string &sql, const std::vector &bindArgs); - void SetAssetStatusWhileInsert(ValueObject &val); + void SetAssetStatusWhileInsert(const ValueObject &val); void DoCloudSync(const std::string &table); int InnerBackup(const std::string databasePath, const std::vector destEncryptKey = std::vector()); - + inline std::string GetSqlArgs(size_t size); const RdbStoreConfig rdbStoreConfig; SqliteConnectionPool *connectionPool; diff --git a/frameworks/native/rdb/src/rdb_store_impl.cpp b/frameworks/native/rdb/src/rdb_store_impl.cpp index 62c18e63b..40719145d 100644 --- a/frameworks/native/rdb/src/rdb_store_impl.cpp +++ b/frameworks/native/rdb/src/rdb_store_impl.cpp @@ -197,6 +197,7 @@ std::map RdbStoreImpl::GetModifyTimeByRowId( } return result; } +#endif std::string RdbStoreImpl::GetSqlArgs(size_t size) { @@ -206,7 +207,6 @@ std::string RdbStoreImpl::GetSqlArgs(size_t size) } return args; } -#endif RdbStoreImpl::RdbStoreImpl(const RdbStoreConfig &config, int &errCode) : rdbStoreConfig(config), connectionPool(nullptr), isOpen(true), path(config.GetPath()), orgPath(config.GetPath()), @@ -258,7 +258,7 @@ int RdbStoreImpl::BatchInsert(int64_t &outInsertNum, const std::string &table, } // prepare batch data & sql std::vector>> vecVectorObj; - for (auto iter = initialBatchValues.begin(); iter != initialBatchValues.end(); iter++) { + for (auto iter = initialBatchValues.begin(); iter != initialBatchValues.end(); ++iter) { auto values = (*iter).GetAll(); vecVectorObj.push_back(GetInsertParams(values, table)); } @@ -292,7 +292,7 @@ int RdbStoreImpl::BatchInsert(int64_t &outInsertNum, const std::string &table, } // batch insert the values - for (auto iter = vecVectorObj.begin(); iter != vecVectorObj.end(); iter++) { + for (auto iter = vecVectorObj.begin(); iter != vecVectorObj.end(); ++iter) { outInsertNum++; errCode = connection->ExecuteSql(iter->first, iter->second); if (errCode != E_OK) { @@ -311,24 +311,29 @@ int RdbStoreImpl::BatchInsert(int64_t &outInsertNum, const std::string &table, std::pair> RdbStoreImpl::GetInsertParams( std::map &valuesMap, const std::string &table) { - std::stringstream sql; + std::string sql; std::vector bindArgs; - sql << "INSERT INTO " << table << '('; + sql.append("INSERT INTO ").append(table).append("("); + size_t bindArgsSize = valuesMap.size(); + if (bindArgsSize == 0) { + sql.append(") VALUES ()"); + return std::make_pair(sql, bindArgs); + } + + bindArgs.reserve(bindArgsSize); + auto valueIter = valuesMap.begin(); + sql.append(valueIter->first); + bindArgs.push_back(valueIter->second); + ++valueIter; // prepare batch values & sql.columnName - for (auto valueIter = valuesMap.begin(); valueIter != valuesMap.end(); valueIter++) { - sql << ((valueIter == valuesMap.begin()) ? "" : ","); - sql << valueIter->first; + for (; valueIter != valuesMap.end(); ++valueIter) { + sql.append(",").append(valueIter->first); bindArgs.push_back(valueIter->second); } + sql.append(") VALUES (").append(GetSqlArgs(bindArgsSize)).append(")"); // prepare sql.value - sql << ") VALUES ("; - for (size_t i = 0; i < valuesMap.size(); i++) { - sql << ((i == 0) ? "?" : ",?"); - } - sql << ')'; - // put sql & vec into map - return std::make_pair(sql.str(), bindArgs); + return std::make_pair(sql, bindArgs); } int RdbStoreImpl::Replace(int64_t &outRowId, const std::string &table, const ValuesBucket &initialValues) @@ -338,18 +343,12 @@ int RdbStoreImpl::Replace(int64_t &outRowId, const std::string &table, const Val int RdbStoreImpl::InsertWithConflictResolution(int64_t &outRowId, const std::string &table, const ValuesBucket &initialValues, ConflictResolution conflictResolution) -{ - return InnerInsert(outRowId, table, initialValues, conflictResolution); -} - -int RdbStoreImpl::InnerInsert(int64_t &outRowId, const std::string &table, - ValuesBucket values, ConflictResolution conflictResolution) { if (table.empty()) { return E_EMPTY_TABLE_NAME; } - if (values.IsEmpty()) { + if (initialValues.IsEmpty()) { return E_EMPTY_VALUES_BUCKET; } @@ -359,14 +358,14 @@ int RdbStoreImpl::InnerInsert(int64_t &outRowId, const std::string &table, return errCode; } - std::stringstream sql; - sql << "INSERT" << conflictClause << " INTO " << table << '('; - + std::string sql; + sql.append("INSERT").append(conflictClause).append(" INTO ").append(table).append("("); + size_t bindArgsSize = initialValues.values_.size(); std::vector bindArgs; + bindArgs.reserve(bindArgsSize); const char *split = ""; - for (auto &[key, val] : values.values_) { - sql << split; - sql << key; // columnName + for (const auto &[key, val] : initialValues.values_) { + sql.append(split).append(key); if (val.GetType() == ValueObject::TYPE_ASSETS && conflictResolution == ConflictResolution::ON_CONFLICT_REPLACE) { return E_INVALID_ARGS; @@ -378,18 +377,18 @@ int RdbStoreImpl::InnerInsert(int64_t &outRowId, const std::string &table, split = ","; } - sql << ") VALUES ("; - for (size_t i = 0; i < bindArgs.size(); i++) { - sql << ((i == 0) ? "?" : ",?"); + sql.append(") VALUES ("); + if (bindArgsSize > 0) { + sql.append(GetSqlArgs(bindArgsSize)); } - sql << ')'; + sql.append(")"); SqliteConnection *connection = connectionPool->AcquireConnection(false); if (connection == nullptr) { return E_CON_OVER_LIMIT; } - errCode = connection->ExecuteForLastInsertedRowId(outRowId, sql.str(), bindArgs); + errCode = connection->ExecuteForLastInsertedRowId(outRowId, sql, bindArgs); connectionPool->ReleaseConnection(connection); if (errCode == E_OK) { DoCloudSync(table); @@ -397,7 +396,7 @@ int RdbStoreImpl::InnerInsert(int64_t &outRowId, const std::string &table, return errCode; } -void RdbStoreImpl::SetAssetStatusWhileInsert(ValueObject &val) +void RdbStoreImpl::SetAssetStatusWhileInsert(const ValueObject &val) { if (val.GetType() == ValueObject::TYPE_ASSET) { auto *asset = Traits::get_if(&val.value); @@ -466,24 +465,25 @@ int RdbStoreImpl::UpdateWithConflictResolution(int &changedRows, const std::stri return errCode; } - std::stringstream sql; - sql << "UPDATE" << conflictClause << " " << table << " SET "; - + std::string sql; + sql.append("UPDATE").append(conflictClause).append(" ").append(table).append(" SET "); std::vector tmpBindArgs; + size_t tmpBindSize = values.values_.size() + bindArgs.size(); + tmpBindArgs.reserve(tmpBindSize); const char *split = ""; for (auto &[key, val] : values.values_) { - sql << split; - if (val.GetType() == ValueObject::TYPE_ASSETS) { - sql << key << "=merge_assets(" << key << ", ?)"; // columnName + sql.append(split); + if (val.GetType() != ValueObject::TYPE_ASSETS) { + sql.append(key).append("=?"); // columnName } else { - sql << key << "=?"; // columnName + sql.append(key).append("=merge_assets(").append(key).append(", ?)"); // columnName } tmpBindArgs.push_back(val); // columnValue split = ","; } if (!whereClause.empty()) { - sql << " WHERE " << whereClause; + sql.append(" WHERE ").append(whereClause); } tmpBindArgs.insert(tmpBindArgs.end(), bindArgs.begin(), bindArgs.end()); @@ -493,7 +493,7 @@ int RdbStoreImpl::UpdateWithConflictResolution(int &changedRows, const std::stri return E_CON_OVER_LIMIT; } - errCode = connection->ExecuteForChangedRowCount(changedRows, sql.str(), tmpBindArgs); + errCode = connection->ExecuteForChangedRowCount(changedRows, sql, tmpBindArgs); connectionPool->ReleaseConnection(connection); if (errCode == E_OK) { DoCloudSync(table); @@ -523,10 +523,10 @@ int RdbStoreImpl::Delete(int &deletedRows, const std::string &table, const std:: return E_EMPTY_TABLE_NAME; } - std::stringstream sql; - sql << "DELETE FROM " << table; + std::string sql; + sql.append("DELETE FROM ").append(table); if (!whereClause.empty()) { - sql << " WHERE " << whereClause; + sql.append(" WHERE ").append(whereClause); } SqliteConnection *connection = connectionPool->AcquireConnection(false); @@ -534,7 +534,7 @@ int RdbStoreImpl::Delete(int &deletedRows, const std::string &table, const std:: return E_CON_OVER_LIMIT; } - int errCode = connection->ExecuteForChangedRowCount(deletedRows, sql.str(), bindArgs); + int errCode = connection->ExecuteForChangedRowCount(deletedRows, sql, bindArgs); connectionPool->ReleaseConnection(connection); if (errCode == E_OK) { DoCloudSync(table); @@ -1204,9 +1204,6 @@ std::string RdbStoreImpl::GetName() void RdbStoreImpl::DoCloudSync(const std::string &table) { #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(ANDROID_PLATFORM) && !defined(IOS_PLATFORM) - if (pool_ == nullptr) { - return; - } { std::shared_lock lock(rwMutex_); if (cloudTables_.empty() || (!table.empty() && cloudTables_.find(table) == cloudTables_.end())) { @@ -1228,6 +1225,9 @@ void RdbStoreImpl::DoCloudSync(const std::string &table) return; } } + if (pool_ == nullptr) { + return; + } auto interval = std::chrono::duration_cast(std::chrono::milliseconds(INTERVAL)); pool_->Schedule(interval, [this]() { diff --git a/frameworks/native/rdb/src/security_policy.cpp b/frameworks/native/rdb/src/security_policy.cpp index 69e197383..75c46ec7c 100644 --- a/frameworks/native/rdb/src/security_policy.cpp +++ b/frameworks/native/rdb/src/security_policy.cpp @@ -22,14 +22,6 @@ namespace OHOS { namespace NativeRdb { using namespace OHOS::Rdb; - -int SecurityPolicy::SetFileSecurityLevel(const std::string &filePath, const std::string &securityLevel) -{ - bool result = FileManagement::ModuleSecurityLabel::SecurityLabel::SetSecurityLabel(filePath, securityLevel); - LOG_INFO("Set database securityLabel:%{public}s, result:%{public}d.", securityLevel.c_str(), result); - return result ? E_OK : E_ERROR; -} - std::string SecurityPolicy::GetSecurityLevelValue(SecurityLevel securityLevel) { switch (securityLevel) { @@ -56,11 +48,16 @@ int SecurityPolicy::SetSecurityLabel(const RdbStoreConfig &config) if (config.GetStorageMode() != StorageMode::MODE_MEMORY && config.GetSecurityLevel() != SecurityLevel::LAST) { std::string currentLevel = GetFileSecurityLevel(config.GetPath()); std::string toSetLevel = GetSecurityLevelValue(config.GetSecurityLevel()); - LOG_INFO("Security level current is %{public}s to %{public}s.", currentLevel.c_str(), toSetLevel.c_str()); + int errCode = E_OK; if (currentLevel.empty()) { - return SetFileSecurityLevel(config.GetPath(), toSetLevel); + errCode = FileManagement::ModuleSecurityLabel::SecurityLabel::SetSecurityLabel(config.GetPath(), + toSetLevel) ? E_OK : E_ERROR; + } else { + errCode = currentLevel == toSetLevel ? E_OK : E_ERROR; } - return currentLevel == toSetLevel ? E_OK : E_ERROR; + LOG_INFO("Set security level from %{public}s to %{public}s, result:%{public}d.", currentLevel.c_str(), + toSetLevel.c_str(), errCode); + return errCode; } return E_OK; } -- Gitee