com.alibaba.fastsql.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock Java Examples

The following examples show how to use com.alibaba.fastsql.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock. 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: SqlToExprTranslator.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
private QueryPlan convertQuery(MySqlSelectQueryBlock sqlSelectQuery) {
    Blackboard blackboard = new Blackboard(rexBuilder, convertFrom(sqlSelectQuery.getFrom()));
    SQLExpr where = sqlSelectQuery.getWhere();
    if (where != null) {
        blackboard.addWhere(convertExpr(where));
    }
    List<SQLSelectItem> selectList = sqlSelectQuery.getSelectList();
    if (selectList != null) {
        blackboard.addSelect(convertSelect(selectList));
    }
    SQLOrderBy orderBy = sqlSelectQuery.getOrderBy();
    if (orderBy != null) {
        blackboard.addOrderBy(convertOrderBy(orderBy));
    }
    SQLSelectGroupByClause groupBy = sqlSelectQuery.getGroupBy();
    if (groupBy != null) {
        convertGroupBy(blackboard, groupBy);
    }
    SQLLimit limit = sqlSelectQuery.getLimit();
    if (limit != null) {
        convertLimit(blackboard, limit);
    }
    return blackboard.build();
}
 
Example #2
Source File: SqlParser.java    From canal-1.1.3 with Apache License 2.0 5 votes vote down vote up
/**
 * 归集字段
 *
 * @param sqlSelectQueryBlock sqlSelectQueryBlock
 * @return 字段属性列表
 */
private static List<FieldItem> collectSelectQueryFields(MySqlSelectQueryBlock sqlSelectQueryBlock) {
    return sqlSelectQueryBlock.getSelectList().stream().map(selectItem -> {
        FieldItem fieldItem = new FieldItem();
        fieldItem.setFieldName(selectItem.getAlias());
        fieldItem.setExpr(selectItem.toString());
        visitColumn(selectItem.getExpr(), fieldItem);
        return fieldItem;
    }).collect(Collectors.toList());
}
 
Example #3
Source File: SqlParser.java    From canal with Apache License 2.0 5 votes vote down vote up
public static String parse4GroupBy(MySqlSelectQueryBlock sqlSelectQueryBlock) {
    SQLSelectGroupByClause expr = sqlSelectQueryBlock.getGroupBy();
    if (expr != null) {
        return SQLUtils.toMySqlString(expr);
    }
    return null;
}
 
Example #4
Source File: SqlParser.java    From canal with Apache License 2.0 5 votes vote down vote up
public static String parse4WhereItem(MySqlSelectQueryBlock sqlSelectQueryBlock) {
    SQLExpr sqlExpr = sqlSelectQueryBlock.getWhere();
    if (sqlExpr != null) {
        return SQLUtils.toMySqlString(sqlExpr);
    }
    return null;
}
 
Example #5
Source File: SqlParser.java    From canal with Apache License 2.0 5 votes vote down vote up
public static String parse4SQLSelectItem(MySqlSelectQueryBlock sqlSelectQueryBlock) {
    List<SQLSelectItem> selectItems = sqlSelectQueryBlock.getSelectList();
    StringBuilder subSql = new StringBuilder();
    int i = 0;
    for (SQLSelectItem sqlSelectItem : selectItems) {
        if (i != 0) {
            subSql.append(",");
        } else {
            i++;
        }
        subSql.append(SQLUtils.toMySqlString(sqlSelectItem));
    }
    return subSql.toString();
}
 
Example #6
Source File: SqlParser.java    From canal with Apache License 2.0 5 votes vote down vote up
public static MySqlSelectQueryBlock parseSQLSelectQueryBlock(String sql) {
    if (sql == null || "".equals(sql)) {
        return null;
    }
    SQLStatementParser parser = new MySqlStatementParser(sql);
    SQLSelectStatement statement = (SQLSelectStatement) parser.parseStatement();
    return (MySqlSelectQueryBlock) statement.getSelect().getQuery();
}
 
