From 66c064b10f94ba9c0bb173c27eef520ee412966f Mon Sep 17 00:00:00 2001
From: CBS <56461666+bright-starry-sky@users.noreply.github.com>
Date: Mon, 15 Mar 2021 19:14:46 +0800
Subject: [PATCH] fixed toString method of create index sentence (#818)

---
 src/parser/MaintainSentences.cpp | 18 +++++++++++----
 src/parser/Sentence.h            |  2 +-
 src/parser/test/ParserTest.cpp   | 38 ++++++++++++++++++++++++++++++++
 3 files changed, 53 insertions(+), 5 deletions(-)

diff --git a/src/parser/MaintainSentences.cpp b/src/parser/MaintainSentences.cpp
index 0a05e4f9..749e0189 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 8a7392c7..ccb56b5c 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 400c6289..c3db9582 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());
     }
 }
 
-- 
GitLab