diff --git a/core/src/main/java/com/alibaba/druid/sql/dialect/bigquery/visitor/BigQueryOutputVisitor.java b/core/src/main/java/com/alibaba/druid/sql/dialect/bigquery/visitor/BigQueryOutputVisitor.java index 514b4121e5..637726ea3b 100644 --- a/core/src/main/java/com/alibaba/druid/sql/dialect/bigquery/visitor/BigQueryOutputVisitor.java +++ b/core/src/main/java/com/alibaba/druid/sql/dialect/bigquery/visitor/BigQueryOutputVisitor.java @@ -17,8 +17,7 @@ public BigQueryOutputVisitor(StringBuilder appender) { } public BigQueryOutputVisitor(StringBuilder appender, boolean parameterized) { - super(appender, parameterized); - this.dbType = DbType.bigquery; + super(appender, DbType.bigquery, parameterized); } protected void printPartitionedBy(SQLCreateTableStatement x) { diff --git a/core/src/main/java/com/alibaba/druid/sql/dialect/blink/vsitor/BlinkOutputVisitor.java b/core/src/main/java/com/alibaba/druid/sql/dialect/blink/vsitor/BlinkOutputVisitor.java index a2932ad31a..0e645b4fb2 100644 --- a/core/src/main/java/com/alibaba/druid/sql/dialect/blink/vsitor/BlinkOutputVisitor.java +++ b/core/src/main/java/com/alibaba/druid/sql/dialect/blink/vsitor/BlinkOutputVisitor.java @@ -25,7 +25,7 @@ public class BlinkOutputVisitor extends SQLASTOutputVisitor implements BlinkVisitor { public BlinkOutputVisitor(StringBuilder appender) { - super(appender); + super(appender, DbType.blink); } public BlinkOutputVisitor(StringBuilder appender, DbType dbType) { @@ -33,7 +33,7 @@ public BlinkOutputVisitor(StringBuilder appender, DbType dbType) { } public BlinkOutputVisitor(StringBuilder appender, boolean parameterized) { - super(appender, parameterized); + super(appender, DbType.blink, parameterized); } @Override diff --git a/core/src/main/java/com/alibaba/druid/sql/dialect/clickhouse/visitor/CKOutputVisitor.java b/core/src/main/java/com/alibaba/druid/sql/dialect/clickhouse/visitor/CKOutputVisitor.java index 2f55aac71b..077b653521 100644 --- a/core/src/main/java/com/alibaba/druid/sql/dialect/clickhouse/visitor/CKOutputVisitor.java +++ b/core/src/main/java/com/alibaba/druid/sql/dialect/clickhouse/visitor/CKOutputVisitor.java @@ -23,7 +23,7 @@ public CKOutputVisitor(StringBuilder appender, DbType dbType) { } public CKOutputVisitor(StringBuilder appender, boolean parameterized) { - super(appender, parameterized); + super(appender, DbType.clickhouse, parameterized); } @Override diff --git a/core/src/main/java/com/alibaba/druid/sql/dialect/db2/visitor/DB2OutputVisitor.java b/core/src/main/java/com/alibaba/druid/sql/dialect/db2/visitor/DB2OutputVisitor.java index 437bb67ebd..0ae0ad255b 100644 --- a/core/src/main/java/com/alibaba/druid/sql/dialect/db2/visitor/DB2OutputVisitor.java +++ b/core/src/main/java/com/alibaba/druid/sql/dialect/db2/visitor/DB2OutputVisitor.java @@ -33,8 +33,7 @@ public DB2OutputVisitor(StringBuilder appender) { } public DB2OutputVisitor(StringBuilder appender, boolean parameterized) { - super(appender, parameterized); - this.dbType = DbType.db2; + super(appender, DbType.db2, parameterized); } @Override diff --git a/core/src/main/java/com/alibaba/druid/sql/dialect/h2/visitor/H2OutputVisitor.java b/core/src/main/java/com/alibaba/druid/sql/dialect/h2/visitor/H2OutputVisitor.java index 995ca844a3..2ce8e8c6dc 100644 --- a/core/src/main/java/com/alibaba/druid/sql/dialect/h2/visitor/H2OutputVisitor.java +++ b/core/src/main/java/com/alibaba/druid/sql/dialect/h2/visitor/H2OutputVisitor.java @@ -43,8 +43,7 @@ public H2OutputVisitor(StringBuilder appender, DbType dbType) { } public H2OutputVisitor(StringBuilder appender, boolean parameterized) { - super(appender, parameterized); - dbType = DbType.h2; + super(appender, DbType.h2, parameterized); } public boolean visit(SQLReplaceStatement x) { diff --git a/core/src/main/java/com/alibaba/druid/sql/dialect/hive/visitor/HiveOutputVisitor.java b/core/src/main/java/com/alibaba/druid/sql/dialect/hive/visitor/HiveOutputVisitor.java index afd24a7249..7934612b1c 100644 --- a/core/src/main/java/com/alibaba/druid/sql/dialect/hive/visitor/HiveOutputVisitor.java +++ b/core/src/main/java/com/alibaba/druid/sql/dialect/hive/visitor/HiveOutputVisitor.java @@ -52,8 +52,7 @@ public HiveOutputVisitor(StringBuilder appender, DbType dbType) { } public HiveOutputVisitor(StringBuilder appender, boolean parameterized) { - super(appender, parameterized); - dbType = DbType.hive; + super(appender, DbType.hive, parameterized); } @Override diff --git a/core/src/main/java/com/alibaba/druid/sql/dialect/infomix/visitor/InformixOutputVisitor.java b/core/src/main/java/com/alibaba/druid/sql/dialect/infomix/visitor/InformixOutputVisitor.java index 9c9108f18c..c49086a8f5 100644 --- a/core/src/main/java/com/alibaba/druid/sql/dialect/infomix/visitor/InformixOutputVisitor.java +++ b/core/src/main/java/com/alibaba/druid/sql/dialect/infomix/visitor/InformixOutputVisitor.java @@ -12,8 +12,7 @@ public InformixOutputVisitor(StringBuilder appender) { } public InformixOutputVisitor(StringBuilder appender, boolean parameterized) { - super(appender, parameterized); - dbType = DbType.informix; + super(appender, DbType.informix, parameterized); } protected void printSelectListBefore(SQLSelectQueryBlock x) { diff --git a/core/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlOutputVisitor.java b/core/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlOutputVisitor.java index 0b236c460f..39b07d3c38 100644 --- a/core/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlOutputVisitor.java +++ b/core/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlOutputVisitor.java @@ -45,11 +45,11 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV } public MySqlOutputVisitor(StringBuilder appender) { - super(appender); + super(appender, DbType.mysql); } public MySqlOutputVisitor(StringBuilder appender, boolean parameterized) { - super(appender, parameterized); + super(appender, DbType.mysql, parameterized); try { configFromProperty(); diff --git a/core/src/main/java/com/alibaba/druid/sql/dialect/odps/visitor/OdpsOutputVisitor.java b/core/src/main/java/com/alibaba/druid/sql/dialect/odps/visitor/OdpsOutputVisitor.java index 85ba137370..3b23d3fe37 100644 --- a/core/src/main/java/com/alibaba/druid/sql/dialect/odps/visitor/OdpsOutputVisitor.java +++ b/core/src/main/java/com/alibaba/druid/sql/dialect/odps/visitor/OdpsOutputVisitor.java @@ -24,7 +24,6 @@ import com.alibaba.druid.sql.dialect.hive.visitor.HiveOutputVisitor; import com.alibaba.druid.sql.dialect.odps.ast.*; import com.alibaba.druid.sql.visitor.VisitorFeature; -import com.alibaba.druid.util.OdpsUtils; import java.math.BigDecimal; import java.util.HashSet; @@ -62,7 +61,6 @@ public OdpsOutputVisitor(boolean ucase) { public OdpsOutputVisitor(StringBuilder appender) { super(appender, DbType.odps); - dialect = OdpsUtils.DIALECT; } @Override diff --git a/core/src/main/java/com/alibaba/druid/sql/dialect/oracle/visitor/OracleOutputVisitor.java b/core/src/main/java/com/alibaba/druid/sql/dialect/oracle/visitor/OracleOutputVisitor.java index ded64bd6ff..73ffaab411 100644 --- a/core/src/main/java/com/alibaba/druid/sql/dialect/oracle/visitor/OracleOutputVisitor.java +++ b/core/src/main/java/com/alibaba/druid/sql/dialect/oracle/visitor/OracleOutputVisitor.java @@ -54,7 +54,7 @@ public OracleOutputVisitor(StringBuilder appender) { } public OracleOutputVisitor(StringBuilder appender, boolean printPostSemi) { - super(appender); + super(appender, DbType.oracle); this.printPostSemi = printPostSemi; } diff --git a/core/src/main/java/com/alibaba/druid/sql/dialect/oscar/visitor/OscarOutputVisitor.java b/core/src/main/java/com/alibaba/druid/sql/dialect/oscar/visitor/OscarOutputVisitor.java index f7ea10cb22..0147c3f18e 100644 --- a/core/src/main/java/com/alibaba/druid/sql/dialect/oscar/visitor/OscarOutputVisitor.java +++ b/core/src/main/java/com/alibaba/druid/sql/dialect/oscar/visitor/OscarOutputVisitor.java @@ -38,13 +38,11 @@ public class OscarOutputVisitor extends SQLASTOutputVisitor implements OscarASTVisitor, OracleASTVisitor { public OscarOutputVisitor(StringBuilder appender) { - super(appender); - this.dbType = DbType.oscar; + super(appender, DbType.oscar); } public OscarOutputVisitor(StringBuilder appender, boolean parameterized) { - super(appender, parameterized); - this.dbType = DbType.oscar; + super(appender, DbType.oscar, parameterized); } @Override diff --git a/core/src/main/java/com/alibaba/druid/sql/dialect/phoenix/visitor/PhoenixOutputVisitor.java b/core/src/main/java/com/alibaba/druid/sql/dialect/phoenix/visitor/PhoenixOutputVisitor.java index d9176d260c..97b8d44cfe 100644 --- a/core/src/main/java/com/alibaba/druid/sql/dialect/phoenix/visitor/PhoenixOutputVisitor.java +++ b/core/src/main/java/com/alibaba/druid/sql/dialect/phoenix/visitor/PhoenixOutputVisitor.java @@ -15,6 +15,7 @@ */ package com.alibaba.druid.sql.dialect.phoenix.visitor; +import com.alibaba.druid.DbType; import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor; /** @@ -22,10 +23,10 @@ */ public class PhoenixOutputVisitor extends SQLASTOutputVisitor implements PhoenixASTVisitor { public PhoenixOutputVisitor(StringBuilder appender) { - super(appender); + super(appender, DbType.phoenix); } public PhoenixOutputVisitor(StringBuilder appender, boolean parameterized) { - super(appender, parameterized); + super(appender, DbType.phoenix, parameterized); } } diff --git a/core/src/main/java/com/alibaba/druid/sql/dialect/postgresql/visitor/PGOutputVisitor.java b/core/src/main/java/com/alibaba/druid/sql/dialect/postgresql/visitor/PGOutputVisitor.java index bd6719306a..e0a879b045 100644 --- a/core/src/main/java/com/alibaba/druid/sql/dialect/postgresql/visitor/PGOutputVisitor.java +++ b/core/src/main/java/com/alibaba/druid/sql/dialect/postgresql/visitor/PGOutputVisitor.java @@ -41,13 +41,11 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor, OracleASTVisitor { public PGOutputVisitor(StringBuilder appender) { - super(appender); - this.dbType = DbType.postgresql; + super(appender, DbType.postgresql); } public PGOutputVisitor(StringBuilder appender, boolean parameterized) { - super(appender, parameterized); - this.dbType = DbType.postgresql; + super(appender, DbType.postgresql, parameterized); } @Override diff --git a/core/src/main/java/com/alibaba/druid/sql/dialect/presto/visitor/PrestoOutputVisitor.java b/core/src/main/java/com/alibaba/druid/sql/dialect/presto/visitor/PrestoOutputVisitor.java index 0f0f4a9b16..f5cd4d3bcb 100644 --- a/core/src/main/java/com/alibaba/druid/sql/dialect/presto/visitor/PrestoOutputVisitor.java +++ b/core/src/main/java/com/alibaba/druid/sql/dialect/presto/visitor/PrestoOutputVisitor.java @@ -39,16 +39,12 @@ * @since 2022-01-07 */ public class PrestoOutputVisitor extends SQLASTOutputVisitor implements PrestoASTVisitor { - { - dbType = DbType.presto; - } - public PrestoOutputVisitor(StringBuilder appender) { super(appender, DbType.presto); } public PrestoOutputVisitor(StringBuilder appender, boolean parameterized) { - super(appender, parameterized); + super(appender, DbType.presto, parameterized); } @Override diff --git a/core/src/main/java/com/alibaba/druid/sql/dialect/sqlserver/visitor/SQLServerOutputVisitor.java b/core/src/main/java/com/alibaba/druid/sql/dialect/sqlserver/visitor/SQLServerOutputVisitor.java index f68f12f167..fe3cbcd5f7 100644 --- a/core/src/main/java/com/alibaba/druid/sql/dialect/sqlserver/visitor/SQLServerOutputVisitor.java +++ b/core/src/main/java/com/alibaba/druid/sql/dialect/sqlserver/visitor/SQLServerOutputVisitor.java @@ -37,8 +37,7 @@ public SQLServerOutputVisitor(StringBuilder appender) { } public SQLServerOutputVisitor(StringBuilder appender, boolean parameterized) { - super(appender, parameterized); - this.dbType = DbType.sqlserver; + super(appender, DbType.sqlserver, parameterized); } public boolean visit(SQLServerSelectQueryBlock x) { diff --git a/core/src/main/java/com/alibaba/druid/sql/dialect/starrocks/visitor/StarRocksOutputVisitor.java b/core/src/main/java/com/alibaba/druid/sql/dialect/starrocks/visitor/StarRocksOutputVisitor.java index 83bb954715..87207a9e10 100644 --- a/core/src/main/java/com/alibaba/druid/sql/dialect/starrocks/visitor/StarRocksOutputVisitor.java +++ b/core/src/main/java/com/alibaba/druid/sql/dialect/starrocks/visitor/StarRocksOutputVisitor.java @@ -28,13 +28,12 @@ public class StarRocksOutputVisitor extends SQLASTOutputVisitor implements StarRocksASTVisitor { { - this.dbType = DbType.starrocks; this.shardingSupport = true; this.quote = '`'; } public StarRocksOutputVisitor(StringBuilder appender) { - super(appender); + super(appender, DbType.starrocks); } public StarRocksOutputVisitor(StringBuilder appender, DbType dbType) { @@ -42,7 +41,7 @@ public StarRocksOutputVisitor(StringBuilder appender, DbType dbType) { } public StarRocksOutputVisitor(StringBuilder appender, boolean parameterized) { - super(appender, parameterized); + super(appender, DbType.starrocks, parameterized); } @Override diff --git a/core/src/main/java/com/alibaba/druid/sql/visitor/SQLASTOutputVisitor.java b/core/src/main/java/com/alibaba/druid/sql/visitor/SQLASTOutputVisitor.java index a3796c5c15..2da85f771e 100644 --- a/core/src/main/java/com/alibaba/druid/sql/visitor/SQLASTOutputVisitor.java +++ b/core/src/main/java/com/alibaba/druid/sql/visitor/SQLASTOutputVisitor.java @@ -123,12 +123,19 @@ public SQLASTOutputVisitor(StringBuilder appender) { public SQLASTOutputVisitor(StringBuilder appender, DbType dbType) { this.appender = appender; this.dbType = dbType; + this.dialect = SQLDialect.of(dbType); } public SQLASTOutputVisitor(StringBuilder appender, boolean parameterized) { this.appender = appender; this.config(VisitorFeature.OutputParameterized, parameterized); } + public SQLASTOutputVisitor(StringBuilder appender, DbType dbType, boolean parameterized) { + this.appender = appender; + this.dbType = dbType; + this.dialect = SQLDialect.of(dbType); + this.config(VisitorFeature.OutputParameterized, parameterized); + } public int getReplaceCount() { return this.replaceCount; diff --git a/core/src/main/resources/META-INF/druid/parser/starrocks/dialect.properties b/core/src/main/resources/META-INF/druid/parser/starrocks/dialect.properties new file mode 100644 index 0000000000..c6282efffb --- /dev/null +++ b/core/src/main/resources/META-INF/druid/parser/starrocks/dialect.properties @@ -0,0 +1 @@ +quote=` diff --git a/core/src/main/resources/META-INF/druid/parser/starrocks/keywords b/core/src/main/resources/META-INF/druid/parser/starrocks/keywords new file mode 100644 index 0000000000..3c3aad960a --- /dev/null +++ b/core/src/main/resources/META-INF/druid/parser/starrocks/keywords @@ -0,0 +1,151 @@ +ADD +ALL +ALTER +ANALYZE +AND +ARRAY +AS +ASC +BETWEEN +BIGINT +BITMAP +BOTH +BY +CASE +CHAR +CHARACTER +CHECK +COLLATE +COLUMN +COMPACTION +CONVERT +CREATE +CROSS +CUBE +CURRENT_DATE +CURRENT_TIME +CURRENT_TIMESTAMP +CURRENT_USER +CURRENT_ROLE +DATABASE +DATABASES +DECIMAL +DECIMALV2 +DECIMAL32 +DECIMAL64 +DECIMAL128 +DEFAULT +DELETE +DENSE_RANK +DESC +DESCRIBE +DISTINCT +DOUBLE +DROP +DUAL +DEFERRED +ELSE +EXCEPT +EXISTS +EXPLAIN +FALSE +FIRST_VALUE +FLOAT +FOR +FORCE +FROM +FULL +FUNCTION +GRANT +GROUP +GROUPS +GROUPING +GROUPING_ID +HAVING +HLL +HOST +IF +IGNORE +IN +INDEX +INFILE +INNER +INSERT +INT +INTEGER +INTERSECT +INTO +IS +IMMEDIATE +JOIN +JSON +KEY +KEYS +KILL +LAG +LARGEINT +LAST_VALUE +LATERAL +LEAD +LEFT +LIKE +LIMIT +LOAD +LOCALTIME +LOCALTIMESTAMP +MAXVALUE +MINUS +MOD +NTILE +NOT +NULL +ON +OR +ORDER +OUTER +OUTFILE +OVER +PARTITION +PERCENTILE +PRIMARY +PROCEDURE +QUALIFY +RANGE +RANK +READ +REGEXP +RELEASE +RENAME +REPLACE +REVOKE +RIGHT +RLIKE +ROW +ROWS +ROW_NUMBER +SCHEMA +SCHEMAS +SELECT +SET +SET_VAR +SHOW +SMALLINT +SYSTEM +TABLE +TERMINATED +TEXT +THEN +TINYINT +TO +TRUE +UNION +UNIQUE +UNSIGNED +UPDATE +USE +USING +VALUES +VARCHAR +WHEN +WHERE +WITH \ No newline at end of file diff --git a/core/src/test/java/com/alibaba/druid/bvt/sql/oracle/select/OracleSysdateTest.java b/core/src/test/java/com/alibaba/druid/bvt/sql/oracle/select/OracleSysdateTest.java index 568a6d7a11..d9daa04818 100644 --- a/core/src/test/java/com/alibaba/druid/bvt/sql/oracle/select/OracleSysdateTest.java +++ b/core/src/test/java/com/alibaba/druid/bvt/sql/oracle/select/OracleSysdateTest.java @@ -32,7 +32,7 @@ public void test_0() throws Exception { OracleSysdateExpr sysdate = new OracleSysdateExpr(); StringBuilder buf = new StringBuilder(); - SQLASTOutputVisitor v = new SQLASTOutputVisitor(buf); + SQLASTOutputVisitor v = new SQLASTOutputVisitor(buf, DbType.oracle); sysdate.accept(v); assertEquals("SYSDATE", buf.toString()); } diff --git a/core/src/test/java/com/alibaba/druid/bvt/sql/starrocks/StarrocksKeywordsTest.java b/core/src/test/java/com/alibaba/druid/bvt/sql/starrocks/StarrocksKeywordsTest.java new file mode 100644 index 0000000000..e60a3c72e8 --- /dev/null +++ b/core/src/test/java/com/alibaba/druid/bvt/sql/starrocks/StarrocksKeywordsTest.java @@ -0,0 +1,65 @@ +package com.alibaba.druid.bvt.sql.starrocks; + +import com.alibaba.druid.DbType; +import com.alibaba.druid.sql.SQLUtils; +import com.alibaba.druid.sql.ast.SQLStatement; +import com.alibaba.druid.sql.parser.SQLParserFeature; +import com.alibaba.druid.sql.parser.SQLParserUtils; +import com.alibaba.druid.sql.parser.SQLStatementParser; +import com.alibaba.druid.sql.parser.Token; +import com.alibaba.druid.sql.visitor.VisitorFeature; +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class StarrocksKeywordsTest { + @Test + public void test_keywords(){ + DbType dbType = DbType.starrocks; + String sql = + "CREATE TABLE test (\n" + + "\tkey int(11) NOT NULL COMMENT 'key',\n" + + "\tgroup varchar(255) NULL COMMENT '分组',\n" + + "\tevent_scen varchar(255) NULL COMMENT '事件触发场景',\n" + + "\tenable varchar(2) NULL COMMENT '是否生效 0 失效 1 生效',\n" + + "\tcreate_time datetime NULL COMMENT '创建时间',\n" + + "\tupdate_by varchar(255) NULL COMMENT '更新人',\n" + + "\tupdate_time datetime NULL COMMENT '更新时间',\n" + + "\tcreate_by varchar(255) NULL COMMENT '创建人',\n" + + "\tvalues varchar(50) NULL COMMENT 'values'\n" + + ") ENGINE = OLAP\n" + + "UNIQUE KEY (id)\n" + + "COMMENT \"埋点业务表\"\n" + + "DISTRIBUTED BY HASH (id) BUCKETS 8\n" + + "PROPERTIES (\n" + + "\t\"replication_num\" = \"tag.location.default: 3\",\n" + + "\t\"storage_format\" = \"V2\"\n" + + ");"; + SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, + dbType, + SQLParserFeature.IgnoreNameQuotes); + SQLStatement stmt = parser.parseStatement(); + assertEquals(Token.EOF, parser.getLexer().token()); + String result = SQLUtils.toSQLString(stmt, dbType, null, VisitorFeature.OutputNameQuote).trim(); + String expectedSql = "CREATE TABLE test (\n" + + "\t`key` int(11) NOT NULL COMMENT 'key',\n" + + "\t`group` varchar(255) NULL COMMENT '分组',\n" + + "\tevent_scen varchar(255) NULL COMMENT '事件触发场景',\n" + + "\tenable varchar(2) NULL COMMENT '是否生效 0 失效 1 生效',\n" + + "\tcreate_time datetime NULL COMMENT '创建时间',\n" + + "\tupdate_by varchar(255) NULL COMMENT '更新人',\n" + + "\tupdate_time datetime NULL COMMENT '更新时间',\n" + + "\tcreate_by varchar(255) NULL COMMENT '创建人',\n" + + "\t`values` varchar(50) NULL COMMENT 'values'\n" + + ") ENGINE = OLAP\n" + + "UNIQUE KEY (id)\n" + + "COMMENT \"埋点业务表\"\n" + + "DISTRIBUTED BY HASH (id) BUCKETS 8\n" + + "PROPERTIES (\n" + + "\t\"replication_num\" = \"tag.location.default: 3\",\n" + + "\t\"storage_format\" = \"V2\"\n" + + ");"; + Assert.assertEquals(expectedSql, result); + } +}