Search in sources :

Example 6 with DataType

use of io.vertx.mysqlclient.impl.datatype.DataType in project Mycat2 by MyCATApache.

the class MycatVertxRowResultDecoder method decodeRow.

@Override
protected Row decodeRow(int len, ByteBuf in) {
    Row row = new MySQLRowImpl(rowDesc);
    if (rowDesc.dataFormat() == DataFormat.BINARY) {
        // BINARY row decoding
        // 0x00 packet header
        // null_bitmap
        int nullBitmapLength = (len + 7 + 2) >> 3;
        int nullBitmapIdx = 1 + in.readerIndex();
        in.skipBytes(1 + nullBitmapLength);
        // values
        for (int c = 0; c < len; c++) {
            int val = c + 2;
            int bytePos = val >> 3;
            int bitPos = val & 7;
            byte mask = (byte) (1 << bitPos);
            byte nullByte = (byte) (in.getByte(nullBitmapIdx + bytePos) & mask);
            Object decoded = null;
            if (nullByte == 0) {
                // non-null
                ColumnDefinition columnDef = rowDesc.columnDefinitions()[c];
                DataType dataType = columnDef.type();
                int collationId = rowDesc.columnDefinitions()[c].characterSet();
                int columnDefinitionFlags = columnDef.flags();
                decoded = DataTypeCodec.decodeBinary(dataType, collationId, columnDefinitionFlags, in);
            }
            row.addValue(decoded);
        }
    } else {
        // TEXT row decoding
        for (int c = 0; c < len; c++) {
            Object decoded = null;
            if (in.getUnsignedByte(in.readerIndex()) == NULL) {
                in.skipBytes(1);
            } else {
                DataType dataType = rowDesc.columnDefinitions()[c].type();
                int columnDefinitionFlags = rowDesc.columnDefinitions()[c].flags();
                int collationId = rowDesc.columnDefinitions()[c].characterSet();
                decoded = DataTypeCodec.decodeText(dataType, collationId, columnDefinitionFlags, in);
            }
            row.addValue(decoded);
        }
    }
    return row;
}
Also used : MySQLRowImpl(io.vertx.mysqlclient.impl.MySQLRowImpl) DataType(io.vertx.mysqlclient.impl.datatype.DataType) Row(io.vertx.sqlclient.Row) ColumnDefinition(io.vertx.mysqlclient.impl.protocol.ColumnDefinition)

Example 7 with DataType

use of io.vertx.mysqlclient.impl.datatype.DataType in project Mycat2 by MyCATApache.

the class VertxMycatTextCollector method onColumnDef.

@Override
public void onColumnDef(MySQLPacket mySQLPacket, int startPos, int endPos) {
    ColumnDefPacketImpl packet = new ColumnDefPacketImpl();
    packet.read(mySQLPacket, startPos, endPos);
    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());
    int flags = packet.getColumnFlags();
    byte decimals = packet.getColumnDecimals();
    ColumnDefinition columnDefinition = new ColumnDefinition(catalog, schema, table, orgTable, name, orgName, characterSet, columnLength, type, flags, decimals);
    this.currentColumnDefList[this.columnCount++] = columnDefinition;
}
Also used : DataType(io.vertx.mysqlclient.impl.datatype.DataType) ColumnDefPacketImpl(io.mycat.beans.mysql.packet.ColumnDefPacketImpl) ColumnDefinition(io.vertx.mysqlclient.impl.protocol.ColumnDefinition)

Example 8 with DataType

use of io.vertx.mysqlclient.impl.datatype.DataType in project Mycat2 by MyCATApache.

the class MycatMySQLRowMetaData method getMycatRelDataType.

