Skip to content

Commit a26629e

Browse files
committed
Update SQL.java
1 parent 27385a7 commit a26629e

1 file changed

Lines changed: 35 additions & 8 deletions

File tree

  • src/main/java/io/github/intisy/utils/custom

src/main/java/io/github/intisy/utils/custom/SQL.java

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -258,23 +258,50 @@ private List<String> getTableColumns(String tableName, DatabaseMetaData metaData
258258
}
259259

260260
private void logTableContents(String tableName, List<String> columns) throws SQLException {
261-
logger.log("\n=== Table: " + tableName + " ===");
262-
logger.log(String.join(" | ", columns));
263-
logger.log(String.join("", Collections.nCopies(columns.size() * 20, "-")));
264-
261+
List<List<String>> rows = new ArrayList<>();
265262
String sql = "SELECT * FROM " + tableName;
266263
try (Statement stmt = getConnection().createStatement();
267264
ResultSet rs = stmt.executeQuery(sql)) {
268265
while (rs.next()) {
269-
StringBuilder row = new StringBuilder();
266+
List<String> row = new ArrayList<>();
270267
for (String column : columns) {
271-
if (row.length() > 0) row.append(" | ");
272268
String value = rs.getString(column);
273-
row.append(value == null ? "NULL" : value);
269+
row.add(value == null ? "NULL" : value);
270+
}
271+
rows.add(row);
272+
}
273+
}
274+
List<String> lines = new ArrayList<>();
275+
int index = 0;
276+
for (String column : columns) {
277+
int length = column.length();
278+
for (List<String> row : rows) {
279+
length = Math.max(length, row.get(index).length());
280+
}
281+
String header = column + String.join("", Collections.nCopies(length - column.length(), " "));
282+
String divider = String.join("", Collections.nCopies(length, "-"));
283+
List<String> content = new ArrayList<>();
284+
for (List<String> row : rows) {
285+
content.add(row.get(index) + String.join("", Collections.nCopies(length - row.get(index).length(), " ")));
286+
}
287+
if (lines.isEmpty()) {
288+
lines.add(header);
289+
lines.add(divider);
290+
lines.addAll(content);
291+
} else {
292+
lines.set(0, lines.get(0) + " | " + header);
293+
lines.set(1, lines.get(1) + "---" + divider);
294+
for (int i = 2; i - 2 < content.size(); i++) {
295+
lines.set(i, lines.get(i) + " | " + content.get(i - 2));
274296
}
275-
logger.log(row.toString());
276297
}
298+
index++;
277299
}
300+
String title = "Table: " + tableName;
301+
String divider = String.join("", Collections.nCopies(Math.max((lines.get(0).length() - title.length()) / 2, 0), "-"));
302+
logger.log("\n" + divider + title + divider);
303+
for (String line : lines)
304+
logger.log(line);
278305
}
279306

280307
private Connection getConnection() throws SQLException {

0 commit comments

Comments
 (0)