use of com.alibaba.druid.sql.ast.statement.SQLExprTableSource in project druid by alibaba.
the class OdpsStatementParser method parseShow.
public SQLStatement parseShow() {
accept(Token.SHOW);
if (identifierEquals("PARTITIONS")) {
lexer.nextToken();
OdpsShowPartitionsStmt stmt = new OdpsShowPartitionsStmt();
SQLExpr expr = this.exprParser.expr();
stmt.setTableSource(new SQLExprTableSource(expr));
return stmt;
}
if (identifierEquals("STATISTIC")) {
lexer.nextToken();
OdpsShowStatisticStmt stmt = new OdpsShowStatisticStmt();
SQLExpr expr = this.exprParser.expr();
stmt.setTableSource(new SQLExprTableSource(expr));
return stmt;
}
if (identifierEquals("TABLES")) {
lexer.nextToken();
SQLShowTablesStatement stmt = new SQLShowTablesStatement();
if (lexer.token() == Token.FROM) {
lexer.nextToken();
stmt.setDatabase(this.exprParser.name());
}
if (lexer.token() == Token.LIKE) {
lexer.nextToken();
stmt.setLike(this.exprParser.expr());
}
return stmt;
}
if (identifierEquals("GRANTS")) {
lexer.nextToken();
OdpsShowGrantsStmt stmt = new OdpsShowGrantsStmt();
if (lexer.token() == Token.FOR) {
lexer.nextToken();
stmt.setUser(this.exprParser.expr());
}
if (lexer.token() == Token.ON) {
lexer.nextToken();
acceptIdentifier("type");
stmt.setObjectType(this.exprParser.expr());
}
return stmt;
}
throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
}
use of com.alibaba.druid.sql.ast.statement.SQLExprTableSource in project druid by alibaba.
the class OdpsStatementParser method parseSet.
public SQLStatement parseSet() {
List<String> comments = null;
if (lexer.isKeepComments() && lexer.hasComment()) {
comments = lexer.readAndResetComments();
}
accept(Token.SET);
if (identifierEquals("LABEL")) {
OdpsSetLabelStatement stmt = new OdpsSetLabelStatement();
if (comments != null) {
stmt.addBeforeComment(comments);
}
lexer.nextToken();
stmt.setLabel(lexer.stringVal());
lexer.nextToken();
accept(Token.TO);
if (lexer.token() == Token.USER) {
lexer.nextToken();
SQLName name = this.exprParser.name();
stmt.setUser(name);
return stmt;
}
accept(Token.TABLE);
SQLExpr expr = this.exprParser.name();
stmt.setTable(new SQLExprTableSource(expr));
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
this.exprParser.names(stmt.getColumns(), stmt);
accept(Token.RPAREN);
}
return stmt;
} else {
SQLSetStatement stmt = new SQLSetStatement(getDbType());
if (comments != null) {
stmt.addBeforeComment(comments);
}
parseAssignItems(stmt.getItems(), stmt);
return stmt;
}
}
use of com.alibaba.druid.sql.ast.statement.SQLExprTableSource in project druid by alibaba.
the class SQLCreateTableParser method parseCrateTable.
public SQLCreateTableStatement parseCrateTable(boolean acceptCreate) {
if (acceptCreate) {
accept(Token.CREATE);
}
SQLCreateTableStatement createTable = newCreateStatement();
if (identifierEquals("GLOBAL")) {
lexer.nextToken();
if (identifierEquals("TEMPORARY")) {
lexer.nextToken();
createTable.setType(SQLCreateTableStatement.Type.GLOBAL_TEMPORARY);
} else {
throw new ParserException("syntax error " + lexer.token() + " " + lexer.stringVal());
}
} else if (lexer.token() == Token.IDENTIFIER && lexer.stringVal().equalsIgnoreCase("LOCAL")) {
lexer.nextToken();
if (lexer.token() == Token.IDENTIFIER && lexer.stringVal().equalsIgnoreCase("TEMPORAY")) {
lexer.nextToken();
createTable.setType(SQLCreateTableStatement.Type.LOCAL_TEMPORARY);
} else {
throw new ParserException("syntax error");
}
}
accept(Token.TABLE);
createTable.setName(this.exprParser.name());
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
for (; ; ) {
if (//
lexer.token() == Token.IDENTIFIER || lexer.token() == Token.LITERAL_ALIAS) {
SQLColumnDefinition column = this.exprParser.parseColumn();
createTable.getTableElementList().add(column);
} else if (//
lexer.token == Token.PRIMARY || //
lexer.token == Token.UNIQUE || //
lexer.token == Token.CHECK || lexer.token == Token.CONSTRAINT) {
SQLConstraint constraint = this.exprParser.parseConstaint();
constraint.setParent(createTable);
createTable.getTableElementList().add((SQLTableElement) constraint);
} else if (lexer.token() == Token.TABLESPACE) {
throw new ParserException("TODO " + lexer.token());
} else {
SQLColumnDefinition column = this.exprParser.parseColumn();
createTable.getTableElementList().add(column);
}
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
if (lexer.token() == Token.RPAREN) {
// compatible for sql server
break;
}
continue;
}
break;
}
// while
// (this.tokenList.current().equals(OracleToken.ConstraintToken)) {
// parseConstaint(table.getConstraints());
//
// if (this.tokenList.current().equals(OracleToken.CommaToken))
// ;
// lexer.nextToken();
// }
accept(Token.RPAREN);
if (identifierEquals("INHERITS")) {
lexer.nextToken();
accept(Token.LPAREN);
SQLName inherits = this.exprParser.name();
createTable.setInherits(new SQLExprTableSource(inherits));
accept(Token.RPAREN);
}
}
return createTable;
}
use of com.alibaba.druid.sql.ast.statement.SQLExprTableSource in project Mycat_plus by coderczp.
the class DruidSelectParser method changeSql.
/**
* 改写sql:需要加limit的加上
*/
@Override
public void changeSql(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, LayerCachePool cachePool) throws SQLNonTransientException {
tryRoute(schema, rrs, cachePool);
rrs.copyLimitToNodes();
SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;
SQLSelectQuery sqlSelectQuery = selectStmt.getSelect().getQuery();
if (sqlSelectQuery instanceof MySqlSelectQueryBlock) {
MySqlSelectQueryBlock mysqlSelectQuery = (MySqlSelectQueryBlock) selectStmt.getSelect().getQuery();
int limitStart = 0;
int limitSize = schema.getDefaultMaxLimit();
// clear group having
SQLSelectGroupByClause groupByClause = mysqlSelectQuery.getGroupBy();
// Modified by winbill, 20160614, do NOT include having clause when routing to multiple nodes
if (groupByClause != null && groupByClause.getHaving() != null && isRoutMultiNode(schema, rrs)) {
groupByClause.setHaving(null);
}
Map<String, Map<String, Set<ColumnRoutePair>>> allConditions = getAllConditions();
boolean isNeedAddLimit = isNeedAddLimit(schema, rrs, mysqlSelectQuery, allConditions);
if (isNeedAddLimit) {
Limit limit = new Limit();
limit.setRowCount(new SQLIntegerExpr(limitSize));
mysqlSelectQuery.setLimit(limit);
rrs.setLimitSize(limitSize);
String sql = getSql(rrs, stmt, isNeedAddLimit);
rrs.changeNodeSqlAfterAddLimit(schema, getCurentDbType(), sql, 0, limitSize, true);
}
Limit limit = mysqlSelectQuery.getLimit();
if (limit != null && !isNeedAddLimit) {
SQLIntegerExpr offset = (SQLIntegerExpr) limit.getOffset();
SQLIntegerExpr count = (SQLIntegerExpr) limit.getRowCount();
if (offset != null) {
limitStart = offset.getNumber().intValue();
rrs.setLimitStart(limitStart);
}
if (count != null) {
limitSize = count.getNumber().intValue();
rrs.setLimitSize(limitSize);
}
if (isNeedChangeLimit(rrs)) {
Limit changedLimit = new Limit();
changedLimit.setRowCount(new SQLIntegerExpr(limitStart + limitSize));
if (offset != null) {
if (limitStart < 0) {
String msg = "You have an error in your SQL syntax; check the manual that " + "corresponds to your MySQL server version for the right syntax to use near '" + limitStart + "'";
throw new SQLNonTransientException(ErrorCode.ER_PARSE_ERROR + " - " + msg);
} else {
changedLimit.setOffset(new SQLIntegerExpr(0));
}
}
mysqlSelectQuery.setLimit(changedLimit);
String sql = getSql(rrs, stmt, isNeedAddLimit);
rrs.changeNodeSqlAfterAddLimit(schema, getCurentDbType(), sql, 0, limitStart + limitSize, true);
// 设置改写后的sql
ctx.setSql(sql);
} else {
rrs.changeNodeSqlAfterAddLimit(schema, getCurentDbType(), getCtx().getSql(), rrs.getLimitStart(), rrs.getLimitSize(), true);
// ctx.setSql(nativeSql);
}
}
if (rrs.isDistTable()) {
SQLTableSource from = mysqlSelectQuery.getFrom();
for (RouteResultsetNode node : rrs.getNodes()) {
SQLIdentifierExpr sqlIdentifierExpr = new SQLIdentifierExpr();
sqlIdentifierExpr.setParent(from);
sqlIdentifierExpr.setName(node.getSubTableName());
SQLExprTableSource from2 = new SQLExprTableSource(sqlIdentifierExpr);
from2.setAlias(from.getAlias());
mysqlSelectQuery.setFrom(from2);
node.setStatement(stmt.toString());
}
}
rrs.setCacheAble(isNeedCache(schema, rrs, mysqlSelectQuery, allConditions));
}
}
use of com.alibaba.druid.sql.ast.statement.SQLExprTableSource in project Mycat_plus by coderczp.
the class DruidMycatRouteStrategy method getDisTable.
private SQLExprTableSource getDisTable(SQLTableSource tableSource, RouteResultsetNode node) throws SQLSyntaxErrorException {
if (node.getSubTableName() == null) {
String msg = " sub table not exists for " + node.getName() + " on " + tableSource;
LOGGER.error("DruidMycatRouteStrategyError " + msg);
throw new SQLSyntaxErrorException(msg);
}
SQLIdentifierExpr sqlIdentifierExpr = new SQLIdentifierExpr();
sqlIdentifierExpr.setParent(tableSource.getParent());
sqlIdentifierExpr.setName(node.getSubTableName());
SQLExprTableSource from2 = new SQLExprTableSource(sqlIdentifierExpr);
return from2;
}
Aggregations