Search in sources :

Example 1 with StreamMysqlCollector

use of io.vertx.mysqlclient.impl.codec.StreamMysqlCollector in project Mycat2 by MyCATApache.

the class RowSetQuery method runTextQuery.

public static PromiseInternal<SqlResult<Void>> runTextQuery(String curSql, MySQLClientSession mySQLClientSession, StreamMysqlCollector collectorArg) {
    if (mySQLClientSession.getCurNIOHandler() != null) {
        throw new IllegalArgumentException();
    }
    PromiseInternal<SqlResult<Void>> promise = VertxUtil.newPromise();
    if (mySQLClientSession.getIOThread() == Thread.currentThread()) {
        VertxMycatTextCollector<Object, Object> resultSetHandler = new VertxMycatTextCollector<Object, Object>((Collector) collectorArg);
        if (LOGGER.isDebugEnabled()) {
            if (curSql.startsWith("XA ROLLBACK")) {
                LOGGER.debug("session id:{} sql:{}", mySQLClientSession.sessionId(), curSql, new Throwable());
            }
            LOGGER.debug("session id:{} sql:{}", mySQLClientSession.sessionId(), curSql);
        }
        resultSetHandler.request(mySQLClientSession, MySQLCommandType.COM_QUERY, curSql.getBytes(), new ResultSetCallBack<MySQLClientSession>() {

            @Override
            public void onFinishedSendException(Exception exception, Object sender, Object attr) {
                MycatException mycatException = new MycatException(MySQLErrorCode.ER_UNKNOWN_ERROR, exception.getMessage());
                LOGGER.error("session id:{} sql:{}", mySQLClientSession.sessionId(), curSql, mycatException);
                promise.tryFail(mycatException);
            }

            @Override
            public void onFinishedException(Exception exception, Object sender, Object attr) {
                MycatException mycatException = new MycatException(MySQLErrorCode.ER_UNKNOWN_ERROR, exception.getMessage());
                LOGGER.error("session id:{} sql:{}", mySQLClientSession.sessionId(), curSql, mycatException);
                promise.tryFail(mycatException);
            }

            @Override
            public void onFinished(boolean monopolize, MySQLClientSession mysql, Object sender, Object attr) {
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("onFinished session id:{} sql:{}", mySQLClientSession.sessionId(), curSql);
                }
                MySqlResult<Void> mySqlResult = new MySqlResult<>(resultSetHandler.getRowCount(), resultSetHandler.getAffectedRows(), resultSetHandler.getLastInsertId(), null, Optional.ofNullable(resultSetHandler.getRowResultDecoder()).map(i -> i.rowDesc).map(i -> i.columnDescriptor()).orElse(Collections.emptyList()));
                promise.complete(mySqlResult);
            }

            @Override
            public void onErrorPacket(ErrorPacketImpl errorPacket, boolean monopolize, MySQLClientSession mysql, Object sender, Object attr) {
                MycatException mycatException = new MycatException(errorPacket.getErrorCode(), errorPacket.getErrorMessageString());
                LOGGER.error("onErrorPacket session id:{} sql:{}", mySQLClientSession.sessionId(), curSql, mycatException);
                promise.tryFail(mycatException);
            }
        });
    } else {
        mySQLClientSession.getIOThread().addNIOJob(new NIOJob() {

            @Override
            public void run(ReactorEnvThread reactor) throws Exception {
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.error("nio job session id:{} sql:{}", mySQLClientSession.sessionId(), curSql, new Throwable());
                }
                runTextQuery(curSql, mySQLClientSession, collectorArg).onComplete(promise);
            }

            @Override
            public void stop(ReactorEnvThread reactor, Exception reason) {
                promise.tryFail(reason);
            }

            @Override
            public String message() {
                return "proxy query text result set";
            }
        });
    }
    return promise;
}
Also used : MySQLClient(io.vertx.mysqlclient.MySQLClient) LoggerFactory(org.slf4j.LoggerFactory) MySQLRowDesc(io.vertx.mysqlclient.impl.MySQLRowDesc) Function(java.util.function.Function) NIOJob(io.mycat.proxy.reactor.NIOJob) MySQLClientSession(io.mycat.proxy.session.MySQLClientSession) MySQLCommandType(io.mycat.beans.mysql.MySQLCommandType) ReactorEnvThread(io.mycat.proxy.reactor.ReactorEnvThread) StreamMysqlCollector(io.vertx.mysqlclient.impl.codec.StreamMysqlCollector) SqlResult(io.vertx.sqlclient.SqlResult) ErrorPacketImpl(io.mycat.beans.mysql.packet.ErrorPacketImpl) MySQLErrorCode(io.mycat.beans.mysql.MySQLErrorCode) RowSet(io.vertx.sqlclient.RowSet) AsyncResult(io.vertx.core.AsyncResult) Collector(java.util.stream.Collector) PromiseInternal(io.vertx.core.impl.future.PromiseInternal) Logger(org.slf4j.Logger) MycatException(io.mycat.MycatException) Query(io.vertx.sqlclient.Query) VertxUtil(io.mycat.util.VertxUtil) Future(io.vertx.core.Future) Row(io.vertx.sqlclient.Row) VertxRowSetImpl(io.vertx.mysqlclient.impl.codec.VertxRowSetImpl) Optional(java.util.Optional) Handler(io.vertx.core.Handler) Collections(java.util.Collections) ResultSetCallBack(io.mycat.proxy.callback.ResultSetCallBack) ReactorEnvThread(io.mycat.proxy.reactor.ReactorEnvThread) MycatException(io.mycat.MycatException) SqlResult(io.vertx.sqlclient.SqlResult) NIOJob(io.mycat.proxy.reactor.NIOJob) MycatException(io.mycat.MycatException) ErrorPacketImpl(io.mycat.beans.mysql.packet.ErrorPacketImpl) MySQLClientSession(io.mycat.proxy.session.MySQLClientSession)

