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

The following examples show how to use com.alibaba.fastsql.sql.ast.SQLStatement#toString() . 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: 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 2
Source File: UpdateSQLHandler.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
public static void updateHandler(SQLStatement sql, MycatDataContext dataContext, SQLExprTableSource tableSource, Response receiver) {
    MycatDBClientMediator mycatDBClientMediator = MycatDBs.createClient(dataContext);
    String schemaName = Optional.ofNullable(tableSource.getSchema() == null ? dataContext.getDefaultSchema() : tableSource.getSchema())
            .map(i-> SQLUtils.normalize(i)).orElse(null);
    String tableName = SQLUtils.normalize(tableSource.getTableName());
    SchemaHandler schemaHandler;
    Optional<Map<String, SchemaHandler>> handlerMapOptional = Optional.ofNullable(mycatDBClientMediator)
            .map(i -> i.config())
            .map(i -> i.getSchemaMap());
    Optional<String> targetNameOptional = Optional.ofNullable(RootHelper.INSTANCE)
            .map(i -> i.getConfigProvider())
            .map(i -> i.currentConfig())
            .map(i -> i.getMetadata())
            .map(i -> i.getPrototype())
            .map(i -> i.getTargetName());
    if (!handlerMapOptional.isPresent()) {
        if (targetNameOptional.isPresent()) {
            receiver.proxyUpdate(targetNameOptional.get(), Objects.toString(sql));
            return;
        } else {
            receiver.sendError(new MycatException("Unable to route:" + sql));
            return;
        }
    } else {
        Map<String, SchemaHandler> handlerMap = handlerMapOptional.get();
        schemaHandler = Optional.ofNullable(handlerMap.get(schemaName))
                .orElseGet(() -> {
                    if (mycatDBClientMediator.getSchema() == null) {
                        throw new MycatException("unknown schema:"+schemaName);//可能schemaName有值,但是值名不是配置的名字
                    }
                    return handlerMap.get(mycatDBClientMediator.getSchema());
                });
        if (schemaHandler == null) {
            receiver.sendError(new MycatException("Unable to route:" + sql));
            return;
        }
    }
    String defaultTargetName = schemaHandler.defaultTargetName();
    Map<String, TableHandler> tableMap = schemaHandler.logicTables();
    TableHandler tableHandler = tableMap.get(tableName);
    ///////////////////////////////common///////////////////////////////
    if (tableHandler == null) {
        receiver.proxyUpdate(defaultTargetName, sql.toString());
        return;
    }
    String string = sql.toString();
    if (sql instanceof MySqlInsertStatement) {
        switch (tableHandler.getType()) {
            case SHARDING:
                receiver.multiInsert(string, tableHandler.insertHandler().apply(new ParseContext(sql.toString())));
                break;
            case GLOBAL:
                receiver.multiGlobalInsert(string, tableHandler.insertHandler().apply(new ParseContext(sql.toString())));
                break;
        }

    } else if (sql instanceof com.alibaba.fastsql.sql.dialect.mysql.ast.statement.MySqlDeleteStatement) {
        switch (tableHandler.getType()) {
            case SHARDING:
                receiver.multiUpdate(string, tableHandler.deleteHandler().apply(new ParseContext(sql.toString())));
                break;
            case GLOBAL:
                receiver.multiGlobalUpdate(string, tableHandler.deleteHandler().apply(new ParseContext(sql.toString())));
                break;
        }

    } else if (sql instanceof com.alibaba.fastsql.sql.dialect.mysql.ast.statement.MySqlUpdateStatement) {
        switch (tableHandler.getType()) {
            case SHARDING:
                receiver.multiUpdate(string, tableHandler.updateHandler().apply(new ParseContext(sql.toString())));
                break;
            case GLOBAL:
                receiver.multiGlobalUpdate(string, tableHandler.deleteHandler().apply(new ParseContext(sql.toString())));
                break;
        }
    } else {
        throw new UnsupportedOperationException("unsupported statement:" + sql);
    }

}