Update ScalarTypeMonth and ScalarTypeDayOfWeek

This commit is contained in:
rbygrave
2014-11-18 20:06:13 +13:00
parent fd4d757f87
commit 98f6e1e6af
3 changed files with 60 additions and 2 deletions
@@ -1,5 +1,7 @@
package com.avaje.ebeaninternal.server.type;
import java.sql.SQLException;
import java.sql.Types;
import java.time.DayOfWeek;
/**
@@ -19,4 +21,31 @@ public class ScalarTypeDayOfWeek extends ScalarTypeEnumWithMapping {
public ScalarTypeDayOfWeek() {
super(beanDbMap, DayOfWeek.class, 1);
}
/**
* Bind DayOfWeek enum using getValue().
*/
@Override
public void bind(DataBind b, Object value) throws SQLException {
if (value == null) {
b.setNull(Types.INTEGER);
} else {
// avoiding the map lookup
b.setInt(((DayOfWeek)value).getValue());
}
}
/**
* Read DayOfWeek using int value.
*/
@Override
public Object read(DataReader dataReader) throws SQLException {
Integer i = dataReader.getInt();
if (i == null) {
return null;
} else {
// avoiding the map lookup
return DayOfWeek.of(i);
}
}
}
@@ -101,7 +101,7 @@ public class ScalarTypeEnumStandard {
if (beanValue == null) {
return null;
}
return ((Enum<?>) beanValue).toString();
return beanValue.toString();
}
public Object toBeanType(Object dbValue) {
@@ -186,7 +186,7 @@ public class ScalarTypeEnumStandard {
return dbValue;
}
int ordinal = ((Integer) dbValue).intValue();
int ordinal = (Integer) dbValue;
if (ordinal < 0 || ordinal >= enumArray.length) {
String m = "Unexpected ordinal [" + ordinal + "] out of range [" + enumArray.length + "]";
throw new IllegalStateException(m);
@@ -1,5 +1,7 @@
package com.avaje.ebeaninternal.server.type;
import java.sql.SQLException;
import java.sql.Types;
import java.time.Month;
/**
@@ -19,4 +21,31 @@ public class ScalarTypeMonth extends ScalarTypeEnumWithMapping {
public ScalarTypeMonth() {
super(beanDbMap, Month.class, 1);
}
/**
* Bind Month enum value.
*/
@Override
public void bind(DataBind b, Object value) throws SQLException {
if (value == null) {
b.setNull(Types.INTEGER);
} else {
// avoiding the map lookup
b.setInt(((Month)value).getValue());
}
}
/**
* Read Month enum from integer.
*/
@Override
public Object read(DataReader dataReader) throws SQLException {
Integer i = dataReader.getInt();
if (i == null) {
return null;
} else {
// avoiding the map lookup
return Month.of(i);
}
}
}