Example 2 with StreamMysqlCollector

use of io.vertx.mysqlclient.impl.codec.StreamMysqlCollector in project Mycat2 by MyCATApache.

the class RowSetJdbcPreparedJdbcQuery method extracted.

public static <R> void extracted(Promise<SqlResult<R>> promise, Statement statement, ResultSet resultSet, Collector<Row, ?, R> collector) throws SQLException {
    try {
        if (resultSet == null) {
            Object o = collector.supplier().get();
            Function<Object, Object> finisher = (Function) collector.finisher();
            promise.complete(new MySqlResult<>(0, 0, 0, (R) finisher.apply(o), Collections.emptyList()));
            return;
        }
        JdbcRowMetaData metaData = new JdbcRowMetaData(resultSet.getMetaData());
        int columnCount = metaData.getColumnCount();
        List<ColumnDescriptor> columnDescriptors = new ArrayList<>();
        for (int i = 0; i < columnCount; i++) {
            int index = i;
            columnDescriptors.add(new ColumnDescriptor() {

                @Override
                public String name() {
                    return metaData.getColumnName(index);
                }

                @Override
                public boolean isArray() {
                    return false;
                }

                @Override
                public String typeName() {
                    return null;
                }

                @Override
                public JDBCType jdbcType() {
                    return JDBCType.valueOf(metaData.getColumnType(index));
                }
            });
        }
        RowDesc rowDesc = new RowDesc(metaData.getColumnList(), columnDescriptors);
        ColumnDefPacket[] columnDefPackets = new ColumnDefPacket[columnCount];
        for (int i = 0; i < columnCount; i++) {
            columnDefPackets[i] = new ColumnDefPacketImpl(metaData, i);
        }
        if (collector instanceof StreamMysqlCollector) {
            MySQLRowDesc mySQLRowDesc = new MySQLRowDesc(Arrays.asList(columnDefPackets).stream().map(packet -> {
                String catalog = new String(packet.getColumnCatalog());
                String schema = new String(packet.getColumnSchema());
                String table = new String(packet.getColumnTable());
                String orgTable = new String(packet.getColumnOrgTable());
                String name = new String(packet.getColumnName());
                String orgName = new String(packet.getColumnOrgName());
                int characterSet = packet.getColumnCharsetSet();
                long columnLength = packet.getColumnLength();
                DataType type = DataType.valueOf(packet.getColumnType() == 15 ? 253 : packet.getColumnType());
                int flags = packet.getColumnFlags();
                byte decimals = packet.getColumnDecimals();
                ColumnDefinition columnDefinition = new ColumnDefinition(catalog, schema, table, orgTable, name, orgName, characterSet, columnLength, type, flags, decimals);
                return columnDefinition;
            }).toArray(n -> new ColumnDefinition[n]), DataFormat.TEXT);
            ((StreamMysqlCollector) collector).onColumnDefinitions(mySQLRowDesc);
        }
        {
            Object supplier = collector.supplier().get();
            BiConsumer<Object, Row> accumulator = (BiConsumer) collector.accumulator();
            Function<Object, Object> finisher = (Function) collector.finisher();
            int count = 0;
            while (resultSet.next()) {
                JDBCRow jdbcRow = new MycatRow(rowDesc);
                for (int i = 0; i < columnCount; i++) {
                    jdbcRow.addValue(resultSet.getObject(i + 1));
                }
                count++;
                accumulator.accept(supplier, jdbcRow);
            }
            finisher.apply(supplier);
            resultSet.close();
            statement.close();
            promise.complete(new MySqlResult<>(count, 0, 0, (R) supplier, columnDescriptors));
        }
    } catch (Throwable throwable) {
        promise.tryFail(throwable);
    }
}
Also used : DataFormat(io.vertx.mysqlclient.impl.datatype.DataFormat) java.sql(java.sql) Arrays(java.util.Arrays) MetaClusterCurrent(io.mycat.MetaClusterCurrent) AbstractMySqlConnectionImpl.adaptType(io.mycat.vertxmycat.AbstractMySqlConnectionImpl.adaptType) LoggerFactory(org.slf4j.LoggerFactory) MySQLRowDesc(io.vertx.mysqlclient.impl.MySQLRowDesc) Function(java.util.function.Function) IOExecutor(io.mycat.IOExecutor) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) JDBCRow(io.vertx.jdbcclient.impl.JDBCRow) StreamMysqlCollector(io.vertx.mysqlclient.impl.codec.StreamMysqlCollector) BiConsumer(java.util.function.BiConsumer) AbstractMySqlConnectionImpl.toObjects(io.mycat.vertxmycat.AbstractMySqlConnectionImpl.toObjects) DataType(io.vertx.mysqlclient.impl.datatype.DataType) ColumnDefinition(io.vertx.mysqlclient.impl.protocol.ColumnDefinition) Collector(java.util.stream.Collector) io.vertx.sqlclient(io.vertx.sqlclient) JdbcRowMetaData(io.mycat.beans.mycat.JdbcRowMetaData) Logger(org.slf4j.Logger) ColumnDefPacket(io.mycat.beans.mysql.packet.ColumnDefPacket) Promise(io.vertx.core.Promise) PreparedStatement(java.sql.PreparedStatement) Future(io.vertx.core.Future) List(java.util.List) ColumnDescriptor(io.vertx.sqlclient.desc.ColumnDescriptor) ColumnDefPacketImpl(io.mycat.beans.mysql.packet.ColumnDefPacketImpl) MycatPreparedStatementUtil(io.mycat.calcite.executor.MycatPreparedStatementUtil) VertxRowSetImpl(io.vertx.mysqlclient.impl.codec.VertxRowSetImpl) RowDesc(io.vertx.sqlclient.impl.RowDesc) Handler(io.vertx.core.Handler) Collections(java.util.Collections) JDBCRow(io.vertx.jdbcclient.impl.JDBCRow) ArrayList(java.util.ArrayList) Function(java.util.function.Function) ColumnDefPacket(io.mycat.beans.mysql.packet.ColumnDefPacket) DataType(io.vertx.mysqlclient.impl.datatype.DataType) JdbcRowMetaData(io.mycat.beans.mycat.JdbcRowMetaData) MySQLRowDesc(io.vertx.mysqlclient.impl.MySQLRowDesc) StreamMysqlCollector(io.vertx.mysqlclient.impl.codec.StreamMysqlCollector) ColumnDescriptor(io.vertx.sqlclient.desc.ColumnDescriptor) ColumnDefPacketImpl(io.mycat.beans.mysql.packet.ColumnDefPacketImpl) ColumnDefinition(io.vertx.mysqlclient.impl.protocol.ColumnDefinition) MySQLRowDesc(io.vertx.mysqlclient.impl.MySQLRowDesc) RowDesc(io.vertx.sqlclient.impl.RowDesc) BiConsumer(java.util.function.BiConsumer)

