Skip to content

Commit

Permalink
Add Trino in pom.xml and Add ResultSetUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
zekai427 committed Aug 13, 2021
1 parent e4bbcee commit 47c6d55
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
7 changes: 7 additions & 0 deletions java/openmldb-batch/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,13 @@
<version>0.9.1</version>
</dependency>

<!-- Trino -->
<dependency>
<groupId>io.trino</groupId>
<artifactId>trino-jdbc</artifactId>
<version>360</version>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com._4paradigm.openmldb.batch.utils;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;

/**
* Print the data in the result set into a table
*/
public class ResultSetUtil {
public static void printResultSet(ResultSet rs) throws SQLException {
ResultSetMetaData resultSetMetaData = rs.getMetaData();
int ColumnCount = resultSetMetaData.getColumnCount();
int[] columnMaxLengths = new int[ColumnCount];
// Save data in ArrayList
ArrayList<String[]> results = new ArrayList<>();
while (rs.next()) {
String[] columnStr = new String[ColumnCount];
for (int i = 0; i < ColumnCount; i++) {
columnStr[i] = rs.getString(i + 1);
if (columnStr[i] != null) {
columnMaxLengths[i] = Math.max(columnMaxLengths[i],
Math.max(columnStr[i].length(), resultSetMetaData.getColumnName(i + 1).length()));
} else {
columnMaxLengths[i] = Math.max(columnMaxLengths[i],
Math.max(4, resultSetMetaData.getColumnName(i + 1).length()));
}
}
results.add(columnStr);
}
printSeparator(columnMaxLengths);
printColumnName(resultSetMetaData, columnMaxLengths);
printSeparator(columnMaxLengths);
Iterator<String[]> iterator = results.iterator();
String[] columnStr;
while (iterator.hasNext()) {
columnStr = iterator.next();
for (int i = 0; i < ColumnCount; i++) {
System.out.printf("|%" + columnMaxLengths[i] + "s", columnStr[i]);
}
System.out.println("|");
}
printSeparator(columnMaxLengths);
}

private static void printColumnName(ResultSetMetaData resultSetMetaData, int[] columnMaxLengths) throws SQLException {
int columnCount = resultSetMetaData.getColumnCount();
for (int i = 0; i < columnCount; i++) {
System.out.printf("|%" + columnMaxLengths[i] + "s", resultSetMetaData.getColumnName(i + 1));
}
System.out.println("|");
}

private static void printSeparator(int[] columnMaxLengths) {
for (int columnMaxLength : columnMaxLengths) {
System.out.print("+");
for (int j = 0; j < columnMaxLength; j++) {
System.out.print("-");
}
}
System.out.println("+");
}

}

0 comments on commit 47c6d55

Please sign in to comment.