Search in sources :

Example 6 with ColumnDefinition

use of io.vertx.mysqlclient.impl.protocol.ColumnDefinition in project Mycat2 by MyCATApache.

the class NewVertxConnectionImpl method resultSetColumnToVectorRowSchema.

public static Schema resultSetColumnToVectorRowSchema(List<ColumnDefinition> columnDefinitions) {
    int columnCount = columnDefinitions.size();
    ImmutableList.Builder<Field> builder = ImmutableList.builder();
    JdbcToArrowConfigBuilder jdbcToArrowConfigBuilder = new JdbcToArrowConfigBuilder();
    JdbcToArrowConfig jdbcToArrowConfig = jdbcToArrowConfigBuilder.build();
    for (int i = 0; i < columnCount; i++) {
        ColumnDefinition columnDefinition = columnDefinitions.get(i);
        String columnName = columnDefinition.name();
        int columnType = columnDefinition.jdbcType().getVendorTypeNumber();
        boolean signed = (columnDefinition.flags() & MySQLFieldsType.UNSIGNED_FLAG) == 0;
        boolean nullable = (columnDefinition.flags() & MySQLFieldsType.UNSIGNED_FLAG) == 0;
        ArrowType arrowType = (ArrowType) jdbcToArrowConfig.getJdbcToArrowTypeConverter().apply(new JdbcFieldInfo(columnType, 0, 0));
        FieldType fieldType = new FieldType(nullable, arrowType, null);
        builder.add(new org.apache.arrow.vector.types.pojo.Field(columnName, fieldType, Collections.emptyList()));
    }
    return new org.apache.arrow.vector.types.pojo.Schema(builder.build());
}
Also used : JdbcToArrowConfigBuilder(org.apache.arrow.adapter.jdbc.JdbcToArrowConfigBuilder) JdbcFieldInfo(org.apache.arrow.adapter.jdbc.JdbcFieldInfo) ImmutableList(com.google.common.collect.ImmutableList) Schema(org.apache.arrow.vector.types.pojo.Schema) ArrowType(org.apache.arrow.vector.types.pojo.ArrowType) Field(org.apache.arrow.vector.types.pojo.Field) ColumnDefinition(io.vertx.mysqlclient.impl.protocol.ColumnDefinition) FieldType(org.apache.arrow.vector.types.pojo.FieldType) Field(org.apache.arrow.vector.types.pojo.Field) org.apache.arrow.vector(org.apache.arrow.vector) JdbcToArrowConfig(org.apache.arrow.adapter.jdbc.JdbcToArrowConfig)

Example 7 with ColumnDefinition

use of io.vertx.mysqlclient.impl.protocol.ColumnDefinition in project Mycat2 by MyCATApache.

the class NewVertxConnectionImpl method call.

@Override
public Future<List<Object>> call(String sql) {
    Query<io.vertx.sqlclient.RowSet<Row>> query = mySQLConnection.query(sql);
    Future<List<Object>> listFuture = query.execute().map(rowRowSet -> {
        ArrayList<Object> resList = new ArrayList<>();
        for (; ; ) {
            if (rowRowSet.rowCount() == 0 && !rowRowSet.columnsNames().isEmpty()) {
                RowIterator<Row> rowIterator = rowRowSet.iterator();
                List<ColumnDefinition> columnDescriptors = (List) rowRowSet.columnDescriptors();
                MycatRowMetaData mycatRowMetaData = toColumnMetaData(columnDescriptors);
                List<Object[]> rows = new LinkedList<>();
                while (rowIterator.hasNext()) {
                    Row row = rowIterator.next();
                    rows.add(normailize(columnDescriptors, row));
                }
                resList.add(new RowSet(mycatRowMetaData, rows));
            } else {
                resList.add(new long[] { rowRowSet.rowCount(), rowRowSet.property(MySQLClient.LAST_INSERTED_ID) });
            }
            rowRowSet = rowRowSet.next();
            if (rowRowSet == null || rowRowSet.rowCount() == 0 && rowRowSet.size() == 0) {
                break;
            }
        }
        return resList;
    });
    return mapException(listFuture);
}
Also used : ColumnDefinition(io.vertx.mysqlclient.impl.protocol.ColumnDefinition) ImmutableList(com.google.common.collect.ImmutableList) MycatRowMetaData(io.mycat.beans.mycat.MycatRowMetaData)

Example 8 with ColumnDefinition

use of io.vertx.mysqlclient.impl.protocol.ColumnDefinition in project Mycat2 by MyCATApache.

the class NewVertxConnectionImpl method prepareQuery.