Example 3 with StreamMysqlCollector

use of io.vertx.mysqlclient.impl.codec.StreamMysqlCollector in project Mycat2 by MyCATApache.

the class RowSetQuery method execute.

@Override
public Future<RowSet<Row>> execute() {
    VertxRowSetImpl vertxRowSet = new VertxRowSetImpl();
    StreamMysqlCollector streamMysqlCollector = new StreamMysqlCollector() {

        @Override
        public void onColumnDefinitions(MySQLRowDesc columnDefinitions) {
            vertxRowSet.setColumnDescriptor(columnDefinitions.columnDescriptor());
        }

        @Override
        public void onRow(Row row) {
            vertxRowSet.list.add(row);
        }
    };
    return runTextQuery(sql, mySQLClientSession.mySQLClientSession, streamMysqlCollector).map(voidSqlResult -> {
        vertxRowSet.setAffectRow(voidSqlResult.rowCount());
        vertxRowSet.setLastInsertId(voidSqlResult.property(MySQLClient.LAST_INSERTED_ID));
        return vertxRowSet;
    });
}
Also used : MySQLRowDesc(io.vertx.mysqlclient.impl.MySQLRowDesc) StreamMysqlCollector(io.vertx.mysqlclient.impl.codec.StreamMysqlCollector) VertxRowSetImpl(io.vertx.mysqlclient.impl.codec.VertxRowSetImpl) Row(io.vertx.sqlclient.Row)

