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);
}
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);
}
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;
}
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;
}
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());
}
Aggregations