|
10 | 10 | import org.elasticsearch.xpack.sql.proto.ColumnInfo;
|
11 | 11 | import org.elasticsearch.xpack.sql.proto.StringUtils;
|
12 | 12 |
|
| 13 | +import java.sql.Date; |
13 | 14 | import java.sql.ResultSet;
|
14 | 15 | import java.sql.ResultSetMetaData;
|
15 | 16 | import java.sql.SQLException;
|
| 17 | +import java.sql.Time; |
16 | 18 | import java.time.Instant;
|
| 19 | +import java.time.LocalDate; |
17 | 20 | import java.time.ZoneId;
|
18 | 21 | import java.time.ZonedDateTime;
|
19 | 22 | import java.util.ArrayList;
|
20 | 23 | import java.util.List;
|
21 | 24 |
|
22 | 25 | import static org.elasticsearch.xpack.sql.action.BasicFormatter.FormatOption.CLI;
|
23 | 26 |
|
24 |
| -public abstract class JdbcTestUtils { |
| 27 | +final class JdbcTestUtils { |
25 | 28 |
|
26 |
| - public static final String SQL_TRACE = "org.elasticsearch.xpack.sql:TRACE"; |
| 29 | + private JdbcTestUtils() {} |
27 | 30 |
|
28 |
| - public static final String JDBC_TIMEZONE = "timezone"; |
29 |
| - |
30 |
| - public static ZoneId UTC = ZoneId.of("Z"); |
| 31 | + private static final int MAX_WIDTH = 20; |
| 32 | + |
| 33 | + static final String SQL_TRACE = "org.elasticsearch.xpack.sql:TRACE"; |
| 34 | + static final String JDBC_TIMEZONE = "timezone"; |
| 35 | + static final LocalDate EPOCH = LocalDate.of(1970, 1, 1); |
31 | 36 |
|
32 |
| - public static void logResultSetMetadata(ResultSet rs, Logger logger) throws SQLException { |
| 37 | + static void logResultSetMetadata(ResultSet rs, Logger logger) throws SQLException { |
33 | 38 | ResultSetMetaData metaData = rs.getMetaData();
|
34 | 39 | // header
|
35 | 40 | StringBuilder sb = new StringBuilder();
|
@@ -59,35 +64,24 @@ public static void logResultSetMetadata(ResultSet rs, Logger logger) throws SQLE
|
59 | 64 | logger.info(sb.toString());
|
60 | 65 | }
|
61 | 66 |
|
62 |
| - private static final int MAX_WIDTH = 20; |
63 |
| - |
64 |
| - public static void logResultSetData(ResultSet rs, Logger log) throws SQLException { |
| 67 | + static void logResultSetData(ResultSet rs, Logger log) throws SQLException { |
65 | 68 | ResultSetMetaData metaData = rs.getMetaData();
|
66 |
| - StringBuilder sb = new StringBuilder(); |
67 |
| - StringBuilder column = new StringBuilder(); |
68 | 69 |
|
69 | 70 | int columns = metaData.getColumnCount();
|
70 | 71 |
|
71 | 72 | while (rs.next()) {
|
72 |
| - sb.setLength(0); |
73 |
| - for (int i = 1; i <= columns; i++) { |
74 |
| - column.setLength(0); |
75 |
| - if (i > 1) { |
76 |
| - sb.append(" | "); |
77 |
| - } |
78 |
| - sb.append(trimOrPad(column.append(rs.getString(i)))); |
79 |
| - } |
80 |
| - log.info(sb); |
| 73 | + log.info(rowAsString(rs, columns)); |
81 | 74 | }
|
82 | 75 | }
|
83 | 76 |
|
84 |
| - public static String resultSetCurrentData(ResultSet rs) throws SQLException { |
| 77 | + static String resultSetCurrentData(ResultSet rs) throws SQLException { |
85 | 78 | ResultSetMetaData metaData = rs.getMetaData();
|
86 |
| - StringBuilder column = new StringBuilder(); |
87 |
| - |
88 |
| - int columns = metaData.getColumnCount(); |
| 79 | + return rowAsString(rs, metaData.getColumnCount()); |
| 80 | + } |
89 | 81 |
|
| 82 | + private static String rowAsString(ResultSet rs, int columns) throws SQLException { |
90 | 83 | StringBuilder sb = new StringBuilder();
|
| 84 | + StringBuilder column = new StringBuilder(); |
91 | 85 | for (int i = 1; i <= columns; i++) {
|
92 | 86 | column.setLength(0);
|
93 | 87 | if (i > 1) {
|
@@ -137,7 +131,18 @@ public static void logLikeCLI(ResultSet rs, Logger logger) throws SQLException {
|
137 | 131 | logger.info("\n" + formatter.formatWithHeader(cols, data));
|
138 | 132 | }
|
139 | 133 |
|
140 |
| - public static String of(long millis, String zoneId) { |
| 134 | + static String of(long millis, String zoneId) { |
141 | 135 | return StringUtils.toString(ZonedDateTime.ofInstant(Instant.ofEpochMilli(millis), ZoneId.of(zoneId)));
|
142 | 136 | }
|
| 137 | + |
| 138 | + static Date asDate(long millis, ZoneId zoneId) { |
| 139 | + return new java.sql.Date( |
| 140 | + ZonedDateTime.ofInstant(Instant.ofEpochMilli(millis), zoneId) |
| 141 | + .toLocalDate().atStartOfDay(zoneId).toInstant().toEpochMilli()); |
| 142 | + } |
| 143 | + |
| 144 | + static Time asTime(long millis, ZoneId zoneId) { |
| 145 | + return new Time(ZonedDateTime.ofInstant(Instant.ofEpochMilli(millis), zoneId) |
| 146 | + .toLocalTime().atDate(JdbcTestUtils.EPOCH).atZone(zoneId).toInstant().toEpochMilli()); |
| 147 | + } |
143 | 148 | }
|
0 commit comments