23
23
import java .nio .file .Path ;
24
24
import java .nio .file .SimpleFileVisitor ;
25
25
import java .nio .file .attribute .BasicFileAttributes ;
26
+ import java .sql .Date ;
26
27
import java .sql .ResultSet ;
27
28
import java .sql .ResultSetMetaData ;
28
29
import java .sql .SQLException ;
30
+ import java .sql .Time ;
29
31
import java .time .Instant ;
32
+ import java .time .LocalDate ;
30
33
import java .time .ZoneId ;
31
34
import java .time .ZonedDateTime ;
32
35
import java .util .ArrayList ;
37
40
38
41
import static org .elasticsearch .xpack .sql .action .BasicFormatter .FormatOption .CLI ;
39
42
40
- public abstract class JdbcTestUtils {
43
+ final class JdbcTestUtils {
41
44
42
- public static final String SQL_TRACE = "org.elasticsearch.xpack.sql:TRACE" ;
45
+ private JdbcTestUtils () {}
43
46
44
- public static final String JDBC_TIMEZONE = "timezone" ;
45
-
46
- public static ZoneId UTC = ZoneId .of ("Z" );
47
+ private static final int MAX_WIDTH = 20 ;
48
+
49
+ static final String SQL_TRACE = "org.elasticsearch.xpack.sql:TRACE" ;
50
+ static final String JDBC_TIMEZONE = "timezone" ;
51
+ static final LocalDate EPOCH = LocalDate .of (1970 , 1 , 1 );
47
52
48
- public static void logResultSetMetadata (ResultSet rs , Logger logger ) throws SQLException {
53
+ static void logResultSetMetadata (ResultSet rs , Logger logger ) throws SQLException {
49
54
ResultSetMetaData metaData = rs .getMetaData ();
50
55
// header
51
56
StringBuilder sb = new StringBuilder ();
@@ -75,35 +80,24 @@ public static void logResultSetMetadata(ResultSet rs, Logger logger) throws SQLE
75
80
logger .info (sb .toString ());
76
81
}
77
82
78
- private static final int MAX_WIDTH = 20 ;
79
-
80
- public static void logResultSetData (ResultSet rs , Logger log ) throws SQLException {
83
+ static void logResultSetData (ResultSet rs , Logger log ) throws SQLException {
81
84
ResultSetMetaData metaData = rs .getMetaData ();
82
- StringBuilder sb = new StringBuilder ();
83
- StringBuilder column = new StringBuilder ();
84
85
85
86
int columns = metaData .getColumnCount ();
86
87
87
88
while (rs .next ()) {
88
- sb .setLength (0 );
89
- for (int i = 1 ; i <= columns ; i ++) {
90
- column .setLength (0 );
91
- if (i > 1 ) {
92
- sb .append (" | " );
93
- }
94
- sb .append (trimOrPad (column .append (rs .getString (i ))));
95
- }
96
- log .info (sb );
89
+ log .info (rowAsString (rs , columns ));
97
90
}
98
91
}
99
92
100
- public static String resultSetCurrentData (ResultSet rs ) throws SQLException {
93
+ static String resultSetCurrentData (ResultSet rs ) throws SQLException {
101
94
ResultSetMetaData metaData = rs .getMetaData ();
102
- StringBuilder column = new StringBuilder ();
103
-
104
- int columns = metaData .getColumnCount ();
95
+ return rowAsString (rs , metaData .getColumnCount ());
96
+ }
105
97
98
+ private static String rowAsString (ResultSet rs , int columns ) throws SQLException {
106
99
StringBuilder sb = new StringBuilder ();
100
+ StringBuilder column = new StringBuilder ();
107
101
for (int i = 1 ; i <= columns ; i ++) {
108
102
column .setLength (0 );
109
103
if (i > 1 ) {
@@ -153,7 +147,7 @@ public static void logLikeCLI(ResultSet rs, Logger logger) throws SQLException {
153
147
logger .info ("\n " + formatter .formatWithHeader (cols , data ));
154
148
}
155
149
156
- public static String of (long millis , String zoneId ) {
150
+ static String of (long millis , String zoneId ) {
157
151
return StringUtils .toString (ZonedDateTime .ofInstant (Instant .ofEpochMilli (millis ), ZoneId .of (zoneId )));
158
152
}
159
153
@@ -165,7 +159,7 @@ public static String of(long millis, String zoneId) {
165
159
* folders in the file-system (typically IDEs) or
166
160
* inside jars (gradle).
167
161
*/
168
- public static List <URL > classpathResources (String pattern ) throws Exception {
162
+ static List <URL > classpathResources (String pattern ) throws Exception {
169
163
while (pattern .startsWith ("/" )) {
170
164
pattern = pattern .substring (1 );
171
165
}
@@ -234,4 +228,15 @@ static Tuple<String, String> pathAndName(String string) {
234
228
}
235
229
return new Tuple <>(folder , file );
236
230
}
237
- }
231
+
232
+ static Date asDate (long millis , ZoneId zoneId ) {
233
+ return new java .sql .Date (
234
+ ZonedDateTime .ofInstant (Instant .ofEpochMilli (millis ), zoneId )
235
+ .toLocalDate ().atStartOfDay (zoneId ).toInstant ().toEpochMilli ());
236
+ }
237
+
238
+ static Time asTime (long millis , ZoneId zoneId ) {
239
+ return new Time (ZonedDateTime .ofInstant (Instant .ofEpochMilli (millis ), zoneId )
240
+ .toLocalTime ().atDate (JdbcTestUtils .EPOCH ).atZone (zoneId ).toInstant ().toEpochMilli ());
241
+ }
242
+ }
0 commit comments