This is an automated email from the ASF dual-hosted git repository.

zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 6c711eb  add special formart for alterTable (#8335)
6c711eb is described below

commit 6c711ebc6bc110c8fb60d9c0c5dcf6370da6dc39
Author: JingShang Lu <[email protected]>
AuthorDate: Thu Nov 26 13:07:00 2020 +0800

    add special formart for alterTable (#8335)
---
 .../visitor/format/impl/MySQLFormatSQLVisitor.java | 96 ++++++++++++++++++++++
 .../sql/parser/mysql/MySQLFormartTest.java         | 10 ++-
 2 files changed, 105 insertions(+), 1 deletion(-)

diff --git 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/format/impl/MySQLFormatSQLVisitor.java
 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/format/impl/MySQLFormatSQLVisitor.java
index 9211bbc..c6db404 100644
--- 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/format/impl/MySQLFormatSQLVisitor.java
+++ 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/format/impl/MySQLFormatSQLVisitor.java
@@ -24,10 +24,15 @@ import org.antlr.v4.runtime.tree.RuleNode;
 import org.antlr.v4.runtime.tree.TerminalNode;
 import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementBaseVisitor;
 import 
org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.AliasContext;
+import 
org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.AlterCommandListContext;
+import 
org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.AlterListContext;
+import 
org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.AlterTableActionsContext;
+import 
org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.AlterTableContext;
 import 
org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.AssignmentValuesContext;
 import 
org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ColumnNameContext;
 import 
org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ColumnNamesContext;
 import 
org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.CreateDefinitionClauseContext;
+import 
org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.CreateTableOptionsSpaceSeparatedContext;
 import 
org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.CteClauseContext;
 import 
org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.DataTypeContext;
 import 
org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ExplicitTableContext;
@@ -46,6 +51,7 @@ import 
org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.QueryEx
 import 
org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.QuerySpecificationContext;
 import 
org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.RowConstructorListContext;
 import 
org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.SelectContext;
+import 
org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.StandaloneAlterTableActionContext;
 import 
org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.StringListContext;
 import 
org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.StringLiteralsContext;
 import 
org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.TableElementListContext;
@@ -208,6 +214,96 @@ public abstract class MySQLFormatSQLVisitor extends 
MySQLStatementBaseVisitor<St
     }
 
     @Override
+    public String visitAlterTable(final AlterTableContext ctx) {
+        visit(ctx.ALTER());
+        formatPrint(" ");
+        visit(ctx.TABLE());
+        formatPrint(" ");
+        visit(ctx.tableName());
+        if (null != ctx.alterTableActions()) {
+            indentCount++;
+            formatPrintln();
+            visit(ctx.alterTableActions());
+            indentCount--;
+        } else if (null != ctx.standaloneAlterTableAction()) {
+            indentCount++;
+            formatPrintln();
+            visit(ctx.standaloneAlterTableAction());
+            indentCount--;
+        }
+        return result.toString();
+    }
+
+    @Override
+    public String visitAlterTableActions(final AlterTableActionsContext ctx) {
+        if (null != ctx.alterCommandList()) {
+            visit(ctx.alterCommandList());
+            if (null != ctx.alterTablePartitionOptions()) {
+                formatPrintln();
+                visit(ctx.alterTablePartitionOptions());
+            }
+        } else {
+            visit(ctx.alterTablePartitionOptions());
+        }
+        return result.toString();
+    }
+
+    @Override
+    public String visitAlterCommandList(final AlterCommandListContext ctx) {
+        if (null != ctx.alterCommandsModifierList()) {
+            visit(ctx.alterCommandsModifierList());
+            if (null != ctx.alterList()) {
+                formatPrintln(",");
+                visit(ctx.alterList());
+            }
+        } else if (null != ctx.alterList()) {
+            visit(ctx.alterList());
+        }
+        return result.toString();
+    }
+
+    @Override
+    public String visitAlterList(final AlterListContext ctx) {
+        int childCount = ctx.getChildCount();
+        for (int i = 0; i < childCount; i++) {
+            ParseTree child = ctx.getChild(i);
+            if (i != 0) {
+                if (child instanceof TerminalNode) {
+                    formatPrintln(",");
+                    continue;
+                } else {
+                    child.accept(this);
+                }
+            } else {
+                child.accept(this);
+            }
+        }
+        return result.toString();
+    }
+
+    @Override
+    public String visitCreateTableOptionsSpaceSeparated(final 
CreateTableOptionsSpaceSeparatedContext ctx) {
+        int childCount = ctx.getChildCount();
+        for (int i = 0; i < childCount; i++) {
+            if (0 != i) {
+                formatPrintln();
+            }
+            visit(ctx.getChild(i));
+        }
+        return result.toString();
+    }
+
+    @Override
+    public String visitStandaloneAlterTableAction(final 
StandaloneAlterTableActionContext ctx) {
+        if (null != ctx.alterCommandsModifierList()) {
+            visit(ctx.alterCommandsModifierList());
+            formatPrintln(",");
+        }
+        visit(ctx.standaloneAlterCommands());
+        return result.toString();
+    }
+
+    @Override
     public String visitRowConstructorList(final RowConstructorListContext ctx) 
{
         int rowCount = ctx.assignmentValues().size();
         for (int i = 0; i < rowCount; i++) {
diff --git 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/test/java/org/apache/shardingsphere/sql/parser/mysql/MySQLFormartTest.java
 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/test/java/org/apache/shardingsphere/sql/parser/mysql/MySQLFormartTest.java
index 8b3132c..0f8be04 100644
--- 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/test/java/org/apache/shardingsphere/sql/parser/mysql/MySQLFormartTest.java
+++ 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/test/java/org/apache/shardingsphere/sql/parser/mysql/MySQLFormartTest.java
@@ -75,7 +75,15 @@ public final class MySQLFormartTest {
                 + "\tandb = ?\n"
                 + "\tandc = ?\n"
                 + "\tandd = ?;"});
-        testUnits.add(new String[]{"alter_table", "ALTER TABLE t_log ADD name 
varchar(10)", "ALTER TABLE t_log ADD name VARCHAR(10)"});
+        testUnits.add(new String[]{"alter_table", "ALTER TABLE t_order ADD 
column4 DATE, ADD column5 DATETIME, engine ss max_rows 10,min_rows 2, ADD 
column6 TIMESTAMP, ADD column7 TIME;", ""
+                + "ALTER TABLE t_order\n"
+                + "\tADD column4 DATE,\n"
+                + "\tADD column5 DATETIME,\n"
+                + "\tENGINE ss\n"
+                + "\tMAX_ROWS 10,\n"
+                + "\tMIN_ROWS 2,\n"
+                + "\tADD column6 TIMESTAMP,\n"
+                + "\tADD column7 TIME"});
         testUnits.add(new String[]{"create_table", "CREATE TABLE IF NOT EXISTS 
`runoob_tbl`(\n"
                 + "`runoob_id` INT UNSIGNED AUTO_INCREMENT,\n"
                 + "`runoob_title` VARCHAR(100) NOT NULL,\n"

Reply via email to