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

The following examples show how to use com.alibaba.fastsql.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock. These examples are extracted from open source projects. 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 Project: Mycat2   Source File: SqlToExprTranslator.java    License: 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 Project: canal-1.1.3   Source File: SqlParser.java    License: 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 3
Source Project: canal-1.1.3   Source File: SqlParser.java    License: 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 4
Source Project: Mycat2   Source File: SqlToExprTranslator.java    License: 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 5
Source Project: Mycat2   Source File: ConditionCollector.java    License: 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 6
Source Project: Mycat2   Source File: ConditionCollector.java    License: 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 7
Source Project: Mycat2   Source File: ParseHelper.java    License: 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 8
Source Project: canal   Source File: SqlParser.java    License: 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 Project: canal   Source File: SqlParser.java    License: 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 10
Source Project: canal   Source File: SqlParser.java    License: 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 11
Source Project: canal   Source File: SqlParser.java    License: 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 12
Source Project: canal   Source File: SqlParser.java    License: 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 13
Source Project: canal   Source File: SqlParser.java    License: 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 14
Source Project: Mycat2   Source File: QueryDataRange.java    License: GNU General Public License v3.0 4 votes vote down vote up
public QueryDataRange(MySqlSelectQueryBlock queryBlock) {
    this.queryBlock = queryBlock;
}
 
Example 15
Source Project: Mycat2   Source File: QueryDataRange.java    License: GNU General Public License v3.0 4 votes vote down vote up
public MySqlSelectQueryBlock getQueryBlock() {
    return queryBlock;
}
 
Example 16
Source Project: Mycat2   Source File: PreProcesssor.java    License: 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 17
Source Project: Mycat2   Source File: PreProcesssor.java    License: GNU General Public License v3.0 4 votes vote down vote up
@Override
public void endVisit(MySqlSelectQueryBlock x) {
    super.endVisit(x);
}
 
Example 18
Source Project: Mycat2   Source File: ParseContext.java    License: GNU General Public License v3.0 4 votes vote down vote up
public MySqlSelectQueryBlock tryGetQueryBlock() {
    return unWapperToQueryBlock(getSqlStatement());
}
 
Example 19
Source Project: canal   Source File: SqlParser.java    License: Apache License 2.0 4 votes vote down vote up
public static String parse4FromTableSource(MySqlSelectQueryBlock sqlSelectQueryBlock) {
    SQLTableSource sqlTableSource = sqlSelectQueryBlock.getFrom();
    return SQLUtils.toMySqlString(sqlTableSource);
}