Search in sources :

Example 16 with ResultSetBuilder

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

the class NativeMycatServer method showBufferUsage.

@Override
public RowBaseIterator showBufferUsage(long sessionId) {
    MycatSession curSession = null;
    for (MycatReactorThread mycatReactorThread : getReactorManager().getList()) {
        SessionManager.FrontSessionManager<MycatSession> frontManager = mycatReactorThread.getFrontManager();
        for (MycatSession session : frontManager.getAllSessions()) {
            if (sessionId == session.sessionId()) {
                curSession = session;
                break;
            }
        }
    }
    ResultSetBuilder builder = ResultSetBuilder.create();
    builder.addColumnInfo("bufferUsage", JDBCType.BIGINT);
    if (curSession != null) {
        builder.addObjectRowPayload(Arrays.asList(curSession.writeBufferPool().trace()));
    }
    return builder.build();
}
Also used : ResultSetBuilder(io.mycat.beans.mycat.ResultSetBuilder)

Example 17 with ResultSetBuilder

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

the class VertxMycatServer method demo.

private static RowBaseIterator demo() {
    ResultSetBuilder resultSetBuilder = ResultSetBuilder.create();
    resultSetBuilder.addColumnInfo("demo", JDBCType.VARCHAR);
    resultSetBuilder.addObjectRowPayload(Arrays.asList("unsupported"));
    return resultSetBuilder.build();
}
Also used : ResultSetBuilder(io.mycat.beans.mycat.ResultSetBuilder)

Example 18 with ResultSetBuilder

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

the class DrdsRunnerHelper method doExplain.

private RowBaseIterator doExplain(MycatRel relNode1) {
    final StringWriter sw = new StringWriter();
    final RelWriter planWriter = new RelWriterImpl(new PrintWriter(sw), SqlExplainLevel.ALL_ATTRIBUTES, false);
    relNode1.explain(planWriter);
    String[] split = sw.getBuffer().toString().split("\n");
    ResultSetBuilder builder = ResultSetBuilder.create();
    builder.addColumnInfo("explain", JDBCType.VARCHAR);
    for (String s : split) {
        builder.addObjectRowPayload(Arrays.asList(s));
    }
    return builder.build();
}
Also used : ResultSetBuilder(io.mycat.beans.mycat.ResultSetBuilder) StringWriter(java.io.StringWriter) RelWriterImpl(org.apache.calcite.rel.externalize.RelWriterImpl) RelWriter(org.apache.calcite.rel.RelWriter) PrintWriter(java.io.PrintWriter)

Example 19 with ResultSetBuilder

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

the class MycatVertxMySQLHandler method handlePrepareStatement.

