From 814472b52b32d2d39e27053b4940cce68557975f Mon Sep 17 00:00:00 2001 From: liao-yonghuang Date: Fri, 12 Dec 2025 14:45:37 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90rdb=E7=AB=AF=E4=BA=91=E3=80=91?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8C=85=E5=90=AB"timestamp"=E5=88=97?= =?UTF-8?q?=E5=90=8D=E7=9A=84=E8=A1=A8=E6=97=A0=E6=B3=95=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liao-yonghuang --- ...e_single_ver_relational_continue_token.cpp | 2 +- ...single_ver_relational_storage_executor.cpp | 2 +- ...ver_relational_storage_executor_extend.cpp | 2 +- .../src/sqlite/sqlite_query_helper.cpp | 13 +++---- .../storage/src/sqlite/sqlite_query_helper.h | 5 +-- .../rdb/distributeddb_basic_rdb_test.cpp | 34 +++++++++++++++++++ 6 files changed, 47 insertions(+), 11 deletions(-) diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_single_ver_relational_continue_token.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_single_ver_relational_continue_token.cpp index bdc6d90ab0d..be38935557c 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_single_ver_relational_continue_token.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_single_ver_relational_continue_token.cpp @@ -192,7 +192,7 @@ int SQLiteSingleVerRelationalContinueToken::GetCloudStatement(sqlite3 *db, Cloud std::string sql = helper.GetRelationalCloudQuerySql(tableSchema_.fields, cloudDataResult.isCloudForcePushStrategy, cloudDataResult.isCompensatedTask, cloudDataResult.mode); helper.SetAppendCondition(!queryObj_.IsRelaxForDelete() || cloudDataResult.mode != CloudWaterType::DELETE); - errCode = helper.GetCloudQueryStatement(true, db, sql, queryStmt_); + errCode = helper.GetCloudQueryStatement(true, db, sql, queryStmt_, true); if (errCode == E_OK) { queryStmt = queryStmt_; } diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_single_ver_relational_storage_executor.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_single_ver_relational_storage_executor.cpp index 3b08f545db4..262b101188f 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_single_ver_relational_storage_executor.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_single_ver_relational_storage_executor.cpp @@ -1550,7 +1550,7 @@ int SQLiteSingleVerRelationalStorageExecutor::GetSyncCloudGid(QuerySyncObject &q } std::string sql = helper.GetGidRelationalCloudQuerySql(tableSchema_.fields, isCloudForcePushStrategy, isCompensatedTask); - errCode = helper.GetCloudQueryStatement(false, dbHandle_, sql, queryStmt); + errCode = helper.GetCloudQueryStatement(false, dbHandle_, sql, queryStmt, true); if (errCode != E_OK) { return errCode; } diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_single_ver_relational_storage_executor_extend.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_single_ver_relational_storage_executor_extend.cpp index f51fff53828..93866f17c57 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_single_ver_relational_storage_executor_extend.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_single_ver_relational_storage_executor_extend.cpp @@ -1600,7 +1600,7 @@ int SQLiteSingleVerRelationalStorageExecutor::GetUploadCountInner(const Timestam SqliteQueryHelper &helper, std::string &sql, int64_t &count) { sqlite3_stmt *stmt = nullptr; - int errCode = helper.GetCloudQueryStatement(false, dbHandle_, sql, stmt); + int errCode = helper.GetCloudQueryStatement(false, dbHandle_, sql, stmt, true); if (errCode != E_OK) { LOGE("failed to get count statement %d", errCode); return errCode; diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_query_helper.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_query_helper.cpp index 3a13408a6d5..63c41e19d81 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_query_helper.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_query_helper.cpp @@ -231,7 +231,7 @@ int SqliteQueryHelper::ToQuerySql() return errCode; } -int SqliteQueryHelper::ToQuerySyncSql(bool hasSubQuery, bool useTimestampAlias) +int SqliteQueryHelper::ToQuerySyncSql(bool hasSubQuery, bool useTimestampAlias, bool isLeftJoin) { int errCode = ParseQueryObjNodeToSQL(true); if (errCode != E_OK) { @@ -240,9 +240,10 @@ int SqliteQueryHelper::ToQuerySyncSql(bool hasSubQuery, bool useTimestampAlias) // Order by time when no order by and no limit and no need order by key. if (!hasOrderBy_ && !hasLimit_ && !isNeedOrderbyKey_) { + std::string timestampStr = isLeftJoin ? "b.timestamp" : "timestamp"; querySql_ += (useTimestampAlias ? ("ORDER BY " + std::string(DBConstant::TIMESTAMP_ALIAS) + " ASC") : - "ORDER BY timestamp ASC"); + "ORDER BY " + timestampStr + " ASC"); } if (!hasSubQuery) { @@ -966,7 +967,7 @@ int SqliteQueryHelper::GetRelationalSyncDataQuerySql(std::string &sql, bool hasS sql = hasSubQuery ? sql : (sql + GetTimeRangeClauseForRDB()); querySql_.clear(); // clear local query sql format - int errCode = ToQuerySyncSql(hasSubQuery, true); + int errCode = ToQuerySyncSql(hasSubQuery, true, true); if (errCode != E_OK) { LOGE("To query sql fail! errCode[%d]", errCode); return errCode; @@ -1070,7 +1071,7 @@ int SqliteQueryHelper::GetRelationalSyncDataQuerySqlWithLimit(const std::vector< sql += " WHERE (b.flag&0x03=0x02)"; querySql_.clear(); // clear local query sql format - int errCode = ToQuerySyncSql(true, true); + int errCode = ToQuerySyncSql(true, true, true); if (errCode != E_OK) { LOGE("To query sql fail! errCode[%d]", errCode); return errCode; @@ -1269,10 +1270,10 @@ void SqliteQueryHelper::AppendCloudGidQuery(bool isCloudForcePush, bool isCompen } int SqliteQueryHelper::GetCloudQueryStatement(bool useTimestampAlias, sqlite3 *dbHandle, std::string &sql, - sqlite3_stmt *&statement) + sqlite3_stmt *&statement, bool isLeftJoin) { querySql_.clear(); // clear local query sql format - int errCode = ToQuerySyncSql(false, useTimestampAlias); + int errCode = ToQuerySyncSql(false, useTimestampAlias, isLeftJoin); if (errCode != E_OK) { LOGE("To query sql fail! errCode[%d]", errCode); return errCode; diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_query_helper.h b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_query_helper.h index 810cf39e36e..2aba37c9545 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_query_helper.h +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_query_helper.h @@ -109,7 +109,8 @@ public: std::string GetGidRelationalCloudQuerySql(const std::vector &fields, bool isCloudForcePush, bool isCompensatedTask); - int GetCloudQueryStatement(bool useTimestampAlias, sqlite3 *dbHandle, std::string &sql, sqlite3_stmt *&statement); + int GetCloudQueryStatement(bool useTimestampAlias, sqlite3 *dbHandle, std::string &sql, sqlite3_stmt *&statement, + bool isLeftJoin = false); int GetAndBindGidKvCloudQueryStatement(const std::string &user, sqlite3 *dbHandle, sqlite3_stmt *&stmt); @@ -134,7 +135,7 @@ public: static std::string GetCloudVersionRecordSql(bool isDeviceEmpty); private: int ToQuerySql(); - int ToQuerySyncSql(bool hasSubQuery, bool useTimestampAlias = false); + int ToQuerySyncSql(bool hasSubQuery, bool useTimestampAlias = false, bool isLeftJoin = false); int ToGetCountSql(); int ParseQueryExpression(const QueryObjNode &queryNode, std::string &querySql, const std::string &accessStr = "", bool placeholder = true); diff --git a/frameworks/libs/distributeddb/test/unittest/common/store_test/rdb/distributeddb_basic_rdb_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/store_test/rdb/distributeddb_basic_rdb_test.cpp index a48d3951c98..08b4263697d 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/store_test/rdb/distributeddb_basic_rdb_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/store_test/rdb/distributeddb_basic_rdb_test.cpp @@ -341,6 +341,40 @@ HWTEST_F(DistributedDBBasicRDBTest, RdbCloudSyncExample006, TestSize.Level0) EXPECT_EQ(RDBGeneralUt::GetCloudDataCount(g_defaultTable1), 0); EXPECT_EQ(RDBGeneralUt::GetCloudDataCount(g_defaultTable2), 2); } + +/** + * @tc.name: RdbCloudSyncExample008 + * @tc.desc: sync when table have field "timestamp" + * @tc.type: FUNC + * @tc.require: + * @tc.author: liaoyonghuang + */ +HWTEST_F(DistributedDBBasicRDBTest, RdbCloudSyncExample008, TestSize.Level0) +{ + // step1: init local table + RelationalStoreDelegate::Option option; + option.tableMode = DistributedTableMode::COLLABORATION; + SetOption(option); + auto info1 = GetStoreInfo1(); + const std::vector filedInfo = { + {{"id", TYPE_INDEX, true, false}, false}, + {{"timestamp", TYPE_INDEX, false, true}, false}, + }; + std::string tableName = "test_table"; + UtDateBaseSchemaInfo schemaInfo = { + .tablesInfo = { + {.name = tableName, .fieldInfo = filedInfo} + } + }; + RDBGeneralUt::SetSchemaInfo(info1, schemaInfo); + ASSERT_EQ(BasicUnitTest::InitDelegate(info1, "dev1"), E_OK); + InsertLocalDBData(0, 30, info1); + // step2: do sync + ASSERT_EQ(SetDistributedTables(info1, {tableName}, TableSyncType::CLOUD_COOPERATION), E_OK); + RDBGeneralUt::SetCloudDbConfig(info1); + Query query = Query::Select().FromTable({tableName}); + RDBGeneralUt::CloudBlockSync(info1, query); +} #endif // USE_DISTRIBUTEDDB_CLOUD /** -- Gitee