mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved odps sql formmatter
This commit is contained in:
@@ -15,7 +15,9 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.odps.visitor;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLDataType;
|
||||
import com.alibaba.druid.sql.ast.SQLHint;
|
||||
@@ -49,7 +51,24 @@ import com.alibaba.druid.sql.dialect.odps.ast.OdpsUDTFSQLSelectItem;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
|
||||
|
||||
public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVisitor {
|
||||
|
||||
|
||||
private Set<String> builtInFunctions = new HashSet<String>();
|
||||
|
||||
{
|
||||
builtInFunctions.add("IF");
|
||||
builtInFunctions.add("COALESCE");
|
||||
builtInFunctions.add("TO_DATE");
|
||||
builtInFunctions.add("SUBSTR");
|
||||
builtInFunctions.add("INSTR");
|
||||
builtInFunctions.add("LENGTH");
|
||||
builtInFunctions.add("SPLIT");
|
||||
builtInFunctions.add("TOLOWER");
|
||||
builtInFunctions.add("TOUPPER");
|
||||
builtInFunctions.add("EXPLODE");
|
||||
builtInFunctions.add("LEAST");
|
||||
builtInFunctions.add("GREATEST");
|
||||
}
|
||||
|
||||
public OdpsOutputVisitor(Appendable appender){
|
||||
super(appender);
|
||||
}
|
||||
@@ -683,7 +702,7 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
|
||||
x.getOwner().accept(this);
|
||||
print(":");
|
||||
}
|
||||
print(x.getMethodName());
|
||||
printFunctionName(x.getMethodName());
|
||||
print("(");
|
||||
printAndAccept(x.getParameters(), ", ");
|
||||
print(")");
|
||||
@@ -712,4 +731,13 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected void printFunctionName(String name) {
|
||||
String upperName = name.toUpperCase();
|
||||
if (builtInFunctions.contains(upperName)) {
|
||||
print(upperName);
|
||||
} else {
|
||||
print(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -562,12 +562,16 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab
|
||||
x.getOwner().accept(this);
|
||||
print(".");
|
||||
}
|
||||
print(x.getMethodName());
|
||||
printFunctionName(x.getMethodName());
|
||||
print("(");
|
||||
printAndAccept(x.getParameters(), ", ");
|
||||
print(")");
|
||||
return false;
|
||||
}
|
||||
|
||||
protected void printFunctionName(String name) {
|
||||
print(name);
|
||||
}
|
||||
|
||||
public boolean visit(SQLAggregateExpr x) {
|
||||
print(x.getMethodName());
|
||||
|
||||
Reference in New Issue
Block a user