diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store.cpp index 4a73c065337d7a62209959f5c4632d79aff9b37d..b44f911a367920be6cef7516fa486a16d5a87423 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store.cpp @@ -433,25 +433,17 @@ int SQLiteRelationalStore::CreateDistributedTable(const std::string &tableName) int SQLiteRelationalStore::RemoveDeviceData() { - auto mode = static_cast(sqliteStorageEngine_->GetProperties().GetIntProp( - RelationalDBProperties::DISTRIBUTED_TABLE_MODE, DistributedTableMode::SPLIT_BY_DEVICE)); - if (mode == DistributedTableMode::COLLABORATION) { - LOGE("Not support remove device data in collaboration mode."); - return -E_NOT_SUPPORT; - } - - TableInfoMap tables = sqliteStorageEngine_->GetSchema().GetTables(); // TableInfoMap - if (tables.empty()) { - return E_OK; + int errCode = RemoveDevDataPreCheck(""); + if (errCode != E_OK) { + return errCode; } - - int errCode = E_OK; auto *handle = GetHandleAndStartTransaction(errCode); if (handle == nullptr) { return errCode; } std::vector tableNameList; + TableInfoMap tables = sqliteStorageEngine_->GetSchema().GetTables(); // TableInfoMap for (const auto &table: tables) { errCode = handle->DeleteDistributedDeviceTable("", table.second.GetTableName()); if (errCode != E_OK) { @@ -481,20 +473,10 @@ int SQLiteRelationalStore::RemoveDeviceData() int SQLiteRelationalStore::RemoveDeviceData(const std::string &device, const std::string &tableName) { - auto mode = static_cast(sqliteStorageEngine_->GetProperties().GetIntProp( - RelationalDBProperties::DISTRIBUTED_TABLE_MODE, DistributedTableMode::SPLIT_BY_DEVICE)); - if (mode == DistributedTableMode::COLLABORATION) { - LOGE("Not support remove device data in collaboration mode."); - return -E_NOT_SUPPORT; - } - - TableInfoMap tables = sqliteStorageEngine_->GetSchema().GetTables(); // TableInfoMap - if (tables.empty() || (!tableName.empty() && tables.find(tableName) == tables.end())) { - LOGE("Remove device data with table name which is not a distributed table or no distributed table found."); - return -E_DISTRIBUTED_SCHEMA_NOT_FOUND; + int errCode = RemoveDevDataPreCheck(tableName); + if (errCode != E_OK) { + return errCode; } - - int errCode = E_OK; auto *handle = GetHandle(true, errCode); if (handle == nullptr) { return errCode; @@ -506,6 +488,7 @@ int SQLiteRelationalStore::RemoveDeviceData(const std::string &device, const std return errCode; } + TableInfoMap tables = sqliteStorageEngine_->GetSchema().GetTables(); // TableInfoMap std::string hashDev = DBCommon::TransferStringToHex(DBCommon::TransferHashString(device)); std::string devTableName = GetDevTableName(device, hashDev); errCode = handle->DeleteDistributedDeviceTable(devTableName, tableName); @@ -773,5 +756,23 @@ SQLiteSingleVerRelationalStorageExecutor *SQLiteRelationalStore::GetHandleAndSta } return handle; } + +int SQLiteRelationalStore::RemoveDevDataPreCheck(const std::string &tableName) const +{ + auto mode = static_cast(sqliteStorageEngine_->GetProperties().GetIntProp( + RelationalDBProperties::DISTRIBUTED_TABLE_MODE, DistributedTableMode::SPLIT_BY_DEVICE)); + if (mode == DistributedTableMode::COLLABORATION) { + LOGE("Not support remove device data in collaboration mode."); + return -E_NOT_SUPPORT; + } + + TableInfoMap tables = sqliteStorageEngine_->GetSchema().GetTables(); // TableInfoMap + if (tables.empty() || (!tableName.empty() && tables.find(tableName) == tables.end())) { + LOGE("Remove device data with table name which is not a distributed table or no distributed table found."); + return -E_DISTRIBUTED_SCHEMA_NOT_FOUND; + } + + return E_OK; +} } #endif \ No newline at end of file diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store.h b/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store.h index 268643ee11a0e0cf62f080775797b1cb183e972f..17582311fadb48554d6de1f4037f7f543b0e572e 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store.h +++ b/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store.h @@ -105,6 +105,8 @@ private: SQLiteSingleVerRelationalStorageExecutor *GetHandleAndStartTransaction(int &errCode) const; + int RemoveDevDataPreCheck(const std::string &tableName) const; + // use for sync Interactive std::unique_ptr syncAbleEngine_ = nullptr; // For storage operate sync function // use ref obj same as kv diff --git a/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_relational_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_relational_test.cpp index 4dabc18f26ae87720b361675ec9e96de5d846479..1b5ec271052919d5ed13e5769219d0f2a6c6bd2a 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_relational_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_relational_test.cpp @@ -729,6 +729,7 @@ HWTEST_F(DistributedDBInterfacesRelationalTest, RelationalRemoveDeviceDataTest00 * @tc.steps:step3. Remove device data * @tc.expected: step3. ok */ + EXPECT_EQ(delegate->RemoveDeviceData(), DISTRIBUTED_SCHEMA_NOT_FOUND); EXPECT_EQ(delegate->RemoveDeviceData("DEVICE_A"), DISTRIBUTED_SCHEMA_NOT_FOUND); EXPECT_EQ(delegate->RemoveDeviceData("DEVICE_D"), DISTRIBUTED_SCHEMA_NOT_FOUND); EXPECT_EQ(delegate->RemoveDeviceData("DEVICE_A", "sync_data"), DISTRIBUTED_SCHEMA_NOT_FOUND);