use of com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelect in project druid by alibaba.
the class OracleSelectParser method select.
public OracleSelect select() {
OracleSelect select = new OracleSelect();
withSubquery(select);
SQLSelectQuery query = query();
select.setQuery(query);
SQLOrderBy orderBy = this.parseOrderBy();
select.setOrderBy(orderBy);
if (orderBy != null && query instanceof SQLSelectQueryBlock) {
SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) query;
parseFetchClause(queryBlock);
}
if (lexer.token() == (Token.FOR)) {
lexer.nextToken();
accept(Token.UPDATE);
OracleSelectForUpdate forUpdate = new OracleSelectForUpdate();
if (lexer.token() == Token.OF) {
lexer.nextToken();
this.exprParser.exprList(forUpdate.getOf(), forUpdate);
}
if (lexer.token() == Token.NOWAIT) {
lexer.nextToken();
forUpdate.setNotWait(true);
} else if (lexer.token() == Token.WAIT) {
lexer.nextToken();
forUpdate.setWait(this.exprParser.primary());
} else if (identifierEquals("SKIP")) {
lexer.nextToken();
acceptIdentifier("LOCKED");
forUpdate.setSkipLocked(true);
}
select.setForUpdate(forUpdate);
}
if (select.getOrderBy() == null) {
select.setOrderBy(this.exprParser.parseOrderBy());
}
if (lexer.token() == Token.WITH) {
lexer.nextToken();
if (identifierEquals("READ")) {
lexer.nextToken();
if (identifierEquals("ONLY")) {
lexer.nextToken();
} else {
throw new ParserException("syntax error");
}
select.setRestriction(new OracleSelectRestriction.ReadOnly());
} else if (lexer.token() == (Token.CHECK)) {
lexer.nextToken();
if (identifierEquals("OPTION")) {
lexer.nextToken();
} else {
throw new ParserException("syntax error");
}
OracleSelectRestriction.CheckOption checkOption = new OracleSelectRestriction.CheckOption();
if (lexer.token() == Token.CONSTRAINT) {
lexer.nextToken();
throw new ParserException("TODO");
}
select.setRestriction(checkOption);
} else {
throw new ParserException("syntax error");
}
}
return select;
}
use of com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelect in project druid by alibaba.
the class OracleCreateTableParser method parseCrateTable.
public OracleCreateTableStatement parseCrateTable(boolean acceptCreate) {
OracleCreateTableStatement stmt = (OracleCreateTableStatement) super.parseCrateTable(acceptCreate);
for (; ; ) {
if (lexer.token() == Token.TABLESPACE) {
lexer.nextToken();
stmt.setTablespace(this.exprParser.name());
continue;
} else if (identifierEquals("IN_MEMORY_METADATA")) {
lexer.nextToken();
stmt.setInMemoryMetadata(true);
continue;
} else if (identifierEquals("CURSOR_SPECIFIC_SEGMENT")) {
lexer.nextToken();
stmt.setCursorSpecificSegment(true);
continue;
} else if (identifierEquals("NOPARALLEL")) {
lexer.nextToken();
stmt.setParallel(false);
continue;
} else if (lexer.token() == Token.LOGGING) {
lexer.nextToken();
stmt.setLogging(Boolean.TRUE);
continue;
} else if (lexer.token() == Token.CACHE) {
lexer.nextToken();
stmt.setCache(Boolean.TRUE);
continue;
} else if (lexer.token() == Token.NOCACHE) {
lexer.nextToken();
stmt.setCache(Boolean.FALSE);
continue;
} else if (lexer.token() == Token.NOCOMPRESS) {
lexer.nextToken();
stmt.setCompress(Boolean.FALSE);
continue;
} else if (lexer.token() == Token.ON) {
lexer.nextToken();
accept(Token.COMMIT);
stmt.setOnCommit(true);
continue;
} else if (identifierEquals("PRESERVE")) {
lexer.nextToken();
acceptIdentifier("ROWS");
stmt.setPreserveRows(true);
continue;
} else if (identifierEquals("STORAGE")) {
OracleStorageClause storage = ((OracleExprParser) this.exprParser).parseStorage();
stmt.setStorage(storage);
continue;
} else if (identifierEquals("organization")) {
lexer.nextToken();
accept(Token.INDEX);
stmt.setOrganizationIndex(true);
continue;
} else if (lexer.token() == Token.PCTFREE) {
lexer.nextToken();
stmt.setPtcfree(this.exprParser.expr());
continue;
} else if (identifierEquals("PCTUSED")) {
lexer.nextToken();
stmt.setPctused(this.exprParser.expr());
continue;
} else if (lexer.token() == Token.STORAGE) {
OracleStorageClause storage = ((OracleExprParser) this.exprParser).parseStorage();
stmt.setStorage(storage);
continue;
} else if (lexer.token() == Token.LOB) {
OracleLobStorageClause lobStorage = ((OracleExprParser) this.exprParser).parseLobStorage();
stmt.setLobStorage(lobStorage);
continue;
} else if (lexer.token() == Token.INITRANS) {
lexer.nextToken();
stmt.setInitrans(this.exprParser.expr());
continue;
} else if (lexer.token() == Token.MAXTRANS) {
lexer.nextToken();
stmt.setMaxtrans(this.exprParser.expr());
continue;
} else if (lexer.token() == Token.SEGMENT) {
lexer.nextToken();
accept(Token.CREATION);
if (lexer.token() == Token.IMMEDIATE) {
lexer.nextToken();
stmt.setDeferredSegmentCreation(DeferredSegmentCreation.IMMEDIATE);
} else {
accept(Token.DEFERRED);
stmt.setDeferredSegmentCreation(DeferredSegmentCreation.DEFERRED);
}
continue;
} else if (identifierEquals("PARTITION")) {
lexer.nextToken();
accept(Token.BY);
if (identifierEquals("RANGE")) {
SQLPartitionByRange partitionByRange = partitionByRange();
partitionClauseRest(partitionByRange);
stmt.setPartitioning(partitionByRange);
continue;
} else if (identifierEquals("HASH")) {
SQLPartitionByHash partitionByHash = partitionByHash();
partitionClauseRest(partitionByHash);
stmt.setPartitioning(partitionByHash);
continue;
} else if (identifierEquals("LIST")) {
SQLPartitionByList partitionByList = partitionByList();
partitionClauseRest(partitionByList);
stmt.setPartitioning(partitionByList);
continue;
} else {
throw new ParserException("TODO : " + lexer.token() + " " + lexer.stringVal());
}
}
break;
}
if (lexer.token() == Token.AS) {
lexer.nextToken();
OracleSelect select = new OracleSelectParser(exprParser).select();
stmt.setSelect(select);
}
return stmt;
}
use of com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelect in project druid by alibaba.
the class OracleExprParser method primary.
public SQLExpr primary() {
final Token tok = lexer.token();
SQLExpr sqlExpr = null;
switch(tok) {
case SYSDATE:
lexer.nextToken();
OracleSysdateExpr sysdate = new OracleSysdateExpr();
if (lexer.token() == Token.MONKEYS_AT) {
lexer.nextToken();
accept(Token.BANG);
sysdate.setOption("!");
}
sqlExpr = sysdate;
return primaryRest(sqlExpr);
case PRIOR:
lexer.nextToken();
sqlExpr = expr();
sqlExpr = new SQLUnaryExpr(SQLUnaryOperator.Prior, sqlExpr);
return primaryRest(sqlExpr);
case COLON:
lexer.nextToken();
if (lexer.token() == Token.LITERAL_INT) {
String name = ":" + lexer.numberString();
lexer.nextToken();
return new SQLVariantRefExpr(name);
} else if (lexer.token() == Token.IDENTIFIER) {
String name = lexer.stringVal();
if (name.charAt(0) == 'B' || name.charAt(0) == 'b') {
lexer.nextToken();
return new SQLVariantRefExpr(":" + name);
}
throw new ParserException("syntax error : " + lexer.token() + " " + lexer.stringVal());
} else {
throw new ParserException("syntax error : " + lexer.token());
}
case LITERAL_ALIAS:
String alias = '"' + lexer.stringVal() + '"';
lexer.nextToken();
return primaryRest(new SQLIdentifierExpr(alias));
case BINARY_FLOAT:
OracleBinaryFloatExpr floatExpr = new OracleBinaryFloatExpr();
floatExpr.setValue(Float.parseFloat(lexer.numberString()));
lexer.nextToken();
return primaryRest(floatExpr);
case BINARY_DOUBLE:
OracleBinaryDoubleExpr doubleExpr = new OracleBinaryDoubleExpr();
doubleExpr.setValue(Double.parseDouble(lexer.numberString()));
lexer.nextToken();
return primaryRest(doubleExpr);
case TABLE:
lexer.nextToken();
return primaryRest(new SQLIdentifierExpr("TABLE"));
case PLUS:
lexer.nextToken();
switch(lexer.token()) {
case LITERAL_INT:
sqlExpr = new SQLIntegerExpr(lexer.integerValue());
lexer.nextToken();
break;
case LITERAL_FLOAT:
sqlExpr = new SQLNumberExpr(lexer.decimalValue());
lexer.nextToken();
break;
case BINARY_FLOAT:
sqlExpr = new OracleBinaryFloatExpr(Float.parseFloat(lexer.numberString()));
lexer.nextToken();
break;
case BINARY_DOUBLE:
sqlExpr = new OracleBinaryDoubleExpr(Double.parseDouble(lexer.numberString()));
lexer.nextToken();
break;
case LPAREN:
lexer.nextToken();
sqlExpr = expr();
accept(Token.RPAREN);
sqlExpr = new SQLUnaryExpr(SQLUnaryOperator.Plus, sqlExpr);
break;
default:
throw new ParserException("TODO");
}
return primaryRest(sqlExpr);
case SUB:
lexer.nextToken();
switch(lexer.token()) {
case LITERAL_INT:
Number integerValue = lexer.integerValue();
if (integerValue instanceof Integer) {
int intVal = ((Integer) integerValue).intValue();
if (intVal == Integer.MIN_VALUE) {
integerValue = Long.valueOf(((long) intVal) * -1);
} else {
integerValue = Integer.valueOf(intVal * -1);
}
} else if (integerValue instanceof Long) {
long longVal = ((Long) integerValue).longValue();
if (longVal == 2147483648L) {
integerValue = Integer.valueOf((int) (((long) longVal) * -1));
} else {
integerValue = Long.valueOf(longVal * -1);
}
} else {
integerValue = ((BigInteger) integerValue).negate();
}
sqlExpr = new SQLIntegerExpr(integerValue);
lexer.nextToken();
break;
case LITERAL_FLOAT:
sqlExpr = new SQLNumberExpr(lexer.decimalValue().negate());
lexer.nextToken();
break;
case BINARY_FLOAT:
sqlExpr = new OracleBinaryFloatExpr(Float.parseFloat(lexer.numberString()) * -1);
lexer.nextToken();
break;
case BINARY_DOUBLE:
sqlExpr = new OracleBinaryDoubleExpr(Double.parseDouble(lexer.numberString()) * -1);
lexer.nextToken();
break;
case VARIANT:
case IDENTIFIER:
sqlExpr = expr();
sqlExpr = new SQLUnaryExpr(SQLUnaryOperator.Negative, sqlExpr);
break;
case LPAREN:
lexer.nextToken();
sqlExpr = expr();
accept(Token.RPAREN);
sqlExpr = new SQLUnaryExpr(SQLUnaryOperator.Negative, sqlExpr);
break;
default:
throw new ParserException("TODO " + lexer.token());
}
return primaryRest(sqlExpr);
case CURSOR:
lexer.nextToken();
accept(Token.LPAREN);
OracleSelect select = createSelectParser().select();
OracleCursorExpr cursorExpr = new OracleCursorExpr(select);
accept(Token.RPAREN);
sqlExpr = cursorExpr;
return primaryRest(sqlExpr);
case MODEL:
case PCTFREE:
case INITRANS:
case MAXTRANS:
case SEGMENT:
case CREATION:
case IMMEDIATE:
case DEFERRED:
case STORAGE:
case NEXT:
case MINEXTENTS:
case MAXEXTENTS:
case MAXSIZE:
case PCTINCREASE:
case FLASH_CACHE:
case CELL_FLASH_CACHE:
case KEEP:
case NONE:
case LOB:
case STORE:
case ROW:
case CHUNK:
case CACHE:
case NOCACHE:
case LOGGING:
case NOCOMPRESS:
case KEEP_DUPLICATES:
case EXCEPTIONS:
case PURGE:
sqlExpr = new SQLIdentifierExpr(lexer.stringVal());
lexer.nextToken();
return primaryRest(sqlExpr);
default:
return super.primary();
}
}
use of com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelect in project Mycat_plus by coderczp.
the class DruidSelectOracleParser method parseOrderAggGroupOracle.
protected void parseOrderAggGroupOracle(SQLStatement stmt, RouteResultset rrs, OracleSelectQueryBlock mysqlSelectQuery, SchemaConfig schema) {
Map<String, String> aliaColumns = parseAggGroupCommon(schema, stmt, rrs, mysqlSelectQuery);
OracleSelect oracleSelect = (OracleSelect) mysqlSelectQuery.getParent();
if (oracleSelect.getOrderBy() != null) {
List<SQLSelectOrderByItem> orderByItems = oracleSelect.getOrderBy().getItems();
rrs.setOrderByCols(buildOrderByCols(orderByItems, aliaColumns));
}
isNeedParseOrderAgg = false;
}
use of com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelect in project Mycat_plus by coderczp.
the class DruidSelectDb2Parser method parseNativePageSql.
protected void parseNativePageSql(SQLStatement stmt, RouteResultset rrs, OracleSelectQueryBlock mysqlSelectQuery, SchemaConfig schema) {
// 第一层子查询
SQLExpr where = mysqlSelectQuery.getWhere();
SQLTableSource from = mysqlSelectQuery.getFrom();
if (where instanceof SQLBinaryOpExpr && from instanceof SQLSubqueryTableSource) {
SQLBinaryOpExpr one = (SQLBinaryOpExpr) where;
SQLExpr left = one.getLeft();
SQLBinaryOperator operator = one.getOperator();
SQLSelectQuery subSelect = ((SQLSubqueryTableSource) from).getSelect().getQuery();
SQLOrderBy orderBy = null;
if (subSelect instanceof OracleSelectQueryBlock) {
boolean hasRowNumber = false;
OracleSelectQueryBlock subSelectOracle = (OracleSelectQueryBlock) subSelect;
List<SQLSelectItem> sqlSelectItems = subSelectOracle.getSelectList();
for (SQLSelectItem sqlSelectItem : sqlSelectItems) {
SQLExpr sqlExpr = sqlSelectItem.getExpr();
if (sqlExpr instanceof SQLAggregateExpr) {
SQLAggregateExpr agg = (SQLAggregateExpr) sqlExpr;
if ("row_number".equalsIgnoreCase(agg.getMethodName()) && agg.getOver() != null) {
hasRowNumber = true;
orderBy = agg.getOver().getOrderBy();
}
}
}
if (hasRowNumber) {
if ((operator == SQLBinaryOperator.LessThan || operator == SQLBinaryOperator.LessThanOrEqual) && one.getRight() instanceof SQLIntegerExpr) {
SQLIntegerExpr right = (SQLIntegerExpr) one.getRight();
int firstrownum = right.getNumber().intValue();
if (operator == SQLBinaryOperator.LessThan && firstrownum != 0) {
firstrownum = firstrownum - 1;
}
if (subSelect instanceof OracleSelectQueryBlock) {
rrs.setLimitStart(0);
rrs.setLimitSize(firstrownum);
// 为了继续解出order by 等
mysqlSelectQuery = (OracleSelectQueryBlock) subSelect;
if (orderBy != null) {
OracleSelect oracleSelect = (OracleSelect) subSelect.getParent();
oracleSelect.setOrderBy(orderBy);
}
parseOrderAggGroupOracle(stmt, rrs, mysqlSelectQuery, schema);
isNeedParseOrderAgg = false;
}
} else if (operator == SQLBinaryOperator.BooleanAnd && left instanceof SQLBinaryOpExpr && one.getRight() instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr leftE = (SQLBinaryOpExpr) left;
SQLBinaryOpExpr rightE = (SQLBinaryOpExpr) one.getRight();
SQLBinaryOpExpr small = null;
SQLBinaryOpExpr larger = null;
int firstrownum = 0;
int lastrownum = 0;
if (leftE.getRight() instanceof SQLIntegerExpr && (leftE.getOperator() == SQLBinaryOperator.GreaterThan || leftE.getOperator() == SQLBinaryOperator.GreaterThanOrEqual)) {
small = leftE;
firstrownum = ((SQLIntegerExpr) leftE.getRight()).getNumber().intValue();
if (leftE.getOperator() == SQLBinaryOperator.GreaterThanOrEqual && firstrownum != 0) {
firstrownum = firstrownum - 1;
}
} else if (leftE.getRight() instanceof SQLIntegerExpr && (leftE.getOperator() == SQLBinaryOperator.LessThan || leftE.getOperator() == SQLBinaryOperator.LessThanOrEqual)) {
larger = leftE;
lastrownum = ((SQLIntegerExpr) leftE.getRight()).getNumber().intValue();
if (leftE.getOperator() == SQLBinaryOperator.LessThan && lastrownum != 0) {
lastrownum = lastrownum - 1;
}
}
if (rightE.getRight() instanceof SQLIntegerExpr && (rightE.getOperator() == SQLBinaryOperator.GreaterThan || rightE.getOperator() == SQLBinaryOperator.GreaterThanOrEqual)) {
small = rightE;
firstrownum = ((SQLIntegerExpr) rightE.getRight()).getNumber().intValue();
if (rightE.getOperator() == SQLBinaryOperator.GreaterThanOrEqual && firstrownum != 0) {
firstrownum = firstrownum - 1;
}
} else if (rightE.getRight() instanceof SQLIntegerExpr && (rightE.getOperator() == SQLBinaryOperator.LessThan || rightE.getOperator() == SQLBinaryOperator.LessThanOrEqual)) {
larger = rightE;
lastrownum = ((SQLIntegerExpr) rightE.getRight()).getNumber().intValue();
if (rightE.getOperator() == SQLBinaryOperator.LessThan && lastrownum != 0) {
lastrownum = lastrownum - 1;
}
}
if (small != null && larger != null) {
setLimitIFChange(stmt, rrs, schema, small, firstrownum, lastrownum);
if (orderBy != null) {
OracleSelect oracleSelect = (OracleSelect) subSelect.getParent();
oracleSelect.setOrderBy(orderBy);
}
parseOrderAggGroupOracle(stmt, rrs, (OracleSelectQueryBlock) subSelect, schema);
isNeedParseOrderAgg = false;
}
}
} else {
parseNativeSql(stmt, rrs, mysqlSelectQuery, schema);
}
}
} else {
parseNativeSql(stmt, rrs, mysqlSelectQuery, schema);
}
if (isNeedParseOrderAgg) {
parseOrderAggGroupOracle(stmt, rrs, mysqlSelectQuery, schema);
}
}
Aggregations