1
0
mirror of https://github.com/apache/nifi.git synced 2025-03-02 07:29:13 +00:00

NIFI-7208: Fixed PutSQL/JdbcCommon handling of timestamps (nanoseconds, e.g.)

This commit is contained in:
Matthew Burgess 2020-02-26 18:21:11 -05:00 committed by markap14
parent 4b6de8d164
commit 74b1b2fc59
2 changed files with 18 additions and 16 deletions
nifi-nar-bundles
nifi-extension-utils/nifi-database-utils/src/main/java/org/apache/nifi/util/db
nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard

@ -79,7 +79,6 @@ import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.Date;
import java.util.Map;
import java.util.function.Function;
@ -773,10 +772,11 @@ public class JdbcCommon {
stmt.setTime(parameterIndex, time);
break;
case Types.TIMESTAMP:
long lTimestamp=0L;
Timestamp ts;
// Backwards compatibility note: Format was unsupported for a timestamp field.
if (valueFormat.equals("")) {
long lTimestamp = 0L;
if(LONG_PATTERN.matcher(parameterValue).matches()){
lTimestamp = Long.parseLong(parameterValue);
} else {
@ -784,15 +784,14 @@ public class JdbcCommon {
java.util.Date parsedDate = dateFormat.parse(parameterValue);
lTimestamp = parsedDate.getTime();
}
ts = new Timestamp(lTimestamp);
} else {
final DateTimeFormatter dtFormatter = getDateTimeFormatter(valueFormat);
TemporalAccessor accessor = dtFormatter.parse(parameterValue);
java.util.Date parsedDate = java.util.Date.from(Instant.from(accessor));
lTimestamp = parsedDate.getTime();
LocalDateTime ldt = LocalDateTime.parse(parameterValue, dtFormatter);
ts = Timestamp.from(ldt.atZone(ZoneId.of("UTC")).toInstant());
}
stmt.setTimestamp(parameterIndex, new Timestamp(lTimestamp));
stmt.setTimestamp(parameterIndex, ts);
break;
case Types.BINARY:
case Types.VARBINARY:

@ -434,16 +434,20 @@ public class TestPutSQL {
runner.enableControllerService(service);
runner.setProperty(PutSQL.CONNECTION_POOL, "dbcp");
final String dateStr = "2002-02-02T12:02:02+00:00";
final long dateInt = 1012651322000L;
final String dateStr1 = "2002-02-02T12:02:02+00:00";
final long dateInt1 = 1012651322000L;
final String dateStr2 = "2002-02-02T12:02:02.123456789";
final long dateInt2 = 1012651322123L;
final long nanoInt2 = 123456789L;
final Map<String, String> attributes = new HashMap<>();
attributes.put("sql.args.1.type", String.valueOf(Types.TIMESTAMP));
attributes.put("sql.args.1.value", dateStr);
attributes.put("sql.args.1.value", dateStr1);
attributes.put("sql.args.1.format", "ISO_OFFSET_DATE_TIME");
attributes.put("sql.args.2.type", String.valueOf(Types.TIMESTAMP));
attributes.put("sql.args.2.value", dateStr);
attributes.put("sql.args.2.format", "yyyy-MM-dd'T'HH:mm:ssXXX");
attributes.put("sql.args.2.value", dateStr2);
attributes.put("sql.args.2.format", "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS");
runner.enqueue("INSERT INTO TIMESTAMPTEST2 (ID, ts1, ts2) VALUES (1, ?, ?)".getBytes(), attributes);
runner.run();
@ -455,8 +459,9 @@ public class TestPutSQL {
final ResultSet rs = stmt.executeQuery("SELECT * FROM TIMESTAMPTEST2");
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
assertEquals(dateInt, rs.getTimestamp(2).getTime());
assertEquals(dateInt, rs.getTimestamp(3).getTime());
assertEquals(dateInt1, rs.getTimestamp(2).getTime());
assertEquals(dateInt2, rs.getTimestamp(3).getTime());
assertEquals(nanoInt2, rs.getTimestamp(3).getNanos());
assertFalse(rs.next());
}
}
@ -477,11 +482,9 @@ public class TestPutSQL {
final String dateStr = "2002-03-04";
final String timeStr = "02:03:04";
final String timeFormatString = "HH:mm:ss";
final String dateFormatString ="yyyy-MM-dd";
final DateTimeFormatter timeFormatter= DateTimeFormatter.ISO_LOCAL_TIME;
LocalTime parsedTime = LocalTime.parse(timeStr, timeFormatter);
Time expectedTime = Time.valueOf(parsedTime);