use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock in project dble by actiontech.
the class DruidSelectParser method changeSql.
/**
* changeSql: add limit if need
*/
@Override
public void changeSql(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, LayerCachePool cachePool) throws SQLException {
if (rrs.isFinishedRoute() || rrs.isFinishedExecute() || rrs.isNeedOptimizer()) {
return;
}
tryRouteSingleTable(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();
Map<String, Map<String, Set<ColumnRoutePair>>> allConditions = getAllConditions();
boolean isNeedAddLimit = isNeedAddLimit(schema, rrs, mysqlSelectQuery, allConditions);
if (isNeedAddLimit) {
SQLLimit limit = new SQLLimit();
limit.setRowCount(new SQLIntegerExpr(limitSize));
mysqlSelectQuery.setLimit(limit);
rrs.setLimitSize(limitSize);
String sql = getSql(rrs, stmt, isNeedAddLimit, schema.getName());
rrs.changeNodeSqlAfterAddLimit(sql, 0, limitSize);
}
SQLLimit 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)) {
SQLLimit changedLimit = new SQLLimit();
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, schema.getName());
rrs.changeNodeSqlAfterAddLimit(sql, 0, limitStart + limitSize);
} else {
rrs.changeNodeSqlAfterAddLimit(rrs.getStatement(), rrs.getLimitStart(), rrs.getLimitSize());
}
}
rrs.setCacheAble(isNeedCache(schema));
}
}
use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock in project dble by actiontech.
the class TestMySQLItemVisitor method testJoinCondition.
@Test
public void testJoinCondition() {
MySqlSelectQueryBlock query = getQuery("select a.col1,b.col2 from table1 a inner join table2 b on a.id =b.id");
SQLJoinTableSource from = (SQLJoinTableSource) query.getFrom();
MySQLItemVisitor v = new MySQLItemVisitor(this.currentDb, utf8Charset, null);
from.getCondition().accept(v);
Item item = v.getItem();
Assert.assertEquals(true, "a.id = b.id".equals(item.getItemName()));
}
use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock in project dble by actiontech.
the class TestMySQLItemVisitor method testGroupbyHaving.
@Test
public void testGroupbyHaving() {
MySqlSelectQueryBlock query = getQuery("select col1 from table1 group by col1 having count(*)>1 ");
SQLSelectGroupByClause groupBy = query.getGroupBy();
SQLExpr q = groupBy.getHaving();
MySQLItemVisitor v = new MySQLItemVisitor(this.currentDb, utf8Charset, null);
q.accept(v);
Item item = v.getItem();
Assert.assertEquals(true, "COUNT(*) > 1".equals(item.getItemName()));
}
use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock in project dble by actiontech.
the class TestMySQLItemVisitor method testWhere.
// TODO:ORDER BY /GROUP BY position
@Test
public void testWhere() {
MySqlSelectQueryBlock query = getQuery("select col1,col2 from table1 where a =1 ");
SQLExpr expr = query.getWhere();
MySQLItemVisitor v = new MySQLItemVisitor(this.currentDb, utf8Charset, null);
expr.accept(v);
Item item = v.getItem();
Assert.assertEquals(true, "a = 1".equals(item.getItemName()));
}
use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock in project dble by actiontech.
the class SelectHandler method isSupportSelect.
private static boolean isSupportSelect(String stmt) {
SQLStatementParser parser = new MySqlStatementParser(stmt);
SQLStatement statement = parser.parseStatement();
if (!(statement instanceof SQLSelectStatement)) {
return false;
}
SQLSelectQuery sqlSelectQuery = ((SQLSelectStatement) statement).getSelect().getQuery();
if (!(sqlSelectQuery instanceof MySqlSelectQueryBlock)) {
return false;
}
MySqlSelectQueryBlock selectQueryBlock = (MySqlSelectQueryBlock) sqlSelectQuery;
SQLTableSource mysqlFrom = selectQueryBlock.getFrom();
if (mysqlFrom != null) {
return false;
}
for (SQLSelectItem item : selectQueryBlock.getSelectList()) {
SQLExpr selectItem = item.getExpr();
if (!isVariantRef(selectItem)) {
return false;
}
}
return true;
}
Aggregations