use of io.mycat.calcite.table.SchemaHandler in project Mycat2 by MyCATApache.
the class StatisticCenterTest method init.
@BeforeClass
public static void init() throws Exception {
HashMap<Class, Object> context = new HashMap<>();
context.put(Vertx.class, Vertx.vertx());
context.put(ServerConfig.class, new ServerConfig());
context.put(DrdsSqlCompiler.class, new DrdsSqlCompiler(new DrdsConst() {
@Override
public NameMap<SchemaHandler> schemas() {
return new NameMap<>();
}
}));
MetaClusterCurrent.register(context);
String customerDatasourceProvider = DruidDatasourceProvider.class.getName();
DatasourceConfig datasourceConfig = new DatasourceConfig();
datasourceConfig.setDbType("mysql");
datasourceConfig.setUser("root");
datasourceConfig.setPassword("123456");
datasourceConfig.setName("prototypeDs");
datasourceConfig.setUrl("jdbc:mysql://localhost:3306/mysql");
Map<String, DatasourceConfig> datasources = Maps.of("prototypeDs", datasourceConfig);
ClusterConfig clusterConfig = new ClusterConfig();
clusterConfig.setName("prototype");
clusterConfig.setMasters(Arrays.asList("prototypeDs"));
Map<String, ClusterConfig> clusterConfigs = Maps.of("prototype", clusterConfig);
LinkedList<Runnable> runnables = new LinkedList<>();
ReplicaSelectorManager manager = ReplicaSelectorRuntime.create(new ArrayList<>(clusterConfigs.values()), datasources, new LoadBalanceManager(), name -> 0, (command, initialDelay, period, unit) -> {
runnables.add(command);
return () -> {
};
});
context.put(ReplicaSelectorManager.class, manager);
context.put(JdbcConnectionManager.class, jdbcManager = new JdbcConnectionManager(DruidDatasourceProvider.class.getName(), datasources));
MetaClusterCurrent.register(context);
statisticCenter.init();
}
use of io.mycat.calcite.table.SchemaHandler in project Mycat2 by MyCATApache.
the class SpmTest method init.
@BeforeClass
public static void init() throws Exception {
HashMap<Class, Object> context = new HashMap<>();
context.put(Vertx.class, Vertx.vertx());
context.put(ServerConfig.class, new ServerConfig());
context.put(IOExecutor.class, IOExecutor.DEFAULT);
context.put(DrdsSqlCompiler.class, new DrdsSqlCompiler(new DrdsConst() {
@Override
public NameMap<SchemaHandler> schemas() {
return new NameMap<>();
}
}));
MetaClusterCurrent.register(context);
String customerDatasourceProvider = DruidDatasourceProvider.class.getName();
DatasourceConfig datasourceConfig = new DatasourceConfig();
datasourceConfig.setDbType("mysql");
datasourceConfig.setUser("root");
datasourceConfig.setPassword("123456");
datasourceConfig.setName("prototypeDs");
datasourceConfig.setUrl("jdbc:mysql://localhost:3306/mysql");
Map<String, DatasourceConfig> datasources = Maps.of("prototypeDs", datasourceConfig);
ClusterConfig clusterConfig = new ClusterConfig();
clusterConfig.setName("prototype");
clusterConfig.setMasters(Arrays.asList("prototypeDs"));
Map<String, ClusterConfig> clusterConfigs = Maps.of("prototype", clusterConfig);
LinkedList<Runnable> runnables = new LinkedList<>();
ReplicaSelectorManager manager = ReplicaSelectorRuntime.create(new ArrayList<>(clusterConfigs.values()), datasources, new LoadBalanceManager(), name -> 0, (command, initialDelay, period, unit) -> {
runnables.add(command);
return () -> {
};
});
context.put(ReplicaSelectorManager.class, manager);
context.put(JdbcConnectionManager.class, jdbcManager = new JdbcConnectionManager(DruidDatasourceProvider.class.getName(), datasources));
MetaClusterCurrent.register(context);
}
use of io.mycat.calcite.table.SchemaHandler in project Mycat2 by MyCATApache.
the class HintHandler method showTables.
@Nullable
private Future<Void> showTables(Response response, String body, MetadataManager metadataManager, MycatRouterConfig routerConfig) {
Map map = JsonUtil.from(body, Map.class);
String type = (String) map.get("type");
String schemaName = (String) map.get("schemaName");
Stream<TableHandler> tables;
Stream<TableHandler> tableHandlerStream;
if (schemaName == null) {
tableHandlerStream = metadataManager.getSchemaMap().values().stream().flatMap(i -> i.logicTables().values().stream());
} else {
SchemaHandler schemaHandler = Objects.requireNonNull(metadataManager.getSchemaMap().get(schemaName));
NameMap<TableHandler> logicTables = schemaHandler.logicTables();
tableHandlerStream = logicTables.values().stream();
}
if ("global".equalsIgnoreCase(type)) {
tables = tableHandlerStream.filter(i -> i.getType() == LogicTableType.GLOBAL);
} else if ("sharding".equalsIgnoreCase(type)) {
tables = tableHandlerStream.filter(i -> i.getType() == LogicTableType.SHARDING);
} else if ("normal".equalsIgnoreCase(type)) {
tables = tableHandlerStream.filter(i -> i.getType() == LogicTableType.NORMAL);
} else if ("custom".equalsIgnoreCase(type)) {
tables = tableHandlerStream.filter(i -> i.getType() == LogicTableType.CUSTOM);
} else {
tables = tableHandlerStream;
}
ResultSetBuilder builder = ResultSetBuilder.create();
builder.addColumnInfo("SCHEMA_NAME", JDBCType.VARCHAR).addColumnInfo("TABLE_NAME", JDBCType.VARCHAR).addColumnInfo("CREATE_TABLE_SQL", JDBCType.VARCHAR).addColumnInfo("TYPE", JDBCType.VARCHAR).addColumnInfo("COLUMNS", JDBCType.VARCHAR).addColumnInfo("CONFIG", JDBCType.VARCHAR);
tables.forEach(table -> {
String SCHEMA_NAME = table.getSchemaName();
String TABLE_NAME = table.getTableName();
String CREATE_TABLE_SQL = table.getCreateTableSQL();
LogicTableType TYPE = table.getType();
String COLUMNS = table.getColumns().stream().map(i -> i.toString()).collect(Collectors.joining(","));
String CONFIG = routerConfig.getSchemas().stream().filter(i -> SCHEMA_NAME.equalsIgnoreCase(i.getSchemaName())).map(i -> {
switch(TYPE) {
case SHARDING:
return NameMap.immutableCopyOf(i.getShardingTables());
case GLOBAL:
return NameMap.immutableCopyOf(i.getGlobalTables());
case NORMAL:
return NameMap.immutableCopyOf(i.getNormalTables());
case CUSTOM:
return NameMap.immutableCopyOf(i.getCustomTables());
default:
return null;
}
}).map(i -> i.get(TABLE_NAME)).findFirst().map(i -> i.toString()).orElse(null);
builder.addObjectRowPayload(Arrays.asList(SCHEMA_NAME, TABLE_NAME, CREATE_TABLE_SQL, TYPE, COLUMNS, CONFIG));
});
return response.sendResultSet(() -> builder.build());
}
use of io.mycat.calcite.table.SchemaHandler 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));
}
Aggregations