use of io.mycat.beans.mycat.MycatRowMetaData in project Mycat2 by MyCATApache.
the class RowIteratorUtil method dumpColumnInfo.
public static String dumpColumnInfo(RowBaseIterator iterator) {
StringBuilder sb = new StringBuilder();
MycatRowMetaData mycatRowMetaData = iterator.getMetaData();
int columnCount = mycatRowMetaData.getColumnCount();
while (iterator.next()) {
for (int i = 0; i < columnCount; i++) {
sb.append(mycatRowMetaData.getColumnName(i)).append(":").append(Objects.toString(iterator.getObject(i))).append(" | ");
}
sb.append("\n");
}
return sb.toString();
}
use of io.mycat.beans.mycat.MycatRowMetaData in project Mycat2 by MyCATApache.
the class MycatResultSet method of.
@SneakyThrows
public static MycatResultSet of(List<ResultSet> resultSets) {
ResultSet resultSet = resultSets.get(0);
MycatRowMetaData mycatRowMetaData = new CopyMycatRowMetaData(new JdbcRowMetaData(resultSet.getMetaData()));
int columnCount = mycatRowMetaData.getColumnCount();
List<List<Object>> rows = new ArrayList<>();
for (ResultSet set : resultSets) {
while (set.next()) {
List<Object> row = new ArrayList<>();
for (int i = 0; i < columnCount; i++) {
row.add(set.getObject(i + 1));
}
rows.add(row);
}
}
return new MycatResultSet(mycatRowMetaData, rows);
}
use of io.mycat.beans.mycat.MycatRowMetaData in project Mycat2 by MyCATApache.
the class PrototypeService method getCreateTableSQLByJDBC.
public Optional<String> getCreateTableSQLByJDBC(String schemaName, String tableName, List<Partition> backends) {
Optional<JdbcConnectionManager> jdbcConnectionManagerOptional = getPrototypeConnectionManager();
if (!jdbcConnectionManagerOptional.isPresent()) {
return Optional.empty();
}
JdbcConnectionManager jdbcConnectionManager = jdbcConnectionManagerOptional.get();
backends = new ArrayList<>(backends);
backends.add(new BackendTableInfo(PROTOTYPE, schemaName, tableName));
if (backends == null || backends.isEmpty()) {
return Optional.empty();
}
for (Partition backend : backends) {
try {
Partition backendTableInfo = backend;
String targetName = backendTableInfo.getTargetName();
String targetSchemaTable = backendTableInfo.getTargetSchemaTable();
try (DefaultConnection connection = jdbcConnectionManager.getConnection(targetName)) {
String sql = "SHOW CREATE TABLE " + targetSchemaTable;
SQLStatement sqlStatement = null;
try (RowBaseIterator rowBaseIterator = connection.executeQuery(sql)) {
rowBaseIterator.next();
String string = rowBaseIterator.getString(1);
sqlStatement = SQLUtils.parseSingleMysqlStatement(string);
if (sqlStatement instanceof MySqlCreateTableStatement) {
MySqlCreateTableStatement sqlStatement1 = (MySqlCreateTableStatement) sqlStatement;
sqlStatement1.setTableName(SQLUtils.normalize(tableName));
// 顺序不能颠倒
sqlStatement1.setSchema(SQLUtils.normalize(schemaName));
return Optional.of(sqlStatement1.toString());
}
} catch (Exception e) {
LOGGER.error("", e);
}
if (sqlStatement == null || !(sqlStatement instanceof SQLCreateTableStatement)) {
try (RowBaseIterator rowBaseIterator = connection.executeQuery("select * from " + targetSchemaTable + " where 0 limit 0")) {
MycatRowMetaData metaData = rowBaseIterator.getMetaData();
String createTableSql = generateSql(schemaName, tableName, metaData.metaData());
return Optional.of(createTableSql);
}
}
continue;
} catch (Exception e) {
LOGGER.error("", e);
}
continue;
} catch (Throwable e) {
LOGGER.error("can not get create table sql from:" + backend.getTargetName() + backend.getTargetSchemaTable(), e);
continue;
}
}
return Optional.empty();
}
use of io.mycat.beans.mycat.MycatRowMetaData in project Mycat2 by MyCATApache.
the class VisualTableHandler method createByMySQL.
public static VisualTableHandler createByMySQL(String sql, Supplier<Observable<Object[]>> rowsProvider) {
SQLStatement sqlStatement1 = SQLUtils.parseSingleMysqlStatement(sql);
MySqlCreateTableStatement sqlStatement = (MySqlCreateTableStatement) sqlStatement1;
String schema = SQLUtils.normalize(sqlStatement.getSchema()).toLowerCase();
String tableName = SQLUtils.normalize(sqlStatement.getTableName()).toLowerCase();
MycatRowMetaData mycatRowMetaData = SQL2ResultSetUtil.getMycatRowMetaData((MySqlCreateTableStatement) sqlStatement);
List<SimpleColumnInfo> columnInfo = CalciteConvertors.getColumnInfo(Objects.requireNonNull(mycatRowMetaData));
LogicTable logicTable = new LogicTable(LogicTableType.NORMAL, schema, tableName, columnInfo, Collections.emptyMap(), sql);
return new VisualTableHandler(logicTable) {
@Override
public Observable<Object[]> scanAll() {
return rowsProvider.get();
}
};
}
use of io.mycat.beans.mycat.MycatRowMetaData in project Mycat2 by MyCATApache.
the class BaseRowObservable method onRow.
@Override
public void onRow(Row row) {
MycatRowMetaData metaData = this.metaData;
int columnCount = this.metaData.getColumnCount();
Object[] objects = getObjects(row, metaData);
observer.onNext(objects);
}
Aggregations