修改查询语句

master
lwj 2024-09-09 02:02:23 +08:00
parent 62696c7ae8
commit d02f078468
1 changed files with 121 additions and 31 deletions

View File

@ -79,9 +79,86 @@ public class MySqlDataSource extends BaseDataAbsSource {
// }
return null;
}
// 第一种方法
// @Override
// public DataValue[][] getRows () {
// MySqlQuery query = getQuery();
// Integer one = Math.toIntExact(query.getOne());
// Integer two = query.getTwo();
// String sql = query.getSql();
// Long dataSourceId = query.getDataSourceId();
// ConcurrentHashMap<Integer, DataValue> map = new ConcurrentHashMap<>();
// Source dataSources = sourceService.getById(dataSourceId);
//
// HikariDataSource hikariDataSource = HikariPool.getHikariDataSource(dataSources);
//
// Connection conn = null;
//// try {
////
//// } catch (SQLException e) {
//// throw new RuntimeException(e);
//// }
// log.info("one------------"+one+"------------two"+two);
// DataValue[][] dataValues = new DataValue[one][two];
// try {
// conn = hikariDataSource.getConnection();
//
// PreparedStatement preparedStatement = conn.prepareStatement(sql);
// ResultSet resultSet = preparedStatement.executeQuery();
// ResultSetMetaData metaData = resultSet.getMetaData();
// int columnCount = metaData.getColumnCount();
// int c = 1;
// while (resultSet.next()){
// for (int i = 1; i <= columnCount; i++) {
// if (resultSet.isFirst()){
// String columnTypeName = metaData.getColumnTypeName(i);
// DatabaseMetaData metaDataColumns = conn.getMetaData();
// ResultSet columns = metaDataColumns.getColumns(null, null, metaData.getTableName(i), metaData.getColumnName(i));
// String remarks =null;
//
// while (columns.next()){
// remarks = columns.getString("REMARKS");
//
// log.info("字段备注:"+remarks);
// }
// DataValue build = DataValue.builder()
// .key(metaData.getColumnName(i))
// .label(remarks)
// .value(resultSet.getObject(i, DataType.convertType(columnTypeName)))
// .type(DataType.findBySqlType(columnTypeName))
// .build();
// map.put(i,build);
// dataValues[c][i-1]=build;
// }else {
// DataValue build = DataValue.builder()
// .key(metaData.getColumnName(i))
// .label(map.get(i).getLabel())
// .value(resultSet.getObject(i, map.get(i).getType().getTargetType()))
// .type(map.get(i).getType())
// .build();
// dataValues[c][i-1]=build;
// }
// }
// c++;
// }
// } catch (SQLException e) {
// throw new RuntimeException(e);
// }finally {
// close(hikariDataSource);
// }
//
// return dataValues;
// }
// private static void close(HikariDataSource dataSource) {
// if (dataSource != null) {
// dataSource.close();
// }
// }
@Override
public DataValue[][] getRows () {
public DataValue[][] getRows() {
MySqlQuery query = getQuery();
Integer one = Math.toIntExact(query.getOne());
Integer two = query.getTwo();
@ -92,34 +169,31 @@ public class MySqlDataSource extends BaseDataAbsSource {
HikariDataSource hikariDataSource = HikariPool.getHikariDataSource(dataSources);
Connection conn = null;
// try {
//
// } catch (SQLException e) {
// throw new RuntimeException(e);
// }
log.info("one------------"+one+"------------two"+two);
DataValue[][] dataValues = new DataValue[one][two];
Connection conn = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
conn = hikariDataSource.getConnection();
PreparedStatement preparedStatement = conn.prepareStatement(sql);
ResultSet resultSet = preparedStatement.executeQuery();
preparedStatement = conn.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
int c = 0;
while (resultSet.next()){
int c = 1;
DataValue[][] dataValues = new DataValue[one][two];
while (resultSet.next()) {
for (int i = 1; i <= columnCount; i++) {
if (resultSet.isFirst()){
if (resultSet.isFirst()) {
String columnTypeName = metaData.getColumnTypeName(i);
DatabaseMetaData metaDataColumns = conn.getMetaData();
ResultSet columns = metaDataColumns.getColumns(null, null, metaData.getTableName(i), metaData.getColumnName(i));
String remarks =null;
String remarks = null;
while (columns.next()){
while (columns.next()) {
remarks = columns.getString("REMARKS");
log.info("字段备注:"+remarks);
log.info("字段备注:" + remarks);
}
DataValue build = DataValue.builder()
.key(metaData.getColumnName(i))
@ -127,31 +201,47 @@ public class MySqlDataSource extends BaseDataAbsSource {
.value(resultSet.getObject(i, DataType.convertType(columnTypeName)))
.type(DataType.findBySqlType(columnTypeName))
.build();
map.put(i,build);
dataValues[c][i-1]=build;
}else {
map.put(i, build);
if (c <= one && i <= columnCount) {
dataValues[c - 1][i - 1] = build;
}
} else {
DataValue build = DataValue.builder()
.key(metaData.getColumnName(i))
.label(map.get(i).getLabel())
.value(resultSet.getObject(i, map.get(i).getType().getTargetType()))
.type(map.get(i).getType())
.build();
dataValues[c][i-1]=build;
if (c <= one && i <= columnCount) {
dataValues[c - 1][i - 1] = build;
}
}
}
c++;
if (c <= one) {
c++;
}
}
return dataValues;
} catch (SQLException e) {
throw new RuntimeException(e);
}finally {
close(hikariDataSource);
} finally {
close(conn, preparedStatement, resultSet);
}
return dataValues;
}
private static void close(HikariDataSource dataSource) {
if (dataSource != null) {
dataSource.close();
private static void close(Connection conn, PreparedStatement preparedStatement, ResultSet resultSet) {
try {
if (resultSet != null) {
resultSet.close();
}
if (preparedStatement != null) {
preparedStatement.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
// log.log(Level.SEVERE, "Error closing resources", e);
}
}
}