Search in sources :

Example 21 with ResultSetBuilder

use of io.mycat.beans.mycat.ResultSetBuilder in project Mycat2 by MyCATApache.

the class ShardingSQLHandler method testExample.

private Optional<Future<Void>> testExample(SQLRequest<SQLSelectStatement> request, MycatDataContext dataContext, Response response) {
    String sqlString = request.getSqlString();
    if (sqlString.equalsIgnoreCase("select swapbuffer")) {
        ResultSetBuilder resultSetBuilder = ResultSetBuilder.create();
        resultSetBuilder.addColumnInfo("1", JDBCType.INTEGER);
        resultSetBuilder.addObjectRowPayload(Arrays.asList(1, 2));
        resultSetBuilder.addObjectRowPayload(Arrays.asList(3, 4));
        MySQLSwapbufferBuilder mySQLSwapbufferBuilder = new MySQLSwapbufferBuilder(resultSetBuilder.build());
        Observable<Buffer> sender = mySQLSwapbufferBuilder.build();
        return Optional.of(response.swapBuffer(sender));
    }
    if (sqlString.equalsIgnoreCase("select arrow")) {
        ResultSetBuilder resultSetBuilder = ResultSetBuilder.create();
        resultSetBuilder.addColumnInfo("1", JDBCType.INTEGER);
        resultSetBuilder.addObjectRowPayload(Arrays.asList(1, 2));
        resultSetBuilder.addObjectRowPayload(Arrays.asList(3, 4));
        RowBaseIterator rowBaseIterator = resultSetBuilder.build();
        Observable<VectorSchemaRoot> observable = ResultWriterUtil.convertToVector(resultSetBuilder.build());
        return Optional.of(response.sendVectorResultSet(rowBaseIterator.getMetaData(), observable));
    }
    return Optional.empty();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) VectorSchemaRoot(org.apache.arrow.vector.VectorSchemaRoot) ResultSetBuilder(io.mycat.beans.mycat.ResultSetBuilder) MySQLSwapbufferBuilder(io.mycat.swapbuffer.MySQLSwapbufferBuilder) RowBaseIterator(io.mycat.api.collector.RowBaseIterator)

Example 22 with ResultSetBuilder

use of io.mycat.beans.mycat.ResultSetBuilder in project Mycat2 by MyCATApache.

the class BaseRowObservable method toColumnMetaData.

public static MycatRowMetaData toColumnMetaData(List<ColumnDescriptor> event) {
    ResultSetBuilder resultSetBuilder = ResultSetBuilder.create();
    for (ColumnDescriptor columnDescriptor : event) {
        if (columnDescriptor instanceof ColumnDefinition) {
            ColumnDefinition columnDefinition = (ColumnDefinition) columnDescriptor;
            String schemaName = columnDefinition.schema();
            String tableName = columnDefinition.orgTable();
            String columnName = columnDefinition.name();
            int columnType = columnDefinition.type().jdbcType.getVendorTypeNumber();
            int precision = 0;
            int scale = 0;
            String columnLabel = columnDefinition.name();
            boolean isAutoIncrement = false;
            boolean isCaseSensitive = false;
            boolean isNullable = (columnDefinition.flags() & ColumnDefinition.ColumnDefinitionFlags.NOT_NULL_FLAG) == 0;
            boolean isSigned = true;
            int displaySize = (int) columnDefinition.columnLength();
            resultSetBuilder.addColumnInfo(schemaName, tableName, columnName, columnType, precision, scale, columnLabel, isAutoIncrement, isCaseSensitive, isNullable, isSigned, displaySize);
        } else {
            resultSetBuilder.addColumnInfo(columnDescriptor.name(), columnDescriptor.jdbcType());
        }
    }
    RowBaseIterator build = resultSetBuilder.build();
    return build.getMetaData();
}
Also used : ResultSetBuilder(io.mycat.beans.mycat.ResultSetBuilder) ColumnDescriptor(io.vertx.sqlclient.desc.ColumnDescriptor) ByteString(org.apache.calcite.avatica.util.ByteString) RowBaseIterator(io.mycat.api.collector.RowBaseIterator) ColumnDefinition(io.vertx.mysqlclient.impl.protocol.ColumnDefinition)

Example 23 with ResultSetBuilder

use of io.mycat.beans.mycat.ResultSetBuilder in project Mycat2 by MyCATApache.

the class HintHandler method showInstances.

