-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes "ResultSet.getTime()
returns null
" bug
#1
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,14 @@ | |
|
||
import java.sql.SQLException; | ||
import java.sql.Time; | ||
import java.time.LocalTime; | ||
import java.sql.Timestamp; | ||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.time.format.DateTimeParseException; | ||
import java.time.temporal.TemporalAccessor; | ||
import java.util.Calendar; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
|
||
public class TimeType implements TypeHelper<Time>{ | ||
|
@@ -24,41 +31,56 @@ public Time fromValue(Object value, Map<String, Object> conversionParams) throws | |
if (value == null) { | ||
return null; | ||
} | ||
Calendar calendar = conversionParams != null ? (Calendar) conversionParams.get("calendar") : null; | ||
if (value instanceof Time) { | ||
return asTime((Time) value); | ||
return (Time) value; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should the returned value here be converted to localtime similar to the case where There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see what adding that extra step will accomplish in this case, so I return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gocha, thanks! |
||
} else if (value instanceof String) { | ||
return asTime((String) value); | ||
return asTime((String) value, calendar); | ||
} else if (value instanceof Number) { | ||
return this.asTime((Number) value); | ||
return asTime((Number) value); | ||
} else { | ||
throw objectConversionException(value); | ||
} | ||
} | ||
|
||
public Time asTime(Time value) { | ||
return localTimetoSqlTime(value.toLocalTime()); | ||
} | ||
public Time asTime(String value, Calendar calendar) { | ||
Time time; | ||
LocalDateTime localDateTime; | ||
|
||
public Time asTime(String value) throws SQLException { | ||
return localTimetoSqlTime(toLocalTime(value)); | ||
} | ||
try { | ||
TemporalAccessor temporal = DateTimeFormatter | ||
.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.getDefault()) | ||
.parse(value); | ||
|
||
localDateTime = LocalDateTime.from(temporal); | ||
time = Time.valueOf(localDateTime.toLocalTime()); | ||
} catch (DateTimeParseException exception) { | ||
time = Time.valueOf(value); | ||
} | ||
|
||
if (calendar == null) { | ||
return time; | ||
} | ||
|
||
private Time localTimetoSqlTime(LocalTime localTime) { | ||
return new Time(localTime.getHour(), localTime.getMinute(), localTime.getSecond()); | ||
localDateTime = time.toLocalTime().atDate(LocalDate.now()); | ||
|
||
return localDateTimeToTime(localDateTime, calendar); | ||
} | ||
|
||
public Time asTime(Number value) { | ||
return new Time(value.longValue()); | ||
} | ||
|
||
private LocalTime toLocalTime(String value) throws SQLException { | ||
if (value == null) | ||
throw stringConversionException(value, null); | ||
return LocalTime.parse(value); | ||
} | ||
|
||
@Override | ||
public String getTypeName() { | ||
return "Time"; | ||
} | ||
|
||
private Time localDateTimeToTime(LocalDateTime ldt, Calendar calendar) { | ||
calendar.set(ldt.getYear(), ldt.getMonthValue()-1, ldt.getDayOfMonth(), | ||
ldt.getHour(), ldt.getMinute(), ldt.getSecond()); | ||
calendar.set(Calendar.MILLISECOND, ldt.getNano()/1000000); | ||
|
||
return new Time(new Timestamp(calendar.getTimeInMillis()).getTime()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see comment: https://github.com/Bit-Quill/opensearch-project-sql/pull/192/files/8d15c35a0065037fdc1c5b18a86ae3260c284716
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in b1f8e06 and b1de4b8