use of io.vertx.mysqlclient.impl.protocol.ColumnDefinition in project Mycat2 by MyCATApache.
the class NewVertxConnectionImpl method resultSetColumnToVectorRowSchema.
public static Schema resultSetColumnToVectorRowSchema(List<ColumnDefinition> columnDefinitions) {
int columnCount = columnDefinitions.size();
ImmutableList.Builder<Field> builder = ImmutableList.builder();
JdbcToArrowConfigBuilder jdbcToArrowConfigBuilder = new JdbcToArrowConfigBuilder();
JdbcToArrowConfig jdbcToArrowConfig = jdbcToArrowConfigBuilder.build();
for (int i = 0; i < columnCount; i++) {
ColumnDefinition columnDefinition = columnDefinitions.get(i);
String columnName = columnDefinition.name();
int columnType = columnDefinition.jdbcType().getVendorTypeNumber();
boolean signed = (columnDefinition.flags() & MySQLFieldsType.UNSIGNED_FLAG) == 0;
boolean nullable = (columnDefinition.flags() & MySQLFieldsType.UNSIGNED_FLAG) == 0;
ArrowType arrowType = (ArrowType) jdbcToArrowConfig.getJdbcToArrowTypeConverter().apply(new JdbcFieldInfo(columnType, 0, 0));
FieldType fieldType = new FieldType(nullable, arrowType, null);
builder.add(new org.apache.arrow.vector.types.pojo.Field(columnName, fieldType, Collections.emptyList()));
}
return new org.apache.arrow.vector.types.pojo.Schema(builder.build());
}
use of io.vertx.mysqlclient.impl.protocol.ColumnDefinition in project Mycat2 by MyCATApache.
the class NewVertxConnectionImpl method call.
@Override
public Future<List<Object>> call(String sql) {
Query<io.vertx.sqlclient.RowSet<Row>> query = mySQLConnection.query(sql);
Future<List<Object>> listFuture = query.execute().map(rowRowSet -> {
ArrayList<Object> resList = new ArrayList<>();
for (; ; ) {
if (rowRowSet.rowCount() == 0 && !rowRowSet.columnsNames().isEmpty()) {
RowIterator<Row> rowIterator = rowRowSet.iterator();
List<ColumnDefinition> columnDescriptors = (List) rowRowSet.columnDescriptors();
MycatRowMetaData mycatRowMetaData = toColumnMetaData(columnDescriptors);
List<Object[]> rows = new LinkedList<>();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
rows.add(normailize(columnDescriptors, row));
}
resList.add(new RowSet(mycatRowMetaData, rows));
} else {
resList.add(new long[] { rowRowSet.rowCount(), rowRowSet.property(MySQLClient.LAST_INSERTED_ID) });
}
rowRowSet = rowRowSet.next();
if (rowRowSet == null || rowRowSet.rowCount() == 0 && rowRowSet.size() == 0) {
break;
}
}
return resList;
});
return mapException(listFuture);
}
use of io.vertx.mysqlclient.impl.protocol.ColumnDefinition in project Mycat2 by MyCATApache.
the class NewVertxConnectionImpl method prepareQuery.
@Override
public void prepareQuery(String sql, List<Object> params, MysqlCollector collector) {
LOGGER.debug("sql:{}", sql);
Future<io.vertx.sqlclient.RowSet<Row>> execute = mySQLConnection.query(sql).execute();
execute = execute.onFailure(event -> collector.onError(event));
execute.onSuccess(event -> {
try {
List<ColumnDefinition> columnDescriptors = (List) event.columnDescriptors();
MycatRowMetaData mycatRowMetaData = toColumnMetaData(columnDescriptors);
collector.onColumnDef(mycatRowMetaData);
int columnCount = mycatRowMetaData.getColumnCount();
RowIterator<Row> iterator = event.iterator();
while (iterator.hasNext()) {
Row next = iterator.next();
Object[] objects = new Object[next.size()];
for (int i = 0; i < columnCount; i++) {
objects[i] = next.getValue(i);
}
collector.onRow(objects);
}
collector.onComplete();
} catch (Exception e) {
collector.onError(e);
}
});
}
use of io.vertx.mysqlclient.impl.protocol.ColumnDefinition in project Mycat2 by MyCATApache.
the class NewVertxConnectionImpl method toColumnMetaData.
public static MycatRowMetaData toColumnMetaData(List<ColumnDefinition> event) {
boolean isMysql = event.get(0) instanceof ColumnDefinition;
if (isMysql) {
List<ColumnDefinition> columnDefinitions = event;
List<ColumnDefPacket> columnDefPackets = new ArrayList<>(event.size());
for (ColumnDefinition columnDefinition : columnDefinitions) {
final String catalog = columnDefinition.catalog();
final String schema = columnDefinition.schema();
final String table = columnDefinition.table();
final String orgTable = columnDefinition.orgTable();
final String name = columnDefinition.name();
final String orgName = columnDefinition.orgName();
final int characterSet = columnDefinition.characterSet();
final long columnLength = columnDefinition.columnLength();
final DataType type = columnDefinition.type();
final int flags = columnDefinition.flags();
byte decimals = columnDefinition.decimals();
if (decimals == 31) {
decimals = 0;
}
ColumnDefPacketImpl mySQLFieldInfo = new ColumnDefPacketImpl();
mySQLFieldInfo.setColumnCatalog(catalog.getBytes());
mySQLFieldInfo.setColumnSchema(schema.getBytes());
mySQLFieldInfo.setColumnTable(table.getBytes());
mySQLFieldInfo.setColumnOrgTable(orgTable.getBytes());
mySQLFieldInfo.setColumnName(name.getBytes());
mySQLFieldInfo.setColumnOrgName(orgName.getBytes());
mySQLFieldInfo.setColumnCharsetSet(characterSet);
mySQLFieldInfo.setColumnLength((int) columnLength);
mySQLFieldInfo.setColumnType(type.id);
mySQLFieldInfo.setColumnFlags(flags);
mySQLFieldInfo.setColumnDecimals(decimals);
columnDefPackets.add(mySQLFieldInfo);
}
return new MycatMySQLRowMetaData(columnDefPackets);
} else {
ResultSetBuilder resultSetBuilder = ResultSetBuilder.create();
for (ColumnDefinition columnDescriptor : event) {
resultSetBuilder.addColumnInfo(columnDescriptor.name(), columnDescriptor.jdbcType());
}
RowBaseIterator build = resultSetBuilder.build();
return build.getMetaData();
}
}
use of io.vertx.mysqlclient.impl.protocol.ColumnDefinition 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);
}
}
Aggregations