Example #7
Source File: SqlParser.java    From canal with Apache License 2.0 5 votes vote down vote up
/**
 * 归集字段
 *
 * @param sqlSelectQueryBlock sqlSelectQueryBlock
 * @return 字段属性列表
 */
private static List<FieldItem> collectSelectQueryFields(MySqlSelectQueryBlock sqlSelectQueryBlock) {
    return sqlSelectQueryBlock.getSelectList().stream().map(selectItem -> {
        FieldItem fieldItem = new FieldItem();
        fieldItem.setFieldName(selectItem.getAlias());
        fieldItem.setExpr(selectItem.toString());
        visitColumn(selectItem.getExpr(), fieldItem);
        return fieldItem;
    }).collect(Collectors.toList());
}
 
Example #8
Source File: SqlParser.java    From canal with Apache License 2.0 5 votes vote down vote up
/**
 * 解析sql
 *
 * @param sql sql
 * @return 视图对象
 */
public static SchemaItem parse(String sql) {
    try {
        SQLStatementParser parser = new MySqlStatementParser(sql);
        SQLSelectStatement statement = (SQLSelectStatement) parser.parseStatement();
        MySqlSelectQueryBlock sqlSelectQueryBlock = (MySqlSelectQueryBlock) statement.getSelect().getQuery();

        SchemaItem schemaItem = new SchemaItem();
        schemaItem.setSql(SQLUtils.toMySqlString(sqlSelectQueryBlock));
        SQLTableSource sqlTableSource = sqlSelectQueryBlock.getFrom();
        List<TableItem> tableItems = new ArrayList<>();
        SqlParser.visitSelectTable(schemaItem, sqlTableSource, tableItems, null);
        tableItems.forEach(tableItem -> schemaItem.getAliasTableItems().put(tableItem.getAlias(), tableItem));

        List<FieldItem> fieldItems = collectSelectQueryFields(sqlSelectQueryBlock);
        fieldItems.forEach(fieldItem -> schemaItem.getSelectFields().put(fieldItem.getFieldName(), fieldItem));

        schemaItem.init();

        if (schemaItem.getAliasTableItems().isEmpty() || schemaItem.getSelectFields().isEmpty()) {
            throw new ParserException("Parse sql error");
        }
        return schemaItem;
    } catch (Exception e) {
        throw new ParserException();
    }
}
 
Example #9
Source File: ParseHelper.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
default public MySqlSelectQueryBlock unWapperToQueryBlock(SQLStatement sqlStatement) {
    if (sqlStatement instanceof SQLSelectStatement) {
        SQLSelectQuery query = ((SQLSelectStatement) sqlStatement).getSelect().getQuery();
        if (query instanceof MySqlSelectQueryBlock) {
            return (MySqlSelectQueryBlock) query;
        }
    }
    return null;
}
 
Example #10
Source File: ConditionCollector.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void endVisit(MySqlSelectQueryBlock x) {
    QueryDataRange pop = stack.pop();
    if (stack.isEmpty()) {
        root = pop;
    } else {
        stack.peek().children.add(pop);
    }
    super.endVisit(x);
}
 
Example #11
Source File: ConditionCollector.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean visit(MySqlSelectQueryBlock x) {
    QueryDataRange queryDataRange = new QueryDataRange(x);
    if (root == null) {
        root = queryDataRange;
    }
    stack.push(queryDataRange);
    return super.visit(x);
}
 
Example #12
Source File: SqlToExprTranslator.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
public QueryPlan convertQueryRecursive(SQLObject sqlSelectQuery) {
    if (sqlSelectQuery instanceof MySqlSelectQueryBlock) {
        return convertQuery((MySqlSelectQueryBlock) sqlSelectQuery);
    } else if (sqlSelectQuery instanceof SQLUnionQuery) {
        return convertUnion((SQLUnionQuery) sqlSelectQuery);
    } else if (sqlSelectQuery instanceof SQLValuesQuery) {
        return convertValues((SQLValuesQuery) sqlSelectQuery);
    }else {
        throw new IllegalArgumentException();
    }
}
 
