Java Code Examples for com.alibaba.fastsql.sql.ast.SQLStatement#accept()

The following examples show how to use com.alibaba.fastsql.sql.ast.SQLStatement#accept() . 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: MycatDBSharedServerImpl.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
@NotNull
private MycatSQLPrepareObject complieQuery(String sql, Long id, SQLStatement
        sqlStatement, MycatDBContext dataContext) {

    SQLSelect select = ((SQLSelectStatement) sqlStatement).getSelect();

    SQLSelectQuery query = select.getQuery();
    SqlNode sqlNode;
    boolean forUpdate = false;
    if (query instanceof SQLSelectQueryBlock) {
        SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) query;
        forUpdate = queryBlock.isForUpdate();
    }
    MycatCalciteMySqlNodeVisitor calciteMySqlNodeVisitor = new MycatCalciteMySqlNodeVisitor();
    sqlStatement.accept(calciteMySqlNodeVisitor);
    sqlNode = calciteMySqlNodeVisitor.getSqlNode();
    return new FastMycatCalciteSQLPrepareObject(id, sql, sqlNode, null, null, forUpdate, dataContext);

}
 
Example 2
Source File: MycatDBSharedServerImpl.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
@NotNull
private MycatSQLPrepareObject getMycatPrepareObject(
        MycatDBContext uponDBContext,
        String templateSql,
        Long id,
        SQLStatement sqlStatement,
        int variantRefCount,
        Function<ParseContext, Iterator<TextUpdateInfo>> accept) {
    return new MycatDelegateSQLPrepareObject(id, uponDBContext, templateSql, new MycatTextUpdatePrepareObject(id, variantRefCount, (prepareObject, params) -> {
        StringBuilder out = new StringBuilder();
        SQLASTOutputVisitor visitor = SQLUtils.createOutputVisitor(out, DbType.mysql);
        visitor.setInputParameters(params);
        sqlStatement.accept(visitor);
        String sql = out.toString();
        ParseContext parseContext = new ParseContext();
        parseContext.setSql(sql);
        return accept.apply(parseContext);
    }, uponDBContext));

}
 
Example 3
Source File: MycatSqlUtil.java    From Mycat2 with 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 4
Source File: PreProcesssorTest.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
private String process(String defaultSchema, String sql) {
    SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement(sql);
    Scope scope = new Scope(defaultSchema);

    sqlStatement.accept(scope);
    scope.build();
    PreProcesssor preProcesssor = new PreProcesssor(defaultSchema);
    sqlStatement.accept(preProcesssor);
    return sqlStatement.toString();
}
 
Example 5
Source File: TableCollector.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
public static Map<String, Collection<String>> collect(String defaultSchema, String sql) {
    Map<String, Collection<String>> collectionMap = new HashMap<>();
    try {
        SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement(sql);
        sqlStatement.accept(new MySqlASTVisitorAdapter() {
            @Override
            public boolean visit(SQLExprTableSource x) {
                String schema = x.getSchema();
                String tableName = x.getTableName();
                if (schema == null) {
                    schema = defaultSchema;
                }
                if (schema == null) {
                    throw new UnsupportedOperationException("please use schema");
                }
                schema = SQLUtils.normalize(schema);
                tableName = SQLUtils.normalize(tableName);
                Collection<String> strings = collectionMap.computeIfAbsent(schema, s -> new HashSet<>());
                strings.add(tableName);
                return super.visit(x);
            }
        });
    } catch (Throwable ignored) {

    }
    return collectionMap;
}
 
Example 6
Source File: MycatDBSharedServerImpl.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
@NotNull
private int getVariantRefCount(SQLStatement sqlStatement) {
    SQLVariantRefExprCounter sqlVariantRefExprCounter = new SQLVariantRefExprCounter();
    sqlStatement.accept(sqlVariantRefExprCounter);
    return sqlVariantRefExprCounter.getCount();
}