public static RowBaseIterator showInstances() {
    MycatRouterConfig routerConfig = MetaClusterCurrent.wrapper(MycatRouterConfig.class);
    ReplicaSelectorManager replicaSelectorRuntime = MetaClusterCurrent.wrapper(ReplicaSelectorManager.class);
    ResultSetBuilder resultSetBuilder = ResultSetBuilder.create();
    resultSetBuilder.addColumnInfo("NAME", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("ALIVE", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("READABLE", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("TYPE", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("SESSION_COUNT", JDBCType.BIGINT);
    resultSetBuilder.addColumnInfo("WEIGHT", JDBCType.BIGINT);
    resultSetBuilder.addColumnInfo("MASTER", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("LIMIT_SESSION_COUNT", JDBCType.BIGINT);
    resultSetBuilder.addColumnInfo("REPLICA", JDBCType.VARCHAR);
    Collection<PhysicsInstance> values = replicaSelectorRuntime.getPhysicsInstances();
    Map<String, DatasourceConfig> dataSourceConfig = routerConfig.getDatasources().stream().collect(Collectors.toMap(k -> k.getName(), v -> v));
    for (PhysicsInstance instance : values) {
        String NAME = instance.getName();
        String TYPE = instance.getType().name();
        boolean READABLE = instance.asSelectRead();
        int SESSION_COUNT = instance.getSessionCounter();
        int WEIGHT = instance.getWeight();
        boolean ALIVE = instance.isAlive();
        boolean MASTER = instance.isMaster();
        Optional<DatasourceConfig> e = Optional.ofNullable(dataSourceConfig.get(NAME));
        String replicaDataSourceSelectorList = String.join(",", replicaSelectorRuntime.getReplicaNameListByInstanceName(NAME));
        resultSetBuilder.addObjectRowPayload(Arrays.asList(NAME, ALIVE, READABLE, TYPE, SESSION_COUNT, WEIGHT, MASTER, e.map(i -> i.getMaxCon()).orElse(-1), replicaDataSourceSelectorList));
    }
    RowBaseIterator rowBaseIterator = resultSetBuilder.build();
    return rowBaseIterator;
}
Also used : ReplicaSelectorManager(io.mycat.replica.ReplicaSelectorManager) MycatdbCommand(io.mycat.commands.MycatdbCommand) io.mycat(io.mycat) CSVRecord(org.apache.commons.csv.CSVRecord) JdbcDataSource(io.mycat.datasource.jdbc.datasource.JdbcDataSource) CSVFormat(org.apache.commons.csv.CSVFormat) HeartbeatFlow(io.mycat.replica.heartbeat.HeartbeatFlow) Duration(java.time.Duration) MySQLErrorCode(io.mycat.beans.mysql.MySQLErrorCode) JsonUtil(io.mycat.util.JsonUtil) MySqlHintStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlHintStatement) io.mycat.config(io.mycat.config) Timestamp(java.sql.Timestamp) Reader(java.io.Reader) Future(io.vertx.core.Future) Collectors(java.util.stream.Collectors) ArrayBindable(org.apache.calcite.runtime.ArrayBindable) MycatSQLLogMonitor(io.mycat.monitor.MycatSQLLogMonitor) io.mycat.sqlhandler(io.mycat.sqlhandler) NormalTable(io.mycat.calcite.table.NormalTable) Nullable(org.jetbrains.annotations.Nullable) ReplicaSelector(io.mycat.replica.ReplicaSelector) UpdateSQLHandler(io.mycat.sqlhandler.dml.UpdateSQLHandler) Stream(java.util.stream.Stream) ReplicaSelectorManager(io.mycat.replica.ReplicaSelectorManager) DrdsRunnerHelper(io.mycat.calcite.DrdsRunnerHelper) PhysicsInstance(io.mycat.replica.PhysicsInstance) NotNull(org.jetbrains.annotations.NotNull) ShardingTable(io.mycat.calcite.table.ShardingTable) VertxExecuter(io.mycat.vertx.VertxExecuter) SqlEntry(io.mycat.monitor.SqlEntry) java.util(java.util) ResultSetBuilder(io.mycat.beans.mycat.ResultSetBuilder) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) LocalDateTime(java.time.LocalDateTime) XaLog(cn.mycat.vertx.xa.XaLog) Supplier(java.util.function.Supplier) Iterators(com.google.common.collect.Iterators) SqlResultSetService(io.mycat.commands.SqlResultSetService) InterruptThreadHint(io.mycat.hint.InterruptThreadHint) MycatSQLExprTableSourceUtil(io.mycat.util.MycatSQLExprTableSourceUtil) KillThreadHint(io.mycat.hint.KillThreadHint) Schedulers(io.reactivex.rxjava3.schedulers.Schedulers) JDBCType(java.sql.JDBCType) PrepareExecutor(io.mycat.calcite.PrepareExecutor) SchemaHandler(io.mycat.calcite.table.SchemaHandler) MySqlInsertStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement) ImmutableList(com.google.common.collect.ImmutableList) DatasourceStatus(io.mycat.replica.heartbeat.DatasourceStatus) Observable(io.reactivex.rxjava3.core.Observable) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) RowBaseIterator(io.mycat.api.collector.RowBaseIterator) StreamSupport(java.util.stream.StreamSupport) GlobalTable(io.mycat.calcite.table.GlobalTable) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLUtils(com.alibaba.druid.sql.SQLUtils) Flowable(io.reactivex.rxjava3.core.Flowable) PromiseInternal(io.vertx.core.impl.future.PromiseInternal) io.mycat.hint(io.mycat.hint) VertxUpdateExecuter(io.mycat.vertx.VertxUpdateExecuter) StringWriter(java.io.StringWriter) SQLInsertStatement(com.alibaba.druid.sql.ast.statement.SQLInsertStatement) ExecutorProvider(io.mycat.calcite.ExecutorProvider) IOException(java.io.IOException) QuoteMode(org.apache.commons.csv.QuoteMode) SQLCommentHint(com.alibaba.druid.sql.ast.SQLCommentHint) SqlRecorderRuntime(io.mycat.exporter.SqlRecorderRuntime) VertxUtil(io.mycat.util.VertxUtil) ReplicaSwitchType(io.mycat.replica.ReplicaSwitchType) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) MycatInsertRel(io.mycat.calcite.physical.MycatInsertRel) io.mycat.calcite.spm(io.mycat.calcite.spm) NameMap(io.mycat.util.NameMap) MysqlPayloadObject(io.mycat.api.collector.MysqlPayloadObject) JdbcUtils(com.alibaba.druid.util.JdbcUtils) CodeExecuterContext(io.mycat.calcite.CodeExecuterContext) HeartBeatStatus(io.mycat.replica.heartbeat.HeartBeatStatus) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) UnmodifiableIterator(com.google.common.collect.UnmodifiableIterator) FileReader(java.io.FileReader) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) StorageManager(io.mycat.sqlhandler.config.StorageManager) ResultSetBuilder(io.mycat.beans.mycat.ResultSetBuilder) InterruptThreadHint(io.mycat.hint.InterruptThreadHint) KillThreadHint(io.mycat.hint.KillThreadHint) io.mycat.hint(io.mycat.hint) SQLCommentHint(com.alibaba.druid.sql.ast.SQLCommentHint) PhysicsInstance(io.mycat.replica.PhysicsInstance) RowBaseIterator(io.mycat.api.collector.RowBaseIterator)