@Override
public MycatRelDataType getMycatRelDataType() {
    List<MycatField> mycatFields = columnDefPackets.stream().map(new Function<ColumnDefPacket, MycatField>() {

        @Override
        public MycatField apply(ColumnDefPacket columnDefPacket) {
            DataType dataType = DataType.valueOf(columnDefPacket.getColumnType());
            String columnName = new String(columnDefPacket.getColumnName());
            JDBCType jdbcType = dataType.jdbcType;
            boolean nullable = columnDefPacket.isNullable();
            boolean signed = columnDefPacket.isSigned();
            int columnDecimals = columnDefPacket.getColumnDecimals();
            MycatDataType mycatDataType = MycatDataType.VARCHAR;
            boolean text = columnDefPacket.getColumnCharsetSet() != CharsetMapping.MYSQL_COLLATION_INDEX_binary;
            boolean binary = (columnDefPacket.getColumnFlags() & MySQLFieldsType.BINARY_FLAG) != 0;
            boolean blob = (columnDefPacket.getColumnFlags() & MySQLFieldsType.BLOB_FLAG) != 0;
            switch(dataType) {
                case INT1:
                    if (columnName.toLowerCase().contains("bool")) {
                        mycatDataType = MycatDataType.BOOLEAN;
                    } else {
                        mycatDataType = signed ? MycatDataType.TINYINT : MycatDataType.UNSIGNED_TINYINT;
                    }
                    break;
                case INT2:
                    mycatDataType = signed ? MycatDataType.SHORT : MycatDataType.UNSIGNED_SHORT;
                    break;
                case INT3:
                case INT4:
                    mycatDataType = signed ? MycatDataType.INT : MycatDataType.UNSIGNED_INT;
                    break;
                case INT8:
                    mycatDataType = signed ? MycatDataType.LONG : MycatDataType.UNSIGNED_LONG;
                    break;
                case DOUBLE:
                    mycatDataType = MycatDataType.DOUBLE;
                    break;
                case FLOAT:
                    mycatDataType = MycatDataType.FLOAT;
                    break;
                case NUMERIC:
                    mycatDataType = MycatDataType.DECIMAL;
                    break;
                case STRING:
                    if (blob || binary) {
                        mycatDataType = MycatDataType.BINARY;
                    } else {
                        mycatDataType = MycatDataType.CHAR;
                    }
                    break;
                case VARSTRING:
                    if (blob || binary) {
                        mycatDataType = MycatDataType.BINARY;
                    } else {
                        mycatDataType = MycatDataType.VARCHAR;
                    }
                    break;
                case TINYBLOB:
                case BLOB:
                case MEDIUMBLOB:
                case LONGBLOB:
                    if (text) {
                        mycatDataType = MycatDataType.VARCHAR;
                    } else {
                        mycatDataType = MycatDataType.BINARY;
                    }
                    break;
                case DATE:
                    mycatDataType = MycatDataType.DATE;
                    break;
                case TIME:
                    mycatDataType = MycatDataType.TIME;
                    break;
                case TIMESTAMP:
                case DATETIME:
                    mycatDataType = MycatDataType.DATETIME;
                    break;
                case YEAR:
                    mycatDataType = MycatDataType.YEAR;
                    break;
                case BIT:
                    if (columnDefPacket.getColumnLength() == 1) {
                        mycatDataType = MycatDataType.BOOLEAN;
                    } else {
                        mycatDataType = MycatDataType.BIT;
                    }
                    break;
                case JSON:
                case GEOMETRY:
                    mycatDataType = MycatDataType.VARCHAR;
                    break;
                case NULL:
                    mycatDataType = MycatDataType.NULL;
                    break;
                case UNBIND:
                    throw new UnsupportedOperationException();
            }
            return MycatField.of(columnName, mycatDataType, nullable, columnDecimals, 0);
        }
    }).collect(Collectors.toList());
    return MycatRelDataType.of(mycatFields);
}
Also used : Function(java.util.function.Function) ColumnDefPacket(io.mycat.beans.mysql.packet.ColumnDefPacket) JDBCType(java.sql.JDBCType) DataType(io.vertx.mysqlclient.impl.datatype.DataType)

Example 9 with DataType

use of io.vertx.mysqlclient.impl.datatype.DataType 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 10 with DataType

use of io.vertx.mysqlclient.impl.datatype.DataType in project Mycat2 by MyCATApache.

the class ObjectArrayDecoder method convert.