Example 4 with StreamMysqlCollector

use of io.vertx.mysqlclient.impl.codec.StreamMysqlCollector in project Mycat2 by MyCATApache.

the class VertxMycatTextCollector method onColumnDefEof.

@Override
public void onColumnDefEof(MySQLPacket mySQLPacket, int startPos, int endPos) {
    rowResultDecoder = new MycatVertxRowResultDecoder(collector, new MySQLRowDesc(currentColumnDefList, DataFormat.TEXT));
    this.c = collector.supplier().get();
    this.accumulator = collector.accumulator();
    if (collector instanceof StreamMysqlCollector) {
        MySQLRowDesc mySQLRowDesc = new MySQLRowDesc(this.currentColumnDefList, DataFormat.TEXT);
        ((StreamMysqlCollector) collector).onColumnDefinitions(mySQLRowDesc);
    }
}
Also used : MySQLRowDesc(io.vertx.mysqlclient.impl.MySQLRowDesc) StreamMysqlCollector(io.vertx.mysqlclient.impl.codec.StreamMysqlCollector)

Aggregations

MySQLRowDesc (io.vertx.mysqlclient.impl.MySQLRowDesc)4 StreamMysqlCollector (io.vertx.mysqlclient.impl.codec.StreamMysqlCollector)4 VertxRowSetImpl (io.vertx.mysqlclient.impl.codec.VertxRowSetImpl)3 Future (io.vertx.core.Future)2 Handler (io.vertx.core.Handler)2 Row (io.vertx.sqlclient.Row)2 Collections (java.util.Collections)2 Function (java.util.function.Function)2 Collector (java.util.stream.Collector)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 IOExecutor (io.mycat.IOExecutor)1 MetaClusterCurrent (io.mycat.MetaClusterCurrent)1 MycatException (io.mycat.MycatException)1 JdbcRowMetaData (io.mycat.beans.mycat.JdbcRowMetaData)1 MySQLCommandType (io.mycat.beans.mysql.MySQLCommandType)1 MySQLErrorCode (io.mycat.beans.mysql.MySQLErrorCode)1 ColumnDefPacket (io.mycat.beans.mysql.packet.ColumnDefPacket)1 ColumnDefPacketImpl (io.mycat.beans.mysql.packet.ColumnDefPacketImpl)1 ErrorPacketImpl (io.mycat.beans.mysql.packet.ErrorPacketImpl)1