Example 24 with ResultSetBuilder

use of io.mycat.beans.mycat.ResultSetBuilder 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());
}
Also used : MycatdbCommand(io.mycat.commands.MycatdbCommand) io.mycat(io.mycat) CSVRecord(org.apache.commons.csv.CSVRecord) JdbcDataSource(io.mycat.datasource.jdbc.datasource.JdbcDataSource) CSVFormat(org.apache.commons.csv.CSVFormat) HeartbeatFlow(io.mycat.replica.heartbeat.HeartbeatFlow) Duration(java.time.Duration) MySQLErrorCode(io.mycat.beans.mysql.MySQLErrorCode) JsonUtil(io.mycat.util.JsonUtil) MySqlHintStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlHintStatement) io.mycat.config(io.mycat.config) Timestamp(java.sql.Timestamp) Reader(java.io.Reader) Future(io.vertx.core.Future) Collectors(java.util.stream.Collectors) ArrayBindable(org.apache.calcite.runtime.ArrayBindable) MycatSQLLogMonitor(io.mycat.monitor.MycatSQLLogMonitor) io.mycat.sqlhandler(io.mycat.sqlhandler) NormalTable(io.mycat.calcite.table.NormalTable) Nullable(org.jetbrains.annotations.Nullable) ReplicaSelector(io.mycat.replica.ReplicaSelector) UpdateSQLHandler(io.mycat.sqlhandler.dml.UpdateSQLHandler) Stream(java.util.stream.Stream) ReplicaSelectorManager(io.mycat.replica.ReplicaSelectorManager) DrdsRunnerHelper(io.mycat.calcite.DrdsRunnerHelper) PhysicsInstance(io.mycat.replica.PhysicsInstance) NotNull(org.jetbrains.annotations.NotNull) ShardingTable(io.mycat.calcite.table.ShardingTable) VertxExecuter(io.mycat.vertx.VertxExecuter) SqlEntry(io.mycat.monitor.SqlEntry) java.util(java.util) ResultSetBuilder(io.mycat.beans.mycat.ResultSetBuilder) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) LocalDateTime(java.time.LocalDateTime) XaLog(cn.mycat.vertx.xa.XaLog) Supplier(java.util.function.Supplier) Iterators(com.google.common.collect.Iterators) SqlResultSetService(io.mycat.commands.SqlResultSetService) InterruptThreadHint(io.mycat.hint.InterruptThreadHint) MycatSQLExprTableSourceUtil(io.mycat.util.MycatSQLExprTableSourceUtil) KillThreadHint(io.mycat.hint.KillThreadHint) Schedulers(io.reactivex.rxjava3.schedulers.Schedulers) JDBCType(java.sql.JDBCType) PrepareExecutor(io.mycat.calcite.PrepareExecutor) SchemaHandler(io.mycat.calcite.table.SchemaHandler) MySqlInsertStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement) ImmutableList(com.google.common.collect.ImmutableList) DatasourceStatus(io.mycat.replica.heartbeat.DatasourceStatus) Observable(io.reactivex.rxjava3.core.Observable) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) RowBaseIterator(io.mycat.api.collector.RowBaseIterator) StreamSupport(java.util.stream.StreamSupport) GlobalTable(io.mycat.calcite.table.GlobalTable) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLUtils(com.alibaba.druid.sql.SQLUtils) Flowable(io.reactivex.rxjava3.core.Flowable) PromiseInternal(io.vertx.core.impl.future.PromiseInternal) io.mycat.hint(io.mycat.hint) VertxUpdateExecuter(io.mycat.vertx.VertxUpdateExecuter) StringWriter(java.io.StringWriter) SQLInsertStatement(com.alibaba.druid.sql.ast.statement.SQLInsertStatement) ExecutorProvider(io.mycat.calcite.ExecutorProvider) IOException(java.io.IOException) QuoteMode(org.apache.commons.csv.QuoteMode) SQLCommentHint(com.alibaba.druid.sql.ast.SQLCommentHint) SqlRecorderRuntime(io.mycat.exporter.SqlRecorderRuntime) VertxUtil(io.mycat.util.VertxUtil) ReplicaSwitchType(io.mycat.replica.ReplicaSwitchType) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) MycatInsertRel(io.mycat.calcite.physical.MycatInsertRel) io.mycat.calcite.spm(io.mycat.calcite.spm) NameMap(io.mycat.util.NameMap) MysqlPayloadObject(io.mycat.api.collector.MysqlPayloadObject) JdbcUtils(com.alibaba.druid.util.JdbcUtils) CodeExecuterContext(io.mycat.calcite.CodeExecuterContext) HeartBeatStatus(io.mycat.replica.heartbeat.HeartBeatStatus) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) UnmodifiableIterator(com.google.common.collect.UnmodifiableIterator) FileReader(java.io.FileReader) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) StorageManager(io.mycat.sqlhandler.config.StorageManager) SchemaHandler(io.mycat.calcite.table.SchemaHandler) ResultSetBuilder(io.mycat.beans.mycat.ResultSetBuilder) NameMap(io.mycat.util.NameMap) Nullable(org.jetbrains.annotations.Nullable)