@Override
public void prepareQuery(String sql, List<Object> params, MysqlCollector collector) {
    LOGGER.debug("sql:{}", sql);
    Future<io.vertx.sqlclient.RowSet<Row>> execute = mySQLConnection.query(sql).execute();
    execute = execute.onFailure(event -> collector.onError(event));
    execute.onSuccess(event -> {
        try {
            List<ColumnDefinition> columnDescriptors = (List) event.columnDescriptors();
            MycatRowMetaData mycatRowMetaData = toColumnMetaData(columnDescriptors);
            collector.onColumnDef(mycatRowMetaData);
            int columnCount = mycatRowMetaData.getColumnCount();
            RowIterator<Row> iterator = event.iterator();
            while (iterator.hasNext()) {
                Row next = iterator.next();
                Object[] objects = new Object[next.size()];
                for (int i = 0; i < columnCount; i++) {
                    objects[i] = next.getValue(i);
                }
                collector.onRow(objects);
            }
            collector.onComplete();
        } catch (Exception e) {
            collector.onError(e);
        }
    });
}
Also used : Schema(org.apache.arrow.vector.types.pojo.Schema) MycatRowMetaData(io.mycat.beans.mycat.MycatRowMetaData) SneakyThrows(lombok.SneakyThrows) LoggerFactory(org.slf4j.LoggerFactory) JdbcFieldInfo(org.apache.arrow.adapter.jdbc.JdbcFieldInfo) ObservableOnSubscribe(io.reactivex.rxjava3.core.ObservableOnSubscribe) MySQLPool(io.vertx.mysqlclient.MySQLPool) RootAllocator(org.apache.arrow.memory.RootAllocator) Duration(java.time.Duration) LocalTime(java.time.LocalTime) DataType(io.vertx.mysqlclient.impl.datatype.DataType) BufferAllocator(org.apache.arrow.memory.BufferAllocator) ColumnDefPacket(io.mycat.beans.mysql.packet.ColumnDefPacket) FieldType(org.apache.arrow.vector.types.pojo.FieldType) SQLReplaceable(com.alibaba.druid.sql.ast.SQLReplaceable) Field(org.apache.arrow.vector.types.pojo.Field) StandardCharsets(java.nio.charset.StandardCharsets) CountDownLatch(java.util.concurrent.CountDownLatch) Buffer(io.vertx.core.buffer.Buffer) ColumnDescriptor(io.vertx.sqlclient.desc.ColumnDescriptor) LocalDate(java.time.LocalDate) NotNull(org.jetbrains.annotations.NotNull) java.util(java.util) ResultSetBuilder(io.mycat.beans.mycat.ResultSetBuilder) io.vertx.core(io.vertx.core) ArrowType(org.apache.arrow.vector.types.pojo.ArrowType) MySQLClient(io.vertx.mysqlclient.MySQLClient) MySQLConnectionImpl(io.vertx.mysqlclient.impl.MySQLConnectionImpl) NonNull(io.reactivex.rxjava3.annotations.NonNull) MySQLException(io.vertx.mysqlclient.MySQLException) Function(java.util.function.Function) JdbcToArrowConfigBuilder(org.apache.arrow.adapter.jdbc.JdbcToArrowConfigBuilder) SQLException(java.sql.SQLException) MySqlASTVisitorAdapter(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter) ImmutableList(com.google.common.collect.ImmutableList) MycatMySQLRowMetaData(io.mycat.beans.mycat.MycatMySQLRowMetaData) Observable(io.reactivex.rxjava3.core.Observable) RowBaseIterator(io.mycat.api.collector.RowBaseIterator) MySQLConnectOptions(io.vertx.mysqlclient.MySQLConnectOptions) ColumnDefinition(io.vertx.mysqlclient.impl.protocol.ColumnDefinition) MycatRelDataType(io.mycat.beans.mycat.MycatRelDataType) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) io.vertx.sqlclient(io.vertx.sqlclient) SQLUtils(com.alibaba.druid.sql.SQLUtils) Logger(org.slf4j.Logger) Numeric(io.vertx.sqlclient.data.Numeric) org.apache.arrow.vector(org.apache.arrow.vector) ObservableEmitter(io.reactivex.rxjava3.core.ObservableEmitter) MySQLFieldsType(io.mycat.beans.mysql.MySQLFieldsType) JdbcToArrowConfig(org.apache.arrow.adapter.jdbc.JdbcToArrowConfig) ColumnDefPacketImpl(io.mycat.beans.mysql.packet.ColumnDefPacketImpl) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) MySQLException(io.vertx.mysqlclient.MySQLException) SQLException(java.sql.SQLException) ColumnDefinition(io.vertx.mysqlclient.impl.protocol.ColumnDefinition) ImmutableList(com.google.common.collect.ImmutableList) MycatRowMetaData(io.mycat.beans.mycat.MycatRowMetaData)

Example 9 with ColumnDefinition

use of io.vertx.mysqlclient.impl.protocol.ColumnDefinition in project Mycat2 by MyCATApache.

the class NewVertxConnectionImpl method toColumnMetaData.

