use of io.mycat.calcite.spm.Plan in project Mycat2 by MyCATApache.
the class UpdateSQLHandler method updateHandler.
@SneakyThrows
public static Future<Void> updateHandler(SQLStatement sqlStatement, MycatDataContext dataContext, SQLTableSource tableSourceArg, Response receiver) {
boolean insert = sqlStatement instanceof SQLInsertStatement;
if (tableSourceArg instanceof SQLExprTableSource) {
SQLExprTableSource tableSource = (SQLExprTableSource) tableSourceArg;
String schemaName = Optional.ofNullable(tableSource.getSchema() == null ? dataContext.getDefaultSchema() : tableSource.getSchema()).map(i -> SQLUtils.normalize(i)).orElse(null);
String tableName = SQLUtils.normalize(tableSource.getTableName());
if (schemaName == null) {
return receiver.sendError("unknown schema", MySQLErrorCode.ER_UNKNOWN_ERROR);
}
tableSource.setSchema(schemaName);
SchemaHandler schemaHandler;
MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
Optional<NameMap<SchemaHandler>> handlerMapOptional = Optional.ofNullable(metadataManager.getSchemaMap());
Optional<String> targetNameOptional = Optional.ofNullable(metadataManager.getPrototype());
if (!handlerMapOptional.isPresent()) {
if (targetNameOptional.isPresent()) {
if (insert) {
DrdsSqlWithParams drdsSqlWithParams = MycatPreparedStatementUtil.outputToParameterizedProxySql((MySqlInsertStatement) sqlStatement);
return receiver.proxyInsert(Collections.singletonList(targetNameOptional.get()), drdsSqlWithParams.getParameterizedSQL(), drdsSqlWithParams.getParams());
} else {
DrdsSqlWithParams drdsSqlWithParams = DrdsRunnerHelper.preParse(sqlStatement, dataContext.getDefaultSchema());
return receiver.proxyUpdate(Collections.singletonList(targetNameOptional.get()), drdsSqlWithParams.getParameterizedSQL(), drdsSqlWithParams.getParams());
}
} else {
return receiver.sendError(new MycatException("Unable to route:" + sqlStatement));
}
} else {
NameMap<SchemaHandler> handlerMap = handlerMapOptional.get();
schemaHandler = Optional.ofNullable(handlerMap.get(schemaName)).orElseGet(() -> {
if (dataContext.getDefaultSchema() == null) {
// 可能schemaName有值,但是值名不是配置的名字
throw new MycatException("unknown schema:" + schemaName);
}
return handlerMap.get(dataContext.getDefaultSchema());
});
if (schemaHandler == null) {
return receiver.sendError(new MycatException("Unable to route:" + sqlStatement));
}
}
String defaultTargetName = schemaHandler.defaultTargetName();
NameMap<TableHandler> tableMap = schemaHandler.logicTables();
TableHandler tableHandler = tableMap.get(tableName);
// /////////////////////////////common///////////////////////////////
if (tableHandler == null) {
if (insert) {
DrdsSqlWithParams drdsSqlWithParams = MycatPreparedStatementUtil.outputToParameterizedProxySql((MySqlInsertStatement) sqlStatement);
return receiver.proxyInsert(Collections.singletonList(Objects.requireNonNull(defaultTargetName, "can not route :" + drdsSqlWithParams)), drdsSqlWithParams.getParameterizedSQL(), drdsSqlWithParams.getParams());
} else {
DrdsSqlWithParams drdsSqlWithParams = DrdsRunnerHelper.preParse(sqlStatement, null);
return receiver.proxyUpdate(Collections.singletonList(Objects.requireNonNull(defaultTargetName, "can not route :" + drdsSqlWithParams)), drdsSqlWithParams.getParameterizedSQL(), drdsSqlWithParams.getParams());
}
}
switch(tableHandler.getType()) {
case NORMAL:
DrdsSqlWithParams drdsSqlWithParams;
if (insert) {
drdsSqlWithParams = MycatPreparedStatementUtil.outputToParameterizedProxySql((MySqlInsertStatement) sqlStatement);
} else {
drdsSqlWithParams = DrdsRunnerHelper.preParse(sqlStatement, dataContext.getDefaultSchema());
}
HackRouter hackRouter = new HackRouter(drdsSqlWithParams.getParameterizedStatement(), dataContext);
if (hackRouter.analyse()) {
Pair<String, String> plan = hackRouter.getPlan();
if (insert) {
return receiver.proxyInsert(Collections.singletonList(plan.getKey()), plan.getValue(), drdsSqlWithParams.getParams());
} else {
return receiver.proxyUpdate(Collections.singletonList(plan.getKey()), plan.getValue(), drdsSqlWithParams.getParams());
}
}
break;
default:
break;
}
DrdsSqlWithParams drdsSqlWithParams = DrdsRunnerHelper.preParse(sqlStatement, dataContext.getDefaultSchema());
return executeUpdate(drdsSqlWithParams, dataContext, receiver, schemaName);
}
DrdsSqlWithParams drdsSqlWithParams = insert ? MycatPreparedStatementUtil.outputToParameterizedProxySql((MySqlInsertStatement) sqlStatement) : DrdsRunnerHelper.preParse(sqlStatement, dataContext.getDefaultSchema());
HackRouter hackRouter = new HackRouter(drdsSqlWithParams.getParameterizedStatement(), dataContext);
if (hackRouter.analyse()) {
Pair<String, String> plan = hackRouter.getPlan();
if (insert) {
return receiver.proxyInsert(Collections.singletonList(plan.getKey()), plan.getValue(), drdsSqlWithParams.getParams());
} else {
return receiver.proxyUpdate(Collections.singletonList(plan.getKey()), plan.getValue(), drdsSqlWithParams.getParams());
}
}
return receiver.sendError(new MycatException("can not route " + sqlStatement));
}
use of io.mycat.calcite.spm.Plan in project Mycat2 by MyCATApache.
the class ShardingJoinTest method parse.
public static Explain parse(String sql) {
DrdsSqlCompiler drds = getDrds();
DrdsSqlWithParams drdsSqlWithParams = DrdsRunnerHelper.preParse(sql, null);
OptimizationContext optimizationContext = new OptimizationContext();
MycatRel dispatch = drds.dispatch(optimizationContext, drdsSqlWithParams);
Plan plan = new PlanImpl(dispatch, DrdsExecutorCompiler.getCodeExecuterContext(optimizationContext.relNodeContext.getConstantMap(), dispatch, false), drdsSqlWithParams.getAliasList());
return new Explain(plan, drdsSqlWithParams);
}
use of io.mycat.calcite.spm.Plan in project Mycat2 by MyCATApache.
the class UnionAllTest method parse.
public static Explain parse(String sql) {
DrdsSqlCompiler drds = getDrds();
DrdsSqlWithParams drdsSqlWithParams = DrdsRunnerHelper.preParse(sql, null);
OptimizationContext optimizationContext = new OptimizationContext();
MycatRel dispatch = drds.dispatch(optimizationContext, drdsSqlWithParams);
Plan plan = new PlanImpl(dispatch, DrdsExecutorCompiler.getCodeExecuterContext(optimizationContext.relNodeContext.getConstantMap(), dispatch, false), drdsSqlWithParams.getAliasList());
return new Explain(plan, drdsSqlWithParams);
}
use of io.mycat.calcite.spm.Plan in project Mycat2 by MyCATApache.
the class AllShardingJoinTest method parse.
public static Explain parse(String sql) {
DrdsSqlCompiler drds = getDrds();
DrdsSqlWithParams drdsSqlWithParams = DrdsRunnerHelper.preParse(sql, null);
OptimizationContext optimizationContext = new OptimizationContext();
MycatRel dispatch = drds.dispatch(optimizationContext, drdsSqlWithParams);
Plan plan = new PlanImpl(dispatch, DrdsExecutorCompiler.getCodeExecuterContext(optimizationContext.relNodeContext.getConstantMap(), dispatch, false), drdsSqlWithParams.getAliasList());
return new Explain(plan, drdsSqlWithParams);
}
use of io.mycat.calcite.spm.Plan in project Mycat2 by MyCATApache.
the class BkaJoinTest method parse.
public static Explain parse(String sql) {
DrdsSqlCompiler drds = getDrds();
DrdsSqlWithParams drdsSqlWithParams = DrdsRunnerHelper.preParse("/*+ mycat:use_bka_join() */" + sql, null);
OptimizationContext optimizationContext = new OptimizationContext();
MycatRel dispatch = drds.dispatch(optimizationContext, drdsSqlWithParams);
Plan plan = new PlanImpl(dispatch, DrdsExecutorCompiler.getCodeExecuterContext(optimizationContext.relNodeContext.getConstantMap(), dispatch, false), drdsSqlWithParams.getAliasList());
return new Explain(plan, drdsSqlWithParams);
}
Aggregations