use of io.vertx.mysqlclient.impl.codec.VertxRowSetImpl 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.mysqlclient.impl.codec.VertxRowSetImpl in project Mycat2 by MyCATApache.
the class RowSetMySqlPreparedTextQuery method executeBatch.
@Override
public Future<RowSet<Row>> executeBatch(List<Tuple> batch) {
Future<Void> future = Future.succeededFuture();
List<long[]> list = new ArrayList<>();
for (Tuple tuple : batch) {
String eachSql = apply(sql, toObjects(tuple));
future = future.flatMap(unused -> {
Query<RowSet<Row>> query = connection.query(eachSql);
return query.execute().map(rows -> {
list.add(new long[] { rows.rowCount(), rows.property(MySQLClient.LAST_INSERTED_ID) });
return null;
});
});
}
return future.map(unused -> {
long[] reduce = list.stream().reduce(new long[] { 0, 0 }, (longs, longs2) -> new long[] { longs[0] + longs2[0], Math.max(longs[1], longs2[1]) });
VertxRowSetImpl vertxRowSet = new VertxRowSetImpl();
vertxRowSet.setAffectRow(reduce[0]);
vertxRowSet.setLastInsertId(reduce[1]);
return vertxRowSet;
});
}
use of io.vertx.mysqlclient.impl.codec.VertxRowSetImpl in project Mycat2 by MyCATApache.
the class RowSetQuery method execute.
@Override
public Future<RowSet<Row>> execute() {
VertxRowSetImpl vertxRowSet = new VertxRowSetImpl();
StreamMysqlCollector streamMysqlCollector = new StreamMysqlCollector() {
@Override
public void onColumnDefinitions(MySQLRowDesc columnDefinitions) {
vertxRowSet.setColumnDescriptor(columnDefinitions.columnDescriptor());
}
@Override
public void onRow(Row row) {
vertxRowSet.list.add(row);
}
};
return runTextQuery(sql, mySQLClientSession.mySQLClientSession, streamMysqlCollector).map(voidSqlResult -> {
vertxRowSet.setAffectRow(voidSqlResult.rowCount());
vertxRowSet.setLastInsertId(voidSqlResult.property(MySQLClient.LAST_INSERTED_ID));
return vertxRowSet;
});
}
Aggregations