From bbc21ded7c1faa8ace144db595d5fa6ccc733191 Mon Sep 17 00:00:00 2001 From: zqq Date: Fri, 7 Nov 2025 15:39:02 +0800 Subject: [PATCH] set persist wal mode after attach db Signed-off-by: zqq --- .../storage/src/sqlite/sqlite_utils.cpp | 10 ++++++ .../storage/src/sqlite/sqlite_utils.h | 5 ++- .../src/sqlite/sqlite_utils_extend.cpp | 4 +-- .../distributeddb_sqlite_utils_test.cpp | 31 +++++++++++++++++++ 4 files changed, 47 insertions(+), 3 deletions(-) diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp index c2b4d562f24..301f44719ae 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp @@ -330,6 +330,16 @@ int SQLiteUtils::AttachNewDatabase(sqlite3 *db, CipherType type, const CipherPas int SQLiteUtils::AttachNewDatabaseInner(sqlite3 *db, CipherType type, const CipherPassword &password, const std::string &attachDbAbsPath, const std::string &attachAsName) +{ + int errCode = AttachNewDatabaseOnly(db, type, password, attachDbAbsPath, attachAsName); + if (errCode != E_OK) { + return errCode; + } + return SetPersistWalMode(db, attachAsName); +} + +int SQLiteUtils::AttachNewDatabaseOnly(sqlite3 *db, CipherType type, const CipherPassword &password, + const std::string &attachDbAbsPath, const std::string &attachAsName) { // example: "ATTACH '../new.db' AS backup KEY XXXX;" std::string attachSql = "ATTACH ? AS " + attachAsName + " KEY ?;"; // Internal interface not need verify alias name diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_utils.h b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_utils.h index 69b1a18c758..6138adde9d0 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_utils.h +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_utils.h @@ -141,6 +141,9 @@ public: static int AttachNewDatabaseInner(sqlite3 *db, CipherType type, const CipherPassword &password, const std::string &attachDbAbsPath, const std::string &attachAsName); + static int AttachNewDatabaseOnly(sqlite3 *db, CipherType type, const CipherPassword &password, + const std::string &attachDbAbsPath, const std::string &attachAsName); + static int CreateMetaDatabase(const std::string &metaDbPath); static int CheckIntegrity(const std::string &dbFile, CipherType type, const CipherPassword &passwd); @@ -181,7 +184,7 @@ public: static int CheckTableEmpty(sqlite3 *db, const std::string &tableName, bool &isEmpty); - static int SetPersistWalMode(sqlite3 *db); + static int SetPersistWalMode(sqlite3 *db, const std::string &name = "main"); static int64_t GetLastRowId(sqlite3 *db); diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_utils_extend.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_utils_extend.cpp index 7100ec78901..be58406d0ae 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_utils_extend.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_utils_extend.cpp @@ -499,13 +499,13 @@ int SQLiteUtils::CheckTableEmpty(sqlite3 *db, const std::string &tableName, bool return SQLiteUtils::MapSQLiteErrno(errCode != E_OK ? errCode : ret); } -int SQLiteUtils::SetPersistWalMode(sqlite3 *db) +int SQLiteUtils::SetPersistWalMode(sqlite3 *db, const std::string &name) { if (db == nullptr) { return -E_INVALID_ARGS; } int opCode = 1; - int errCode = sqlite3_file_control(db, "main", SQLITE_FCNTL_PERSIST_WAL, &opCode); + int errCode = sqlite3_file_control(db, name.c_str(), SQLITE_FCNTL_PERSIST_WAL, &opCode); if (errCode != SQLITE_OK) { LOGE("Set persist wal mode failed. %d", errCode); } diff --git a/frameworks/libs/distributeddb/test/unittest/common/storage/distributeddb_sqlite_utils_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/storage/distributeddb_sqlite_utils_test.cpp index 77d10cd5e13..30b6664a0b2 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/storage/distributeddb_sqlite_utils_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/storage/distributeddb_sqlite_utils_test.cpp @@ -741,4 +741,35 @@ HWTEST_F(DistributedDBSqliteUtilsTest, GetKvDbSizeTest002, TestSize.Level0) EXPECT_EQ(KvDBUtils::GetKvDbSize(g_dirAll, g_dirStoreOnly, g_name, size), -E_INVALID_DB); EXPECT_EQ(chmod(g_dirStoreOnly.c_str(), (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)), E_OK); } +} + +/** + * @tc.name: AttachSQLiteTest001 + * @tc.desc: Test attach sqlite with + * @tc.type: FUNC + * @tc.author: zqq + */ +HWTEST_F(DistributedDBSqliteUtilsTest, AttachSQLiteTest001, TestSize.Level0) +{ + // create db1 first + auto db1 = NativeSqlite::CreateDataBase(g_dbDir + "test1.db"); + ASSERT_NE(db1, nullptr); + auto ret = sqlite3_close_v2(db1); + ASSERT_EQ(ret, SQLITE_OK); + // create db2 and attach db1 + auto db2 = NativeSqlite::CreateDataBase(g_dbDir + "test2.db"); + ASSERT_NE(db2, nullptr); + ret = SQLiteUtils::AttachNewDatabase(db2, CipherType::DEFAULT, {}, g_dbDir + "test1.db", "test1"); + EXPECT_EQ(ret, E_OK); + // wal not exist because not operate test1.db + EXPECT_FALSE(OS::CheckPathExistence(g_dbDir + "test1.db-wal")); + ret = SQLiteUtils::ExecuteRawSQL(db2, "CREATE TABLE IF NOT EXISTS test1.TEST(COL1 INTEGER PRIMARY KEY)"); + EXPECT_EQ(ret, E_OK); + ret = sqlite3_close_v2(db2); + ASSERT_EQ(ret, SQLITE_OK); + // check db1 db2 wal exist + EXPECT_TRUE(OS::CheckPathExistence(g_dbDir + "test1.db-wal")); + EXPECT_FALSE(OS::CheckPathExistence(g_dbDir + "test2.db-wal")); + ret = SQLiteUtils::AttachNewDatabase(nullptr, CipherType::DEFAULT, {}, g_dbDir + "testxx.db"); + EXPECT_EQ(ret, -E_INVALID_DB); } \ No newline at end of file -- Gitee