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

The following examples show how to use com.alibaba.druid.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: RouterUtil.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
private  static String changeCreateTable(SchemaConfig schema,String tableName,String sql) {
	if (schema.getTables().containsKey(tableName)) {
		MySqlStatementParser parser = new MySqlStatementParser(sql);
		SQLStatement insertStatement = parser.parseStatement();
		if (insertStatement instanceof MySqlCreateTableStatement) {
			TableConfig tableConfig = schema.getTables().get(tableName);
			AbstractPartitionAlgorithm algorithm = tableConfig.getRule().getRuleAlgorithm();
			if (algorithm instanceof SlotFunction) {
				SQLColumnDefinition column = new SQLColumnDefinition();
				column.setDataType(new SQLCharacterDataType("int"));
				column.setName(new SQLIdentifierExpr("_slot"));
				column.setComment(new SQLCharExpr("自动迁移算法slot,禁止修改"));
				((SQLCreateTableStatement) insertStatement).getTableElementList().add(column);
				return insertStatement.toString();

			}
		}

	}
	return sql;
}
 
Example 2
Source File: DefaultRouteStrategy.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
@Override
public RouteResultset routeNormalSqlWithAST(SchemaConfig schema,
                                            String originSql, RouteResultset rrs,
                                            LayerCachePool cachePool, ServerConnection sc, boolean isExplain) throws SQLException {
    SQLStatement statement = parserSQL(originSql, sc);
    if (sc.getSession2().getIsMultiStatement().get()) {
        originSql = statement.toString();
        rrs.setStatement(originSql);
        rrs.setSrcStatement(originSql);
    }
    sc.getSession2().endParse();
    DruidParser druidParser = DruidParserFactory.create(statement, rrs.getSqlType());
    return RouterUtil.routeFromParser(druidParser, schema, rrs, statement, new ServerSchemaStatVisitor(), sc, isExplain);

}
 
Example 3
Source File: SqlToCountSqlRewrite.java    From Zebra with Apache License 2.0 5 votes vote down vote up
public String rewrite(String sql, List<ParamContext> countParams) {
	MySqlStatementParser parser = new MySqlStatementParser(sql);
	SQLStatement stmt = parser.parseStatement();
	RewriteSqlToCountSqlVisitor visitor = new RewriteSqlToCountSqlVisitor(countParams);
	stmt.accept(visitor);

	return stmt.toString();
}
 
Example 4
Source File: DruidMysqlRouteStrategyTest.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
private String formatSql(String sql) {
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    SQLStatement stmt = parser.parseStatement();
    return stmt.toString();
}
 
Example 5
Source File: DruidMysqlRouteStrategyTest.java    From dble with GNU General Public License v2.0 4 votes vote down vote up
private String formatSql(String sql) {
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    SQLStatement stmt = parser.parseStatement();
    return stmt.toString();
}