Example 25 with ResultSetBuilder

use of io.mycat.beans.mycat.ResultSetBuilder in project Mycat2 by MyCATApache.

the class MysqlShowDatabaseStatusHandler method onExecute.

@Override
protected Future<Void> onExecute(SQLRequest<MySqlShowDatabaseStatusStatement> request, MycatDataContext dataContext, Response response) {
    ResultSetBuilder resultSetBuilder = ResultSetBuilder.create();
    resultSetBuilder.addColumnInfo("ID", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("NAME", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("CONNECTION_STRING", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("PHYSICAL_DB", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("SIZE_IN_MB", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("RATIO", JDBCType.VARCHAR);
    resultSetBuilder.addColumnInfo("THREAD_RUNNING", JDBCType.VARCHAR);
    return response.sendResultSet(resultSetBuilder.build());
}
Also used : ResultSetBuilder(io.mycat.beans.mycat.ResultSetBuilder)

Aggregations

ResultSetBuilder (io.mycat.beans.mycat.ResultSetBuilder)32 RowBaseIterator (io.mycat.api.collector.RowBaseIterator)13 JDBCType (java.sql.JDBCType)12 LocalDateTime (java.time.LocalDateTime)11 io.mycat.config (io.mycat.config)10 ReplicaSelectorManager (io.mycat.replica.ReplicaSelectorManager)10 IOException (java.io.IOException)10 Timestamp (java.sql.Timestamp)10 java.util (java.util)10 Collectors (java.util.stream.Collectors)10 NotNull (org.jetbrains.annotations.NotNull)10 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)9 NameMap (io.mycat.util.NameMap)9 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)8 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)8 SQLInsertStatement (com.alibaba.druid.sql.ast.statement.SQLInsertStatement)8 SQLCommentHint (com.alibaba.druid.sql.ast.SQLCommentHint)7 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)7 ImmutableList (com.google.common.collect.ImmutableList)7 MysqlPayloadObject (io.mycat.api.collector.MysqlPayloadObject)7