Example #13
Source File: SqlParser.java    From canal-1.1.3 with Apache License 2.0 5 votes vote down vote up
/**
 * 解析sql
 *
 * @param sql sql
 * @return 视图对象
 */
public static SchemaItem parse(String sql) {
    try {
        SQLStatementParser parser = new MySqlStatementParser(sql);
        SQLSelectStatement statement = (SQLSelectStatement) parser.parseStatement();
        MySqlSelectQueryBlock sqlSelectQueryBlock = (MySqlSelectQueryBlock) statement.getSelect().getQuery();

        SchemaItem schemaItem = new SchemaItem();
        schemaItem.setSql(SQLUtils.toMySqlString(sqlSelectQueryBlock));
        SQLTableSource sqlTableSource = sqlSelectQueryBlock.getFrom();
        List<TableItem> tableItems = new ArrayList<>();
        SqlParser.visitSelectTable(schemaItem, sqlTableSource, tableItems, null);
        tableItems.forEach(tableItem -> schemaItem.getAliasTableItems().put(tableItem.getAlias(), tableItem));

        List<FieldItem> fieldItems = collectSelectQueryFields(sqlSelectQueryBlock);
        fieldItems.forEach(fieldItem -> schemaItem.getSelectFields().put(fieldItem.getFieldName(), fieldItem));

        schemaItem.init();

        if (schemaItem.getAliasTableItems().isEmpty() || schemaItem.getSelectFields().isEmpty()) {
            throw new ParserException("Parse sql error");
        }
        return schemaItem;
    } catch (Exception e) {
        throw new ParserException();
    }
}
 
Example #14
Source File: PreProcesssor.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void endVisit(MySqlSelectQueryBlock x) {
    super.endVisit(x);
}
 
Example #15
Source File: PreProcesssor.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean visit(MySqlSelectQueryBlock x) {

    List<SQLSelectItem> selectList = x.getSelectList();
    SQLTableSource from = x.getFrom();
    SQLExprTableSource into = x.getInto();
    SQLExpr where = x.getWhere();
    SQLExpr startWith = x.getStartWith();
    SQLExpr connectBy = x.getConnectBy();
    SQLSelectGroupByClause groupBy = x.getGroupBy();
    SQLOrderBy orderBy = x.getOrderBy();
    List<SQLWindow> windows = x.getWindows();
    List<SQLSelectOrderByItem> distributeBy = x.getDistributeBy();
    SQLLimit limit = x.getLimit();

    if (from != null) {
        from.accept(this);
    }

    for (int i = 0; i < selectList.size(); i++) {
        SQLSelectItem item = selectList.get(i);
        if (item != null) {
            item.accept(this);
        }
    }

    if (into != null) {
        throw new UnsupportedOperationException();
    }

    if (where != null) {
        where.accept(this);
    }

    if (groupBy != null) {
        groupBy.accept(this);
    }


    if (orderBy != null) {
        orderBy.accept(this);
    }


    if (limit != null) {
        limit.accept(this);
    }

    if (from != null) {
        stack.pop();
    }

    return false;
}
 
Example #16
Source File: ParseContext.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
public MySqlSelectQueryBlock tryGetQueryBlock() {
    return unWapperToQueryBlock(getSqlStatement());
}
 
Example #17
Source File: QueryDataRange.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
public MySqlSelectQueryBlock getQueryBlock() {
    return queryBlock;
}
 
Example #18
Source File: QueryDataRange.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
public QueryDataRange(MySqlSelectQueryBlock queryBlock) {
    this.queryBlock = queryBlock;
}
 
Example #19
Source File: SqlParser.java    From canal with Apache License 2.0 4 votes vote down vote up
public static String parse4FromTableSource(MySqlSelectQueryBlock sqlSelectQueryBlock) {
    SQLTableSource sqlTableSource = sqlSelectQueryBlock.getFrom();
    return SQLUtils.toMySqlString(sqlTableSource);
}