public static MycatRowMetaData toColumnMetaData(List<ColumnDefinition> event) {
    boolean isMysql = event.get(0) instanceof ColumnDefinition;
    if (isMysql) {
        List<ColumnDefinition> columnDefinitions = event;
        List<ColumnDefPacket> columnDefPackets = new ArrayList<>(event.size());
        for (ColumnDefinition columnDefinition : columnDefinitions) {
            final String catalog = columnDefinition.catalog();
            final String schema = columnDefinition.schema();
            final String table = columnDefinition.table();
            final String orgTable = columnDefinition.orgTable();
            final String name = columnDefinition.name();
            final String orgName = columnDefinition.orgName();
            final int characterSet = columnDefinition.characterSet();
            final long columnLength = columnDefinition.columnLength();
            final DataType type = columnDefinition.type();
            final int flags = columnDefinition.flags();
            byte decimals = columnDefinition.decimals();
            if (decimals == 31) {
                decimals = 0;
            }
            ColumnDefPacketImpl mySQLFieldInfo = new ColumnDefPacketImpl();
            mySQLFieldInfo.setColumnCatalog(catalog.getBytes());
            mySQLFieldInfo.setColumnSchema(schema.getBytes());
            mySQLFieldInfo.setColumnTable(table.getBytes());
            mySQLFieldInfo.setColumnOrgTable(orgTable.getBytes());
            mySQLFieldInfo.setColumnName(name.getBytes());
            mySQLFieldInfo.setColumnOrgName(orgName.getBytes());
            mySQLFieldInfo.setColumnCharsetSet(characterSet);
            mySQLFieldInfo.setColumnLength((int) columnLength);
            mySQLFieldInfo.setColumnType(type.id);
            mySQLFieldInfo.setColumnFlags(flags);
            mySQLFieldInfo.setColumnDecimals(decimals);
            columnDefPackets.add(mySQLFieldInfo);
        }
        return new MycatMySQLRowMetaData(columnDefPackets);
    } else {
        ResultSetBuilder resultSetBuilder = ResultSetBuilder.create();
        for (ColumnDefinition columnDescriptor : event) {
            resultSetBuilder.addColumnInfo(columnDescriptor.name(), columnDescriptor.jdbcType());
        }
        RowBaseIterator build = resultSetBuilder.build();
        return build.getMetaData();
    }
}
Also used : ResultSetBuilder(io.mycat.beans.mycat.ResultSetBuilder) MycatMySQLRowMetaData(io.mycat.beans.mycat.MycatMySQLRowMetaData) ColumnDefinition(io.vertx.mysqlclient.impl.protocol.ColumnDefinition) ColumnDefPacketImpl(io.mycat.beans.mysql.packet.ColumnDefPacketImpl) ColumnDefPacket(io.mycat.beans.mysql.packet.ColumnDefPacket) DataType(io.vertx.mysqlclient.impl.datatype.DataType) MycatRelDataType(io.mycat.beans.mycat.MycatRelDataType) RowBaseIterator(io.mycat.api.collector.RowBaseIterator)

Example 10 with ColumnDefinition

use of io.vertx.mysqlclient.impl.protocol.ColumnDefinition 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)

Aggregations

ColumnDefinition (io.vertx.mysqlclient.impl.protocol.ColumnDefinition)13 DataType (io.vertx.mysqlclient.impl.datatype.DataType)7 ColumnDefPacketImpl (io.mycat.beans.mysql.packet.ColumnDefPacketImpl)4 ImmutableList (com.google.common.collect.ImmutableList)3 RowBaseIterator (io.mycat.api.collector.RowBaseIterator)3 ResultSetBuilder (io.mycat.beans.mycat.ResultSetBuilder)3 ColumnDefPacket (io.mycat.beans.mysql.packet.ColumnDefPacket)3 ColumnDescriptor (io.vertx.sqlclient.desc.ColumnDescriptor)3 MycatMySQLRowMetaData (io.mycat.beans.mycat.MycatMySQLRowMetaData)2 MycatRelDataType (io.mycat.beans.mycat.MycatRelDataType)2 MycatRowMetaData (io.mycat.beans.mycat.MycatRowMetaData)2 MySQLRowImpl (io.vertx.mysqlclient.impl.MySQLRowImpl)2 io.vertx.sqlclient (io.vertx.sqlclient)2 Row (io.vertx.sqlclient.Row)2 Duration (java.time.Duration)2 Function (java.util.function.Function)2 JdbcFieldInfo (org.apache.arrow.adapter.jdbc.JdbcFieldInfo)2 JdbcToArrowConfig (org.apache.arrow.adapter.jdbc.JdbcToArrowConfig)2 JdbcToArrowConfigBuilder (org.apache.arrow.adapter.jdbc.JdbcToArrowConfigBuilder)2 org.apache.arrow.vector (org.apache.arrow.vector)2