mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
Add support for Teradata trim()
This commit is contained in:
@@ -172,6 +172,14 @@ public class SQLUtils {
|
||||
public static String toSQLServerString(SQLObject sqlObject, FormatOption option) {
|
||||
return toSQLString(sqlObject, JdbcConstants.SQL_SERVER, option);
|
||||
}
|
||||
|
||||
public static String toTeradataString(SQLObject sqlObject) {
|
||||
return toTeradataString(sqlObject, null);
|
||||
}
|
||||
|
||||
public static String toTeradataString(SQLObject sqlObject, FormatOption option) {
|
||||
return toSQLString(sqlObject, JdbcConstants.TERADATA, option);
|
||||
}
|
||||
|
||||
public static String formatPGSql(String sql, FormatOption option) {
|
||||
return format(sql, JdbcConstants.POSTGRESQL, option);
|
||||
|
||||
+5
@@ -1,5 +1,6 @@
|
||||
package com.alibaba.druid.sql.dialect.teradata.ast.stmt;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLOrderBy;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.teradata.visitor.TeradataASTVisitor;
|
||||
@@ -41,4 +42,8 @@ public class TeradataSelectQueryBlock extends SQLSelectQueryBlock {
|
||||
public void setOrderBy(SQLOrderBy orderBy) {
|
||||
this.orderBy = orderBy;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return SQLUtils.toTeradataString(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ import com.alibaba.druid.sql.ast.expr.SQLAggregateExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLAggregateOption;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLCastExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLQueryExpr;
|
||||
import com.alibaba.druid.sql.dialect.teradata.ast.expr.TeradataAnalytic;
|
||||
import com.alibaba.druid.sql.dialect.teradata.ast.expr.TeradataAnalyticWindowing;
|
||||
@@ -81,6 +82,44 @@ public class TeradataExprParser extends SQLExprParser {
|
||||
}
|
||||
}
|
||||
|
||||
protected SQLExpr methodRest(SQLExpr expr, boolean acceptLPAREN) {
|
||||
if (acceptLPAREN) {
|
||||
accept(Token.LPAREN);
|
||||
}
|
||||
|
||||
if (expr instanceof SQLIdentifierExpr) {
|
||||
String methodName = ((SQLIdentifierExpr) expr).getName();
|
||||
SQLMethodInvokeExpr methodExpr = new SQLMethodInvokeExpr(methodName);
|
||||
if ("trim".equalsIgnoreCase(methodName)) {
|
||||
if (identifierEquals("LEADING")
|
||||
|| identifierEquals("TRAILING")
|
||||
|| identifierEquals("BOTH")) {
|
||||
methodExpr.putAttribute("trim_option", lexer.stringVal());
|
||||
lexer.nextToken();
|
||||
if (lexer.token() == Token.LITERAL_CHARS) {
|
||||
SQLExpr trim_character = this.primary();
|
||||
trim_character.setParent(methodExpr);
|
||||
methodExpr.putAttribute("trim_character", trim_character);
|
||||
}
|
||||
} else {
|
||||
SQLExpr trim_character = this.primary();
|
||||
trim_character.setParent(methodExpr);
|
||||
methodExpr.putAttribute("trim_character", trim_character);
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.FROM) {
|
||||
lexer.nextToken();
|
||||
SQLExpr trim_source = this.expr();
|
||||
methodExpr.addParameter(trim_source);
|
||||
}
|
||||
|
||||
accept(Token.RPAREN);
|
||||
return primaryRest(methodExpr);
|
||||
}
|
||||
}
|
||||
return super.methodRest(expr, false);
|
||||
}
|
||||
|
||||
protected SQLAggregateExpr parseAggregateExpr(String methodName) {
|
||||
methodName = methodName.toUpperCase();
|
||||
|
||||
|
||||
+30
@@ -15,10 +15,12 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.teradata.visitor;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLObject;
|
||||
import com.alibaba.druid.sql.ast.SQLOrderBy;
|
||||
import com.alibaba.druid.sql.ast.SQLSetQuantifier;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLAggregateExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelect;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.teradata.ast.expr.TeradataAnalytic;
|
||||
@@ -173,4 +175,32 @@ public class TeradataOutputVisitor extends SQLASTOutputVisitor implements Terada
|
||||
public void endVisit(TeradataIntervalExpr x) {
|
||||
|
||||
}
|
||||
|
||||
public void endVisit(SQLMethodInvokeExpr x) {
|
||||
|
||||
}
|
||||
|
||||
public boolean visit(SQLMethodInvokeExpr x) {
|
||||
if ("trim".equalsIgnoreCase(x.getMethodName())) {
|
||||
SQLExpr trim_character = (SQLExpr) x.getAttribute("trim_character");
|
||||
|
||||
print0(x.getMethodName());
|
||||
print('(');
|
||||
String trim_option = (String) x.getAttribute("trim_option");
|
||||
if (trim_option != null && trim_option.length() != 0) {
|
||||
print0(trim_option);
|
||||
print(' ');
|
||||
}
|
||||
if (trim_character != null) {
|
||||
trim_character.accept(this);
|
||||
}
|
||||
if (x.getParameters().size() > 0) {
|
||||
print0(ucase ? " FROM " : " from ");
|
||||
x.getParameters().get(0).accept(this);
|
||||
}
|
||||
print(')');
|
||||
return false;
|
||||
}
|
||||
return super.visit(x);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user