use of io.mycat.datasource.jdbc.datasource.DefaultConnection in project Mycat2 by MyCATApache.
the class StatisticCenter method fetchRowCount.
private Double fetchRowCount(String targetName, String sql) {
try {
ReplicaSelectorManager runtime = MetaClusterCurrent.wrapper(ReplicaSelectorManager.class);
targetName = runtime.getDatasourceNameByReplicaName(targetName, false, null);
JdbcConnectionManager jdbcConnectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
try (DefaultConnection connection = jdbcConnectionManager.getConnection(targetName)) {
try (RowBaseIterator rowBaseIterator = connection.executeQuery(sql)) {
rowBaseIterator.next();
return rowBaseIterator.getBigDecimal(1).doubleValue();
}
}
} catch (Throwable e) {
LOGGER.error("不能获取行统计 " + targetName + " " + sql, e);
return null;
}
}
use of io.mycat.datasource.jdbc.datasource.DefaultConnection in project Mycat2 by MyCATApache.
the class StatisticCenter method updateRowCount.
@SneakyThrows
private void updateRowCount(Key key1, Double value) {
if (value == null)
return;
// lock
StatisticObject res = statisticMap.compute(key1, (key, statisticObject) -> {
if (statisticObject == null) {
statisticObject = new StatisticObject();
}
statisticObject.setRowCount(value);
return statisticObject;
});
JdbcConnectionManager jdbcConnectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
try (DefaultConnection connection = jdbcConnectionManager.getConnection(MetadataManager.getPrototype())) {
Connection rawConnection = connection.getRawConnection();
JdbcUtils.execute(rawConnection, "insert into mycat.analyze_table (table_rows,name) values(?,?)", Arrays.asList(value, key1.getSchemaName() + key1.getTableName()));
}
LOGGER.info("行统计更新 tableName:" + key1 + " " + res);
}
use of io.mycat.datasource.jdbc.datasource.DefaultConnection in project Mycat2 by MyCATApache.
the class MySQLCheckHandler method check.
@NotNull
private Set<String> check(MetadataManager metadataManager, JdbcConnectionManager jdbcConnectionManager, List<Throwable> throwables, Stream<Partition> dataNodeStream) {
Set<String> errorInfo;
List<Each> eachColumnInfos = Collections.synchronizedList(new LinkedList<>());
dataNodeStream.forEach(dataNode -> {
try (DefaultConnection connection = jdbcConnectionManager.getConnection(dataNode.getTargetName())) {
eachColumnInfos.add(new Each(dataNode, JdbcUtils.executeQuery(connection.getRawConnection(), "show full columns from " + dataNode.getTargetSchemaTable(), Collections.emptyList())));
} catch (Throwable throwable) {
throwables.add(throwable);
}
});
if (eachColumnInfos.isEmpty() || eachColumnInfos.size() == 1) {
return Collections.emptySet();
}
List<Map<String, Object>> curRrototypeColumnInfo = eachColumnInfos.get(0).getInfo();
errorInfo = eachColumnInfos.stream().filter(i -> !i.getInfo().equals(curRrototypeColumnInfo)).map(i -> i.getPartition().getUniqueName()).collect(Collectors.toSet());
return errorInfo;
}
use of io.mycat.datasource.jdbc.datasource.DefaultConnection in project Mycat2 by MyCATApache.
the class ShowColumnsSQLHandler method onExecute.
@Override
protected Future<Void> onExecute(SQLRequest<SQLShowColumnsStatement> request, MycatDataContext dataContext, Response response) {
SQLShowColumnsStatement ast = request.getAst();
if (ast.getDatabase() == null && dataContext.getDefaultSchema() != null) {
ast.setDatabase(new SQLIdentifierExpr(dataContext.getDefaultSchema()));
}
try {
JdbcConnectionManager jdbcConnectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
String database = SQLUtils.normalize(ast.getDatabase().getSimpleName());
String table = SQLUtils.normalize(ast.getTable().getSimpleName());
MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
TableHandler tableHandler = metadataManager.getTable(database, table);
boolean okOnPrototype = false;
try (DefaultConnection connection = jdbcConnectionManager.getConnection(MetadataManager.getPrototype())) {
JdbcUtils.executeQuery(connection.getRawConnection(), ast.toString(), Collections.emptyList());
okOnPrototype = true;
} catch (Throwable throwable) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("try query {} from prototype fail", ast);
}
}
if (okOnPrototype) {
return response.proxySelect(Collections.singletonList(MetadataManager.getPrototype()), ast.toString(), Collections.emptyList());
}
Partition dataNode = null;
boolean okOnDataNode = false;
if (tableHandler.getType() == LogicTableType.NORMAL) {
dataNode = ((NormalTable) tableHandler).getDataNode();
} else if (tableHandler.getType() == LogicTableType.GLOBAL) {
dataNode = ((GlobalTable) tableHandler).getDataNode();
} else if (tableHandler.getType() == LogicTableType.SHARDING) {
dataNode = ((ShardingTable) tableHandler).getBackends().get(0);
}
if (dataNode != null) {
SQLShowColumnsStatement tryAst = (SQLShowColumnsStatement) ast.clone();
tryAst.setTable(new SQLIdentifierExpr("`" + dataNode.getTable() + "`"));
tryAst.setDatabase(new SQLIdentifierExpr("`" + dataNode.getSchema() + "`"));
try (DefaultConnection connection = jdbcConnectionManager.getConnection(dataNode.getTargetName())) {
JdbcUtils.executeQuery(connection.getRawConnection(), ast.toString(), Collections.emptyList());
okOnDataNode = true;
} catch (Throwable throwable) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("try query {} from partition fail", ast);
}
}
if (okOnDataNode) {
return response.proxySelect(Collections.singletonList(dataNode.getTargetName()), ast.toString(), Collections.emptyList());
}
}
} catch (Exception e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("try query {} from ds fail", ast);
}
}
String sql = toNormalSQL(request.getAst());
return DrdsRunnerHelper.runOnDrds(dataContext, DrdsRunnerHelper.preParse(sql, dataContext.getDefaultSchema()), response);
}
use of io.mycat.datasource.jdbc.datasource.DefaultConnection in project Mycat2 by MyCATApache.
the class SQLCreateProcedureHandler method onExecute.
@Override
@SneakyThrows
protected Future<Void> onExecute(SQLRequest<SQLCreateProcedureStatement> request, MycatDataContext dataContext, Response response) {
SQLCreateProcedureStatement ast = request.getAst();
if (ast.getName() instanceof SQLIdentifierExpr) {
String defaultSchema = dataContext.getDefaultSchema();
if (defaultSchema != null) {
ast.setName(new SQLPropertyExpr(defaultSchema, ((SQLIdentifierExpr) ast.getName()).getName()));
}
}
if (!(ast.getName() instanceof SQLPropertyExpr)) {
throw new IllegalArgumentException("unknown schema:");
}
SQLPropertyExpr pNameExpr = (SQLPropertyExpr) ast.getName();
String schemaName = SQLUtils.normalize(pNameExpr.getOwnerName().toLowerCase());
String pName = SQLUtils.normalize(pNameExpr.getName().toLowerCase());
List<SQLParameter> sqlParameters = Optional.ofNullable(ast.getParameters()).orElse(Collections.emptyList());
Map<SQLParameter.ParameterType, List<SQLParameter>> parameterTypeListMap = sqlParameters.stream().collect(Collectors.groupingBy(k -> k.getParamType()));
SQLBlockStatement block = (SQLBlockStatement) ast.getBlock();
if (dataContext.getDefaultSchema() != null) {
block.accept(new MySqlASTVisitorAdapter() {
@Override
public void endVisit(SQLExprTableSource x) {
resolveSQLExprTableSource(x, dataContext);
}
});
}
Map<String, Collection<String>> collect = TableCollector.collect(dataContext.getDefaultSchema(), block);
int resultSetCount = getResultSetCount(block);
List<TableHandler> tableHandlers = getTableHandlers(block);
MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
NormalProcedureConfig normalProcedureConfig = new NormalProcedureConfig();
normalProcedureConfig.setCreateProcedureSQL(ast.toString());
NormalBackEndProcedureInfoConfig normalBackEndProcedureInfoConfig = new NormalBackEndProcedureInfoConfig();
normalBackEndProcedureInfoConfig.setProcedureName(pName);
normalBackEndProcedureInfoConfig.setSchemaName(schemaName);
normalBackEndProcedureInfoConfig.setTargetName(MetadataManager.getPrototype());
normalProcedureConfig.setLocality(normalBackEndProcedureInfoConfig);
try (MycatRouterConfigOps ops = ConfigUpdater.getOps()) {
ops.addProcedure(schemaName, pName, normalProcedureConfig);
ops.commit();
}
JdbcConnectionManager jdbcConnectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
try (DefaultConnection connection = jdbcConnectionManager.getConnection(MetadataManager.getPrototype())) {
connection.executeUpdate(ast.toString(), false);
}
return response.sendOk();
}
Aggregations