Java Code Examples for com.alibaba.fastsql.sql.ast.statement.SQLSelectStatement

The following examples show how to use com.alibaba.fastsql.sql.ast.statement.SQLSelectStatement. 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: 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 2
Source Project: Mycat2   Source File: MycatSqlUtil.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static String getCalciteSQL(SQLStatement sqlStatement) {
    SQLSelectQueryBlock queryBlock = ((SQLSelectStatement) sqlStatement).getSelect().getQueryBlock();
    MycatCalciteMySqlNodeVisitor calciteMySqlNodeVisitor = new MycatCalciteMySqlNodeVisitor();
    sqlStatement.accept(calciteMySqlNodeVisitor);
    SqlNode sqlNode = calciteMySqlNodeVisitor.getSqlNode();
    return sqlNode.toSqlString(MysqlSqlDialect.DEFAULT).getSql();
}
 
Example 3
Source Project: Mycat2   Source File: ValuesPlanTest.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Test
public void test4(){
    MyRelBuilder builder = new MyRelBuilder();
    SqlToExprTranslator sqlToExprTranslator = new SqlToExprTranslator(builder);
    SQLSelectStatement sqlSelectStatement = (SQLSelectStatement)SQLUtils.parseSingleMysqlStatement("select 1");
    SQLSelectQuery query = sqlSelectStatement.getSelect().getQuery();
    QueryPlan queryPlan1 = sqlToExprTranslator.convertQueryRecursive(query);
    Scanner scan = queryPlan1.scan(dataContext, 0);
    RowType type = queryPlan1.getType();
    String collect = scan.stream().map(i -> i.toString()).collect(Collectors.joining());
}
 
Example 4
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 5
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 6
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 7
Source Project: Mycat2   Source File: Response.java    License: GNU General Public License v3.0 4 votes vote down vote up
default void proxySelect(String defaultTargetName, SQLSelectStatement statement) {
    proxySelect(defaultTargetName, statement.toString());
}
 
Example 8
Source Project: Mycat2   Source File: RexTranslator.java    License: GNU General Public License v3.0 4 votes vote down vote up
@Override
public void endVisit(SQLSelectStatement node) {
    result = convertScalarSubQuery(node.getSelect().getQuery());
}