use of com.actiontech.dble.route.RouteResultset in project dble by actiontech.
the class AbstractRouteStrategy method route.
@Override
public RouteResultset route(SchemaConfig schema, int sqlType, String origSQL, ServerConnection sc, LayerCachePool cachePool) throws SQLException {
RouteResultset rrs = new RouteResultset(origSQL, sqlType);
/*
* debug mode and load data ,no cache
*/
if (LOGGER.isDebugEnabled() && origSQL.startsWith(LoadData.LOAD_DATA_HINT)) {
rrs.setCacheAble(false);
}
if (sqlType == ServerParse.CALL) {
rrs.setCallStatement(true);
Procedure procedure = parseProcedure(origSQL);
rrs.setProcedure(procedure);
}
if (schema == null) {
rrs = routeNormalSqlWithAST(null, origSQL, rrs, cachePool, sc);
} else {
if (sqlType == ServerParse.SHOW) {
rrs.setStatement(origSQL);
rrs = RouterUtil.routeToSingleNode(rrs, schema.getRandomDataNode());
} else {
rrs = routeNormalSqlWithAST(schema, origSQL, rrs, cachePool, sc);
}
}
return rrs;
}
use of com.actiontech.dble.route.RouteResultset in project dble by actiontech.
the class DruidUpdateParserTest method throwExceptionParse.
public void throwExceptionParse(String sql, boolean throwException) throws NoSuchMethodException {
MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement sqlStatement = statementList.get(0);
MySqlUpdateStatement update = (MySqlUpdateStatement) sqlStatement;
SchemaConfig schemaConfig = mock(SchemaConfig.class);
Map<String, TableConfig> tables = mock(Map.class);
TableConfig tableConfig = mock(TableConfig.class);
String tableName = "hotnews";
when((schemaConfig).getTables()).thenReturn(tables);
when(tables.get(tableName)).thenReturn(tableConfig);
when(tableConfig.getParentTC()).thenReturn(null);
RouteResultset routeResultset = new RouteResultset(sql, 11);
Class c = DruidUpdateParser.class;
Method method = c.getDeclaredMethod("confirmShardColumnNotUpdated", new Class[] { SQLUpdateStatement.class, SchemaConfig.class, String.class, String.class, String.class, RouteResultset.class });
method.setAccessible(true);
try {
method.invoke(c.newInstance(), update, schemaConfig, tableName, "ID", "", routeResultset);
if (throwException) {
System.out.println("Not passed without exception is not correct");
Assert.assertTrue(false);
} else {
System.out.println("Passed without exception. Maybe the partition key exists in update statement,but not update in fact");
Assert.assertTrue(true);
}
} catch (Exception e) {
if (throwException) {
System.out.println(e.getCause().getClass());
Assert.assertTrue(e.getCause() instanceof SQLNonTransientException);
System.out.println("SQLNonTransientException is expected");
} else {
System.out.println("need checked");
Assert.assertTrue(false);
}
}
}
use of com.actiontech.dble.route.RouteResultset in project dble by actiontech.
the class RouteResultCopy method rrCopy.
public static RouteResultset rrCopy(RouteResultset rrs, int sqlType, String stmt) {
RouteResultset rr = new RouteResultset(stmt, sqlType);
rr.setRunOnSlave(rrs.getRunOnSlave());
rr.setFinishedRoute(rrs.isFinishedRoute());
rr.setGlobalTable(rrs.isGlobalTable());
rr.setCanRunInReadDB(false);
RouteResultsetNode[] ns = rrs.getNodes();
RouteResultsetNode[] nodes = new RouteResultsetNode[ns.length];
for (int i = 0; i < ns.length; i++) {
nodes[i] = rrnCopy(ns[i], sqlType, stmt);
}
rr.setNodes(nodes);
return rr;
}
use of com.actiontech.dble.route.RouteResultset in project dble by actiontech.
the class RouterUtil method routeFromParser.
public static RouteResultset routeFromParser(DruidParser druidParser, SchemaConfig schema, RouteResultset rrs, SQLStatement statement, String originSql, LayerCachePool cachePool, ServerSchemaStatVisitor visitor, ServerConnection sc) throws SQLException {
schema = druidParser.parser(schema, rrs, statement, originSql, cachePool, visitor, sc);
if (rrs.isFinishedExecute()) {
return null;
}
if (rrs.isFinishedRoute()) {
return rrs;
}
/**
* no name table or others
*/
DruidShardingParseInfo ctx = druidParser.getCtx();
if ((ctx.getTables() == null || ctx.getTables().size() == 0) && (ctx.getTableAliasMap() == null || ctx.getTableAliasMap().isEmpty())) {
if (schema == null) {
schema = DbleServer.getInstance().getConfig().getSchemas().get(SchemaUtil.getRandomDb());
}
return RouterUtil.routeToSingleNode(rrs, schema.getRandomDataNode());
}
/* multi-tables*/
if (druidParser.getCtx().getRouteCalculateUnits().size() == 0) {
RouteCalculateUnit routeCalculateUnit = new RouteCalculateUnit();
druidParser.getCtx().addRouteCalculateUnit(routeCalculateUnit);
}
SortedSet<RouteResultsetNode> nodeSet = new TreeSet<>();
for (RouteCalculateUnit unit : druidParser.getCtx().getRouteCalculateUnits()) {
RouteResultset rrsTmp = RouterUtil.tryRouteForTables(schema, druidParser.getCtx(), unit, rrs, isSelect(statement), cachePool);
if (rrsTmp != null && rrsTmp.getNodes() != null) {
Collections.addAll(nodeSet, rrsTmp.getNodes());
if (rrsTmp.isGlobalTable()) {
break;
}
}
}
RouteResultsetNode[] nodes = new RouteResultsetNode[nodeSet.size()];
int i = 0;
for (RouteResultsetNode aNodeSet : nodeSet) {
nodes[i] = aNodeSet;
i++;
}
rrs.setNodes(nodes);
return rrs;
}
use of com.actiontech.dble.route.RouteResultset in project dble by actiontech.
the class Explain2Handler method handle.
public static void handle(String stmt, ServerConnection c, int offset) {
try {
stmt = stmt.substring(offset);
if (!stmt.toLowerCase().contains("datanode=") || !stmt.toLowerCase().contains("sql=")) {
showError(stmt, c, "explain2 datanode=? sql=?");
return;
}
String dataNode = stmt.substring(stmt.indexOf("=") + 1, stmt.indexOf("sql=")).trim();
String sql = "explain " + stmt.substring(stmt.indexOf("sql=") + 4, stmt.length()).trim();
if (dataNode == null || dataNode.isEmpty() || sql == null || sql.isEmpty()) {
showError(stmt, c, "dataNode or sql is null or empty");
return;
}
RouteResultsetNode node = new RouteResultsetNode(dataNode, ServerParse.SELECT, sql);
RouteResultset rrs = new RouteResultset(sql, ServerParse.SELECT);
EMPTY_ARRAY[0] = node;
rrs.setNodes(EMPTY_ARRAY);
SingleNodeHandler singleNodeHandler = new SingleNodeHandler(rrs, c.getSession2());
singleNodeHandler.execute();
} catch (Exception e) {
LOGGER.info(e.getMessage(), e.getCause());
showError(stmt, c, e.getMessage());
}
}
Aggregations