Java Code Examples for com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock#Limit

The following examples show how to use com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock#Limit . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MySQLSelectASTVisitor.java    From Zebra with Apache License 2.0 6 votes vote down vote up
@Override
public boolean visit(MySqlSelectQueryBlock.Limit x) {
	if (x.getOffset() instanceof SQLIntegerExpr) {
		SQLIntegerExpr offsetExpr = (SQLIntegerExpr) x.getOffset();
		if (offsetExpr != null) {
			int offset = offsetExpr.getNumber().intValue();
			result.getMergeContext().setOffset(offset);
		}
	}

	if (x.getRowCount() instanceof SQLIntegerExpr) {
		SQLIntegerExpr rowCountExpr = (SQLIntegerExpr) x.getRowCount();
		if (rowCountExpr != null) {
			int limit = rowCountExpr.getNumber().intValue();
			result.getMergeContext().setLimit(limit);
		}
	}

	result.getMergeContext().setLimitExpr(x);
	return true;
}
 
Example 2
Source File: DruidSelectParser.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
private void fixLimit(MySqlSelectQueryBlock mysqlSelectQuery, RouteResultsetNode node) {
	if(!getCurentDbType().equalsIgnoreCase("mysql")) {
		MySqlSelectQueryBlock.Limit  _limit = mysqlSelectQuery.getLimit();
		if (_limit != null) {
			SQLIntegerExpr offset = (SQLIntegerExpr) _limit.getOffset();
			SQLIntegerExpr count = (SQLIntegerExpr) _limit.getRowCount();
			if (offset != null && count != null) {
				String nativeSql = PageSQLUtil
						.convertLimitToNativePageSql(getCurentDbType(), node.getStatement(),
								offset.getNumber().intValue(), count.getNumber().intValue());
				node.setStatement(nativeSql);
			}
		}
	}
}
 
Example 3
Source File: DruidSelectOracleParser.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void statementParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt) {
	SQLSelectStatement selectStmt = (SQLSelectStatement)stmt;
	SQLSelectQuery sqlSelectQuery = selectStmt.getSelect().getQuery();
      //从mysql解析过来
	if(sqlSelectQuery instanceof MySqlSelectQueryBlock) {
		MySqlSelectQueryBlock mysqlSelectQuery = (MySqlSelectQueryBlock)selectStmt.getSelect().getQuery();
		MySqlSelectQueryBlock.Limit limit=mysqlSelectQuery.getLimit();
		if(limit==null)
		{
			  //使用oracle的解析,否则会有部分oracle语法识别错误
			  OracleStatementParser oracleParser = new OracleStatementParser(getCtx().getSql());
			  SQLSelectStatement oracleStmt = (SQLSelectStatement) oracleParser.parseStatement();
               selectStmt= oracleStmt;
			  SQLSelectQuery oracleSqlSelectQuery = oracleStmt.getSelect().getQuery();
			  if(oracleSqlSelectQuery instanceof OracleSelectQueryBlock)
			  {
				  parseNativePageSql(oracleStmt, rrs, (OracleSelectQueryBlock) oracleSqlSelectQuery, schema);
			  }



		  }
		if(isNeedParseOrderAgg)
		{
			parseOrderAggGroupMysql(schema, selectStmt,rrs, mysqlSelectQuery);
			//更改canRunInReadDB属性
			if ((mysqlSelectQuery.isForUpdate() || mysqlSelectQuery.isLockInShareMode()) && rrs.isAutocommit() == false)
			{
				rrs.setCanRunInReadDB(false);
			}
		}

	}


}
 
Example 4
Source File: DruidSelectSqlServerParser.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void statementParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt) {
	SQLSelectStatement selectStmt = (SQLSelectStatement)stmt;
	SQLSelectQuery sqlSelectQuery = selectStmt.getSelect().getQuery();
	//从mysql解析过来
	if(sqlSelectQuery instanceof MySqlSelectQueryBlock) {
		MySqlSelectQueryBlock mysqlSelectQuery = (MySqlSelectQueryBlock)selectStmt.getSelect().getQuery();
		MySqlSelectQueryBlock.Limit limit=mysqlSelectQuery.getLimit();
		if(limit==null)
		{
               sqlserverParse(schema, rrs);


           }
		if(isNeedParseOrderAgg)
		{
			parseOrderAggGroupMysql(schema, stmt,rrs, mysqlSelectQuery);
			//更改canRunInReadDB属性
			if ((mysqlSelectQuery.isForUpdate() || mysqlSelectQuery.isLockInShareMode()) && rrs.isAutocommit() == false)
			{
				rrs.setCanRunInReadDB(false);
			}
		}

	}


}
 
