3 import static org.junit.Assert.*;
5 import static java.lang.Math.ulp;
7 import org.junit.After;
8 import org.junit.Before;
12 import java.text.DateFormat;
13 import java.text.SimpleDateFormat;
14 import java.util.Properties;
15 import java.util.TimeZone;
16 import java.util.concurrent.CompletableFuture;
17 import java.util.concurrent.Future;
18 import java.util.function.Supplier;
22 static final String
url = PROPERTIES.getProperty(
"default_db_connection_url");
23 static final String
user = PROPERTIES.getProperty(
"default_super_user");
24 static final String
password = PROPERTIES.getProperty(
"default_user_password");
29 public void setUp() throws Exception {
30 Properties pt =
new Properties();
31 pt.setProperty(
"user",
user);
32 pt.setProperty(
"password",
password);
33 m_conn = DriverManager.getConnection(
url, pt);
44 +
"m_timestamp TIMESTAMP,"
45 +
"m_timestamp_3 TIMESTAMP(3),"
46 +
"m_timestamp_6 TIMESTAMP(6),"
47 +
"m_timestamp_9 TIMESTAMP(9))";
50 "insert into test_jdbc_tm_tble values ('1910-01-01 00:00:10', '1910-01-01 00:00:10.001', '1910-01-01 00:00:10.555556', '1910-01-01 00:00:10.999999999')";
53 "insert into test_jdbc_tm_tble values ('1969-12-31 23:00:00', '1969-12-31 23:00:00.001', '1969-12-31 23:00:00.000001', '1969-12-31 23:00:00.000000001')";
56 "insert into test_jdbc_tm_tble values ('1970-01-01 00:00:10', '1970-01-01 00:00:10.001', '1970-01-01 00:00:10.000001', '1970-01-01 00:00:10.000000001')";
59 "insert into test_jdbc_tm_tble (m_timestamp, m_timestamp_3, m_timestamp_6, m_timestamp_9) values (?, ?, ?, ?)";
63 "insert into test_jdbc_tm_tble values ('2970-01-01 00:00:10', '2970-01-01 00:00:10.001', '2970-01-01 00:00:10.000001', '2262-04-11 23:47:16.850000001')";
68 Statement statement = m_conn.createStatement();
72 TimeZone.setDefault(TimeZone.getTimeZone(
"GMT"));
73 DateFormat date_format =
new SimpleDateFormat(
"yyyy-MM-dd hh:mm:ss.SSS");
75 java.util.Date date_tm = date_format.parse(
"1918-11-11 11:11:00.000");
76 Timestamp tm =
new Timestamp(date_tm.getTime());
78 date_tm = date_format.parse(
"1918-11-11 11:11:00.001");
79 Timestamp tm_3 =
new Timestamp(date_tm.getTime());
81 date_tm = date_format.parse(
"1918-11-11 11:11:00.000");
82 Timestamp tm_6 =
new Timestamp(date_tm.getTime());
83 tm_6.setNanos(999999000);
85 date_tm = date_format.parse(
"1918-11-11 11:11:00.000");
86 Timestamp tm_9 =
new Timestamp(date_tm.getTime());
87 tm_9.setNanos(123456789);
90 pr.setTimestamp(1, tm);
91 pr.setTimestamp(2, tm_3);
92 pr.setTimestamp(3, tm_6);
93 pr.setTimestamp(4, tm_9);
99 Timestamp r_tm = rs.getTimestamp(
"m_timestamp");
100 assertTrue(r_tm.equals(tm));
101 Timestamp r_tm3 = rs.getTimestamp(
"m_timestamp_3");
102 assertTrue(r_tm3.equals(tm_3));
103 Timestamp r_tm6 = rs.getTimestamp(
"m_timestamp_6");
104 assertTrue(r_tm6.equals(tm_6));
105 Timestamp r_tm9 = rs.getTimestamp(
"m_timestamp_9");
106 assertTrue(r_tm9.equals(tm_9));
114 Statement statement = m_conn.createStatement();
124 for (
int i = 0; rs.next(); ++i) {
125 TimeZone.setDefault(TimeZone.getTimeZone(
"GMT"));
126 Timestamp timestamp = rs.getTimestamp(
"m_timestamp");
128 Timestamp timestamp_3 = rs.getTimestamp(
"m_timestamp_3");
129 Timestamp timestamp_6 = rs.getTimestamp(
"m_timestamp_6");
130 Timestamp timestamp_9 = rs.getTimestamp(
"m_timestamp_9");
132 assertEquals(
"1910-01-01 00:00:10.0", timestamp.toString());
133 assertEquals(
"1910-01-01 00:00:10.001", timestamp_3.toString());
134 assertEquals(
"1910-01-01 00:00:10.555556", timestamp_6.toString());
135 assertEquals(
"1910-01-01 00:00:10.999999999", timestamp_9.toString());
138 assertEquals(
"1969-12-31 23:00:00.0", timestamp.toString());
139 assertEquals(
"1969-12-31 23:00:00.001", timestamp_3.toString());
140 assertEquals(
"1969-12-31 23:00:00.000001", timestamp_6.toString());
141 assertEquals(
"1969-12-31 23:00:00.000000001", timestamp_9.toString());
144 assertEquals(
"1970-01-01 00:00:10.0", timestamp.toString());
145 assertEquals(
"1970-01-01 00:00:10.001", timestamp_3.toString());
146 assertEquals(
"1970-01-01 00:00:10.000001", timestamp_6.toString());
147 assertEquals(
"1970-01-01 00:00:10.000000001", timestamp_9.toString());
150 assertEquals(
"2970-01-01 00:00:10.0", timestamp.toString());
151 assertEquals(
"2970-01-01 00:00:10.001", timestamp_3.toString());
152 assertEquals(
"2970-01-01 00:00:10.000001", timestamp_6.toString());
153 assertEquals(
"2262-04-11 23:47:16.850000001", timestamp_9.toString());
160 static String
sql_drop_tbl =
"drop table if exists test_jdbc_types_tble";
163 +
"m_decimal DECIMAL(8,3),"
168 +
"m_smallint SMALLINT,"
169 +
"m_tinyint TINYINT,"
170 +
"m_boolean BOOLEAN,"
171 +
"m_text_encoded TEXT ENCODING DICT,"
172 +
"m_text_encoded_none TEXT ENCODING NONE,"
175 +
"m_timestamp TIMESTAMP)";
177 static String
sql_insert =
"insert into test_jdbc_types_tble values ("
178 +
"12345.123" + +Integer.MAX_VALUE +
"," + Integer.MAX_VALUE +
","
179 + Float.MAX_VALUE +
"," + Double.MAX_VALUE +
"," + Long.MAX_VALUE +
","
180 + Short.MAX_VALUE +
"," + Byte.MAX_VALUE +
","
182 +
"'String 1 - encoded', 'String 2 - not encoded', '00:00:00', '1970-01-01', '1970-01-01 00:00:00')";
188 Statement statement = m_conn.createStatement();
189 TimeZone.setDefault(TimeZone.getTimeZone(
"GMT"));
193 "select {fn cos(1)} as m_cos, {d '1999-01-09'} as m_date, {t '20:00:03'} as m_time, {ts '1990-01-09 20:00:03'} as m_timestamp, {fn week({d '2005-01-24'})} as m_week";
194 ResultSet rs = statement.executeQuery(d_select);
195 for (
int i = 0; rs.next(); ++i) {
196 Date r_date = rs.getDate(
"m_date");
197 assertEquals(
"1999-01-09", r_date.toString());
198 Time r_time = rs.getTime(
"m_time");
199 assertEquals(
"20:00:03", r_time.toString());
200 Timestamp ts_time = rs.getTimestamp(
"m_timestamp");
201 assertEquals(
"1990-01-09 20:00:03.0", ts_time.toString());
202 double m_cos = rs.getDouble(
"m_cos");
203 assertEquals(
Double.compare(m_cos, 0.5403023058681398), 0);
204 int m_week = rs.getInt(
"m_week");
205 assertEquals(m_week, 4);
208 d_select =
"select {fn FLOOR(-1 * {fn dayofmonth({d '1990-01-31'})})} as WWW";
209 rs = statement.executeQuery(d_select);
210 for (
int i = 0; rs.next(); ++i) {
211 int www = rs.getInt(
"WWW");
212 assertEquals(-31, www);
215 "select {fn FLOOR(-1 * {fn dayofmonth(cast('1990-01-31' as date))})} as XXX";
216 rs = statement.executeQuery(d_select);
217 for (
int i = 0; rs.next(); ++i) {
218 int xxx = rs.getInt(
"XXX");
219 assertEquals(-31, xxx);
222 d_select =
"select {fn floor(1.005)} as YYY limit 1000 {";
224 statement.executeQuery(d_select);
226 }
catch (RuntimeException rE) {
229 d_select =
"select ' {fn floor(1.005)} as YYY limit 1000 {";
231 statement.executeQuery(d_select);
233 }
catch (RuntimeException rE) {
236 d_select =
"select ' {fn floor(1.005)} as YYY limit 1000 }";
238 statement.executeQuery(d_select);
240 }
catch (RuntimeException rE) {
245 Statement statement = m_conn.createStatement();
246 TimeZone.setDefault(TimeZone.getTimeZone(
"GMT"));
248 String sql_drop_tester =
"drop table if exists tester";
250 statement.executeUpdate(sql_drop_tester);
251 String sql_create_tester =
252 "CREATE table tester(Connection_start TIMESTAMP, d_start DATE)";
253 statement.executeUpdate(sql_create_tester);
255 String sql_insert_tester1 =
256 "insert into tester values ('2018-11-08 12:19:59', '2018-11-08')";
257 statement.executeUpdate(sql_insert_tester1);
261 "select COUNT( {fn TIMESTAMPDIFF(SQL_TSI_DAY, {fn TIMESTAMPADD(SQL_TSI_DAY,CAST((-1 * (EXTRACT(DAY FROM tester.d_start) - 1)) AS INTEGER),CAST( tester.d_start AS DATE))}, {fn TIMESTAMPADD(SQL_TSI_DAY,CAST((-1 * (EXTRACT(DAY FROM {fn TIMESTAMPADD(SQL_TSI_MONTH,1, tester.d_start)}) - 1)) AS INTEGER),CAST( {fn TIMESTAMPADD(SQL_TSI_MONTH,1, tester.d_start)} AS DATE)) }) }) AS TEMP from tester";
262 ResultSet rs = statement.executeQuery(e_statement);
263 rs = statement.executeQuery(e_statement);
264 for (
int i = 0; rs.next(); ++i) {
265 int r_count = rs.getInt(
"TEMP");
266 assertEquals(r_count, 1);
270 "SELECT TOP 1000 sum(1) AS sum_Number_of_Records_ok, {fn TIMESTAMPADD(SQL_TSI_HOUR, EXTRACT(HOUR FROM tester.Connection_Start), CAST(tester.Connection_Start as DATE))} AS thr_Connection_Start_ok FROM tester Where ((tester.Connection_Start >= {ts '2018-11-01 00:00:00'}) AND (tester.Connection_Start <= {ts '2018-11-08 23:59:59'})) GROUP BY 2";
271 rs = statement.executeQuery(x_select);
272 for (
int i = 0; rs.next(); ++i) {
273 int r_count = rs.getInt(
"sum_Number_of_Records_ok");
274 assertEquals(r_count, 1);
275 Timestamp ts_time = rs.getTimestamp(
"thr_Connection_Start_ok");
276 assertEquals(ts_time.toString(),
"2018-11-08 12:00:00.0");
280 String d_simple_quarter =
"select quarter(Connection_start) as m_quarter from tester";
281 rs = statement.executeQuery(d_simple_quarter);
282 for (
int i = 0; rs.next(); ++i) {
283 int r_quarter = rs.getInt(
"m_quarter");
284 assertEquals(4, r_quarter);
287 "select quarter(cast('2019-04-03' as date)) as m_quarter from tester";
288 rs = statement.executeQuery(d_simple_quarter);
289 for (
int i = 0; rs.next(); ++i) {
290 int r_quarter = rs.getInt(
"m_quarter");
291 assertEquals(2, r_quarter);
297 Statement statement = m_conn.createStatement();
298 TimeZone.setDefault(TimeZone.getTimeZone(
"GMT"));
301 String d_ceiling =
"select {fn ceiling(1.005)} as YYY";
302 ResultSet rs = statement.executeQuery(d_ceiling);
303 for (
int i = 0; rs.next(); ++i) {
304 float yyy = rs.getFloat(
"YYY");
305 assertEquals(
Float.compare(2.0F, yyy), 0);
309 String d_floor =
"select {fn floor(1.005)} as YYY";
310 rs = statement.executeQuery(d_floor);
311 for (
int i = 0; rs.next(); ++i) {
312 float yyy = rs.getFloat(
"YYY");
313 assertEquals(
Float.compare(1.0F, yyy), 0);
317 String d_ln =
"select {fn log(10)} as m_ln";
318 rs = statement.executeQuery(d_ln);
319 for (
int i = 0; rs.next(); ++i) {
320 double r_ln = rs.getDouble(
"m_ln");
321 assertEquals(
Double.compare(r_ln, 2.302585092994046), 0);
324 d_ln =
"select {fn ln(10)} as m_ln";
325 rs = statement.executeQuery(d_ln);
326 for (
int i = 0; rs.next(); ++i) {
327 double r_ln = rs.getDouble(
"m_ln");
328 assertEquals(
Double.compare(r_ln, 2.302585092994046), 0);
332 String d_log10 =
"select {fn log10(10)} as m_log10";
333 rs = statement.executeQuery(d_log10);
334 for (
int i = 0; rs.next(); ++i) {
335 double r_log10 = rs.getDouble(
"m_log10");
336 assertEquals(
Double.compare(r_log10, 1), 0);
340 String d_abs =
"select {fn abs(-1.45)} as m_abs";
341 rs = statement.executeQuery(d_abs);
342 for (
int i = 0; rs.next(); ++i) {
343 float r_abs = rs.getFloat(
"m_abs");
344 assertEquals(
Float.compare(r_abs, (
float) Math.abs(-1.45)), 0);
348 String d_power =
"select {fn power(2,10)} as YYY";
349 rs = statement.executeQuery(d_power);
350 for (
int i = 0; rs.next(); ++i) {
351 float yyy = rs.getFloat(
"YYY");
352 assertEquals(
Float.compare((
float) Math.pow(2, 10), yyy), 0);
355 String d_truncate =
"select {fn truncate(2.01,1)} as YYY";
356 rs = statement.executeQuery(d_truncate);
357 for (
int i = 0; rs.next(); ++i) {
358 float yyy = rs.getFloat(
"YYY");
359 assertEquals(
Float.compare(2.0F, yyy), 0);
363 String d_length =
"select {fn length('12345')} as YYY";
364 rs = statement.executeQuery(d_length);
365 for (
int i = 0; rs.next(); ++i) {
366 int yyy = rs.getInt(
"YYY");
367 assertEquals(5, yyy);
371 String d_quarter =
"select {fn quarter({d '2005-01-24'})} as m_quarter";
372 rs = statement.executeQuery(d_quarter);
373 for (
int i = 0; rs.next(); ++i) {
374 int r_quarter = rs.getInt(
"m_quarter");
375 assertEquals(1, r_quarter);
379 String d_dayofyear =
"select {fn DAYOFYEAR({d '2005-01-24'})} as m_dayofyear";
380 rs = statement.executeQuery(d_dayofyear);
381 for (
int i = 0; rs.next(); ++i) {
382 int r_dayofyear = rs.getInt(
"m_dayofyear");
383 assertEquals(24, r_dayofyear);
387 String d_dayofweek =
"select {fn dayofweek({d '2005-01-24'})} as m_dayofweek";
388 rs = statement.executeQuery(d_dayofweek);
389 for (
int i = 0; rs.next(); ++i) {
390 int r_dayofweek = rs.getInt(
"m_dayofweek");
391 assertEquals(2, r_dayofweek);
394 String d_dayofmonth =
"select {fn dayofmonth({d '2005-01-24'})} as m_dayofmonth";
395 rs = statement.executeQuery(d_dayofmonth);
396 for (
int i = 0; rs.next(); ++i) {
397 int r_dayofmonth = rs.getInt(
"m_dayofmonth");
398 assertEquals(24, r_dayofmonth);
401 String d_hour =
"select {fn hour({ts '2005-01-24 10:11:12'})} as m_hour";
402 rs = statement.executeQuery(d_hour);
403 for (
int i = 0; rs.next(); ++i) {
404 int r_hour = rs.getInt(
"m_hour");
405 assertEquals(10, r_hour);
410 "select {fn TIMESTAMPADD(SQL_TSI_DAY, 1, CURRENT_DATE())} m, TIMESTAMPADD(SQL_TSI_WEEK, 1, CURRENT_DATE) y";
411 ResultSet rx = statement.executeQuery(d_c);
412 for (
int i = 0; rx.next(); ++i) {
413 Date r_m = rx.getDate(
"m");
414 Date r_y = rx.getDate(
"y");
418 d_c =
"select TIMESTAMPADD(SQL_TSI_DAY, 1, CURRENT_DATE) as m_zzz";
419 rs = statement.executeQuery(d_c);
420 for (
int i = 0; rs.next(); ++i) {
421 Timestamp r_zzz = rs.getTimestamp(
"m_zzz");
424 d_c =
"select TIMESTAMPADD(SQL_TSI_DAY, 1, CURRENT_DATE_TIME) as m_zzz";
426 rs = statement.executeQuery(d_c);
428 }
catch (SQLException sE) {
429 assertTrue(sE.toString().contains(
"CURRENT_DATE_TIME' not found in any table"));
430 }
catch (Exception ex) {
437 Statement statement = m_conn.createStatement();
445 for (; rs.next(); ++i) {
446 int r_int = rs.getInt(
"m_int");
447 assertEquals(
Integer.MAX_VALUE, r_int);
448 float r_float = rs.getFloat(
"m_float");
449 float delta_f = ulp(
Float.MAX_VALUE);
450 assertEquals(
Float.MAX_VALUE, r_float, delta_f);
452 double r_double = rs.getDouble(
"m_double");
453 double delta_d = ulp(
Double.MAX_VALUE);
454 assertEquals(
Double.MAX_VALUE, r_double, delta_d);
456 long r_long = rs.getLong(
"m_bigint");
457 assertEquals(Long.MAX_VALUE, r_long);
459 short r_short = rs.getShort(
"m_smallint");
460 assertEquals(Short.MAX_VALUE, r_short);
462 byte r_byte = (byte) rs.getShort(
"m_tinyint");
463 assertEquals(Byte.MAX_VALUE, r_byte);
465 String decimal_str = rs.getString(
"m_decimal");
466 assertEquals(
"12345.123", decimal_str);
469 byte r_boolean = (byte) rs.getShort(
"m_boolean");
470 assertEquals(0, r_boolean);
472 String r_text_encoded = rs.getString(
"m_text_encoded");
473 assertEquals(
"String 1 - encoded", r_text_encoded);
475 String r_text_encoded_none = rs.getString(
"m_text_encoded_none");
476 assertEquals(
"String 2 - not encoded", r_text_encoded_none);
479 TimeZone.setDefault(TimeZone.getTimeZone(
"GMT"));
481 Timestamp r_timestamp = rs.getTimestamp(
"m_timestamp");
482 assertEquals(
"1970-01-01 00:00:00.0", r_timestamp.toString());
484 Date r_date = rs.getDate(
"m_date");
485 assertEquals(
"1970-01-01", r_date.toString());
487 Time r_time = rs.getTime(
"m_time");
488 assertEquals(
"00:00:00", r_time.toString());
498 Statement statement = m_conn.createStatement();
503 Supplier<Boolean> B = () -> {
507 }
catch (SQLException | InterruptedException e) {
513 Future<Boolean>
result = CompletableFuture.supplyAsync(B);
519 assertEquals(size, 1);
529 assertEquals(size, 2);
543 Statement statement = m_conn.createStatement();
544 statement.executeUpdate(
"DROP TABLE IF EXISTS max_rows_tbl");
545 statement.executeUpdate(
"CREATE TABLE max_rows_tbl (i INTEGER)");
546 statement.executeUpdate(
"INSERT INTO max_rows_tbl VALUES (1)");
547 statement.executeUpdate(
"INSERT INTO max_rows_tbl VALUES (2)");
548 statement.executeUpdate(
"INSERT INTO max_rows_tbl VALUES (3)");
549 ResultSet rs = statement.executeQuery(
"SELECT * FROM max_rows_tbl");
551 statement.setMaxRows(1);
552 rs = statement.executeQuery(
"SELECT * FROM max_rows_tbl");
556 Properties pt =
new Properties();
557 pt.setProperty(
"user",
user);
558 pt.setProperty(
"password",
password);
559 pt.setProperty(
"max_rows",
"2");
560 Connection limited_conn = DriverManager.getConnection(
url, pt);
561 statement = limited_conn.createStatement();
562 rs = statement.executeQuery(
"SELECT * FROM max_rows_tbl");
564 statement.setMaxRows(1);
565 rs = statement.executeQuery(
"SELECT * FROM max_rows_tbl");
568 limited_conn.close();
static String sql_insert_tm_1
static String sql_select_all
static String sql_insert_tm_3
static String sql_drop_tbl_tm
static String sql_insert_tm_4
int resultSetSize(ResultSet rs)
static String sql_drop_tbl
static String sql_create_tbl_tm
static String sql_create_tbl
static final String password
static String sql_select_tm
static String insert_prepare_tm
static Properties PROPERTIES
static String sql_insert_tm_2