diff --git a/src/parser/MaintainSentences.cpp b/src/parser/MaintainSentences.cpp index 0a05e4f9751688e5738b19462da6bc90ab22538b..749e0189a1371c8ffd947283c0258cc9543b9910 100644 --- a/src/parser/MaintainSentences.cpp +++ b/src/parser/MaintainSentences.cpp @@ -232,14 +232,19 @@ std::string CreateTagIndexSentence::toString() const { std::string buf; buf.reserve(256); buf += "CREATE TAG INDEX "; + if (isIfNotExist()) { + buf += "IF NOT EXISTS "; + } buf += *indexName_; buf += " ON "; buf += *tagName_; - buf += " ("; + buf += "("; std::vector<std::string> fieldDefs; for (const auto& field : this->fields()) { std::string f = field.get_name(); - if (field.__isset.type_length) f + "(" +field.get_type_length() + ")"; + if (field.__isset.type_length) { + f += "(" + std::to_string(*field.get_type_length()) + ")"; + } fieldDefs.emplace_back(std::move(f)); } std::string fields; @@ -254,14 +259,19 @@ std::string CreateEdgeIndexSentence::toString() const { std::string buf; buf.reserve(256); buf += "CREATE EDGE INDEX "; + if (isIfNotExist()) { + buf += "IF NOT EXISTS "; + } buf += *indexName_; buf += " ON "; buf += *edgeName_; - buf += " ("; + buf += "("; std::vector<std::string> fieldDefs; for (const auto& field : this->fields()) { std::string f = field.get_name(); - if (field.__isset.type_length) f + "(" +field.get_type_length() + ")"; + if (field.__isset.type_length) { + f += "(" + std::to_string(*field.get_type_length()) + ")"; + } fieldDefs.emplace_back(std::move(f)); } std::string fields; diff --git a/src/parser/Sentence.h b/src/parser/Sentence.h index 8a7392c7ba8b5dd24514e6e8927867ef0e3fa09d..ccb56b5c8a45bd535d6fdf3e1a53ea6089d66437 100644 --- a/src/parser/Sentence.h +++ b/src/parser/Sentence.h @@ -144,7 +144,7 @@ public: explicit CreateSentence(bool ifNotExist) : ifNotExist_{ifNotExist} {} virtual ~CreateSentence() {} - bool isIfNotExist() { + bool isIfNotExist() const { return ifNotExist_; } diff --git a/src/parser/test/ParserTest.cpp b/src/parser/test/ParserTest.cpp index 400c62898616eb2f51d0d00ac61b2fc6cac0cdbc..c3db9582f9133ab64b64730c03017b3e8e47fe3c 100644 --- a/src/parser/test/ParserTest.cpp +++ b/src/parser/test/ParserTest.cpp @@ -495,96 +495,134 @@ TEST(Parser, IndexOperation) { std::string query = "CREATE TAG INDEX empty_field_index ON person()"; auto result = parse(query); ASSERT_TRUE(result.ok()) << result.status(); + auto& sentence = result.value(); + EXPECT_EQ(query, sentence->toString()); } { std::string query = "CREATE TAG INDEX name_index ON person(name)"; auto result = parse(query); ASSERT_TRUE(result.ok()) << result.status(); + auto& sentence = result.value(); + EXPECT_EQ(query, sentence->toString()); } { std::string query = "CREATE TAG INDEX IF NOT EXISTS name_index ON person(name)"; auto result = parse(query); ASSERT_TRUE(result.ok()) << result.status(); + auto& sentence = result.value(); + EXPECT_EQ(query, sentence->toString()); } { std::string query = "CREATE TAG INDEX IF NOT EXISTS name_index ON person(name, age)"; auto result = parse(query); ASSERT_TRUE(result.ok()) << result.status(); + auto& sentence = result.value(); + EXPECT_EQ(query, sentence->toString()); } { std::string query = "CREATE EDGE INDEX IF NOT EXISTS empty_field_index ON service()"; auto result = parse(query); ASSERT_TRUE(result.ok()) << result.status(); + auto& sentence = result.value(); + EXPECT_EQ(query, sentence->toString()); } { std::string query = "CREATE EDGE INDEX IF NOT EXISTS like_index ON service(like)"; auto result = parse(query); ASSERT_TRUE(result.ok()) << result.status(); + auto& sentence = result.value(); + EXPECT_EQ(query, sentence->toString()); } { std::string query = "CREATE EDGE INDEX IF NOT EXISTS like_index ON service(like, score)"; auto result = parse(query); ASSERT_TRUE(result.ok()) << result.status(); + auto& sentence = result.value(); + EXPECT_EQ(query, sentence->toString()); } { std::string query = "CREATE EDGE INDEX IF NOT EXISTS std_index ON t1(c1(10), c2(20))"; auto result = parse(query); ASSERT_TRUE(result.ok()) << result.status(); + auto& sentence = result.value(); + EXPECT_EQ(query, sentence->toString()); } { std::string query = "CREATE EDGE INDEX IF NOT EXISTS std_index ON t1(c1, c2(20))"; auto result = parse(query); ASSERT_TRUE(result.ok()) << result.status(); + auto& sentence = result.value(); + EXPECT_EQ(query, sentence->toString()); } { std::string query = "CREATE EDGE INDEX IF NOT EXISTS std_index ON t1(c1(10), c2)"; auto result = parse(query); ASSERT_TRUE(result.ok()) << result.status(); + auto& sentence = result.value(); + EXPECT_EQ(query, sentence->toString()); } { std::string query = "CREATE TAG INDEX IF NOT EXISTS std_index ON t1(c1(10), c2(20))"; auto result = parse(query); ASSERT_TRUE(result.ok()) << result.status(); + auto& sentence = result.value(); + EXPECT_EQ(query, sentence->toString()); } { std::string query = "CREATE TAG INDEX IF NOT EXISTS std_index ON t1(c1, c2(20))"; auto result = parse(query); ASSERT_TRUE(result.ok()) << result.status(); + auto& sentence = result.value(); + EXPECT_EQ(query, sentence->toString()); } { std::string query = "CREATE TAG INDEX IF NOT EXISTS std_index ON t1(c1(10), c2)"; auto result = parse(query); ASSERT_TRUE(result.ok()) << result.status(); + auto& sentence = result.value(); + EXPECT_EQ(query, sentence->toString()); } { std::string query = "DROP TAG INDEX name_index"; auto result = parse(query); ASSERT_TRUE(result.ok()) << result.status(); + auto& sentence = result.value(); + EXPECT_EQ(query, sentence->toString()); } { std::string query = "DROP EDGE INDEX like_index"; auto result = parse(query); ASSERT_TRUE(result.ok()) << result.status(); + auto& sentence = result.value(); + EXPECT_EQ(query, sentence->toString()); } { std::string query = "DESCRIBE TAG INDEX name_index"; auto result = parse(query); ASSERT_TRUE(result.ok()) << result.status(); + auto& sentence = result.value(); + EXPECT_EQ(query, sentence->toString()); } { std::string query = "DESCRIBE EDGE INDEX like_index"; auto result = parse(query); ASSERT_TRUE(result.ok()) << result.status(); + auto& sentence = result.value(); + EXPECT_EQ(query, sentence->toString()); } { std::string query = "REBUILD TAG INDEX name_index"; auto result = parse(query); ASSERT_TRUE(result.ok()) << result.status(); + auto& sentence = result.value(); + EXPECT_EQ(query, sentence->toString()); } { std::string query = "REBUILD EDGE INDEX like_index"; auto result = parse(query); ASSERT_TRUE(result.ok()) << result.status(); + auto& sentence = result.value(); + EXPECT_EQ(query, sentence->toString()); } }