use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.
the class MySqlShowKeysStatement method setTable.
public void setTable(SQLName table) {
if (table instanceof SQLPropertyExpr) {
SQLPropertyExpr propExpr = (SQLPropertyExpr) table;
this.setDatabase((SQLName) propExpr.getOwner());
this.table = new SQLIdentifierExpr(propExpr.getName());
return;
}
this.table = table;
}
use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.
the class PGExprParser method parseInterval.
@Override
protected SQLExpr parseInterval() {
accept(Token.INTERVAL);
PGIntervalExpr intervalExpr = new PGIntervalExpr();
if (lexer.token() != Token.LITERAL_CHARS) {
return new SQLIdentifierExpr("INTERVAL");
}
intervalExpr.setValue(new SQLCharExpr(lexer.stringVal()));
lexer.nextToken();
return intervalExpr;
}
use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.
the class PGSQLStatementParser method withQuery.
private PGWithQuery withQuery() {
PGWithQuery withQuery = new PGWithQuery();
if (lexer.token() == Token.LITERAL_ALIAS) {
withQuery.setName(new SQLIdentifierExpr("\"" + lexer.stringVal() + "\""));
} else {
withQuery.setName(new SQLIdentifierExpr(lexer.stringVal()));
}
lexer.nextToken();
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
for (; ; ) {
SQLExpr expr = this.exprParser.expr();
withQuery.addColumn(expr);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
} else {
break;
}
}
accept(Token.RPAREN);
}
accept(Token.AS);
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
SQLStatement query;
if (lexer.token() == Token.SELECT) {
query = this.parseSelect();
} else if (lexer.token() == Token.INSERT) {
query = this.parseInsert();
} else if (lexer.token() == Token.UPDATE) {
query = this.parseUpdateStatement();
} else if (lexer.token() == Token.DELETE) {
query = this.parseDeleteStatement();
} else if (lexer.token() == Token.VALUES) {
query = this.parseSelect();
} else {
throw new ParserException("syntax error, support token '" + lexer.token() + "'");
}
withQuery.setQuery(query);
accept(Token.RPAREN);
}
return withQuery;
}
use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.
the class PGSelectParser method query.
@Override
public SQLSelectQuery query() {
if (lexer.token() == Token.VALUES) {
lexer.nextToken();
accept(Token.LPAREN);
PGValuesQuery valuesQuery = new PGValuesQuery();
this.exprParser.exprList(valuesQuery.getValues(), valuesQuery);
accept(Token.RPAREN);
return queryRest(valuesQuery);
}
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
SQLSelectQuery select = query();
if (select instanceof SQLSelectQueryBlock) {
((SQLSelectQueryBlock) select).setParenthesized(true);
}
accept(Token.RPAREN);
return queryRest(select);
}
PGSelectQueryBlock queryBlock = new PGSelectQueryBlock();
if (lexer.token() == Token.SELECT) {
lexer.nextToken();
if (lexer.token() == Token.COMMENT) {
lexer.nextToken();
}
if (lexer.token() == Token.DISTINCT) {
queryBlock.setDistionOption(SQLSetQuantifier.DISTINCT);
lexer.nextToken();
if (lexer.token() == Token.ON) {
lexer.nextToken();
for (; ; ) {
SQLExpr expr = this.createExprParser().expr();
queryBlock.getDistinctOn().add(expr);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
} else {
break;
}
}
}
} else if (lexer.token() == Token.ALL) {
queryBlock.setDistionOption(SQLSetQuantifier.ALL);
lexer.nextToken();
}
parseSelectList(queryBlock);
if (lexer.token() == Token.INTO) {
lexer.nextToken();
if (lexer.token() == Token.TEMPORARY) {
lexer.nextToken();
queryBlock.setIntoOption(IntoOption.TEMPORARY);
} else if (lexer.token() == Token.TEMP) {
lexer.nextToken();
queryBlock.setIntoOption(IntoOption.TEMP);
} else if (lexer.token() == Token.UNLOGGED) {
lexer.nextToken();
queryBlock.setIntoOption(IntoOption.UNLOGGED);
}
if (lexer.token() == Token.TABLE) {
lexer.nextToken();
}
SQLExpr name = this.createExprParser().name();
queryBlock.setInto(new SQLExprTableSource(name));
}
}
parseFrom(queryBlock);
parseWhere(queryBlock);
parseGroupBy(queryBlock);
if (lexer.token() == Token.WINDOW) {
lexer.nextToken();
PGSelectQueryBlock.WindowClause window = new PGSelectQueryBlock.WindowClause();
window.setName(this.expr());
accept(Token.AS);
for (; ; ) {
SQLExpr expr = this.createExprParser().expr();
window.getDefinition().add(expr);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
} else {
break;
}
}
queryBlock.setWindow(window);
}
queryBlock.setOrderBy(this.createExprParser().parseOrderBy());
for (; ; ) {
if (lexer.token() == Token.LIMIT) {
SQLLimit limit = new SQLLimit();
lexer.nextToken();
if (lexer.token() == Token.ALL) {
limit.setRowCount(new SQLIdentifierExpr("ALL"));
lexer.nextToken();
} else {
limit.setRowCount(expr());
}
queryBlock.setLimit(limit);
} else if (lexer.token() == Token.OFFSET) {
SQLLimit limit = queryBlock.getLimit();
if (limit == null) {
limit = new SQLLimit();
queryBlock.setLimit(limit);
}
lexer.nextToken();
SQLExpr offset = expr();
limit.setOffset(offset);
if (lexer.token() == Token.ROW || lexer.token() == Token.ROWS) {
lexer.nextToken();
}
} else {
break;
}
}
if (lexer.token() == Token.FETCH) {
lexer.nextToken();
PGSelectQueryBlock.FetchClause fetch = new PGSelectQueryBlock.FetchClause();
if (lexer.token() == Token.FIRST) {
fetch.setOption(PGSelectQueryBlock.FetchClause.Option.FIRST);
} else if (lexer.token() == Token.NEXT) {
fetch.setOption(PGSelectQueryBlock.FetchClause.Option.NEXT);
} else {
throw new ParserException("expect 'FIRST' or 'NEXT'");
}
SQLExpr count = expr();
fetch.setCount(count);
if (lexer.token() == Token.ROW || lexer.token() == Token.ROWS) {
lexer.nextToken();
} else {
throw new ParserException("expect 'ROW' or 'ROWS'");
}
if (lexer.token() == Token.ONLY) {
lexer.nextToken();
} else {
throw new ParserException("expect 'ONLY'");
}
queryBlock.setFetch(fetch);
}
if (lexer.token() == Token.FOR) {
lexer.nextToken();
PGSelectQueryBlock.ForClause forClause = new PGSelectQueryBlock.ForClause();
if (lexer.token() == Token.UPDATE) {
forClause.setOption(PGSelectQueryBlock.ForClause.Option.UPDATE);
lexer.nextToken();
} else if (lexer.token() == Token.SHARE) {
forClause.setOption(PGSelectQueryBlock.ForClause.Option.SHARE);
lexer.nextToken();
} else {
throw new ParserException("expect 'FIRST' or 'NEXT'");
}
if (lexer.token() == Token.OF) {
for (; ; ) {
SQLExpr expr = this.createExprParser().expr();
forClause.getOf().add(expr);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
} else {
break;
}
}
}
if (lexer.token() == Token.NOWAIT) {
lexer.nextToken();
forClause.setNoWait(true);
}
queryBlock.setForClause(forClause);
}
return queryRest(queryBlock);
}
use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.
the class OracleSelectParser method parsePivot.
private void parsePivot(OracleSelectTableSource tableSource) {
OracleSelectPivot.Item item;
if (identifierEquals("PIVOT")) {
lexer.nextToken();
OracleSelectPivot pivot = new OracleSelectPivot();
if (identifierEquals("XML")) {
lexer.nextToken();
pivot.setXml(true);
}
accept(Token.LPAREN);
while (true) {
item = new OracleSelectPivot.Item();
item.setExpr((SQLAggregateExpr) this.exprParser.expr());
item.setAlias(as());
pivot.addItem(item);
if (!(lexer.token() == (Token.COMMA))) {
break;
}
lexer.nextToken();
}
accept(Token.FOR);
if (lexer.token() == (Token.LPAREN)) {
lexer.nextToken();
while (true) {
pivot.getPivotFor().add(new SQLIdentifierExpr(lexer.stringVal()));
lexer.nextToken();
if (!(lexer.token() == (Token.COMMA))) {
break;
}
lexer.nextToken();
}
accept(Token.RPAREN);
} else {
pivot.getPivotFor().add(new SQLIdentifierExpr(lexer.stringVal()));
lexer.nextToken();
}
accept(Token.IN);
accept(Token.LPAREN);
if (lexer.token() == (Token.LPAREN)) {
throw new ParserException("TODO");
}
if (lexer.token() == (Token.SELECT)) {
throw new ParserException("TODO");
}
for (; ; ) {
item = new OracleSelectPivot.Item();
item.setExpr(this.exprParser.expr());
item.setAlias(as());
pivot.getPivotIn().add(item);
if (lexer.token() != Token.COMMA) {
break;
}
lexer.nextToken();
}
accept(Token.RPAREN);
accept(Token.RPAREN);
tableSource.setPivot(pivot);
} else if (identifierEquals("UNPIVOT")) {
lexer.nextToken();
OracleSelectUnPivot unPivot = new OracleSelectUnPivot();
if (identifierEquals("INCLUDE")) {
lexer.nextToken();
acceptIdentifier("NULLS");
unPivot.setNullsIncludeType(OracleSelectUnPivot.NullsIncludeType.INCLUDE_NULLS);
} else if (identifierEquals("EXCLUDE")) {
lexer.nextToken();
acceptIdentifier("NULLS");
unPivot.setNullsIncludeType(OracleSelectUnPivot.NullsIncludeType.EXCLUDE_NULLS);
}
accept(Token.LPAREN);
if (lexer.token() == (Token.LPAREN)) {
lexer.nextToken();
this.exprParser.exprList(unPivot.getItems(), unPivot);
accept(Token.RPAREN);
} else {
unPivot.addItem(this.exprParser.expr());
}
accept(Token.FOR);
if (lexer.token() == (Token.LPAREN)) {
lexer.nextToken();
while (true) {
unPivot.getPivotFor().add(new SQLIdentifierExpr(lexer.stringVal()));
lexer.nextToken();
if (!(lexer.token() == (Token.COMMA))) {
break;
}
lexer.nextToken();
}
accept(Token.RPAREN);
} else {
unPivot.getPivotFor().add(new SQLIdentifierExpr(lexer.stringVal()));
lexer.nextToken();
}
accept(Token.IN);
accept(Token.LPAREN);
if (lexer.token() == (Token.LPAREN)) {
throw new ParserException("TODO");
}
if (lexer.token() == (Token.SELECT)) {
throw new ParserException("TODO");
}
for (; ; ) {
item = new OracleSelectPivot.Item();
item.setExpr(this.exprParser.expr());
item.setAlias(as());
unPivot.getPivotIn().add(item);
if (lexer.token() != Token.COMMA) {
break;
}
lexer.nextToken();
}
accept(Token.RPAREN);
accept(Token.RPAREN);
tableSource.setPivot(unPivot);
}
}
Aggregations