@Override
public Object[] convert(Buffer payload) {
    try {
        pos = 0;
        final int NULL = 0xFB;
        Object[] row = new Object[columnCount];
        for (int c = 0; c < columnCount; c++) {
            if ((payload.getByte(pos) & 0xff) == NULL) {
                pos++;
            } else {
                int startPos = pos;
                int length = (int) readLengthEncodedInteger(payload);
                Object value = null;
                DataType dataType = this.dataTypes[c];
                switch(dataType) {
                    case INT1:
                    case INT2:
                    case INT3:
                    case INT4:
                    case INT8:
                        {
                            boolean signed = this.signeds[c];
                            long l;
                            switch(dataType) {
                                case INT1:
                                    {
                                        l = PacketUtil.decodeDecStringToLong(pos, length, payload);
                                        if (signed) {
                                            value = (byte) l;
                                        } else {
                                            value = (short) l;
                                        }
                                        break;
                                    }
                                case INT2:
                                    {
                                        l = PacketUtil.decodeDecStringToLong(pos, length, payload);
                                        if (signed) {
                                            value = (short) l;
                                        } else {
                                            value = (int) l;
                                        }
                                        break;
                                    }
                                case INT3:
                                    {
                                        l = PacketUtil.decodeDecStringToLong(pos, length, payload);
                                        value = (int) l;
                                        break;
                                    }
                                case INT4:
                                    {
                                        l = PacketUtil.decodeDecStringToLong(pos, length, payload);
                                        if (signed) {
                                            value = (int) l;
                                        } else {
                                            value = (long) l;
                                        }
                                        break;
                                    }
                                case INT8:
                                    {
                                        if (signed) {
                                            value = PacketUtil.decodeDecStringToLong(pos, length, payload);
                                            break;
                                        } else {
                                            String string = payload.getString(pos, pos + length);
                                            value = new BigInteger(string);
                                        }
                                        break;
                                    }
                            }
                            break;
                        }
                    case DOUBLE:
                        {
                            String string = payload.getString(pos, pos + length);
                            value = Double.parseDouble(string);
                            break;
                        }
                    case FLOAT:
                        {
                            String string = payload.getString(pos, pos + length);
                            value = Float.parseFloat(string);
                            break;
                        }
                    case NUMERIC:
                        {
                            String string = payload.getString(pos, pos + length);
                            value = new BigDecimal(string);
                            break;
                        }
                    case JSON:
                    case GEOMETRY:
                    case STRING:
                    case VARSTRING:
                        if (columnDefPackets[c].getColumnCharsetSet() == 63) {
                            value = payload.getBytes(pos, pos + length);
                        } else {
                            value = payload.getString(pos, pos + length);
                        }
                        break;
                    case TINYBLOB:
                    case BLOB:
                    case MEDIUMBLOB:
                    case LONGBLOB:
                        value = payload.getBytes(pos, pos + length);
                        break;
                    case DATE:
                        {
                            try {
                                String string = payload.getString(pos, pos + length);
                                if (string.equals("0000-00-00")) {
                                    value = null;
                                } else {
                                    value = LocalDate.parse(string);
                                }
                            } catch (Exception e) {
                                System.out.println();
                            }
                            break;
                        }
                    case TIME:
                        {
                            value = PacketUtil.textDecodeTime(payload.getString(pos, pos + length));
                            break;
                        }
                    case TIMESTAMP:
                    case DATETIME:
                        String cs = payload.getString(pos, pos + length);
                        value = PacketUtil.textDecodeDateTime(cs);
                        break;
                    case YEAR:
                        value = PacketUtil.textDecodeDateTime(payload.getString(pos, pos + length));
                        break;
                    case BIT:
                        value = PacketUtil.decodeBit(payload, pos, pos + length);
                        break;
                    case NULL:
                        value = null;
                        break;
                    case UNBIND:
                        throw new UnsupportedOperationException();
                }
                pos = pos + length;
                row[c] = value;
            }
        }
        return row;
    } finally {
    }
}
Also used : DataType(io.vertx.mysqlclient.impl.datatype.DataType) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal)

Aggregations

DataType (io.vertx.mysqlclient.impl.datatype.DataType)11 ColumnDefinition (io.vertx.mysqlclient.impl.protocol.ColumnDefinition)6 ColumnDefPacket (io.mycat.beans.mysql.packet.ColumnDefPacket)3 ColumnDefPacketImpl (io.mycat.beans.mysql.packet.ColumnDefPacketImpl)3 ByteBuf (io.netty.buffer.ByteBuf)2 MySQLRowImpl (io.vertx.mysqlclient.impl.MySQLRowImpl)2 Row (io.vertx.sqlclient.Row)2 BigDecimal (java.math.BigDecimal)2 Function (java.util.function.Function)2 IOExecutor (io.mycat.IOExecutor)1 MetaClusterCurrent (io.mycat.MetaClusterCurrent)1 RowBaseIterator (io.mycat.api.collector.RowBaseIterator)1 JdbcRowMetaData (io.mycat.beans.mycat.JdbcRowMetaData)1 MycatMySQLRowMetaData (io.mycat.beans.mycat.MycatMySQLRowMetaData)1 MycatRelDataType (io.mycat.beans.mycat.MycatRelDataType)1 ResultSetBuilder (io.mycat.beans.mycat.ResultSetBuilder)1 MycatPreparedStatementUtil (io.mycat.calcite.executor.MycatPreparedStatementUtil)1 AbstractMySqlConnectionImpl.adaptType (io.mycat.vertxmycat.AbstractMySqlConnectionImpl.adaptType)1 AbstractMySqlConnectionImpl.toObjects (io.mycat.vertxmycat.AbstractMySqlConnectionImpl.toObjects)1 Future (io.vertx.core.Future)1