SQL语句匿名化

Signed-off-by: bjd <baijidong@huawei.com>
This commit is contained in:
bjd 2024-11-20 17:53:42 +08:00
parent fe6a3f5467
commit 71691ea074
2 changed files with 18 additions and 14 deletions

View File

@ -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<std::string> SELECT_ARRAY = { "AS", "GROUPBY", "GROUP", "BY", "LIMIT", "COUNT", "AVERAGE", "SELECT",
"FROM", "WHERE", "DISTRICT" };
const std::vector<std::string> INSERT_ARRAY = { "INSERT", "INTO", "VALUES" };
const std::vector<std::string> UPDATE_ARRAY = { "UPDATE", "SET", "WHERE", "AND", "OR" };
const std::vector<std::string> DELETE_ARRAY = { "DELETE", "FROM", "WHERE" };
const std::vector<std::string> DROP_ARRAY = { "DROP", "TABLE", "IF", "EXISTS", "DATABASE" };
const std::vector<std::string> 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<std::string> &array)
std::string AnonySqlString(const std::string &input, const char *const array[])
{
std::vector<std::string> words = SplitString(input);
std::string result;
std::vector<std::string> 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;

View File

@ -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)