mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql parser & fix testcase
This commit is contained in:
@@ -180,6 +180,7 @@ public class MySqlSelectParser extends SQLSelectParser {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
while (true) {
|
||||
Token token = lexer.token();
|
||||
if (token == (Token.DISTINCT)) {
|
||||
@@ -191,6 +192,9 @@ public class MySqlSelectParser extends SQLSelectParser {
|
||||
} else if (token == (Token.ALL)) {
|
||||
queryBlock.setDistionOption(SQLSetQuantifier.ALL);
|
||||
lexer.nextToken();
|
||||
} else if (token == (Token.UNIQUE)) {
|
||||
queryBlock.setDistionOption(SQLSetQuantifier.UNIQUE);
|
||||
lexer.nextToken();
|
||||
} else if (lexer.identifierEquals(FnvHash.Constants.HIGH_PRIORITY)) {
|
||||
queryBlock.setHignPriority(true);
|
||||
lexer.nextToken();
|
||||
|
||||
@@ -118,13 +118,21 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
print(' ');
|
||||
}
|
||||
|
||||
final int distionOption = x.getDistionOption();
|
||||
if (SQLSetQuantifier.ALL == distionOption) {
|
||||
print0(ucase ? "ALL " : "all ");
|
||||
} else if (SQLSetQuantifier.DISTINCT == distionOption) {
|
||||
print0(ucase ? "DISTINCT " : "distinct ");
|
||||
} else if (SQLSetQuantifier.DISTINCTROW == distionOption) {
|
||||
print0(ucase ? "DISTINCTROW " : "distinctrow ");
|
||||
switch (x.getDistionOption()) {
|
||||
case SQLSetQuantifier.ALL:
|
||||
print0(ucase ? "ALL " : "all ");
|
||||
break;
|
||||
case SQLSetQuantifier.DISTINCT:
|
||||
print0(ucase ? "DISTINCT " : "distinct ");
|
||||
break;
|
||||
case SQLSetQuantifier.DISTINCTROW:
|
||||
print0(ucase ? "DISTINCTROW " : "distinctrow ");
|
||||
break;
|
||||
case SQLSetQuantifier.UNIQUE:
|
||||
print0(ucase ? "UNIQUE " : "unique ");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (x.isHignPriority()) {
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.alibaba.druid.support.opds.udf;
|
||||
|
||||
import com.alibaba.druid.DbType;
|
||||
import com.alibaba.druid.sql.parser.Lexer;
|
||||
import com.alibaba.druid.sql.parser.ParserException;
|
||||
import com.alibaba.druid.sql.parser.SQLParserUtils;
|
||||
import com.alibaba.druid.sql.parser.SQLType;
|
||||
import com.alibaba.druid.sql.visitor.ParameterizedOutputVisitorUtils;
|
||||
import com.aliyun.odps.udf.UDF;
|
||||
|
||||
public class SqlTypeUDF extends UDF {
|
||||
public String evaluate(String sql) {
|
||||
return evaluate(sql, null, false);
|
||||
}
|
||||
|
||||
public String evaluate(String sql, String dbTypeName) {
|
||||
return evaluate(sql, dbTypeName, false);
|
||||
}
|
||||
|
||||
public String evaluate(String sql, String dbTypeName, boolean throwError) {
|
||||
if (sql == null || sql.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
DbType dbType = dbTypeName == null ? null : DbType.valueOf(dbTypeName);
|
||||
Lexer lexer = SQLParserUtils.createLexer(sql, dbType);
|
||||
SQLType sqlType = lexer.scanSQLTypeV2();
|
||||
return sqlType != null ? sqlType.name() : "null";
|
||||
} catch (ParserException ex) {
|
||||
if (throwError) {
|
||||
throw new IllegalArgumentException("error sql : \n" + sql, ex);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user