Search in sources :

Example 46 with DefaultConnection

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;
    }
}
Also used : ReplicaSelectorManager(io.mycat.replica.ReplicaSelectorManager) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) RowBaseIterator(io.mycat.api.collector.RowBaseIterator)

Example 47 with DefaultConnection

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);
}
Also used : DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) Connection(java.sql.Connection) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager)

Example 48 with DefaultConnection

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;
}
Also used : java.util(java.util) SQLUtils(com.alibaba.druid.sql.SQLUtils) ResultSetBuilder(io.mycat.beans.mycat.ResultSetBuilder) io.mycat(io.mycat) Getter(lombok.Getter) SQLRequest(io.mycat.sqlhandler.SQLRequest) MySqlCheckTableStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCheckTableStatement) AbstractSQLHandler(io.mycat.sqlhandler.AbstractSQLHandler) GlobalTableHandler(io.mycat.calcite.table.GlobalTableHandler) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) Future(io.vertx.core.Future) Collectors(java.util.stream.Collectors) ShardingTableHandler(io.mycat.router.ShardingTableHandler) JDBCType(java.sql.JDBCType) Stream(java.util.stream.Stream) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) JdbcUtils(com.alibaba.druid.util.JdbcUtils) AllArgsConstructor(lombok.AllArgsConstructor) NotNull(org.jetbrains.annotations.NotNull) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) NotNull(org.jetbrains.annotations.NotNull)

Example 49 with DefaultConnection

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);
}
Also used : SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) GlobalTable(io.mycat.calcite.table.GlobalTable) SQLShowColumnsStatement(com.alibaba.druid.sql.ast.statement.SQLShowColumnsStatement) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) ShardingTable(io.mycat.calcite.table.ShardingTable)

Example 50 with DefaultConnection

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();
}
Also used : java.util(java.util) SQLUtils(com.alibaba.druid.sql.SQLUtils) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) io.mycat(io.mycat) SneakyThrows(lombok.SneakyThrows) SQLRequest(io.mycat.sqlhandler.SQLRequest) NormalBackEndProcedureInfoConfig(io.mycat.config.NormalBackEndProcedureInfoConfig) AbstractSQLHandler(io.mycat.sqlhandler.AbstractSQLHandler) SQLBlockStatement(com.alibaba.druid.sql.ast.statement.SQLBlockStatement) SQLCreateProcedureStatement(com.alibaba.druid.sql.ast.statement.SQLCreateProcedureStatement) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) ConfigUpdater(io.mycat.sqlhandler.ConfigUpdater) Future(io.vertx.core.Future) Collectors(java.util.stream.Collectors) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) MySqlASTVisitorAdapter(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter) MycatRouterConfigOps(io.mycat.config.MycatRouterConfigOps) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) SQLParameter(com.alibaba.druid.sql.ast.SQLParameter) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) NormalProcedureConfig(io.mycat.config.NormalProcedureConfig) NotNull(org.jetbrains.annotations.NotNull) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) MySqlASTVisitorAdapter(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter) NormalBackEndProcedureInfoConfig(io.mycat.config.NormalBackEndProcedureInfoConfig) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) MycatRouterConfigOps(io.mycat.config.MycatRouterConfigOps) SQLCreateProcedureStatement(com.alibaba.druid.sql.ast.statement.SQLCreateProcedureStatement) SQLBlockStatement(com.alibaba.druid.sql.ast.statement.SQLBlockStatement) NormalProcedureConfig(io.mycat.config.NormalProcedureConfig) SQLParameter(com.alibaba.druid.sql.ast.SQLParameter) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) DefaultConnection(io.mycat.datasource.jdbc.datasource.DefaultConnection) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) SneakyThrows(lombok.SneakyThrows)

Aggregations

DefaultConnection (io.mycat.datasource.jdbc.datasource.DefaultConnection)53 JdbcConnectionManager (io.mycat.datasource.jdbc.datasource.JdbcConnectionManager)39 SneakyThrows (lombok.SneakyThrows)23 Connection (java.sql.Connection)20 MetadataManager (io.mycat.MetadataManager)6 java.util (java.util)6 SQLException (java.sql.SQLException)5 Statement (java.sql.Statement)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 Collectors (java.util.stream.Collectors)5 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)4 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)4 RowBaseIterator (io.mycat.api.collector.RowBaseIterator)4 CopyMycatRowMetaData (io.mycat.beans.mycat.CopyMycatRowMetaData)4 ReplicaSelectorManager (io.mycat.replica.ReplicaSelectorManager)4 NameMap (io.mycat.util.NameMap)4 NotNull (org.jetbrains.annotations.NotNull)4 SQLUtils (com.alibaba.druid.sql.SQLUtils)3 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)3 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)3