use of com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr in project druid by alibaba.
the class OracleSelectParser method parseTableSourceQueryTableExpr.
private void parseTableSourceQueryTableExpr(OracleSelectTableReference tableReference) {
tableReference.setExpr(this.exprParser.expr());
{
FlashbackQueryClause clause = flashback();
tableReference.setFlashback(clause);
}
if (identifierEquals("SAMPLE")) {
lexer.nextToken();
SampleClause sample = new SampleClause();
if (identifierEquals("BLOCK")) {
sample.setBlock(true);
lexer.nextToken();
}
accept(Token.LPAREN);
this.exprParser.exprList(sample.getPercent(), sample);
accept(Token.RPAREN);
if (identifierEquals("SEED")) {
lexer.nextToken();
accept(Token.LPAREN);
sample.setSeedValue(expr());
accept(Token.RPAREN);
}
tableReference.setSampleClause(sample);
}
if (identifierEquals("PARTITION")) {
lexer.nextToken();
PartitionExtensionClause partition = new PartitionExtensionClause();
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
partition.setPartition(exprParser.name());
accept(Token.RPAREN);
} else {
accept(Token.FOR);
accept(Token.LPAREN);
exprParser.names(partition.getFor());
accept(Token.RPAREN);
}
tableReference.setPartition(partition);
}
if (identifierEquals("SUBPARTITION")) {
lexer.nextToken();
PartitionExtensionClause partition = new PartitionExtensionClause();
partition.setSubPartition(true);
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
partition.setPartition(exprParser.name());
accept(Token.RPAREN);
} else {
accept(Token.FOR);
accept(Token.LPAREN);
exprParser.names(partition.getFor());
accept(Token.RPAREN);
}
tableReference.setPartition(partition);
}
if (identifierEquals("VERSIONS")) {
lexer.nextToken();
if (lexer.token() == Token.BETWEEN) {
lexer.nextToken();
VersionsFlashbackQueryClause clause = new VersionsFlashbackQueryClause();
if (identifierEquals("SCN")) {
clause.setType(AsOfFlashbackQueryClause.Type.SCN);
lexer.nextToken();
} else {
acceptIdentifier("TIMESTAMP");
clause.setType(AsOfFlashbackQueryClause.Type.TIMESTAMP);
}
SQLBinaryOpExpr binaryExpr = (SQLBinaryOpExpr) exprParser.expr();
if (binaryExpr.getOperator() != SQLBinaryOperator.BooleanAnd) {
throw new ParserException("syntax error : " + binaryExpr.getOperator());
}
clause.setBegin(binaryExpr.getLeft());
clause.setEnd(binaryExpr.getRight());
tableReference.setFlashback(clause);
} else {
throw new ParserException("TODO");
}
}
}
use of com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr in project druid by alibaba.
the class OracleStatementParser method parseStatementList.
public void parseStatementList(List<SQLStatement> statementList, int max) {
for (; ; ) {
if (max != -1) {
if (statementList.size() >= max) {
return;
}
}
if (lexer.token() == Token.EOF) {
return;
}
if (lexer.token() == Token.END) {
return;
}
if (lexer.token() == Token.ELSE) {
return;
}
if (lexer.token() == (Token.SEMI)) {
lexer.nextToken();
continue;
}
if (lexer.token() == (Token.SELECT)) {
SQLSelectStatement stmt = new SQLSelectStatement(new OracleSelectParser(this.exprParser).select(), JdbcConstants.ORACLE);
statementList.add(stmt);
continue;
}
if (lexer.token() == (Token.UPDATE)) {
statementList.add(parseUpdateStatement());
continue;
}
if (lexer.token() == (Token.CREATE)) {
statementList.add(parseCreate());
continue;
}
if (lexer.token() == Token.INSERT) {
statementList.add(parseInsert());
continue;
}
if (lexer.token() == (Token.DELETE)) {
statementList.add(parseDeleteStatement());
continue;
}
if (lexer.token() == (Token.SLASH)) {
lexer.nextToken();
statementList.add(new OraclePLSQLCommitStatement());
continue;
}
if (lexer.token() == Token.ALTER) {
statementList.add(parserAlter());
continue;
}
if (lexer.token() == Token.WITH) {
statementList.add(new SQLSelectStatement(new OracleSelectParser(this.exprParser).select()));
continue;
}
if (lexer.token() == Token.LBRACE || identifierEquals("CALL")) {
statementList.add(this.parseCall());
continue;
}
if (lexer.token() == Token.MERGE) {
statementList.add(this.parseMerge());
continue;
}
if (lexer.token() == Token.BEGIN) {
statementList.add(this.parseBlock());
continue;
}
if (lexer.token() == Token.DECLARE) {
statementList.add(this.parseBlock());
continue;
}
if (lexer.token() == Token.LOCK) {
statementList.add(this.parseLock());
continue;
}
if (lexer.token() == Token.TRUNCATE) {
statementList.add(this.parseTruncate());
continue;
}
if (lexer.token() == Token.VARIANT) {
SQLExpr variant = this.exprParser.primary();
if (variant instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr binaryOpExpr = (SQLBinaryOpExpr) variant;
if (binaryOpExpr.getOperator() == SQLBinaryOperator.Assignment) {
SQLSetStatement stmt = new SQLSetStatement(binaryOpExpr.getLeft(), binaryOpExpr.getRight(), getDbType());
statementList.add(stmt);
continue;
}
}
accept(Token.COLONEQ);
SQLExpr value = this.exprParser.expr();
SQLSetStatement stmt = new SQLSetStatement(variant, value, getDbType());
statementList.add(stmt);
continue;
}
if (lexer.token() == Token.EXCEPTION) {
statementList.add(this.parseException());
continue;
}
if (identifierEquals("EXIT")) {
lexer.nextToken();
OracleExitStatement stmt = new OracleExitStatement();
if (lexer.token() == Token.WHEN) {
lexer.nextToken();
stmt.setWhen(this.exprParser.expr());
}
statementList.add(stmt);
continue;
}
if (lexer.token() == Token.FETCH || identifierEquals("FETCH")) {
SQLStatement stmt = parseFetch();
statementList.add(stmt);
continue;
}
if (identifierEquals("ROLLBACK")) {
SQLRollbackStatement stmt = parseRollback();
statementList.add(stmt);
continue;
}
if (lexer.token() == Token.EXPLAIN) {
statementList.add(this.parseExplain());
continue;
}
if (lexer.token() == Token.IDENTIFIER) {
SQLExpr expr = exprParser.expr();
OracleExprStatement stmt = new OracleExprStatement(expr);
statementList.add(stmt);
continue;
}
if (lexer.token() == Token.LPAREN) {
char ch = lexer.current();
int bp = lexer.bp();
lexer.nextToken();
if (lexer.token() == Token.SELECT) {
lexer.reset(bp, ch, Token.LPAREN);
statementList.add(this.parseSelect());
continue;
} else {
throw new ParserException("TODO : " + lexer.token() + " " + lexer.stringVal());
}
}
if (lexer.token() == Token.SET) {
statementList.add(this.parseSet());
continue;
}
if (lexer.token() == Token.GRANT) {
statementList.add(this.parseGrant());
continue;
}
if (lexer.token() == Token.REVOKE) {
statementList.add(this.parseRevoke());
continue;
}
if (lexer.token() == Token.COMMENT) {
statementList.add(this.parseComment());
continue;
}
if (lexer.token() == Token.FOR) {
statementList.add(this.parseFor());
continue;
}
if (lexer.token() == Token.LOOP) {
statementList.add(this.parseLoop());
continue;
}
if (lexer.token() == Token.IF) {
statementList.add(this.parseIf());
continue;
}
if (lexer.token() == Token.GOTO) {
lexer.nextToken();
SQLName label = this.exprParser.name();
OracleGotoStatement stmt = new OracleGotoStatement(label);
statementList.add(stmt);
continue;
}
if (lexer.token() == Token.COMMIT) {
lexer.nextToken();
if (identifierEquals("WORK")) {
lexer.nextToken();
}
OracleCommitStatement stmt = new OracleCommitStatement();
if (identifierEquals("WRITE")) {
stmt.setWrite(true);
lexer.nextToken();
for (; ; ) {
if (lexer.token() == Token.WAIT) {
lexer.nextToken();
stmt.setWait(Boolean.TRUE);
continue;
} else if (lexer.token() == Token.NOWAIT) {
lexer.nextToken();
stmt.setWait(Boolean.FALSE);
continue;
} else if (lexer.token() == Token.IMMEDIATE) {
lexer.nextToken();
stmt.setImmediate(Boolean.TRUE);
continue;
} else if (identifierEquals("BATCH")) {
lexer.nextToken();
stmt.setImmediate(Boolean.FALSE);
continue;
}
break;
}
}
statementList.add(stmt);
continue;
}
if (lexer.token() == Token.SAVEPOINT) {
lexer.nextToken();
OracleSavePointStatement stmt = new OracleSavePointStatement();
if (lexer.token() == Token.TO) {
lexer.nextToken();
stmt.setTo(this.exprParser.name());
}
statementList.add(stmt);
continue;
}
if (lexer.token() == Token.LTLT) {
lexer.nextToken();
SQLName label = this.exprParser.name();
OracleLabelStatement stmt = new OracleLabelStatement(label);
accept(Token.GTGT);
statementList.add(stmt);
continue;
}
if (lexer.token() == Token.DROP) {
lexer.nextToken();
if (lexer.token() == Token.TABLE) {
SQLDropTableStatement stmt = parseDropTable(false);
statementList.add(stmt);
continue;
}
boolean isPublic = false;
if (identifierEquals("PUBLIC")) {
lexer.nextToken();
isPublic = true;
}
if (lexer.token() == Token.DATABASE) {
lexer.nextToken();
if (identifierEquals("LINK")) {
lexer.nextToken();
OracleDropDbLinkStatement stmt = new OracleDropDbLinkStatement();
if (isPublic) {
stmt.setPublic(isPublic);
}
stmt.setName(this.exprParser.name());
statementList.add(stmt);
continue;
}
}
if (lexer.token() == Token.INDEX) {
SQLStatement stmt = parseDropIndex();
statementList.add(stmt);
continue;
}
if (lexer.token() == Token.VIEW) {
SQLStatement stmt = parseDropView(false);
statementList.add(stmt);
continue;
}
if (lexer.token() == Token.SEQUENCE) {
SQLDropSequenceStatement stmt = parseDropSequece(false);
statementList.add(stmt);
continue;
}
if (lexer.token() == Token.TRIGGER) {
SQLDropTriggerStatement stmt = parseDropTrigger(false);
statementList.add(stmt);
continue;
}
if (lexer.token() == Token.USER) {
SQLDropUserStatement stmt = parseDropUser();
statementList.add(stmt);
continue;
}
if (lexer.token() == Token.PROCEDURE) {
SQLDropProcedureStatement stmt = parseDropProcedure(false);
statementList.add(stmt);
continue;
}
throw new ParserException("TODO : " + lexer.token() + " " + lexer.stringVal());
}
if (lexer.token() == Token.NULL) {
lexer.nextToken();
OracleExprStatement stmt = new OracleExprStatement(new SQLNullExpr());
statementList.add(stmt);
continue;
}
if (lexer.token() == Token.OPEN) {
SQLStatement stmt = this.parseOpen();
statementList.add(stmt);
continue;
}
throw new ParserException("TODO : " + lexer.token() + " " + lexer.stringVal());
}
}
use of com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr 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.expr.SQLBinaryOpExpr in project Mycat-Server by MyCATApache.
the class JoinParser method opSQLExpr.
private void opSQLExpr(SQLBinaryOpExpr expr, String Operator) {
if (expr == null) {
return;
}
SQLExpr exprL = expr.getLeft();
if (!(exprL instanceof SQLBinaryOpExpr)) {
String field = exprL.toString();
String value = getExpValue(expr.getRight()).toString();
if (expr.getOperator() == SQLBinaryOperator.Equality) {
if (checkJoinField(value)) {
//joinLkey=field;
//joinRkey=value;
tableFilter.setJoinKey(field, value);
} else {
tableFilter.addWhere(field, value, expr.getOperator().getName(), Operator);
}
} else {
tableFilter.addWhere(field, value, expr.getOperator().getName(), Operator);
}
}
}
use of com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr in project Mycat-Server by MyCATApache.
the class MycatSchemaStatVisitor method splitUntilNoOr.
/**
* 递归拆分OR
*
* @param whereUnit
* TODO:考虑嵌套or语句,条件中有子查询、 exists等很多种复杂情况是否能兼容
*/
private void splitUntilNoOr(WhereUnit whereUnit) {
if (whereUnit.isFinishedParse()) {
if (whereUnit.getSubWhereUnit().size() > 0) {
for (int i = 0; i < whereUnit.getSubWhereUnit().size(); i++) {
splitUntilNoOr(whereUnit.getSubWhereUnit().get(i));
}
}
} else {
SQLBinaryOpExpr expr = whereUnit.getCanSplitExpr();
if (expr.getOperator() == SQLBinaryOperator.BooleanOr) {
// whereUnit.addSplitedExpr(expr.getRight());
addExprIfNotFalse(whereUnit, expr.getRight());
if (expr.getLeft() instanceof SQLBinaryOpExpr) {
whereUnit.setCanSplitExpr((SQLBinaryOpExpr) expr.getLeft());
splitUntilNoOr(whereUnit);
} else {
addExprIfNotFalse(whereUnit, expr.getLeft());
}
} else {
addExprIfNotFalse(whereUnit, expr);
whereUnit.setFinishedParse(true);
}
}
}
Aggregations