use of io.vertx.sqlclient.desc.ColumnDescriptor in project vertx-sql-client by eclipse-vertx.
the class BinaryDataTypeDecodeTestBase method testDecodeGeneric.
protected <T> void testDecodeGeneric(TestContext ctx, String columnName, Class<? extends T> clazz, JDBCType jdbcType, T expected) {
connector.connect(ctx.asyncAssertSuccess(conn -> {
conn.preparedQuery("SELECT " + columnName + " FROM basicdatatype WHERE id = 1").execute(ctx.asyncAssertSuccess(result -> {
ctx.assertEquals(1, result.size());
Row row = result.iterator().next();
ctx.assertEquals(expected, row.getValue(0));
ctx.assertEquals(expected, row.getValue(columnName));
ColumnDescriptor columnDescriptor = result.columnDescriptors().get(0);
ctx.assertEquals(jdbcType, columnDescriptor.jdbcType());
verifyTypeName(ctx, columnDescriptor);
conn.close();
}));
}));
}
use of io.vertx.sqlclient.desc.ColumnDescriptor in project Mycat2 by MyCATApache.
the class RowSetJdbcPreparedJdbcQuery method innerExecute.
private RowSet<Row> innerExecute(Tuple tuple) throws SQLException {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(sql);
}
try (PreparedStatement preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
setParams(tuple, preparedStatement);
if (!preparedStatement.execute()) {
VertxRowSetImpl vertxRowSet = new VertxRowSetImpl();
vertxRowSet.setAffectRow(preparedStatement.getUpdateCount());
ResultSet generatedKeys = preparedStatement.getGeneratedKeys();
if (generatedKeys != null) {
if (generatedKeys.next()) {
Number object = (Number) generatedKeys.getObject(1);
if (object != null) {
vertxRowSet.setLastInsertId(object.longValue());
}
}
}
return (vertxRowSet);
}
ResultSet resultSet = preparedStatement.getResultSet();
if (resultSet == null) {
return new VertxRowSetImpl();
}
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));
}
});
}
VertxRowSetImpl vertxRowSet = new VertxRowSetImpl();
RowDesc rowDesc = new RowDesc(metaData.getColumnList(), columnDescriptors);
while (resultSet.next()) {
JDBCRow jdbcRow = new MycatRow(rowDesc);
for (int i = 0; i < columnCount; i++) {
jdbcRow.addValue(resultSet.getObject(i + 1));
}
vertxRowSet.list.add(jdbcRow);
}
return (vertxRowSet);
}
}
use of io.vertx.sqlclient.desc.ColumnDescriptor 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.sqlclient.desc.ColumnDescriptor in project Mycat2 by MyCATApache.
the class BaseRowObservable method toColumnMetaData.
public static MycatRowMetaData toColumnMetaData(List<ColumnDescriptor> event) {
ResultSetBuilder resultSetBuilder = ResultSetBuilder.create();
for (ColumnDescriptor columnDescriptor : event) {
if (columnDescriptor instanceof ColumnDefinition) {
ColumnDefinition columnDefinition = (ColumnDefinition) columnDescriptor;
String schemaName = columnDefinition.schema();
String tableName = columnDefinition.orgTable();
String columnName = columnDefinition.name();
int columnType = columnDefinition.type().jdbcType.getVendorTypeNumber();
int precision = 0;
int scale = 0;
String columnLabel = columnDefinition.name();
boolean isAutoIncrement = false;
boolean isCaseSensitive = false;
boolean isNullable = (columnDefinition.flags() & ColumnDefinition.ColumnDefinitionFlags.NOT_NULL_FLAG) == 0;
boolean isSigned = true;
int displaySize = (int) columnDefinition.columnLength();
resultSetBuilder.addColumnInfo(schemaName, tableName, columnName, columnType, precision, scale, columnLabel, isAutoIncrement, isCaseSensitive, isNullable, isSigned, displaySize);
} else {
resultSetBuilder.addColumnInfo(columnDescriptor.name(), columnDescriptor.jdbcType());
}
}
RowBaseIterator build = resultSetBuilder.build();
return build.getMetaData();
}
Aggregations