From a895034ec623474110fec353b8217e2cc344aa48 Mon Sep 17 00:00:00 2001 From: zqq Date: Sat, 18 Jan 2025 11:57:21 +0800 Subject: [PATCH] support from tables Signed-off-by: zqq --- .../distributeddb/common/src/db_common.cpp | 5 -- .../sqlite_relational_store_connection.cpp | 22 ++------ .../single_ver_relational_syncer.cpp | 56 ++++++++++++------- .../singlever/single_ver_relational_syncer.h | 2 + .../distributeddb_rdb_collaboration_test.cpp | 44 ++++++++++++++- 5 files changed, 85 insertions(+), 44 deletions(-) diff --git a/frameworks/libs/distributeddb/common/src/db_common.cpp b/frameworks/libs/distributeddb/common/src/db_common.cpp index 4f5c51f5336..b63ee273a29 100644 --- a/frameworks/libs/distributeddb/common/src/db_common.cpp +++ b/frameworks/libs/distributeddb/common/src/db_common.cpp @@ -492,11 +492,6 @@ bool DBCommon::CheckIsAlnumOrUnderscore(const std::string &text) bool DBCommon::CheckQueryWithoutMultiTable(const Query &query) { - QuerySyncObject syncObject(query); - if (!syncObject.GetRelationTableNames().empty()) { - LOGE("check query table names from tables failed!"); - return false; - } if (!QuerySyncObject::GetQuerySyncObject(query).empty()) { LOGE("check query object from table failed!"); return false; diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store_connection.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store_connection.cpp index a9ddc5dd672..628fcf1e65a 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store_connection.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store_connection.cpp @@ -238,15 +238,6 @@ int SQLiteRelationalStoreConnection::SyncToDevice(SyncInfo &info) return -E_INVALID_CONNECTION; } - { - AutoLock lockGuard(this); - if (IsKilled()) { - // If this happens, users are using a closed connection. - LOGE("Sync on a closed connection."); - return -E_STALE; - } - IncObjRef(this); - } ISyncer::SyncParma syncParam; syncParam.devices = info.devices; syncParam.mode = info.mode; @@ -254,20 +245,15 @@ int SQLiteRelationalStoreConnection::SyncToDevice(SyncInfo &info) syncParam.isQuerySync = true; syncParam.relationOnComplete = info.onComplete; syncParam.syncQuery = QuerySyncObject(info.query); - syncParam.onFinalize = [this]() { - DecObjRef(this); - }; if (syncParam.syncQuery.GetSortType() != SortType::NONE) { LOGE("not support order by timestamp"); - DecObjRef(this); return -E_NOT_SUPPORT; } - int errCode = store->Sync(syncParam, GetConnectionId()); - if (errCode != E_OK) { - DecObjRef(this); - return errCode; + if (syncParam.syncQuery.GetValidStatus() != E_OK) { + LOGE("invalid sync query origin valid status %d", syncParam.syncQuery.GetValidStatus()); + return -E_INVALID_ARGS; } - return E_OK; + return store->Sync(syncParam, GetConnectionId()); } int SQLiteRelationalStoreConnection::RegisterLifeCycleCallback(const DatabaseLifeCycleNotifier ¬ifier) diff --git a/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_relational_syncer.cpp b/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_relational_syncer.cpp index 0671b028b98..8b090a45f72 100644 --- a/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_relational_syncer.cpp +++ b/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_relational_syncer.cpp @@ -43,7 +43,7 @@ int SingleVerRelationalSyncer::PrepareSync(const SyncParma ¶m, uint32_t sync const auto &syncInterface = static_cast(syncInterface_); std::vector tablesQuery; if (param.isQuerySync) { - tablesQuery.push_back(param.syncQuery); + tablesQuery = GetQuerySyncObject(param); } else { tablesQuery = syncInterface->GetTablesQuery(); } @@ -178,25 +178,31 @@ int SingleVerRelationalSyncer::SyncConditionCheck(const SyncParma ¶m, const if (!param.isQuerySync) { return E_OK; } - QuerySyncObject query = param.syncQuery; - int errCode = static_cast(storage)->CheckAndInitQueryCondition(query); - if (errCode != E_OK) { - LOGE("[SingleVerRelationalSyncer] QuerySyncObject check failed"); - return errCode; - } - const RelationalSchemaObject &schemaObj = static_cast(storage)->GetSchemaInfo(); + auto queryList = GetQuerySyncObject(param); + const RelationalSchemaObject schemaObj = static_cast(storage)->GetSchemaInfo(); + const std::vector &sTable = schemaObj.GetDistributedSchema().tables; if (schemaObj.GetTableMode() == DistributedTableMode::COLLABORATION) { - const std::vector &sTable = schemaObj.GetDistributedSchema().tables; if (sTable.empty()) { LOGE("[SingleVerRelationalSyncer] Distributed schema not set in COLLABORATION mode"); return -E_SCHEMA_MISMATCH; } - auto iter = std::find_if(sTable.begin(), sTable.end(), [¶m](const DistributedTable &table) { - return DBCommon::ToLowerCase(table.tableName) == DBCommon::ToLowerCase(param.syncQuery.GetTableName()); - }); - if (iter == sTable.end()) { - LOGE("[SingleVerRelationalSyncer] table name mismatch"); - return -E_SCHEMA_MISMATCH; + } + for (auto &item : queryList) { + int errCode = static_cast(storage)->CheckAndInitQueryCondition(item); + if (errCode != E_OK) { + LOGE("[SingleVerRelationalSyncer] table %s query check failed %d", + DBCommon::StringMiddleMasking(item.GetTableName()).c_str(), errCode); + return errCode; + } + if (schemaObj.GetTableMode() == DistributedTableMode::COLLABORATION) { + auto iter = std::find_if(sTable.begin(), sTable.end(), [&item](const DistributedTable &table) { + return DBCommon::ToLowerCase(table.tableName) == DBCommon::ToLowerCase(item.GetTableName()); + }); + if (iter == sTable.end()) { + LOGE("[SingleVerRelationalSyncer] table %s mismatch distributed schema", + DBCommon::StringMiddleMasking(item.GetTableName()).c_str()); + return -E_SCHEMA_MISMATCH; + } } } if (param.mode == SUBSCRIBE_QUERY) { @@ -207,10 +213,6 @@ int SingleVerRelationalSyncer::SyncConditionCheck(const SyncParma ¶m, const int SingleVerRelationalSyncer::QuerySyncPreCheck(const SyncParma ¶m) const { - if (!param.syncQuery.GetRelationTableNames().empty()) { - LOGE("[SingleVerRelationalSyncer] sync with not support query"); - return -E_NOT_SUPPORT; - } if (!param.isQuerySync) { return E_OK; } @@ -218,11 +220,25 @@ int SingleVerRelationalSyncer::QuerySyncPreCheck(const SyncParma ¶m) const LOGE("[SingleVerRelationalSyncer] sync with not support push_pull mode"); return -E_NOT_SUPPORT; } - if (param.syncQuery.GetRelationTableName().empty()) { + if (param.syncQuery.GetRelationTableName().empty() && param.syncQuery.GetRelationTableNames().empty()) { LOGE("[SingleVerRelationalSyncer] sync with empty table"); return -E_NOT_SUPPORT; } return E_OK; } + +std::vector SingleVerRelationalSyncer::GetQuerySyncObject(const SyncParma ¶m) +{ + std::vector res; + auto tables = param.syncQuery.GetRelationTableNames(); + if (!tables.empty()) { + for (const auto &it : tables) { + res.emplace_back(Query::Select(it)); + } + } else { + res.push_back(param.syncQuery); + } + return res; +} } #endif \ No newline at end of file diff --git a/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_relational_syncer.h b/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_relational_syncer.h index 04102c8d95f..93be5b2532a 100644 --- a/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_relational_syncer.h +++ b/frameworks/libs/distributeddb/syncer/src/device/singlever/single_ver_relational_syncer.h @@ -52,6 +52,8 @@ private: int QuerySyncPreCheck(const SyncParma ¶m) const; + static std::vector GetQuerySyncObject(const SyncParma ¶m); + mutable std::mutex syncMapLock_; std::map> fullSyncIdMap_; std::map>> resMap_; diff --git a/frameworks/libs/distributeddb/test/unittest/common/storage/distributeddb_rdb_collaboration_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/storage/distributeddb_rdb_collaboration_test.cpp index df7eba6f184..d842ed65962 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/storage/distributeddb_rdb_collaboration_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/storage/distributeddb_rdb_collaboration_test.cpp @@ -1243,7 +1243,7 @@ HWTEST_F(DistributedDBRDBCollaborationTest, NormalSync011, TestSize.Level0) * @tc.steps: step3. Sync to real device * @tc.expected: step3.ok */ - Query query = Query::Select(tableSchema.name); + Query query = Query::Select().FromTable({tableSchema.name}); DistributedDBToolsUnitTest::BlockSync(*delegate_, query, SYNC_MODE_PUSH_ONLY, OK, {deviceB_->GetDeviceId()}); } @@ -1345,6 +1345,48 @@ HWTEST_F(DistributedDBRDBCollaborationTest, NormalSync013, TestSize.Level0) EXPECT_EQ(changeData[tableSchema.name].field.size(), 1u); } +/** + * @tc.name: NormalSync014 + * @tc.desc: Test sync multi table. + * @tc.type: FUNC + * @tc.require: + * @tc.author: zqq + */ +HWTEST_F(DistributedDBRDBCollaborationTest, NormalSync014, TestSize.Level0) +{ + /** + * @tc.steps: step1. Create device table and cloud table in COLLABORATION + * @tc.expected: step1.ok + */ + ASSERT_NO_FATAL_FAILURE(InitDelegate()); + auto schema = GetSchema(); + auto distributedSchema = RDBDataGenerator::ParseSchema(schema, true); + deviceB_->SetDistributedSchema(distributedSchema); + EXPECT_EQ(delegate_->SetDistributedSchema(distributedSchema), OK); + EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE, TableSyncType::DEVICE_COOPERATION), OK); + EXPECT_EQ(delegate_->CreateDistributedTable(DEVICE_SYNC_TABLE_UPGRADE, TableSyncType::DEVICE_COOPERATION), OK); + /** + * @tc.steps: step2. Insert one data + * @tc.expected: step2.ok + */ + auto tableSchema = GetTableSchema(); + EXPECT_EQ(RDBDataGenerator::InsertLocalDBData(0, 1, db_, GetTableSchema()), E_OK); + ASSERT_EQ(RDBDataGenerator::PrepareVirtualDeviceEnv(tableSchema.name, db_, deviceB_), E_OK); + /** + * @tc.steps: step3. Sync to real device + * @tc.expected: step3.ok + */ + Query query = Query::Select().FromTable({DEVICE_SYNC_TABLE, DEVICE_SYNC_TABLE_UPGRADE}); + DistributedDBToolsUnitTest::BlockSync(*delegate_, query, SYNC_MODE_PUSH_ONLY, OK, {deviceB_->GetDeviceId()}); + /** + * @tc.steps: step4. Sync with invalid args + * @tc.expected: step4.invalid args + */ + query.And(); + DBStatus callStatus = delegate_->Sync({deviceB_->GetDeviceId()}, SYNC_MODE_PUSH_ONLY, query, nullptr, true); + EXPECT_EQ(callStatus, INVALID_ARGS); +} + /** * @tc.name: InvalidSync001 * @tc.desc: Test remote set empty distributed schema and sync. -- Gitee