Example 5
Source File: MySqlSelectParser.java    From baymax with Apache License 2.0 5 votes vote down vote up
protected void parseLimit(ParseResult result, ExecutePlan plan, MySqlSelectQueryBlock mysqlSelectQuery){
    MySqlSelectQueryBlock.Limit x = mysqlSelectQuery.getLimit();
    if (x == null){
        return;
    }
    Map<Integer, Object> overrideParameters = new HashMap<Integer, Object>(2);
    int offset = 0;
    if (null != x.getOffset()) {
        if (x.getOffset() instanceof SQLNumericLiteralExpr) {
            offset = ((SQLNumericLiteralExpr) x.getOffset()).getNumber().intValue();
            SQLNumberExpr offsetExpr = new SQLNumberExpr();
            offsetExpr.setNumber(0);
            x.setOffset(offsetExpr);
        } else {
            offset = ((Number) parameters.get(((SQLVariantRefExpr) x.getOffset()).getIndex())).intValue();
            overrideParameters.put(((SQLVariantRefExpr) x.getOffset()).getIndex() + 1, 0);
        }
    }
    int rowCount;
    if (x.getRowCount() instanceof SQLNumericLiteralExpr) {
        rowCount = ((SQLNumericLiteralExpr) x.getRowCount()).getNumber().intValue();
        SQLNumberExpr rowsExpr = new SQLNumberExpr();
        rowsExpr.setNumber(rowCount + offset);
        x.setRowCount(rowsExpr);
    } else {
        rowCount = ((Number) parameters.get(((SQLVariantRefExpr) x.getRowCount()).getIndex())).intValue();
        overrideParameters.put(((SQLVariantRefExpr) x.getRowCount()).getIndex() + 1, rowCount + offset);
    }
    plan.setLimit(new Limit(offset, rowCount));
    plan.setOverrideParameters(overrideParameters);
}
 
Example 6
Source File: DefaultSQLRewrite.java    From Zebra with Apache License 2.0 5 votes vote down vote up
@Override
public boolean visit(MySqlSelectQueryBlock.Limit x) {
	print0(ucase ? "LIMIT " : "limit ");

	int offset = Integer.MIN_VALUE;
	if (x.getOffset() != null) {
		if (x.getOffset() instanceof SQLIntegerExpr && !pr.getMergeContext().isOrderBySplitSql()) {
			SQLIntegerExpr offsetExpr = (SQLIntegerExpr) x.getOffset();
			offset = (Integer) offsetExpr.getValue();
			offsetExpr.setNumber(0);
			offsetExpr.accept(this);
		} else {
			x.getOffset().accept(this);
		}

		print0(", ");
	}

	int limit = Integer.MAX_VALUE;
	if (x.getRowCount() instanceof SQLIntegerExpr && !pr.getMergeContext().isOrderBySplitSql()) {
		SQLIntegerExpr rowCountExpr = (SQLIntegerExpr) x.getRowCount();
		if (offset != Integer.MIN_VALUE) {
			limit = (Integer) rowCountExpr.getValue();
			rowCountExpr.setNumber(offset + limit);
		}
		rowCountExpr.accept(this);
	} else {
		x.getRowCount().accept(this);
	}

	return false;
}
 
Example 7
Source File: ShardPreparedStatement.java    From Zebra with Apache License 2.0 5 votes vote down vote up
private void replaceLimitParams(SQLParsedResult parseResult) {
	if (parseResult != null) {
		SQLStatement sqlStatement = parseResult.getStmt();
		if (parseResult.getStmt() != null && sqlStatement instanceof SQLSelectStatement) {
			SQLSelect sqlSelect = ((SQLSelectStatement) sqlStatement).getSelect();
			if (sqlSelect != null) {
				SQLSelectQuery sqlSelectQuery = sqlSelect.getQuery();
				if (sqlSelectQuery != null && sqlSelectQuery instanceof MySqlSelectQueryBlock) {
					MySqlSelectQueryBlock sqlSelectQueryBlock = (MySqlSelectQueryBlock) sqlSelectQuery;
					MySqlSelectQueryBlock.Limit limitExpr = sqlSelectQueryBlock.getLimit();
					if (limitExpr != null) {
						int offsetRefIndex = -1;
						int countRefIndex = -1;
						if (limitExpr.getOffset() instanceof SQLVariantRefExpr
						      && limitExpr.getRowCount() instanceof SQLVariantRefExpr) {
							SQLVariantRefExpr offsetExpr = (SQLVariantRefExpr) limitExpr.getOffset();
							SQLVariantRefExpr countExpr = (SQLVariantRefExpr) limitExpr.getRowCount();

							offsetRefIndex = offsetExpr.getIndex();
							countRefIndex = countExpr.getIndex();

							if (offsetRefIndex > countRefIndex && offsetRefIndex != -1 && countRefIndex != -1) {
								offsetExpr.setIndex(countRefIndex);
								countExpr.setIndex(offsetRefIndex);
							}
						}
					}
				}
			}
		}
	}
}
 
Example 8
Source File: MergeContext.java    From Zebra with Apache License 2.0 4 votes vote down vote up
public MySqlSelectQueryBlock.Limit getLimitExpr() {
	return limitExpr;
}
 
Example 9
Source File: MergeContext.java    From Zebra with Apache License 2.0 4 votes vote down vote up
public void setLimitExpr(MySqlSelectQueryBlock.Limit limitExpr) {
	this.limitExpr = limitExpr;
}