mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improve Oracle SQL Parser, support syntax : varchar2(20 char)
This commit is contained in:
@@ -20,8 +20,13 @@ import com.alibaba.druid.sql.ast.SQLDataTypeImpl;
|
||||
@SuppressWarnings("serial")
|
||||
public class SQLCharactorDataType extends SQLDataTypeImpl {
|
||||
|
||||
private String charSetName;
|
||||
private String collate;
|
||||
private String charSetName;
|
||||
private String collate;
|
||||
|
||||
private String charType;
|
||||
|
||||
public final static String CHAR_TYPE_BYTE = "BYTE";
|
||||
public final static String CHAR_TYPE_CHAR = "CHAR";
|
||||
|
||||
public SQLCharactorDataType(String name){
|
||||
super(name);
|
||||
@@ -43,4 +48,12 @@ public class SQLCharactorDataType extends SQLDataTypeImpl {
|
||||
this.collate = collate;
|
||||
}
|
||||
|
||||
public String getCharType() {
|
||||
return charType;
|
||||
}
|
||||
|
||||
public void setCharType(String charType) {
|
||||
this.charType = charType;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -35,6 +35,7 @@ import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLUnaryExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLUnaryOperator;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCharactorDataType;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
|
||||
import com.alibaba.druid.sql.dialect.oracle.ast.OracleOrderBy;
|
||||
import com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleAggregateExpr;
|
||||
@@ -66,6 +67,14 @@ import com.alibaba.druid.sql.parser.SQLExprParser;
|
||||
import com.alibaba.druid.sql.parser.Token;
|
||||
|
||||
public class OracleExprParser extends SQLExprParser {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public boolean allowStringAdditive = false;
|
||||
|
||||
/**
|
||||
@@ -91,6 +100,17 @@ public class OracleExprParser extends SQLExprParser {
|
||||
this.lexer.nextToken();
|
||||
}
|
||||
|
||||
protected boolean isCharType(String dataTypeName) {
|
||||
return "varchar2".equalsIgnoreCase(dataTypeName) //
|
||||
|| "nvarchar2".equalsIgnoreCase(dataTypeName) //
|
||||
|| "char".equalsIgnoreCase(dataTypeName) //
|
||||
|| "varchar".equalsIgnoreCase(dataTypeName) //
|
||||
|| "nchar".equalsIgnoreCase(dataTypeName) //
|
||||
|| "nvarchar".equalsIgnoreCase(dataTypeName) //
|
||||
//
|
||||
;
|
||||
}
|
||||
|
||||
public SQLDataType parseDataType() {
|
||||
|
||||
if (lexer.token() == Token.DEFAULT || lexer.token() == Token.NOT || lexer.token() == Token.NULL) {
|
||||
@@ -100,6 +120,28 @@ public class OracleExprParser extends SQLExprParser {
|
||||
SQLName typeExpr = name();
|
||||
String typeName = typeExpr.toString();
|
||||
|
||||
if (isCharType(typeName)) {
|
||||
SQLCharactorDataType charType = new SQLCharactorDataType(typeName);
|
||||
|
||||
if (lexer.token() == Token.LPAREN) {
|
||||
lexer.nextToken();
|
||||
|
||||
charType.getArguments().add(this.expr());
|
||||
|
||||
if (identifierEquals("CHAR")) {
|
||||
lexer.nextToken();
|
||||
charType.setCharType(SQLCharactorDataType.CHAR_TYPE_CHAR);
|
||||
} else if (identifierEquals("BYTE")) {
|
||||
lexer.nextToken();
|
||||
charType.setCharType(SQLCharactorDataType.CHAR_TYPE_BYTE);
|
||||
}
|
||||
|
||||
accept(Token.RPAREN);
|
||||
}
|
||||
|
||||
return parseCharTypeRest(charType);
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.PERCENT) {
|
||||
lexer.nextToken();
|
||||
if (identifierEquals("TYPE")) {
|
||||
@@ -113,7 +155,7 @@ public class OracleExprParser extends SQLExprParser {
|
||||
}
|
||||
}
|
||||
|
||||
SQLDataType dataType = new SQLDataTypeImpl(typeName);
|
||||
SQLDataType dataType = new SQLDataTypeImpl(typeName);
|
||||
return parseDataTypeRest(dataType);
|
||||
}
|
||||
|
||||
|
||||
@@ -1134,6 +1134,18 @@ public class SQLExprParser extends SQLParser {
|
||||
|
||||
SQLName typeExpr = name();
|
||||
String typeName = typeExpr.toString();
|
||||
|
||||
if (isCharType(typeName)) {
|
||||
SQLCharactorDataType charType = new SQLCharactorDataType(typeName);
|
||||
|
||||
if (lexer.token() == Token.LPAREN) {
|
||||
lexer.nextToken();
|
||||
charType.getArguments().add(this.expr());
|
||||
accept(Token.RPAREN);
|
||||
}
|
||||
|
||||
return parseCharTypeRest(charType);
|
||||
}
|
||||
|
||||
if ("character".equalsIgnoreCase(typeName) && "varying".equalsIgnoreCase(lexer.stringVal())) {
|
||||
typeName += ' ' + lexer.stringVal();
|
||||
@@ -1149,16 +1161,12 @@ public class SQLExprParser extends SQLParser {
|
||||
lexer.nextToken();
|
||||
exprList(dataType.getArguments());
|
||||
accept(Token.RPAREN);
|
||||
|
||||
return parseCharTypeRest(dataType);
|
||||
}
|
||||
|
||||
return dataType;
|
||||
}
|
||||
|
||||
protected boolean isCharType(SQLDataType dataType) {
|
||||
String dataTypeName = dataType.getName();
|
||||
|
||||
protected boolean isCharType(String dataTypeName) {
|
||||
return "char".equalsIgnoreCase(dataTypeName) //
|
||||
|| "varchar".equalsIgnoreCase(dataTypeName)
|
||||
|| "nchar".equalsIgnoreCase(dataTypeName)
|
||||
@@ -1168,7 +1176,7 @@ public class SQLExprParser extends SQLParser {
|
||||
}
|
||||
|
||||
protected SQLDataType parseCharTypeRest(SQLDataType dataType) {
|
||||
if (!isCharType(dataType)) {
|
||||
if (!isCharType(dataType.getName())) {
|
||||
return dataType;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user