diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp index c2b4d562f2414dd95990583d1891b832e31931ac..301f44719aed863ae25b7108e88132bb66398b79 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 69b1a18c75891541ab13fb27faa260cbb2f52db7..6138adde9d0a164ed808364d8ed37ec35c53465d 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 7100ec7890170e218e6b8cf7a06877ed8eeec44f..be58406d0aeb0429056e9c109a909fb54fbb294c 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 77d10cd5e133b575de35bb5384039729b2d69959..30b6664a0b229b678e8596d483327c7148ee0395 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