if (query instanceof SQLSelectQueryBlock) { if (query instanceof MySqlSelectQueryBlock) { SQLLimit limit = ((MySqlSelectQueryBlock) query).getLimit();
@Override public boolean visit(MySqlSelectQueryBlock x) { SQLOrderBy orderBy = x.getOrderBy(); SQLLimit limit = x.getLimit(); if (limit != null && (orderBy == null || orderBy.getItems().size() == 0)) { boolean subQueryHasOrderBy = false; SQLTableSource from = x.getFrom(); if (from instanceof SQLSubqueryTableSource) { SQLSubqueryTableSource subqueryTabSrc = (SQLSubqueryTableSource) from; SQLSelect select = subqueryTabSrc.getSelect(); if (select.getQuery() instanceof SQLSelectQueryBlock) { SQLSelectQueryBlock subquery = (SQLSelectQueryBlock) select.getQuery(); if (subquery.getOrderBy() != null && subquery.getOrderBy().getItems().size() > 0) { subQueryHasOrderBy = true; } } } if (!subQueryHasOrderBy) { unorderedLimitCount++; } } return true; } }
public JoinSelect parseJoinSelect(SQLQueryExpr sqlExpr) throws SqlParseException { MySqlSelectQueryBlock query = (MySqlSelectQueryBlock) sqlExpr.getSubQuery().getQuery(); List<From> joinedFrom = findJoinedFrom(query.getFrom()); if (joinedFrom.size() != 2) throw new RuntimeException("currently supports only 2 tables join"); JoinSelect joinSelect = createBasicJoinSelectAccordingToTableSource((SQLJoinTableSource) query.getFrom()); List<Hint> hints = parseHints(query.getHints()); joinSelect.setHints(hints); String firstTableAlias = joinedFrom.get(0).getAlias(); String secondTableAlias = joinedFrom.get(1).getAlias(); Map<String, Where> aliasToWhere = splitAndFindWhere(query.getWhere(), firstTableAlias, secondTableAlias); Map<String, List<SQLSelectOrderByItem>> aliasToOrderBy = splitAndFindOrder(query.getOrderBy(), firstTableAlias, secondTableAlias); List<Condition> connectedConditions = getConditionsFlatten(joinSelect.getConnectedWhere()); joinSelect.setConnectedConditions(connectedConditions); fillTableSelectedJoin(joinSelect.getFirstTable(), query, joinedFrom.get(0), aliasToWhere.get(firstTableAlias), aliasToOrderBy.get(firstTableAlias), connectedConditions); fillTableSelectedJoin(joinSelect.getSecondTable(), query, joinedFrom.get(1), aliasToWhere.get(secondTableAlias), aliasToOrderBy.get(secondTableAlias), connectedConditions); updateJoinLimit(query.getLimit(), joinSelect); //todo: throw error feature not supported: no group bys on joins ? return joinSelect; }
findLimit(query.getLimit(), select);
@Override public boolean visit(MySqlSelectQueryBlock x) { if (x.getLimit() != null) { if (x.getLimit().getOffset() != null) { if (x.getLimit().getOffset() instanceof SQLVariantRefExpr && params != null && params.size() > 0) { SQLVariantRefExpr ref = (SQLVariantRefExpr) x.getLimit().getOffset(); Integer offset = (Integer) params.get(ref.getIndex()); params.set(ref.getIndex(), offset / this.splitNum); } else { SQLIntegerExpr offset = (SQLIntegerExpr) x.getLimit().getOffset(); // 去尾法是符合算法的 offset.setNumber(offset.getNumber().longValue() / this.splitNum); } } } return super.visit(x); } }
@Override public boolean visit(MySqlSelectQueryBlock x) { List<SQLSelectItem> selectList = x.getSelectList(); SQLAggregateExpr countExpr = new SQLAggregateExpr("COUNT"); countExpr.getArguments().add(new SQLAllColumnExpr()); if (x.getLimit() != null) { if (x.getLimit().getOffset() instanceof SQLVariantRefExpr && countParams != null && countParams.size() > 0) { SQLVariantRefExpr ref = (SQLVariantRefExpr) x.getLimit().getOffset(); Object[] values = { 0 }; countParams.get(ref.getIndex()).setValues(values); } else if (x.getLimit().getOffset() != null) { x.getLimit().setOffset(new SQLNumberExpr(0)); } if (x.getLimit().getRowCount() instanceof SQLVariantRefExpr && countParams != null && countParams.size() > 0) { SQLVariantRefExpr ref = (SQLVariantRefExpr) x.getLimit().getRowCount(); Object[] values = { 1 }; countParams.get(ref.getIndex()).setValues(values); } else if (x.getLimit().getRowCount() != null) { x.getLimit().setRowCount(new SQLNumberExpr(1)); } } x.setOrderBy(null); selectList.clear(); selectList.add(new SQLSelectItem(countExpr, countAlias)); return false; } }
SQLLimit limit = x.getLimit(); if (limit != null) { println();
x.setWhere(condition); if (x.getLimit() != null) { if (x.getLimit().getOffset() instanceof SQLVariantRefExpr && params != null && params.size() > 0) { SQLVariantRefExpr ref = (SQLVariantRefExpr) x.getLimit().getOffset(); index1 = ref.getIndex(); if (x.getLimit().getRowCount() instanceof SQLVariantRefExpr && params != null && params.size() > 0) { SQLVariantRefExpr ref = (SQLVariantRefExpr) x.getLimit().getRowCount(); index2 = ref.getIndex();
if (query instanceof SQLSelectQueryBlock) { if (query instanceof MySqlSelectQueryBlock) { SQLLimit limit = ((MySqlSelectQueryBlock) query).getLimit();
@Override public boolean visit(MySqlSelectQueryBlock x) { SQLOrderBy orderBy = x.getOrderBy(); SQLLimit limit = x.getLimit(); if (limit != null && (orderBy == null || orderBy.getItems().size() == 0)) { boolean subQueryHasOrderBy = false; SQLTableSource from = x.getFrom(); if (from instanceof SQLSubqueryTableSource) { SQLSubqueryTableSource subqueryTabSrc = (SQLSubqueryTableSource) from; SQLSelect select = subqueryTabSrc.getSelect(); if (select.getQuery() instanceof SQLSelectQueryBlock) { SQLSelectQueryBlock subquery = (SQLSelectQueryBlock) select.getQuery(); if (subquery.getOrderBy() != null && subquery.getOrderBy().getItems().size() > 0) { subQueryHasOrderBy = true; } } } if (!subQueryHasOrderBy) { unorderedLimitCount++; } } return true; } }
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); } } } } } } } }
} else if (schema.getDefaultMaxLimit() == -1) { return false; } else if (mysqlSelectQuery.getLimit() != null) { // has already limit return false; } else if (ctx.getTables().size() == 1) {
SQLLimit limit = x.getLimit(); if (limit != null) { println();
public JoinSelect parseJoinSelect(SQLQueryExpr sqlExpr) throws SqlParseException { MySqlSelectQueryBlock query = (MySqlSelectQueryBlock) sqlExpr.getSubQuery().getQuery(); List<From> joinedFrom = findJoinedFrom(query.getFrom()); if (joinedFrom.size() != 2) throw new RuntimeException("currently supports only 2 tables join"); JoinSelect joinSelect = createBasicJoinSelectAccordingToTableSource((SQLJoinTableSource) query.getFrom()); List<Hint> hints = parseHints(query.getHints()); joinSelect.setHints(hints); String firstTableAlias = joinedFrom.get(0).getAlias(); String secondTableAlias = joinedFrom.get(1).getAlias(); Map<String, Where> aliasToWhere = splitAndFindWhere(query.getWhere(), firstTableAlias, secondTableAlias); Map<String, List<SQLSelectOrderByItem>> aliasToOrderBy = splitAndFindOrder(query.getOrderBy(), firstTableAlias, secondTableAlias); List<Condition> connectedConditions = getConditionsFlatten(joinSelect.getConnectedWhere()); joinSelect.setConnectedConditions(connectedConditions); fillTableSelectedJoin(joinSelect.getFirstTable(), query, joinedFrom.get(0), aliasToWhere.get(firstTableAlias), aliasToOrderBy.get(firstTableAlias), connectedConditions); fillTableSelectedJoin(joinSelect.getSecondTable(), query, joinedFrom.get(1), aliasToWhere.get(secondTableAlias), aliasToOrderBy.get(secondTableAlias), connectedConditions); updateJoinLimit(query.getLimit(), joinSelect); //todo: throw error feature not supported: no group bys on joins ? return joinSelect; }
protected void parseLimit(ParseResult result, ExecutePlan plan, MySqlSelectQueryBlock mysqlSelectQuery){ MySqlSelectQueryBlock.Limit x = mysqlSelectQuery.getLimit(); if (x == null){ return;
public Select parseSelect(MySqlSelectQueryBlock query) throws SqlParseException { Select select = new Select(); WhereParser whereParser = new WhereParser(this, query); findSelect(query, select, query.getFrom().getAlias()); select.getFrom().addAll(findFrom(query.getFrom())); select.setWhere(whereParser.findWhere()); select.fillSubQueries(); select.getHints().addAll(parseHints(query.getHints())); findLimit(query.getLimit(), select); findOrderBy(query, select); findGroupBy(query, select); return select; }
private void tryAddLimit(SchemaConfig schema, TableConfig tableConfig, MySqlSelectQueryBlock mysqlSelectQuery) { if (schema.getDefaultMaxLimit() == -1) { return; } else if (mysqlSelectQuery.getLimit() != null) { return; } else if (!tableConfig.isNeedAddLimit()) { return; } else if (mysqlSelectQuery.isForUpdate() || mysqlSelectQuery.isLockInShareMode()) { return; } SQLLimit limit = new SQLLimit(); limit.setRowCount(new SQLIntegerExpr(schema.getDefaultMaxLimit())); mysqlSelectQuery.setLimit(limit); }
SQLLimit limit = sqlSelectQuery.getLimit(); if (limit != null) { handleLimit(limit);
SQLLimit limit = mysqlSelectQuery.getLimit(); if (limit != null && !isNeedAddLimit) { SQLIntegerExpr offset = (SQLIntegerExpr) limit.getOffset();