修改查询语句

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,6 +79,83 @@ public class MySqlDataSource extends BaseDataAbsSource {
// } // }
return null; 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 @Override
public DataValue[][] getRows() { public DataValue[][] getRows() {
@ -93,21 +170,19 @@ public class MySqlDataSource extends BaseDataAbsSource {
HikariDataSource hikariDataSource = HikariPool.getHikariDataSource(dataSources); HikariDataSource hikariDataSource = HikariPool.getHikariDataSource(dataSources);
Connection conn = null; Connection conn = null;
// try { PreparedStatement preparedStatement = null;
// ResultSet resultSet = null;
// } catch (SQLException e) {
// throw new RuntimeException(e);
// }
log.info("one------------"+one+"------------two"+two);
DataValue[][] dataValues = new DataValue[one][two];
try { try {
conn = hikariDataSource.getConnection(); conn = hikariDataSource.getConnection();
preparedStatement = conn.prepareStatement(sql);
PreparedStatement preparedStatement = conn.prepareStatement(sql); resultSet = preparedStatement.executeQuery();
ResultSet resultSet = preparedStatement.executeQuery();
ResultSetMetaData metaData = resultSet.getMetaData(); ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount(); int columnCount = metaData.getColumnCount();
int c = 0; int c = 1;
DataValue[][] dataValues = new DataValue[one][two];
while (resultSet.next()) { while (resultSet.next()) {
for (int i = 1; i <= columnCount; i++) { for (int i = 1; i <= columnCount; i++) {
if (resultSet.isFirst()) { if (resultSet.isFirst()) {
@ -118,7 +193,6 @@ public class MySqlDataSource extends BaseDataAbsSource {
while (columns.next()) { while (columns.next()) {
remarks = columns.getString("REMARKS"); remarks = columns.getString("REMARKS");
log.info("字段备注:" + remarks); log.info("字段备注:" + remarks);
} }
DataValue build = DataValue.builder() DataValue build = DataValue.builder()
@ -128,7 +202,9 @@ public class MySqlDataSource extends BaseDataAbsSource {
.type(DataType.findBySqlType(columnTypeName)) .type(DataType.findBySqlType(columnTypeName))
.build(); .build();
map.put(i, build); map.put(i, build);
dataValues[c][i-1]=build; if (c <= one && i <= columnCount) {
dataValues[c - 1][i - 1] = build;
}
} else { } else {
DataValue build = DataValue.builder() DataValue build = DataValue.builder()
.key(metaData.getColumnName(i)) .key(metaData.getColumnName(i))
@ -136,22 +212,36 @@ public class MySqlDataSource extends BaseDataAbsSource {
.value(resultSet.getObject(i, map.get(i).getType().getTargetType())) .value(resultSet.getObject(i, map.get(i).getType().getTargetType()))
.type(map.get(i).getType()) .type(map.get(i).getType())
.build(); .build();
dataValues[c][i-1]=build; if (c <= one && i <= columnCount) {
dataValues[c - 1][i - 1] = build;
} }
} }
}
if (c <= one) {
c++; c++;
} }
}
return dataValues;
} catch (SQLException e) { } catch (SQLException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} finally { } finally {
close(hikariDataSource); close(conn, preparedStatement, resultSet);
}
} }
return dataValues; private static void close(Connection conn, PreparedStatement preparedStatement, ResultSet resultSet) {
try {
if (resultSet != null) {
resultSet.close();
} }
private static void close(HikariDataSource dataSource) { if (preparedStatement != null) {
if (dataSource != null) { preparedStatement.close();
dataSource.close(); }
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
// log.log(Level.SEVERE, "Error closing resources", e);
} }
} }
} }