Skip to content

Commit

Permalink
JDBC driver implements Result#getTimestamp and Result#getTime with Ca…
Browse files Browse the repository at this point in the history
…lendar
  • Loading branch information
zhaohehuhu committed Nov 23, 2023
1 parent 7623b85 commit 33e4305
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public Date getDate(int columnIndex) throws SQLException {
public Date getDate(String columnName) throws SQLException {
return getDate(findColumn(columnName));
}

@Override
public Date getDate(int columnIndex, Calendar cal) throws SQLException {
Date value = getDate(columnIndex);
Expand Down Expand Up @@ -434,6 +434,84 @@ public Timestamp getTimestamp(String columnName) throws SQLException {
return getTimestamp(findColumn(columnName));
}

@Override
public Timestamp getTimestamp(int columnIndex, Calendar cal) throws SQLException {
Timestamp value = getTimestamp(columnIndex);
if (value == null) {
return null;
} else {
try {
return parseTimestamp(value, cal);
} catch (IllegalArgumentException e) {
throw new KyuubiSQLException("Cannot convert column " + columnIndex + " to timestamp: " + e, e);
}
}
}

@Override
public Timestamp getTimestamp(String columnLabel, Calendar cal) throws SQLException {
return this.getTimestamp(findColumn(columnLabel), cal);
}

private Timestamp parseTimestamp(Timestamp timestamp, Calendar cal) {
if (cal == null) {
cal = Calendar.getInstance();
}
long v = timestamp.getTime();
v -= cal.getTimeZone().getOffset(v);
timestamp = new Timestamp(v);
return timestamp;
}

@Override
public Time getTime(int columnIndex) throws SQLException {
Object obj = getObject(columnIndex);
if (obj == null) {
return null;
}
if (obj instanceof Time) {
return (Time) obj;
}
if (obj instanceof String) {
return Time.valueOf((String) obj);
}
throw new KyuubiSQLException("Illegal conversion");
}

@Override
public Time getTime(String columnLabel) throws SQLException {
return getTime(findColumn(columnLabel));
}

@Override
public Time getTime(int columnIndex, Calendar cal) throws SQLException {
Time value = getTime(columnIndex);
if (value == null) {
return null;
} else {
try {
return parseTime(value, cal);
} catch (IllegalArgumentException e) {
throw new KyuubiSQLException("Cannot convert column " + columnIndex + " to time: " + e, e);
}
}
}

@Override
public Time getTime(String columnLabel, Calendar cal) throws SQLException {
return this.getTime(findColumn(columnLabel), cal);
}

private Time parseTime(Time date, Calendar cal) {
if (cal == null) {
cal = Calendar.getInstance();
}
long v = date.getTime();
v -= cal.getTimeZone().getOffset(v);
date = new Time(v);
return date;
}

@Override
public int getType() throws SQLException {
return ResultSet.TYPE_FORWARD_ONLY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,83 @@ public Timestamp getTimestamp(int columnIndex) throws SQLException {
public Timestamp getTimestamp(String columnName) throws SQLException {
return getTimestamp(findColumn(columnName));
}
@Override
public Timestamp getTimestamp(int columnIndex, Calendar cal) throws SQLException {
Timestamp value = getTimestamp(columnIndex);
if (value == null) {
return null;
} else {
try {
return parseTimestamp(value, cal);
} catch (IllegalArgumentException e) {
throw new KyuubiSQLException("Cannot convert column " + columnIndex + " to timestamp: " + e, e);
}
}
}

@Override
public Timestamp getTimestamp(String columnLabel, Calendar cal) throws SQLException {
return this.getTimestamp(findColumn(columnLabel), cal);
}

private Timestamp parseTimestamp(Timestamp timestamp, Calendar cal) {
if (cal == null) {
cal = Calendar.getInstance();
}
long v = timestamp.getTime();
v -= cal.getTimeZone().getOffset(v);
timestamp = new Timestamp(v);
return timestamp;
}

@Override
public Time getTime(int columnIndex) throws SQLException {
Object obj = getObject(columnIndex);
if (obj == null) {
return null;
}
if (obj instanceof Time) {
return (Time) obj;
}
if (obj instanceof String) {
return Time.valueOf((String) obj);
}
throw new KyuubiSQLException("Illegal conversion");
}

@Override
public Time getTime(String columnLabel) throws SQLException {
return getTime(findColumn(columnLabel));
}

@Override
public Time getTime(int columnIndex, Calendar cal) throws SQLException {
Time value = getTime(columnIndex);
if (value == null) {
return null;
} else {
try {
return parseTime(value, cal);
} catch (IllegalArgumentException e) {
throw new KyuubiSQLException("Cannot convert column " + columnIndex + " to time: " + e, e);
}
}
}

@Override
public Time getTime(String columnLabel, Calendar cal) throws SQLException {
return this.getTime(findColumn(columnLabel), cal);
}

private Time parseTime(Time date, Calendar cal) {
if (cal == null) {
cal = Calendar.getInstance();
}
long v = date.getTime();
v -= cal.getTimeZone().getOffset(v);
date = new Time(v);
return date;
}

@Override
public int getType() throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,30 +435,6 @@ default Clob getClob(String columnLabel) throws SQLException {
default Array getArray(String columnLabel) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
@Override
default Date getDate(String columnLabel, Calendar cal) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}

@Override
default Time getTime(int columnIndex, Calendar cal) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}

@Override
default Time getTime(String columnLabel, Calendar cal) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}

@Override
default Timestamp getTimestamp(int columnIndex, Calendar cal) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}

@Override
default Timestamp getTimestamp(String columnLabel, Calendar cal) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}

@Override
default URL getURL(int columnIndex) throws SQLException {
Expand Down

0 comments on commit 33e4305

Please sign in to comment.