public boolean visit(MySqlSelectQueryBlock x) { final boolean bracket = x.isBracket(); if (bracket) { print('('); if ((!isParameterized()) && isPrettyFormat() && x.hasBeforeComment()) { printlnComments(x.getBeforeCommentsDirect()); String cachedSelectList = x.getCachedSelectList(); for (int i = 0, size = x.getHintsSize(); i < size; ++i) { SQLCommentHint hint = x.getHints().get(i); hint.accept(this); print(' '); final int distionOption = x.getDistionOption(); if (SQLSetQuantifier.ALL == distionOption) { print0(ucase ? "ALL " : "all "); if (x.isHignPriority()) { print0(ucase ? "HIGH_PRIORITY " : "high_priority "); if (x.isStraightJoin()) { print0(ucase ? "STRAIGHT_JOIN " : "straight_join "); if (x.isSmallResult()) { print0(ucase ? "SQL_SMALL_RESULT " : "sql_small_result "); if (x.isBigResult()) { print0(ucase ? "SQL_BIG_RESULT " : "sql_big_result ");
@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; }
for (int i = 0, size = x.getHintsSize(); i < size; ++i) { SQLCommentHint hint = x.getHints().get(i); hint.accept(this); print(' '); if (SQLSetQuantifier.ALL == x.getDistionOption()) { print0(ucase ? "ALL " : "all "); } else if (SQLSetQuantifier.DISTINCT == x.getDistionOption()) { print0(ucase ? "DISTINCT " : "distinct "); } else if (SQLSetQuantifier.DISTINCTROW == x.getDistionOption()) { print0(ucase ? "DISTINCTROW " : "distinctrow "); if (x.isHignPriority()) { print0(ucase ? "HIGH_PRIORITY " : "high_priority "); if (x.isStraightJoin()) { print0(ucase ? "STRAIGHT_JOIN " : "straight_join "); if (x.isSmallResult()) { print0(ucase ? "SQL_SMALL_RESULT " : "sql_small_result "); if (x.isBigResult()) { print0(ucase ? "SQL_BIG_RESULT " : "sql_big_result "); if (x.isBufferResult()) { print0(ucase ? "SQL_BUFFER_RESULT " : "sql_buffer_result "); if (x.getCache() != null) { if (x.getCache().booleanValue()) {
public boolean visit(MySqlSelectQueryBlock x) { if (x.getOrderBy() != null) { x.getOrderBy().setParent(x); if (x.getSelectList() != null) { rewriteDistinct = visitSelectItems(x.getSelectList(), SQLSetQuantifier.DISTINCT == x.getDistionOption()); if (x.getFrom() != null) { println(); print0(ucase ? "FROM " : "from "); x.getFrom().accept(this); if (x.getWhere() != null) { println(); print0(ucase ? "WHERE " : "where "); x.getWhere().setParent(x); x.getWhere().accept(this); if (x.getGroupBy() != null) { println(); x.getGroupBy().accept(this); if (x.getOrderBy() != null) { println(); x.getOrderBy().accept(this);
MySqlSelectQueryBlock queryBlock = new MySqlSelectQueryBlock(); this.exprParser.parseHints(queryBlock.getHints()); queryBlock.setDistionOption(SQLSetQuantifier.DISTINCT); lexer.nextToken(); } else if (lexer.identifierEquals("DISTINCTROW")) { queryBlock.setDistionOption(SQLSetQuantifier.DISTINCTROW); lexer.nextToken(); } else if (lexer.token() == (Token.ALL)) { queryBlock.setDistionOption(SQLSetQuantifier.ALL); lexer.nextToken(); queryBlock.setHignPriority(true); lexer.nextToken(); queryBlock.setStraightJoin(true); lexer.nextToken(); queryBlock.setSmallResult(true); lexer.nextToken(); queryBlock.setBigResult(true); lexer.nextToken(); queryBlock.setBufferResult(true); lexer.nextToken(); queryBlock.setCache(true);
MySqlSelectQueryBlock queryBlock = new MySqlSelectQueryBlock(); queryBlock.setParent(parent); queryBlock.addBeforeComment(lexer.readAndResetComments()); this.exprParser.parseHints(queryBlock.getHints()); } else { break; queryBlock.setDistionOption(SQLSetQuantifier.DISTINCT); lexer.nextToken(); } else if (lexer.identifierEquals(FnvHash.Constants.DISTINCTROW)) { queryBlock.setDistionOption(SQLSetQuantifier.DISTINCTROW); lexer.nextToken(); } else if (token == (Token.ALL)) { queryBlock.setDistionOption(SQLSetQuantifier.ALL); lexer.nextToken(); queryBlock.setHignPriority(true); lexer.nextToken(); queryBlock.setStraightJoin(true); lexer.nextToken(); queryBlock.setSmallResult(true); lexer.nextToken(); queryBlock.setBigResult(true); lexer.nextToken();
String fTableName = parseFromClause(sqlSelectQueryBlock.getFrom()); LOG.debug("SELECT SQL TableSource " + sqlSelectQueryBlock.getFrom()); AggregateInfo aggregateInfo = parseAggregateClause(sqlSelectQueryBlock.getSelectList(), table); if(aggregateInfo == null) { if (sqlSelectQueryBlock.getSelectList().size() == 1 && sqlSelectQueryBlock.getSelectList().get(0).getExpr() instanceof SQLAllColumnExpr) { selectItem = parseSelectClause(sqlSelectQueryBlock.getSelectList()); + sqlSelectQueryBlock.getSelectList()); SQLExpr where = sqlSelectQueryBlock.getWhere(); LOG.debug("SELECT SQL:where " + where); QueryInfo actionInfo = parseWhereClause(table, metaEventOperation, where, sqlSelectQueryBlock.isForUpdate()); LOG.debug("ActionInfo " + actionInfo.toString()); if (sqlSelectQueryBlock.getLimit() != null) { rowCount = sqlSelectQueryBlock.getLimit().getRowCount();
@Override public boolean visit(MySqlSelectQueryBlock x) { for (val item : x.getSelectList()) { item.accept(this); } if (x.getFrom() != null) { x.getFrom().accept(this); } acceptWhere(x.getWhere()); return false; }
if (x.getWhere() != null) { x.setWhere(new SQLBinaryOpExpr(condition, SQLBinaryOperator.BooleanAnd, x.getWhere())); } else { 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(); MySqlSelectQueryBlock.Limit limit = new MySqlSelectQueryBlock.Limit(rowCount); x.setLimit(limit);
@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; } }
private static boolean isJoin(SQLQueryExpr sqlExpr,String sql) { MySqlSelectQueryBlock query = (MySqlSelectQueryBlock) sqlExpr.getSubQuery().getQuery(); return query.getFrom() instanceof SQLJoinTableSource && ((SQLJoinTableSource) query.getFrom()).getJoinType() != SQLJoinTableSource.JoinType.COMMA && sql.toLowerCase().contains("join"); }
private void findSelect(MySqlSelectQueryBlock query, Select select, String tableAlias) throws SqlParseException { List<SQLSelectItem> selectList = query.getSelectList(); for (SQLSelectItem sqlSelectItem : selectList) { Field field = FieldMaker.makeField(sqlSelectItem.getExpr(), sqlSelectItem.getAlias(), tableAlias); select.addField(field); } }
private void findGroupBy(MySqlSelectQueryBlock query, Select select) throws SqlParseException { SQLSelectGroupByClause groupBy = query.getGroupBy(); SQLTableSource sqlTableSource = query.getFrom(); if (groupBy == null) { return;
if (sqlSelectQuery instanceof MySqlSelectQueryBlock) { MySqlSelectQueryBlock mysqlSelectQuery = (MySqlSelectQueryBlock) sqlSelectQuery; if (mysqlSelectQuery.getInto() != null) { throw new SQLNonTransientException("select ... into is not supported!"); SQLTableSource mysqlFrom = mysqlSelectQuery.getFrom(); if (mysqlFrom == null) { super.visitorParse(schema, rrs, stmt, visitor, sc); if ((mysqlSelectQuery.isForUpdate() || mysqlSelectQuery.isLockInShareMode()) && !sc.isAutocommit()) { rrs.setCanRunInReadDB(false);
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); }
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; }
if(mysqlSelectQuery.getGroupBy() == null) { return; List<SQLExpr> groupByItems = mysqlSelectQuery.getGroupBy().getItems(); if (groupByItems == null || groupByItems.size() == 0){ return; List<SQLSelectItem> selectList = mysqlSelectQuery.getSelectList(); List<String> groupbyColumns = new ArrayList<String>(groupByItems.size()); for(SQLExpr item : groupByItems){
if (mysqlSelectQuery.getOrderBy() == null){ return; List<SQLSelectOrderByItem> orderByItems = mysqlSelectQuery.getOrderBy().getItems(); if (orderByItems == null || orderByItems.size() == 0){ return; List<SQLSelectItem> selectList = mysqlSelectQuery.getSelectList(); List<OrderbyColumn> orderbyColumns = new ArrayList<OrderbyColumn>(orderByItems.size()); for(SQLSelectOrderByItem item : orderByItems){