From 71691ea07464b8bed139669bff4737068b0ff6f9 Mon Sep 17 00:00:00 2001 From: bjd Date: Wed, 20 Nov 2024 17:53:42 +0800 Subject: [PATCH] =?UTF-8?q?SQL=E8=AF=AD=E5=8F=A5=E5=8C=BF=E5=90=8D?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: bjd --- frameworks/native/rdb/src/sqlite_utils.cpp | 22 +++++++++++-------- .../native/rdb/unittest/sqlite_utils_test.cpp | 10 ++++----- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/frameworks/native/rdb/src/sqlite_utils.cpp b/frameworks/native/rdb/src/sqlite_utils.cpp index 722340954..fa7c4f252 100644 --- a/frameworks/native/rdb/src/sqlite_utils.cpp +++ b/frameworks/native/rdb/src/sqlite_utils.cpp @@ -50,13 +50,13 @@ constexpr int32_t MIN_ANONYMIZE_LENGTH = 2; constexpr int32_t MAX_ANONYMIZE_LENGTH = 4; constexpr int32_t OTHER_SIZE = 6; constexpr int32_t START_SIZE = 0; -const std::vector SELECT_ARRAY = { "AS", "GROUPBY", "GROUP", "BY", "LIMIT", "COUNT", "AVERAGE", "SELECT", - "FROM", "WHERE", "DISTRICT" }; -const std::vector INSERT_ARRAY = { "INSERT", "INTO", "VALUES" }; -const std::vector UPDATE_ARRAY = { "UPDATE", "SET", "WHERE", "AND", "OR" }; -const std::vector DELETE_ARRAY = { "DELETE", "FROM", "WHERE" }; -const std::vector DROP_ARRAY = { "DROP", "TABLE", "IF", "EXISTS", "DATABASE" }; -const std::vector PRAGMA_ARRAY = { "PRAGMA" }; +constexpr const char *SELECT_ARRAY[] = { "AS", "GROUPBY", "GROUP", "BY", "LIMIT", "COUNT", "AVERAGE", "SELECT", "FROM", + "WHERE", "DISTRICT" }; +constexpr const char *INSERT_ARRAY[] = { "INSERT", "INTO", "VALUES" }; +constexpr const char *UPDATE_ARRAY[] = { "UPDATE", "SET", "WHERE", "AND", "OR" }; +constexpr const char *DELETE_ARRAY[] = { "DELETE", "FROM", "WHERE" }; +constexpr const char *DROP_ARRAY[] = { "DROP", "TABLE", "IF", "EXISTS", "DATABASE" }; +constexpr const char *PRAGMA_ARRAY[] = { "PRAGMA" }; constexpr SqliteUtils::SqlType SqliteUtils::SQL_TYPE_MAP[]; constexpr const char *SqliteUtils::ON_CONFLICT_CLAUSE[]; @@ -326,14 +326,18 @@ std::string AnonyString(const std::string &input) return result; } -std::string AnonySqlString(const std::string &input, const std::vector &array) +std::string AnonySqlString(const std::string &input, const char *const array[]) { std::vector words = SplitString(input); std::string result; + std::vector arrayVector; + for (int i = 0; array[i] != nullptr; i++) { + arrayVector.emplace_back(array[i]); + } for (const std::string &word : words) { std::string anonyWord = word; std::string upperWord = SqliteUtils::StrToUpper(word); - if (std::find(array.begin(), array.end(), upperWord) == array.end()) { + if (std::find(arrayVector.begin(), arrayVector.end(), upperWord) == arrayVector.end()) { anonyWord = AnonyWord(anonyWord); } result += anonyWord; diff --git a/test/native/rdb/unittest/sqlite_utils_test.cpp b/test/native/rdb/unittest/sqlite_utils_test.cpp index ce50b6d8d..cd5ee446a 100644 --- a/test/native/rdb/unittest/sqlite_utils_test.cpp +++ b/test/native/rdb/unittest/sqlite_utils_test.cpp @@ -164,19 +164,19 @@ HWTEST_F(SqliteUtilsTest, SqliteUtils_Test_0023, TestSize.Level1) HWTEST_F(SqliteUtilsTest, SqliteUtils_Test_0024, TestSize.Level1) { - EXPECT_EQ(SqliteUtils::AnonySql("select value1, value2 from table WHERE case = 1."), - "select ***ue1, ***ue2 from **ble WHERE *ase = *."); + EXPECT_EQ(SqliteUtils::AnonySql("select value1, value2 from table1 WHERE case = 1."), + "select ***ue1, ***ue2 from ***le1 WHERE *ase = *."); } HWTEST_F(SqliteUtilsTest, SqliteUtils_Test_0025, TestSize.Level1) { - EXPECT_EQ(SqliteUtils::AnonySql("select district value1, value2 from table WHERE case = 1 groupby value1 limit 1."), - "select district ***ue1, ***ue2 from **ble WHERE *ase = * groupby ***ue1 limit *."); + EXPECT_EQ(SqliteUtils::AnonySql("select district value1, value2 from table1 WHERE case = 1 groupby value1 limit 1."), + "select district ***ue1, ***ue2 from ***le1 WHERE *ase = * groupby ***ue1 limit *."); } HWTEST_F(SqliteUtilsTest, SqliteUtils_Test_0026, TestSize.Level1) { - EXPECT_EQ(SqliteUtils::AnonySql("select value1, value2 from table."), "select ***ue1, ***ue2 from **ble."); + EXPECT_EQ(SqliteUtils::AnonySql("select value1, value2 from table1."), "select ***ue1, ***ue2 from ***le1."); } HWTEST_F(SqliteUtilsTest, SqliteUtils_Test_0027, TestSize.Level1)