Java Code Examples for com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock#getLimit()

The following examples show how to use com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock#getLimit() . 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: DruidSelectParser.java    From dble with GNU General Public License v2.0 6 votes vote down vote up
private void tryAddLimit(SchemaConfig schema, TableConfig tableConfig,
                         MySqlSelectQueryBlock mysqlSelectQuery, RouteResultset rrs) {
    if (schema.getDefaultMaxLimit() == -1) {
        return;
    } else if (mysqlSelectQuery.getLimit() != null) {
        return;
    } else if (!tableConfig.isNeedAddLimit()) {
        return;
    } else if (mysqlSelectQuery.isForUpdate() || mysqlSelectQuery.isLockInShareMode()) {
        return;
    } else if (rrs.isContainsPrimaryFilter()) {
        // single table and has primary key , need not limit because of only one row
        return;
    }
    SQLLimit limit = new SQLLimit();
    limit.setRowCount(new SQLIntegerExpr(schema.getDefaultMaxLimit()));
    mysqlSelectQuery.setLimit(limit);
}
 
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: 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);
							}
						}
					}
				}
			}
		}
	}
}