use of io.mycat.beans.mysql.packet.ColumnDefPacket 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.mycat.beans.mysql.packet.ColumnDefPacket in project Mycat2 by MyCATApache.
the class BinaryResultSetHandler method onBinaryRow.
@Override
public void onBinaryRow(MySQLPacket mySQLPacket, int startPos, int endPos) {
collector.onRowStart();
int nullBitMapStartPos = startPos + 4 + 1;
int nullBitMapEndPos = nullBitMapStartPos + binaryNullBitMapLength;
mySQLPacket.packetReadStartIndex(nullBitMapEndPos);
for (int columnIndex = 0; columnIndex < currentColumnDefList.length; columnIndex++) {
ColumnDefPacket columnDef = currentColumnDefList[columnIndex];
int i = nullBitMapStartPos + (columnIndex + 2) / 8;
byte aByte = mySQLPacket.getByte(i);
boolean isNull = ((aByte & (1 << (columnIndex & 7))) != 0);
int startIndex = mySQLPacket.packetReadStartIndex();
int columnType = columnDef.getColumnType();
if (isNull) {
switch(columnType) {
default:
{
throw new MycatException("unknown field message:{}", columnType);
}
case FIELD_TYPE_DECIMAL:
{
collector.collectNullDecimal(columnIndex, columnDef, columnDef.getColumnDecimals() & 0xff);
break;
}
case FIELD_TYPE_TINY:
{
collector.collectNullTiny(columnIndex, columnDef);
break;
}
case FIELD_TYPE_SHORT:
{
collector.collectNullShort(columnIndex, columnDef);
break;
}
case FIELD_TYPE_LONG:
{
collector.collectNullLong(columnIndex, columnDef);
break;
}
case FIELD_TYPE_FLOAT:
{
collector.collectNullFloat(columnIndex, columnDef);
break;
}
case FIELD_TYPE_DOUBLE:
{
collector.collectNullDouble(columnIndex, columnDef);
break;
}
case FIELD_TYPE_NULL:
{
collector.collectNull(columnIndex, columnDef);
break;
}
case FIELD_TYPE_TIMESTAMP:
{
collector.collectNullTimestamp(columnIndex, columnDef);
break;
}
case FIELD_TYPE_LONGLONG:
{
collector.collectNullLongLong(columnIndex, columnDef);
break;
}
case MySQLFieldsType.FIELD_TYPE_INT24:
{
collector.collectNullInt24(columnIndex, columnDef);
break;
}
case FIELD_TYPE_DATE:
{
collector.collectNullDate(columnIndex, columnDef);
break;
}
case FIELD_TYPE_TIME:
{
collector.collectNullTime(columnIndex, columnDef);
break;
}
case FIELD_TYPE_DATETIME:
{
collector.collectNullDatetime(columnIndex, columnDef);
break;
}
case FIELD_TYPE_YEAR:
{
collector.collectNullYear(columnIndex, columnDef);
break;
}
case FIELD_TYPE_NEWDATE:
{
collector.collectNullNewDate(columnIndex, columnDef);
break;
}
case FIELD_TYPE_VARCHAR:
{
collector.collectNullVarChar(columnIndex, columnDef);
break;
}
case FIELD_TYPE_BIT:
{
collector.collectNullBit(columnIndex, columnDef);
break;
}
case FIELD_TYPE_NEW_DECIMAL:
{
collector.collectNullNewDecimal(columnIndex, columnDef, columnDef.getColumnDecimals() & 0xff);
break;
}
case FIELD_TYPE_ENUM:
{
collector.collectNullEnum(columnIndex, columnDef);
break;
}
case FIELD_TYPE_SET:
{
collector.collectNullSet(columnIndex, columnDef);
break;
}
case FIELD_TYPE_TINY_BLOB:
{
collector.collectNullTinyBlob(columnIndex, columnDef);
break;
}
case FIELD_TYPE_MEDIUM_BLOB:
{
collector.collectNullMediumBlob(columnIndex, columnDef);
break;
}
case FIELD_TYPE_LONG_BLOB:
{
collector.collectNullLongBlob(columnIndex, columnDef);
break;
}
case FIELD_TYPE_BLOB:
{
collector.collectNullBlob(columnIndex, columnDef);
break;
}
case FIELD_TYPE_VAR_STRING:
{
collector.collectNullVarString(columnIndex, columnDef);
break;
}
case FIELD_TYPE_STRING:
{
collector.collectNullTinyString(columnIndex, columnDef);
break;
}
case FIELD_TYPE_GEOMETRY:
{
collector.collectNullGeometry(columnIndex, columnDef);
break;
}
}
continue;
}
/**
* 二进制格式,详细看协议,startIndex 是
* 字符串类型,长度的开始位置;
* 值类型,不带长度
*/
switch(columnType) {
default:
{
throw new MycatException("unknown field message {}", (columnType));
}
case FIELD_TYPE_DECIMAL:
{
collector.collectDecimal(columnIndex, columnDef, columnDef.getColumnDecimals() & 0xff, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_TINY:
{
collector.collectTiny(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_SHORT:
{
collector.collectShort(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_LONG:
{
collector.collectLong(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_FLOAT:
{
collector.collectFloat(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_DOUBLE:
{
collector.collectDouble(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_NULL:
{
collector.collectNull(columnIndex, columnDef);
break;
}
case FIELD_TYPE_TIMESTAMP:
{
collector.collectTimestamp(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_LONGLONG:
{
collector.collectLongLong(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_INT24:
{
collector.collectInt24(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_DATE:
{
collector.collectDate(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_TIME:
{
collector.collectTime(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_DATETIME:
{
collector.collectDatetime(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_YEAR:
{
collector.collectYear(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_NEWDATE:
{
collector.collectNewDate(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_VARCHAR:
{
collector.collectVarChar(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_BIT:
{
collector.collectBit(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_NEW_DECIMAL:
{
collector.collectNewDecimal(columnIndex, columnDef, columnDef.getColumnDecimals() & 0xff, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_ENUM:
{
collector.collectEnum(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_SET:
{
collector.collectSet(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_TINY_BLOB:
{
collector.collectTinyBlob(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_MEDIUM_BLOB:
{
collector.collectMediumBlob(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_LONG_BLOB:
{
collector.collectLongBlob(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_BLOB:
{
collector.collectBlob(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_VAR_STRING:
{
collector.collectVarString(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_STRING:
{
collector.collectTinyString(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_GEOMETRY:
{
collector.collectGeometry(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
}
}
collector.onRowEnd();
}
use of io.mycat.beans.mysql.packet.ColumnDefPacket in project Mycat2 by MyCATApache.
the class TextResultSetHandler method onTextRow.
@Override
public void onTextRow(MySQLPacket mySQLPacket, int startPos, int endPos) throws MycatException {
for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {
int startIndex = mySQLPacket.packetReadStartIndex();
if (!columnFilter(columnIndex)) {
mySQLPacket.skipLenencBytes(startIndex);
continue;
}
ColumnDefPacket columnDef = currentColumnDefList[columnIndex];
int columnType = columnDef.getColumnType();
if (isNull(mySQLPacket, startIndex)) {
mySQLPacket.packetReadStartIndex(startIndex + 1);
switch(columnType) {
default:
{
throw new MycatException("unknown field message:{}", (columnType));
}
case FIELD_TYPE_DECIMAL:
{
collector.collectNullDecimal(columnIndex, columnDef, columnDef.getColumnDecimals() & 0xff);
break;
}
case FIELD_TYPE_TINY:
{
collector.collectNullTiny(columnIndex, columnDef);
break;
}
case FIELD_TYPE_SHORT:
{
collector.collectNullShort(columnIndex, columnDef);
break;
}
case FIELD_TYPE_LONG:
{
collector.collectNullLong(columnIndex, columnDef);
break;
}
case FIELD_TYPE_FLOAT:
{
collector.collectNullFloat(columnIndex, columnDef);
break;
}
case FIELD_TYPE_DOUBLE:
{
collector.collectNullDouble(columnIndex, columnDef);
break;
}
case FIELD_TYPE_NULL:
{
collector.collectNull(columnIndex, columnDef);
break;
}
case FIELD_TYPE_TIMESTAMP:
{
collector.collectNullTimestamp(columnIndex, columnDef);
break;
}
case FIELD_TYPE_LONGLONG:
{
collector.collectNullLongLong(columnIndex, columnDef);
break;
}
case FIELD_TYPE_INT24:
{
collector.collectNullInt24(columnIndex, columnDef);
break;
}
case FIELD_TYPE_DATE:
{
collector.collectNullDate(columnIndex, columnDef);
break;
}
case FIELD_TYPE_TIME:
{
collector.collectNullTime(columnIndex, columnDef);
break;
}
case FIELD_TYPE_DATETIME:
{
collector.collectNullDatetime(columnIndex, columnDef);
break;
}
case FIELD_TYPE_YEAR:
{
collector.collectNullYear(columnIndex, columnDef);
break;
}
case FIELD_TYPE_NEWDATE:
{
collector.collectNullNewDate(columnIndex, columnDef);
break;
}
case FIELD_TYPE_VARCHAR:
{
collector.collectNullVarChar(columnIndex, columnDef);
break;
}
case FIELD_TYPE_BIT:
{
collector.collectNullBit(columnIndex, columnDef);
break;
}
case FIELD_TYPE_NEW_DECIMAL:
{
collector.collectNullNewDecimal(columnIndex, columnDef, columnDef.getColumnDecimals() & 0xff);
break;
}
case FIELD_TYPE_ENUM:
{
collector.collectNullEnum(columnIndex, columnDef);
break;
}
case FIELD_TYPE_SET:
{
collector.collectNullSet(columnIndex, columnDef);
break;
}
case FIELD_TYPE_TINY_BLOB:
{
collector.collectNullTinyBlob(columnIndex, columnDef);
break;
}
case FIELD_TYPE_MEDIUM_BLOB:
{
collector.collectNullMediumBlob(columnIndex, columnDef);
break;
}
case FIELD_TYPE_LONG_BLOB:
{
collector.collectNullLongBlob(columnIndex, columnDef);
break;
}
case FIELD_TYPE_BLOB:
{
collector.collectNullBlob(columnIndex, columnDef);
break;
}
case FIELD_TYPE_VAR_STRING:
{
collector.collectNullVarString(columnIndex, columnDef);
break;
}
case FIELD_TYPE_STRING:
{
collector.collectNullTinyString(columnIndex, columnDef);
break;
}
case FIELD_TYPE_GEOMETRY:
{
collector.collectNullGeometry(columnIndex, columnDef);
break;
}
}
continue;
}
switch(columnType) {
default:
{
throw new MycatException("unknown field message:{}", (columnType));
}
case FIELD_TYPE_DECIMAL:
{
collector.collectDecimal(columnIndex, columnDef, columnDef.getColumnDecimals() & 0xff, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_TINY:
{
collector.collectTiny(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_SHORT:
{
collector.collectShort(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_LONG:
{
collector.collectLong(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_FLOAT:
{
collector.collectFloat(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_DOUBLE:
{
collector.collectDouble(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_NULL:
{
collector.collectNull(columnIndex, columnDef);
break;
}
case FIELD_TYPE_TIMESTAMP:
{
collector.collectTimestamp(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_LONGLONG:
{
collector.collectLongLong(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_INT24:
{
collector.collectInt24(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_DATE:
{
collector.collectDate(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_TIME:
{
collector.collectTime(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_DATETIME:
{
collector.collectDatetime(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_YEAR:
{
collector.collectYear(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_NEWDATE:
{
collector.collectNewDate(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_VARCHAR:
{
collector.collectVarChar(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_BIT:
{
collector.collectBit(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_NEW_DECIMAL:
{
collector.collectNewDecimal(columnIndex, columnDef, columnDef.getColumnDecimals() & 0xff, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_ENUM:
{
collector.collectEnum(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_SET:
{
collector.collectSet(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_TINY_BLOB:
{
collector.collectTinyBlob(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_MEDIUM_BLOB:
{
collector.collectMediumBlob(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_LONG_BLOB:
{
collector.collectLongBlob(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_BLOB:
{
collector.collectBlob(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_VAR_STRING:
{
collector.collectVarString(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_STRING:
{
collector.collectTinyString(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
case FIELD_TYPE_GEOMETRY:
{
collector.collectGeometry(columnIndex, columnDef, mySQLPacket, startIndex);
break;
}
}
int mayBeErrorStartIndex = mySQLPacket.packetReadStartIndex();
if (mySQLPacket.skipLenencBytes(startIndex) == mayBeErrorStartIndex) {
}
}
collector.onRowEnd();
}
use of io.mycat.beans.mysql.packet.ColumnDefPacket in project Mycat2 by MyCATApache.
the class MySQLResultSet method create.
public static MySQLResultSet create(ColumnDefPacket[] columnDefPackets, List<Object[]> rows) {
MySQLResultSet mySQLResultSet = new MySQLResultSet();
for (ColumnDefPacket columnDefPacket : columnDefPackets) {
mySQLResultSet.addColumn(columnDefPacket);
}
mySQLResultSet.rows = rows;
return mySQLResultSet;
}
use of io.mycat.beans.mysql.packet.ColumnDefPacket in project Mycat2 by MyCATApache.
the class OneResultSetCollector method collectColumnList.
public void collectColumnList(ColumnDefPacket[] packets) {
columns = new HashMap<>(packets.length);
for (int i = 0; i < packets.length; i++) {
ColumnDefPacket packet = packets[i];
columns.put(packet.getColumnNameString(), i);
}
result = new ArrayList[packets.length];
for (int i = 0; i < packets.length; i++) {
result[i] = new ArrayList();
columnCount++;
}
}
Aggregations