Unverified 提交 0494963b authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov 提交者: GitHub

Merge pull request #1308 from katzyn/calendar

Fix JdbcPreparedStatement.set*() methods with null Calendar argument
...@@ -734,7 +734,8 @@ public class JdbcPreparedStatement extends JdbcStatement implements ...@@ -734,7 +734,8 @@ public class JdbcPreparedStatement extends JdbcStatement implements
if (x == null) { if (x == null) {
setParameter(parameterIndex, ValueNull.INSTANCE); setParameter(parameterIndex, ValueNull.INSTANCE);
} else { } else {
setParameter(parameterIndex, DateTimeUtils.convertDate(x, calendar)); setParameter(parameterIndex,
calendar != null ? DateTimeUtils.convertDate(x, calendar) : ValueDate.get(x));
} }
} catch (Exception e) { } catch (Exception e) {
throw logAndConvert(e); throw logAndConvert(e);
...@@ -760,7 +761,8 @@ public class JdbcPreparedStatement extends JdbcStatement implements ...@@ -760,7 +761,8 @@ public class JdbcPreparedStatement extends JdbcStatement implements
if (x == null) { if (x == null) {
setParameter(parameterIndex, ValueNull.INSTANCE); setParameter(parameterIndex, ValueNull.INSTANCE);
} else { } else {
setParameter(parameterIndex, DateTimeUtils.convertTime(x, calendar)); setParameter(parameterIndex,
calendar != null ? DateTimeUtils.convertTime(x, calendar) : ValueTime.get(x));
} }
} catch (Exception e) { } catch (Exception e) {
throw logAndConvert(e); throw logAndConvert(e);
...@@ -787,7 +789,8 @@ public class JdbcPreparedStatement extends JdbcStatement implements ...@@ -787,7 +789,8 @@ public class JdbcPreparedStatement extends JdbcStatement implements
if (x == null) { if (x == null) {
setParameter(parameterIndex, ValueNull.INSTANCE); setParameter(parameterIndex, ValueNull.INSTANCE);
} else { } else {
setParameter(parameterIndex, DateTimeUtils.convertTimestamp(x, calendar)); setParameter(parameterIndex,
calendar != null ? DateTimeUtils.convertTimestamp(x, calendar) : ValueTimestamp.get(x));
} }
} catch (Exception e) { } catch (Exception e) {
throw logAndConvert(e); throw logAndConvert(e);
......
...@@ -13,7 +13,6 @@ import java.util.Calendar; ...@@ -13,7 +13,6 @@ import java.util.Calendar;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import java.util.TimeZone; import java.util.TimeZone;
import org.h2.engine.Mode; import org.h2.engine.Mode;
import org.h2.message.DbException;
import org.h2.value.Value; import org.h2.value.Value;
import org.h2.value.ValueDate; import org.h2.value.ValueDate;
import org.h2.value.ValueNull; import org.h2.value.ValueNull;
...@@ -278,9 +277,6 @@ public class DateTimeUtils { ...@@ -278,9 +277,6 @@ public class DateTimeUtils {
* @return the date * @return the date
*/ */
public static ValueDate convertDate(Date x, Calendar calendar) { public static ValueDate convertDate(Date x, Calendar calendar) {
if (calendar == null) {
throw DbException.getInvalidValueException("calendar", null);
}
Calendar cal = (Calendar) calendar.clone(); Calendar cal = (Calendar) calendar.clone();
cal.setTimeInMillis(x.getTime()); cal.setTimeInMillis(x.getTime());
long dateValue = dateValueFromCalendar(cal); long dateValue = dateValueFromCalendar(cal);
...@@ -295,9 +291,6 @@ public class DateTimeUtils { ...@@ -295,9 +291,6 @@ public class DateTimeUtils {
* @return the time * @return the time
*/ */
public static ValueTime convertTime(Time x, Calendar calendar) { public static ValueTime convertTime(Time x, Calendar calendar) {
if (calendar == null) {
throw DbException.getInvalidValueException("calendar", null);
}
Calendar cal = (Calendar) calendar.clone(); Calendar cal = (Calendar) calendar.clone();
cal.setTimeInMillis(x.getTime()); cal.setTimeInMillis(x.getTime());
long nanos = nanosFromCalendar(cal); long nanos = nanosFromCalendar(cal);
...@@ -313,9 +306,6 @@ public class DateTimeUtils { ...@@ -313,9 +306,6 @@ public class DateTimeUtils {
*/ */
public static ValueTimestamp convertTimestamp(Timestamp x, public static ValueTimestamp convertTimestamp(Timestamp x,
Calendar calendar) { Calendar calendar) {
if (calendar == null) {
throw DbException.getInvalidValueException("calendar", null);
}
Calendar cal = (Calendar) calendar.clone(); Calendar cal = (Calendar) calendar.clone();
cal.setTimeInMillis(x.getTime()); cal.setTimeInMillis(x.getTime());
long dateValue = dateValueFromCalendar(cal); long dateValue = dateValueFromCalendar(cal);
......
...@@ -1497,6 +1497,12 @@ public class TestResultSet extends TestDb { ...@@ -1497,6 +1497,12 @@ public class TestResultSet extends TestDb {
java.sql.Timestamp.valueOf("2107-08-09 10:11:12.131415")); java.sql.Timestamp.valueOf("2107-08-09 10:11:12.131415"));
prep.execute(); prep.execute();
prep.setInt(1, 5);
prep.setDate(2, java.sql.Date.valueOf("2101-02-03"), null);
prep.setTime(3, java.sql.Time.valueOf("14:05:06"), null);
prep.setTimestamp(4, java.sql.Timestamp.valueOf("2107-08-09 10:11:12.131415"), null);
prep.execute();
rs = stat.executeQuery("SELECT * FROM TEST ORDER BY ID"); rs = stat.executeQuery("SELECT * FROM TEST ORDER BY ID");
assertResultSetMeta(rs, 4, assertResultSetMeta(rs, 4,
new String[] { "ID", "D", "T", "TS" }, new String[] { "ID", "D", "T", "TS" },
...@@ -1545,6 +1551,13 @@ public class TestResultSet extends TestDb { ...@@ -1545,6 +1551,13 @@ public class TestResultSet extends TestDb {
assertEquals("14:05:06", rs.getTime("T").toString()); assertEquals("14:05:06", rs.getTime("T").toString());
assertEquals("2101-02-03", rs.getDate("D").toString()); assertEquals("2101-02-03", rs.getDate("D").toString());
rs.next();
assertEquals(5, rs.getInt("ID"));
assertEquals("2107-08-09 10:11:12.131415",
rs.getTimestamp("TS").toString());
assertEquals("14:05:06", rs.getTime("T").toString());
assertEquals("2101-02-03", rs.getDate("D").toString());
assertFalse(rs.next()); assertFalse(rs.next());
stat.execute("DROP TABLE TEST"); stat.execute("DROP TABLE TEST");
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论