use of com.alibaba.druid.sql.ast.statement.SQLExprTableSource in project druid by alibaba.
the class OdpsStatementParser method parseInsert.
public SQLStatement parseInsert() {
OdpsInsertStatement stmt = new OdpsInsertStatement();
if (lexer.token() == Token.FROM) {
lexer.nextToken();
if (lexer.token() == Token.IDENTIFIER) {
SQLName tableName = this.exprParser.name();
SQLExprTableSource from = new SQLExprTableSource(tableName);
stmt.setFrom(from);
} else {
accept(Token.LPAREN);
SQLSelectParser selectParser = createSQLSelectParser();
SQLSelect select = selectParser.select();
accept(Token.RPAREN);
String alias = lexer.stringVal();
accept(Token.IDENTIFIER);
SQLSubqueryTableSource from = new SQLSubqueryTableSource(select, alias);
stmt.setFrom(from);
}
}
for (; ; ) {
OdpsInsert insert = parseOdpsInsert();
stmt.addItem(insert);
if (lexer.token() != Token.INSERT) {
break;
}
}
return stmt;
}
use of com.alibaba.druid.sql.ast.statement.SQLExprTableSource in project druid by alibaba.
the class Demo3 method convert.
private String convert(String sql, List<Object> parameters) {
SQLStatementParser parser = new MySqlStatementParser(sql);
//
List<SQLStatement> stmtList = parser.parseStatementList();
SQLStatement first = (SQLStatement) stmtList.get(0);
MyVisitor visitor = new MyVisitor();
first.accept(visitor);
if (visitor.getVariantList().size() > 0) {
SQLExpr firstVar = visitor.getVariantList().get(0);
int userId;
if (firstVar instanceof SQLVariantRefExpr) {
int varIndex = (Integer) firstVar.getAttribute("varIndex");
userId = (Integer) parameters.get(varIndex);
} else {
userId = ((SQLNumericLiteralExpr) firstVar).getNumber().intValue();
}
String tableName;
if (userId == 1) {
tableName = "user_1";
} else {
tableName = "user_x";
}
for (SQLExprTableSource tableSource : visitor.getTableSourceList()) {
SQLExpr expr = tableSource.getExpr();
if (expr instanceof SQLIdentifierExpr) {
SQLIdentifierExpr identExpr = (SQLIdentifierExpr) expr;
String ident = identExpr.getName();
if (ident.equals("user")) {
identExpr.setName(tableName);
}
} else if (expr instanceof SQLPropertyExpr) {
SQLPropertyExpr proExpr = (SQLPropertyExpr) expr;
String ident = proExpr.getName();
if (ident.equals("user")) {
proExpr.setName(tableName);
}
}
}
}
String realSql = SQLUtils.toOracleString(first);
return realSql;
}
Aggregations