use of com.alibaba.druid.sql.ast.statement.SQLSelect in project druid by alibaba.
the class OracleToMySqlOutputVisitor method visit.
public boolean visit(OracleSelectQueryBlock x) {
boolean parentIsSelectStatment = false;
{
if (x.getParent() instanceof SQLSelect) {
SQLSelect select = (SQLSelect) x.getParent();
if (select.getParent() instanceof SQLSelectStatement || select.getParent() instanceof SQLSubqueryTableSource) {
parentIsSelectStatment = true;
}
}
}
if (!parentIsSelectStatment) {
return super.visit(x);
}
if (//
x.getWhere() instanceof SQLBinaryOpExpr && //
x.getFrom() instanceof SQLSubqueryTableSource) {
int rownum;
String ident;
SQLBinaryOpExpr where = (SQLBinaryOpExpr) x.getWhere();
if (where.getRight() instanceof SQLIntegerExpr && where.getLeft() instanceof SQLIdentifierExpr) {
rownum = ((SQLIntegerExpr) where.getRight()).getNumber().intValue();
ident = ((SQLIdentifierExpr) where.getLeft()).getName();
} else {
return super.visit(x);
}
SQLSelect select = ((SQLSubqueryTableSource) x.getFrom()).getSelect();
SQLSelectQueryBlock queryBlock = null;
SQLSelect subSelect = null;
SQLBinaryOpExpr subWhere = null;
boolean isSubQueryRowNumMapping = false;
if (select.getQuery() instanceof SQLSelectQueryBlock) {
queryBlock = (SQLSelectQueryBlock) select.getQuery();
if (queryBlock.getWhere() instanceof SQLBinaryOpExpr) {
subWhere = (SQLBinaryOpExpr) queryBlock.getWhere();
}
for (SQLSelectItem selectItem : queryBlock.getSelectList()) {
if (isRowNumber(selectItem.getExpr())) {
if (where.getLeft() instanceof SQLIdentifierExpr && ((SQLIdentifierExpr) where.getLeft()).getName().equals(selectItem.getAlias())) {
isSubQueryRowNumMapping = true;
}
}
}
SQLTableSource subTableSource = queryBlock.getFrom();
if (subTableSource instanceof SQLSubqueryTableSource) {
subSelect = ((SQLSubqueryTableSource) subTableSource).getSelect();
}
}
if ("ROWNUM".equalsIgnoreCase(ident)) {
SQLBinaryOperator op = where.getOperator();
Integer limit = null;
if (op == SQLBinaryOperator.LessThanOrEqual) {
limit = rownum;
} else if (op == SQLBinaryOperator.LessThan) {
limit = rownum - 1;
}
if (limit != null) {
select.accept(this);
println();
print0(ucase ? "LIMIT " : "limit ");
print(limit);
return false;
}
} else if (isSubQueryRowNumMapping) {
SQLBinaryOperator op = where.getOperator();
SQLBinaryOperator subOp = subWhere.getOperator();
if (//
isRowNumber(subWhere.getLeft()) && subWhere.getRight() instanceof SQLIntegerExpr) {
int subRownum = ((SQLIntegerExpr) subWhere.getRight()).getNumber().intValue();
Integer offset = null;
if (op == SQLBinaryOperator.GreaterThanOrEqual) {
offset = rownum + 1;
} else if (op == SQLBinaryOperator.GreaterThan) {
offset = rownum;
}
if (offset != null) {
Integer limit = null;
if (subOp == SQLBinaryOperator.LessThanOrEqual) {
limit = subRownum - offset;
} else if (subOp == SQLBinaryOperator.LessThan) {
limit = subRownum - 1 - offset;
}
if (limit != null) {
subSelect.accept(this);
println();
print0(ucase ? "LIMIT " : "limit ");
print(offset);
print0(", ");
print(limit);
return false;
}
}
}
}
}
return super.visit(x);
}
use of com.alibaba.druid.sql.ast.statement.SQLSelect in project druid by alibaba.
the class SQLSelectParser method parseTableSource.
public SQLTableSource parseTableSource() {
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
SQLTableSource tableSource;
if (lexer.token() == Token.SELECT || lexer.token() == Token.WITH || lexer.token == Token.SEL) {
SQLSelect select = select();
accept(Token.RPAREN);
SQLSelectQuery query = queryRest(select.getQuery());
if (query instanceof SQLUnionQuery) {
tableSource = new SQLUnionQueryTableSource((SQLUnionQuery) query);
} else {
tableSource = new SQLSubqueryTableSource(select);
}
} else if (lexer.token() == Token.LPAREN) {
tableSource = parseTableSource();
accept(Token.RPAREN);
} else {
tableSource = parseTableSource();
accept(Token.RPAREN);
}
return parseTableSourceRest(tableSource);
}
if (lexer.token() == Token.SELECT) {
throw new ParserException("TODO");
}
SQLExprTableSource tableReference = new SQLExprTableSource();
parseTableSourceQueryTableExpr(tableReference);
SQLTableSource tableSrc = parseTableSourceRest(tableReference);
if (lexer.hasComment() && lexer.isKeepComments()) {
tableSrc.addAfterComment(lexer.readAndResetComments());
}
return tableSrc;
}
use of com.alibaba.druid.sql.ast.statement.SQLSelect in project druid by alibaba.
the class SQLSelectParser method select.
public SQLSelect select() {
SQLSelect select = new SQLSelect();
withSubquery(select);
select.setQuery(query());
select.setOrderBy(parseOrderBy());
if (select.getOrderBy() == null) {
select.setOrderBy(parseOrderBy());
}
while (lexer.token() == Token.HINT) {
this.exprParser.parseHints(select.getHints());
}
return select;
}
use of com.alibaba.druid.sql.ast.statement.SQLSelect in project druid by alibaba.
the class OdpsStatementParser method parseOdpsInsert.
public OdpsInsert parseOdpsInsert() {
OdpsInsert insert = new OdpsInsert();
if (lexer.isKeepComments() && lexer.hasComment()) {
insert.addBeforeComment(lexer.readAndResetComments());
}
SQLSelectParser selectParser = createSQLSelectParser();
accept(Token.INSERT);
if (lexer.token() == Token.INTO) {
lexer.nextToken();
} else {
accept(Token.OVERWRITE);
insert.setOverwrite(true);
}
accept(Token.TABLE);
insert.setTableSource(this.exprParser.name());
if (lexer.token() == Token.PARTITION) {
lexer.nextToken();
accept(Token.LPAREN);
for (; ; ) {
SQLAssignItem ptExpr = new SQLAssignItem();
ptExpr.setTarget(this.exprParser.name());
if (lexer.token() == Token.EQ) {
lexer.nextToken();
SQLExpr ptValue = this.exprParser.expr();
ptExpr.setValue(ptValue);
}
insert.addPartition(ptExpr);
if (!(lexer.token() == (Token.COMMA))) {
break;
} else {
lexer.nextToken();
}
}
accept(Token.RPAREN);
}
SQLSelect query = selectParser.select();
insert.setQuery(query);
return insert;
}
use of com.alibaba.druid.sql.ast.statement.SQLSelect in project druid by alibaba.
the class MySqlSelectIntoParser method parseSelectInto.
public MySqlSelectIntoStatement parseSelectInto() {
SQLSelect select = select();
MySqlSelectIntoStatement stmt = new MySqlSelectIntoStatement();
stmt.setSelect(select);
stmt.setVarList(argsList);
return stmt;
}
Aggregations