You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2020/11/24 07:49:06 UTC

[shardingsphere] branch master updated: add special formart for createTable (#8304)

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

panjuan 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 5c53bbe  add special formart for createTable (#8304)
5c53bbe is described below

commit 5c53bbeb7874c5a7bd19fb8d22b79227e2acfa19
Author: JingShang Lu <lu...@apache.org>
AuthorDate: Tue Nov 24 15:48:38 2020 +0800

    add special formart for createTable (#8304)
---
 .../src/main/antlr4/imports/mysql/BaseRule.g4      |  2 +-
 .../visitor/format/impl/MySQLFormatSQLVisitor.java | 95 ++++++++++++++++++++++
 .../sql/parser/mysql/MySQLFormartTest.java         | 17 +++-
 3 files changed, 112 insertions(+), 2 deletions(-)

diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/BaseRule.g4 b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/BaseRule.g4
index ab83b6b..0b09d05 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/BaseRule.g4
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/antlr4/imports/mysql/BaseRule.g4
@@ -642,7 +642,7 @@ dataType
     | dataTypeName = BIT fieldLength?
     | dataTypeName = (BOOL | BOOLEAN)
     | dataTypeName = CHAR fieldLength? charsetWithOptBinary?
-    | nchar fieldLength? BINARY?
+    | (dataTypeName = NCHAR | dataTypeName = NATIONAL CHAR) fieldLength? BINARY?
     | dataTypeName = BINARY fieldLength?
     | (dataTypeName = CHAR VARYING | dataTypeName = VARCHAR) fieldLength charsetWithOptBinary?
     | (dataTypeName = NATIONAL VARCHAR | dataTypeName = NVARCHAR | dataTypeName = NCHAR VARCHAR | dataTypeName = NATIONAL CHAR VARYING | dataTypeName = NCHAR VARYING) fieldLength BINARY?
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 5026a2e..a5f1c47 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
@@ -27,13 +27,17 @@ import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.AliasCo
 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.CteClauseContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.DataTypeContext;
 import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ExplicitTableContext;
 import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ExprContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.FieldLengthContext;
 import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.IdentifierContext;
 import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.IgnoredIdentifierContext;
 import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.LiteralsContext;
 import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.NumberLiteralsContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.PrecisionContext;
 import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ProjectionContext;
 import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ProjectionsContext;
 import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.QueryExpressionBodyContext;
@@ -42,9 +46,12 @@ 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.StringListContext;
 import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.StringLiteralsContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.TableElementListContext;
 import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.TableNameContext;
 import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.TableValueConstructorContext;
+import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.TypeDatetimePrecisionContext;
 import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.WhereClauseContext;
 import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.WithClauseContext;
 
@@ -309,6 +316,87 @@ public abstract class MySQLFormatSQLVisitor extends MySQLStatementBaseVisitor<St
     }
 
     @Override
+    public String visitCreateDefinitionClause(final CreateDefinitionClauseContext ctx) {
+        this.indentCount++;
+        formartPrint("(");
+        formartPrintln();
+        visit(ctx.tableElementList());
+        formartPrint("\n");
+        formartPrint(")");
+        this.indentCount--;
+        return result.toString();
+    }
+
+    @Override
+    public String visitTableElementList(final TableElementListContext ctx) {
+        int tableElementCount = ctx.tableElement().size();
+        for (int i = 0; i < tableElementCount; i++) {
+            if (0 == i) {
+                visit(ctx.tableElement(i));
+            } else {
+                formartPrintln(",");
+                visit(ctx.tableElement(i));
+            }
+        }
+        return result.toString();
+    }
+
+    @Override
+    public String visitFieldLength(final FieldLengthContext ctx) {
+        formartPrint("(");
+        formartPrint(ctx.NUMBER_().getText());
+        formartPrint(")");
+        return result.toString();
+    }
+
+    @Override
+    public String visitPrecision(final PrecisionContext ctx) {
+        formartPrint("(");
+        formartPrint(ctx.NUMBER_(0).getText());
+        formartPrint(", ");
+        formartPrint(ctx.NUMBER_(1).getText());
+        formartPrint(")");
+        return super.visitPrecision(ctx);
+    }
+
+    @Override
+    public String visitTypeDatetimePrecision(final TypeDatetimePrecisionContext ctx) {
+        formartPrint("(");
+        formartPrint(ctx.NUMBER_().getText());
+        formartPrint(")");
+        return result.toString();
+    }
+
+    @Override
+    public String visitDataType(final DataTypeContext ctx) {
+        int n = ctx.getChildCount();
+        for (int i = 0; i < n; i++) {
+            ParseTree c = ctx.getChild(i);
+            if (i != 0 && !(c instanceof FieldLengthContext || c instanceof PrecisionContext || c instanceof StringListContext || c instanceof TypeDatetimePrecisionContext)) {
+                formartPrint(" ");
+            }
+            c.accept(this);
+        }
+        return result.toString();
+    }
+
+    @Override
+    public String visitStringList(final StringListContext ctx) {
+        int stringCount = ctx.textString().size();
+        formartPrint("(");
+        for (int i = 0; i < stringCount; i++) {
+            if (0 == i) {
+                formartPrint(ctx.textString(i).getText());
+            } else {
+                formartPrintln(",");
+                visit(ctx.textString(i));
+            }
+        }
+        formartPrint(")");
+        return result.toString();
+    }
+
+    @Override
     public String visitTerminal(final TerminalNode node) {
         if (isUperCase()) {
             formartPrint(node.getText().toUpperCase());
@@ -461,4 +549,11 @@ public abstract class MySQLFormatSQLVisitor extends MySQLStatementBaseVisitor<St
         lines++;
         formartPrintIndent();
     }
+
+    private void formartPrintln(final String text) {
+        formartPrint(text);
+        formartPrint('\n');
+        lines++;
+        formartPrintIndent();
+    }
 }
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 852b878..8b3132c 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,22 @@ 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_log ADD name varchar(10)", "ALTER TABLE t_log ADD name VARCHAR(10)"});
+        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"
+                + "`runoob_author` VARCHAR(40) NOT NULL,\n"
+                + "`runoob_test` NATIONAL CHAR(40),\n"
+                + "`submission_date` DATE,\n"
+                + "PRIMARY KEY ( `runoob_id` )\n"
+                + ")ENGINE=InnoDB DEFAULT CHARSET=utf8;", "CREATE TABLE IF NOT EXISTS `runoob_tbl` (\n"
+                + "\t`runoob_id` INT UNSIGNED AUTO_INCREMENT,\n"
+                + "\t`runoob_title` VARCHAR(100) NOT NULL,\n"
+                + "\t`runoob_author` VARCHAR(40) NOT NULL,\n"
+                + "\t`runoob_test` NATIONAL CHAR(40),\n"
+                + "\t`submission_date` DATE,\n"
+                + "\tPRIMARY KEY ( `runoob_id` )\n"
+                + ") ENGINE = InnoDB DEFAULT CHARSET = utf8"});
     }
 
     private final String caseId;