private Future<Void> handlePrepareStatement(byte[] bytes, MycatVertxMysqlSession mysqlSession) {
    boolean deprecateEOF = mysqlSession.isDeprecateEOF();
    String sql = new String(bytes);
    // ///////////////////////////////////////////////////
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("received pstmt sql:{}", sql);
    }
    SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement(sql);
    boolean allow = (sqlStatement instanceof SQLSelectStatement || sqlStatement instanceof SQLInsertStatement || sqlStatement instanceof SQLUpdateStatement || sqlStatement instanceof SQLDeleteStatement);
    // MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
    MycatRowMetaData fields;
    if ((sqlStatement instanceof SQLSelectStatement)) {
        PrototypeService prototypeService = MetaClusterCurrent.wrapper(PrototypeService.class);
        Optional<MycatRowMetaData> mycatRowMetaDataForPrepareStatement = prototypeService.getMycatRowMetaDataForPrepareStatement(mysqlSession.getDataContext().getDefaultSchema(), sql);
        if (!mycatRowMetaDataForPrepareStatement.isPresent()) {
            return VertxUtil.castPromise(Future.failedFuture(new SQLException("This command is not supported in the prepared statement protocol yet", "HY000", 1295)));
        }
        fields = mycatRowMetaDataForPrepareStatement.get();
    } else {
        fields = ResultSetBuilder.create().build().getMetaData();
    }
    ResultSetBuilder paramsBuilder = ResultSetBuilder.create();
    sqlStatement.accept(new MySqlASTVisitorAdapter() {

        @Override
        public void endVisit(SQLVariantRefExpr x) {
            if ("?".equalsIgnoreCase(x.getName())) {
                JDBCType res = JDBCType.VARCHAR;
                paramsBuilder.addColumnInfo("", res);
            }
            super.endVisit(x);
        }
    });
    MycatRowMetaData params = paramsBuilder.build().getMetaData();
    long stmtId = mycatDataContext.nextPrepareStatementId();
    Map<Long, io.mycat.PreparedStatement> statementMap = this.mycatDataContext.getPrepareInfo();
    PreparedStatement preparedStatement = new PreparedStatement(stmtId, sqlStatement, params.getColumnCount());
    for (int i = 0; i < params.getColumnCount(); i++) {
        preparedStatement.getParametersType()[i] = MysqlDefs.FIELD_TYPE_STRING;
    }
    statementMap.put(stmtId, preparedStatement);
    DefaultPreparedOKPacket info = new DefaultPreparedOKPacket(stmtId, fields.getColumnCount(), params.getColumnCount(), session.getWarningCount());
    Future<Void> writeEndFuture = Future.succeededFuture();
    if (info.getPrepareOkColumnsCount() == 0 && info.getPrepareOkParametersCount() == 0) {
        return session.writeBytes(MySQLPacketUtil.generatePrepareOk(info), true);
    }
    session.writeBytes(MySQLPacketUtil.generatePrepareOk(info), false);
    if (info.getPrepareOkParametersCount() > 0 && info.getPrepareOkColumnsCount() == 0) {
        for (int i = 0; i < info.getPrepareOkParametersCount(); i++) {
            writeEndFuture = session.writeBytes(MySQLPacketUtil.generateColumnDefPayload(params, i), info.getPrepareOkParametersCount() - 1 == i && deprecateEOF);
        }
        if (deprecateEOF) {
            return writeEndFuture;
        } else {
            return session.writeBytes(MySQLPacketUtil.generateEof(session.getWarningCount(), session.getServerStatusValue()), true);
        }
    } else if (info.getPrepareOkParametersCount() == 0 && info.getPrepareOkColumnsCount() > 0) {
        for (int i = 0; i < info.getPrepareOkColumnsCount(); i++) {
            writeEndFuture = session.writeBytes(MySQLPacketUtil.generateColumnDefPayload(fields, i), info.getPrepareOkColumnsCount() - 1 == i && deprecateEOF);
        }
        if (deprecateEOF) {
            return writeEndFuture;
        } else {
            return session.writeBytes(MySQLPacketUtil.generateEof(session.getWarningCount(), session.getServerStatusValue()), true);
        }
    } else {
        for (int i = 0; i < info.getPrepareOkParametersCount(); i++) {
            writeEndFuture = session.writeBytes(MySQLPacketUtil.generateColumnDefPayload(params, i), false);
        }
        writeEndFuture = session.writeColumnEndPacket(false);
        for (int i = 0; i < info.getPrepareOkColumnsCount(); i++) {
            writeEndFuture = session.writeBytes(MySQLPacketUtil.generateColumnDefPayload(fields, i), info.getPrepareOkColumnsCount() - 1 == i && deprecateEOF);
        }
        if (deprecateEOF) {
            return writeEndFuture;
        } else {
            return session.writeBytes(MySQLPacketUtil.generateEof(session.getWarningCount(), session.getServerStatusValue()), true);
        }
    }
}
Also used : MySqlASTVisitorAdapter(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter) ResultSetBuilder(io.mycat.beans.mycat.ResultSetBuilder) SQLException(java.sql.SQLException) DefaultPreparedOKPacket(io.mycat.beans.mysql.packet.DefaultPreparedOKPacket) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLInsertStatement(com.alibaba.druid.sql.ast.statement.SQLInsertStatement) JDBCType(java.sql.JDBCType) SQLUpdateStatement(com.alibaba.druid.sql.ast.statement.SQLUpdateStatement) PrototypeService(io.mycat.prototypeserver.mysql.PrototypeService) SQLDeleteStatement(com.alibaba.druid.sql.ast.statement.SQLDeleteStatement) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) MycatRowMetaData(io.mycat.beans.mycat.MycatRowMetaData)

Example 20 with ResultSetBuilder

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

the class BinlogUtil method list.

public static RowBaseIterator list(List<BinlogScheduler> schedulers) {
    ResultSetBuilder builder = ResultSetBuilder.create();
    builder.addColumnInfo("ID", JDBCType.VARCHAR);
    builder.addColumnInfo("NAME", JDBCType.VARCHAR);
    builder.addColumnInfo("INFO", JDBCType.VARCHAR);
    builder.addColumnInfo("START_TIME", JDBCType.TIMESTAMP);
    for (BinlogScheduler scheduler : schedulers) {
        String id = scheduler.getId();
        String name = scheduler.getName();
        Map<String, Map<String, List<Partition>>> listMap = scheduler.getListMap();
        LocalDateTime startTime = scheduler.getStartTime();
        builder.addObjectRowPayload(Arrays.asList(id, name, listMap.toString(), startTime));
    }
    return builder.build();
}
Also used : LocalDateTime(java.time.LocalDateTime) Partition(io.mycat.Partition) ResultSetBuilder(io.mycat.beans.mycat.ResultSetBuilder) ToString(groovy.transform.ToString) NameMap(io.mycat.util.NameMap)

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