Search in sources :

Example 1 with ColumnDefPacket

use of io.mycat.beans.mysql.packet.ColumnDefPacket 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 2 with ColumnDefPacket

use of io.mycat.beans.mysql.packet.ColumnDefPacket in project Mycat2 by MyCATApache.

the class PrototypeService method getShowDatabasesColumns.

@NotNull
private List<ColumnDefPacket> getShowDatabasesColumns() {
    String Catalog = "def";
    String Database = "";
    String Table = "SCHEMATA";
    String OriginalTable = "";
    String Name = "Database";
    String OriginalName = "Database";
    int CharsetNumber = 33;
    int Length = 192;
    int Type = 253;
    int Flags = 0;
    byte Decimals = 0;
    ColumnDefPacket column = createColumn(Catalog, Database, Table, OriginalTable, Name, OriginalName, CharsetNumber, Length, Type, Flags, Decimals);
    return Arrays.asList(column);
}
Also used : ColumnDefPacket(io.mycat.beans.mysql.packet.ColumnDefPacket) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with ColumnDefPacket

use of io.mycat.beans.mysql.packet.ColumnDefPacket in project Mycat2 by MyCATApache.

the class NewMycatConnectionImpl method getJdbcRowMetaData.

@NotNull
private MycatRowMetaData getJdbcRowMetaData(ResultSetMetaData jdbcMetaData) throws SQLException {
    MycatRowMetaData mycatRowMetaData;
    if (isMySQLDriver || "com.mysql.cj.jdbc.result.ResultSetMetaData".equals(jdbcMetaData.getClass().getCanonicalName())) {
        isMySQLDriver = true;
        com.mysql.cj.jdbc.result.ResultSetMetaData mysqlJdbcMetaData = (com.mysql.cj.jdbc.result.ResultSetMetaData) jdbcMetaData;
        int columnCount = mysqlJdbcMetaData.getColumnCount();
        Field[] fields = mysqlJdbcMetaData.getFields();
        List<ColumnDefPacket> columnDefPackets = new ArrayList<>(columnCount);
        for (int i = 0; i < columnCount; i++) {
            ColumnDefPacketImpl columnDefPacket = new ColumnDefPacketImpl();
            Field field = fields[i];
            columnDefPacket.setColumnSchema(ColumnDefPacketImpl.getBytes(field.getDatabaseName()));
            columnDefPacket.setColumnTable(ColumnDefPacketImpl.getBytes(field.getTableName()));
            columnDefPacket.setColumnOrgTable(ColumnDefPacketImpl.getBytes(field.getOriginalTableName()));
            columnDefPacket.setColumnName(ColumnDefPacketImpl.getBytes(field.getName()));
            columnDefPacket.setColumnOrgName(ColumnDefPacketImpl.getBytes(field.getOriginalName()));
            columnDefPacket.setColumnCharsetSet((field.getCollationIndex()));
            columnDefPacket.setColumnLength((int) field.getLength());
            columnDefPacket.setColumnType((int) field.getMysqlTypeId());
            columnDefPacket.setColumnFlags((int) field.getFlags());
            columnDefPacket.setColumnDecimals((byte) field.getDecimals());
            columnDefPackets.add(columnDefPacket);
        }
        mycatRowMetaData = new MycatMySQLRowMetaData(columnDefPackets);
    } else {
        mycatRowMetaData = new CopyMycatRowMetaData(new JdbcRowMetaData(jdbcMetaData));
    }
    return mycatRowMetaData;
}
Also used : ArrayList(java.util.ArrayList) ColumnDefPacketImpl(io.mycat.beans.mysql.packet.ColumnDefPacketImpl) Field(com.mysql.cj.result.Field) ColumnDefPacket(io.mycat.beans.mysql.packet.ColumnDefPacket) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with ColumnDefPacket

use of io.mycat.beans.mysql.packet.ColumnDefPacket in project Mycat2 by MyCATApache.

the class StringArrayDecoder method convert.

@Override
public Object[] convert(Buffer payload) {
    final int NULL = 0xFB;
    Object[] row = new Object[columnCount][];
    ReadView readView = new ReadView(payload);
    // TEXT row decoding
    for (int c = 0; c < columnCount; c++) {
        if ((readView.getByte() & 0xff) == NULL) {
            readView.skipInReading(1);
        } else {
            ColumnDefPacket columnDefPacket = columnDefPackets[c];
            int columnFlags = columnDefPacket.getColumnFlags();
            int columnType = columnDefPacket.getColumnType();
            if (columnDefPacket.getColumnCharsetSet() == 63) {
                row[c] = readView.readLenencBytes();
            } else {
                row[c] = readView.readLenencString();
            }
        }
    }
    return row;
}
Also used : ColumnDefPacket(io.mycat.beans.mysql.packet.ColumnDefPacket) ReadView(io.mycat.vertx.ReadView)

Example 5 with ColumnDefPacket

use of io.mycat.beans.mysql.packet.ColumnDefPacket in project Mycat2 by MyCATApache.

the class ShowWarningsSQLHandler method onExecute.

@Override
protected Future<Void> onExecute(SQLRequest<MySqlShowWarningsStatement> request, MycatDataContext dataContext, Response response) {
    List<ColumnDefPacket> columnDefPacketList = getShowWarningsColumns();
    MySQLResultSet mySQLResultSet = MySQLResultSet.create(columnDefPacketList);
    mySQLResultSet.setRows(Collections.emptyList());
    return response.sendResultSet(mySQLResultSet.build());
}
Also used : ColumnDefPacket(io.mycat.beans.mysql.packet.ColumnDefPacket) MySQLResultSet(io.mycat.prototypeserver.mysql.MySQLResultSet)

Aggregations

ColumnDefPacket (io.mycat.beans.mysql.packet.ColumnDefPacket)16 MySQLResultSet (io.mycat.prototypeserver.mysql.MySQLResultSet)6 ColumnDefPacketImpl (io.mycat.beans.mysql.packet.ColumnDefPacketImpl)3 DataType (io.vertx.mysqlclient.impl.datatype.DataType)3 ArrayList (java.util.ArrayList)3 MycatException (io.mycat.MycatException)2 ColumnDefinition (io.vertx.mysqlclient.impl.protocol.ColumnDefinition)2 Function (java.util.function.Function)2 NotNull (org.jetbrains.annotations.NotNull)2 SQLName (com.alibaba.druid.sql.ast.SQLName)1 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)1 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)1 SQLCreateDatabaseStatement (com.alibaba.druid.sql.ast.statement.SQLCreateDatabaseStatement)1 SQLShowCreateTableStatement (com.alibaba.druid.sql.ast.statement.SQLShowCreateTableStatement)1 Field (com.mysql.cj.result.Field)1 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