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