mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
No effective change - code format and tidy
This commit is contained in:
@@ -5,78 +5,76 @@ import com.avaje.ebean.config.CompoundTypeProperty;
|
||||
/**
|
||||
* Wraps a CompoundTypeProperty with it's type and parent for nested compound
|
||||
* types.
|
||||
*
|
||||
* @author rbygrave
|
||||
*/
|
||||
public class CtCompoundProperty {
|
||||
|
||||
private final String relativeName;
|
||||
private final String relativeName;
|
||||
|
||||
private final CtCompoundProperty parent;
|
||||
private final CtCompoundProperty parent;
|
||||
|
||||
private final CtCompoundType<?> compoundType;
|
||||
private final CtCompoundType<?> compoundType;
|
||||
|
||||
@SuppressWarnings({ "rawtypes" })
|
||||
private final CompoundTypeProperty property;
|
||||
@SuppressWarnings({"rawtypes"})
|
||||
private final CompoundTypeProperty property;
|
||||
|
||||
public CtCompoundProperty(String relativeName, CtCompoundProperty parent, CtCompoundType<?> ctType,
|
||||
CompoundTypeProperty<?, ?> property) {
|
||||
public CtCompoundProperty(String relativeName, CtCompoundProperty parent, CtCompoundType<?> ctType,
|
||||
CompoundTypeProperty<?, ?> property) {
|
||||
|
||||
this.relativeName = relativeName;
|
||||
this.parent = parent;
|
||||
this.compoundType = ctType;
|
||||
this.property = property;
|
||||
this.relativeName = relativeName;
|
||||
this.parent = parent;
|
||||
this.compoundType = ctType;
|
||||
this.property = property;
|
||||
}
|
||||
|
||||
/**
|
||||
* The property name relative to the root of the compound type.
|
||||
*/
|
||||
public String getRelativeName() {
|
||||
return relativeName;
|
||||
}
|
||||
|
||||
/**
|
||||
* The property name local to its type.
|
||||
*/
|
||||
public String getPropertyName() {
|
||||
return property.getName();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return relativeName;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Object getValue(Object valueObject) {
|
||||
if (valueObject == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The property name relative to the root of the compound type.
|
||||
*/
|
||||
public String getRelativeName() {
|
||||
return relativeName;
|
||||
if (parent != null) {
|
||||
valueObject = parent.getValue(valueObject);
|
||||
}
|
||||
return property.getValue(valueObject);
|
||||
}
|
||||
|
||||
/**
|
||||
* The property name local to its type.
|
||||
*/
|
||||
public String getPropertyName() {
|
||||
return property.getName();
|
||||
}
|
||||
/**
|
||||
* Set a scalar value that is used to build the immutable compound value
|
||||
* object.
|
||||
* <p>
|
||||
* When all the scalar values have been collected then the compound value
|
||||
* object is built and this can be recursive for nested compound types.
|
||||
* </p>
|
||||
*/
|
||||
public Object setValue(Object bean, Object value) {
|
||||
|
||||
public String toString() {
|
||||
return relativeName;
|
||||
}
|
||||
// compoundType and propertyName should be correct depth
|
||||
Object compoundValue = ImmutableCompoundTypeBuilder.set(compoundType, property.getName(), value);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Object getValue(Object valueObject) {
|
||||
if (valueObject == null) {
|
||||
return null;
|
||||
}
|
||||
if (parent != null) {
|
||||
valueObject = parent.getValue(valueObject);
|
||||
}
|
||||
return property.getValue(valueObject);
|
||||
}
|
||||
if (compoundValue != null && parent != null) {
|
||||
// Continue up the tree
|
||||
return parent.setValue(bean, compoundValue);
|
||||
|
||||
/**
|
||||
* Set a scalar value that is used to build the immutable compound value
|
||||
* object.
|
||||
* <p>
|
||||
* When all the scalar values have been collected then the compound value
|
||||
* object is built and this can be recursive for nested compound types.
|
||||
* </p>
|
||||
*/
|
||||
public Object setValue(Object bean, Object value) {
|
||||
|
||||
// compoundType and propertyName should be correct depth
|
||||
Object compoundValue = ImmutableCompoundTypeBuilder.set(compoundType, property.getName(), value);
|
||||
|
||||
if (compoundValue != null && parent != null) {
|
||||
// Continue up the tree
|
||||
return parent.setValue(bean, compoundValue);
|
||||
|
||||
} else {
|
||||
return compoundValue;
|
||||
}
|
||||
} else {
|
||||
return compoundValue;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,214 +1,193 @@
|
||||
package com.avaje.ebeaninternal.server.type;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.avaje.ebean.config.CompoundType;
|
||||
import com.avaje.ebean.config.CompoundTypeProperty;
|
||||
import com.avaje.ebeaninternal.server.text.json.WriteJson;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* The internal representation of a Compound Type (Immutable Compound Value Object).
|
||||
*
|
||||
* @param <V>
|
||||
* The Type of the "Immutable Compound Value Object".
|
||||
*
|
||||
* @param <V> The Type of the "Immutable Compound Value Object".
|
||||
*/
|
||||
public final class CtCompoundType<V> implements ScalarDataReader<V> {
|
||||
|
||||
private final Class<V> cvoClass;
|
||||
|
||||
private final CompoundType<V> cvoType;
|
||||
private final Class<V> cvoClass;
|
||||
|
||||
private final Map<String,CompoundTypeProperty<V, ?>> propertyMap;
|
||||
private final CompoundType<V> cvoType;
|
||||
|
||||
private final ScalarDataReader<Object>[] propReaders;
|
||||
private final ScalarDataReader<Object>[] propReaders;
|
||||
|
||||
private final CompoundTypeProperty<V, ?>[] properties;
|
||||
private final CompoundTypeProperty<V, ?>[] properties;
|
||||
|
||||
public CtCompoundType(Class<V> cvoClass, CompoundType<V> cvoType, ScalarDataReader<Object>[] propReaders) {
|
||||
|
||||
this.cvoClass = cvoClass;
|
||||
this.cvoType = cvoType;
|
||||
this.properties = cvoType.getProperties();
|
||||
this.propReaders = propReaders;
|
||||
|
||||
this.propertyMap = new LinkedHashMap<String, CompoundTypeProperty<V,?>>();
|
||||
for (CompoundTypeProperty<V,?> cp: properties) {
|
||||
propertyMap.put(cp.getName(), cp);
|
||||
}
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return cvoClass.toString();
|
||||
public CtCompoundType(Class<V> cvoClass, CompoundType<V> cvoType, ScalarDataReader<Object>[] propReaders) {
|
||||
|
||||
this.cvoClass = cvoClass;
|
||||
this.cvoType = cvoType;
|
||||
this.properties = cvoType.getProperties();
|
||||
this.propReaders = propReaders;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return cvoClass.toString();
|
||||
}
|
||||
|
||||
public Class<V> getCompoundTypeClass() {
|
||||
return cvoClass;
|
||||
}
|
||||
|
||||
public V create(Object[] propertyValues) {
|
||||
return cvoType.create(propertyValues);
|
||||
}
|
||||
|
||||
|
||||
public V create(Map<String, Object> valueMap) {
|
||||
|
||||
if (valueMap.size() != properties.length) {
|
||||
// not enough elements in the map
|
||||
return null;
|
||||
}
|
||||
|
||||
public Class<V> getCompoundTypeClass() {
|
||||
return cvoClass;
|
||||
// we expect the map to contain a value for
|
||||
// each property and that the values are the
|
||||
// correct type
|
||||
Object[] propertyValues = new Object[properties.length];
|
||||
for (int i = 0; i < properties.length; i++) {
|
||||
propertyValues[i] = valueMap.get(properties[i].getName());
|
||||
if (propertyValues[i] == null) {
|
||||
String m = "Null value for " + properties[i].getName() + " in map " + valueMap;
|
||||
throw new RuntimeException(m);
|
||||
}
|
||||
}
|
||||
|
||||
public V create(Object[] propertyValues) {
|
||||
return cvoType.create(propertyValues);
|
||||
return create(propertyValues);
|
||||
}
|
||||
|
||||
public CompoundTypeProperty<V, ?>[] getProperties() {
|
||||
|
||||
return cvoType.getProperties();
|
||||
}
|
||||
|
||||
public V read(DataReader source) throws SQLException {
|
||||
|
||||
boolean nullValue = false;
|
||||
Object[] values = new Object[propReaders.length];
|
||||
|
||||
for (int i = 0; i < propReaders.length; i++) {
|
||||
Object o = propReaders[i].read(source);
|
||||
values[i] = o;
|
||||
if (o == null) {
|
||||
nullValue = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public V create(Map<String, Object> valueMap) {
|
||||
|
||||
if (valueMap.size() != properties.length) {
|
||||
// not enough elements in the map
|
||||
return null;
|
||||
}
|
||||
|
||||
// we expect the map to contain a value for
|
||||
// each property and that the values are the
|
||||
// correct type
|
||||
Object[] propertyValues = new Object[properties.length];
|
||||
for (int i = 0; i < properties.length; i++) {
|
||||
propertyValues[i] = valueMap.get(properties[i].getName());
|
||||
if (propertyValues[i] == null) {
|
||||
String m = "Null value for " + properties[i].getName() + " in map " + valueMap;
|
||||
throw new RuntimeException(m);
|
||||
}
|
||||
}
|
||||
|
||||
return create(propertyValues);
|
||||
if (nullValue) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public CompoundTypeProperty<V, ?>[] getProperties() {
|
||||
return create(values);
|
||||
}
|
||||
|
||||
return cvoType.getProperties();
|
||||
public void loadIgnore(DataReader dataReader) {
|
||||
for (int i = 0; i < propReaders.length; i++) {
|
||||
propReaders[i].loadIgnore(dataReader);
|
||||
}
|
||||
}
|
||||
|
||||
public void bind(DataBind b, V value) throws SQLException {
|
||||
|
||||
CompoundTypeProperty<V, ?>[] props = cvoType.getProperties();
|
||||
|
||||
for (int i = 0; i < props.length; i++) {
|
||||
Object o = props[i].getValue(value);
|
||||
propReaders[i].bind(b, o);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively accumulate all the scalar types (in depth first order).
|
||||
* <p>
|
||||
* This creates a flat list of scalars even when compound types are embedded
|
||||
* inside compound types.
|
||||
* </p>
|
||||
*/
|
||||
public void accumulateScalarTypes(String parent, CtCompoundTypeScalarList list) {
|
||||
|
||||
CompoundTypeProperty<V, ?>[] props = cvoType.getProperties();
|
||||
|
||||
for (int i = 0; i < propReaders.length; i++) {
|
||||
String propName = getFullPropName(parent, props[i].getName());
|
||||
|
||||
list.addCompoundProperty(propName, this, props[i]);
|
||||
|
||||
propReaders[i].accumulateScalarTypes(propName, list);
|
||||
}
|
||||
|
||||
public Object[] getPropertyValues(V valueObject) {
|
||||
}
|
||||
|
||||
Object[] values = new Object[properties.length];
|
||||
for (int i = 0; i < properties.length; i++) {
|
||||
values[i] = properties[i].getValue(valueObject);
|
||||
}
|
||||
return values;
|
||||
/**
|
||||
* Return the full property name (for compound types embedded in other
|
||||
* compound types).
|
||||
*
|
||||
* @param parent the parent property name
|
||||
* @param propName the local property name
|
||||
*/
|
||||
private String getFullPropName(String parent, String propName) {
|
||||
if (parent == null) {
|
||||
return propName;
|
||||
} else {
|
||||
return parent + "." + propName;
|
||||
}
|
||||
}
|
||||
|
||||
public Object jsonConvert(Map<String, Object> map) {
|
||||
return readJsonElementObject(map);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Object readJsonElementObject(Map<String, Object> jsonObject) {
|
||||
|
||||
boolean nullValue = false;
|
||||
Object[] values = new Object[propReaders.length];
|
||||
|
||||
for (int i = 0; i < propReaders.length; i++) {
|
||||
String propName = properties[i].getName();
|
||||
Object jsonElement = jsonObject.get(propName);
|
||||
|
||||
if (propReaders[i] instanceof CtCompoundType<?>) {
|
||||
values[i] = ((CtCompoundType<?>) propReaders[i]).readJsonElementObject((Map<String, Object>) jsonElement);
|
||||
} else {
|
||||
values[i] = ((ScalarType<?>) propReaders[i]).parse(jsonElement.toString());
|
||||
}
|
||||
if (values[i] == null) {
|
||||
nullValue = true;
|
||||
}
|
||||
}
|
||||
|
||||
public V read(DataReader source) throws SQLException {
|
||||
|
||||
boolean nullValue = false;
|
||||
Object[] values = new Object[propReaders.length];
|
||||
|
||||
for (int i = 0; i < propReaders.length; i++) {
|
||||
Object o = propReaders[i].read(source);
|
||||
values[i] = o;
|
||||
if (o == null){
|
||||
nullValue = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (nullValue){
|
||||
return null;
|
||||
}
|
||||
|
||||
return create(values);
|
||||
if (nullValue) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void loadIgnore(DataReader dataReader) {
|
||||
for (int i = 0; i < propReaders.length; i++) {
|
||||
propReaders[i].loadIgnore(dataReader);
|
||||
}
|
||||
}
|
||||
return create(values);
|
||||
}
|
||||
|
||||
public void bind(DataBind b, V value) throws SQLException {
|
||||
|
||||
CompoundTypeProperty<V, ?>[] props = cvoType.getProperties();
|
||||
|
||||
for (int i = 0; i < props.length; i++) {
|
||||
Object o = props[i].getValue(value);
|
||||
propReaders[i].bind(b, o);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively accumulate all the scalar types (in depth first order).
|
||||
* <p>
|
||||
* This creates a flat list of scalars even when compound types are embedded
|
||||
* inside compound types.
|
||||
* </p>
|
||||
*/
|
||||
public void accumulateScalarTypes(String parent, CtCompoundTypeScalarList list) {
|
||||
|
||||
CompoundTypeProperty<V, ?>[] props = cvoType.getProperties();
|
||||
|
||||
for (int i = 0; i < propReaders.length; i++) {
|
||||
String propName = getFullPropName(parent, props[i].getName());
|
||||
|
||||
list.addCompoundProperty(propName, this, props[i]);
|
||||
|
||||
propReaders[i].accumulateScalarTypes(propName, list);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the full property name (for compound types embedded in other
|
||||
* compound types).
|
||||
*
|
||||
* @param parent
|
||||
* the parent property name
|
||||
* @param propName
|
||||
* the local property name
|
||||
*/
|
||||
private String getFullPropName(String parent, String propName) {
|
||||
if (parent == null) {
|
||||
return propName;
|
||||
} else {
|
||||
return parent + "." + propName;
|
||||
}
|
||||
}
|
||||
|
||||
public Object jsonConvert(Map<String, Object> map) {
|
||||
return readJsonElementObject(map);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Object readJsonElementObject(Map<String,Object> jsonObject){
|
||||
|
||||
boolean nullValue = false;
|
||||
Object[] values = new Object[propReaders.length];
|
||||
|
||||
for (int i = 0; i < propReaders.length; i++) {
|
||||
String propName = properties[i].getName();
|
||||
Object jsonElement = jsonObject.get(propName);
|
||||
|
||||
if (propReaders[i] instanceof CtCompoundType<?>) {
|
||||
values[i] = ((CtCompoundType<?>)propReaders[i]).readJsonElementObject((Map<String,Object>)jsonElement);
|
||||
} else {
|
||||
//((ScalarType<?>)propReaders[i]).jsonFromString(jsonElement.toPrimitiveString(), ctx.getValueAdapter());
|
||||
values[i] = ((ScalarType<?>)propReaders[i]).parse(jsonElement.toString());;
|
||||
}
|
||||
if (values[i] == null){
|
||||
nullValue = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (nullValue){
|
||||
return null;
|
||||
}
|
||||
|
||||
return create(values);
|
||||
}
|
||||
|
||||
|
||||
public void jsonWrite(WriteJson ctx, Object valueObject, String propertyName) throws IOException {
|
||||
|
||||
ctx.beginAssocOne(propertyName, valueObject);
|
||||
jsonWriteProps(ctx, valueObject, propertyName);
|
||||
ctx.endAssocOne();
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
private void jsonWriteProps(WriteJson ctx, Object valueObject, String propertyName) throws IOException {
|
||||
|
||||
if (propertyName != null) {
|
||||
ctx.gen().writeFieldName(propertyName);
|
||||
}
|
||||
}
|
||||
ctx.gen().writeStartObject();
|
||||
|
||||
for (int i = 0; i < properties.length; i++) {
|
||||
@@ -219,7 +198,6 @@ public final class CtCompoundType<V> implements ScalarDataReader<V> {
|
||||
|
||||
} else {
|
||||
((ScalarType) propReaders[i]).jsonWrite(ctx.gen(), propName, value);
|
||||
//ctx.appendNameValue(propName, (ScalarType) propReaders[i], value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,109 +12,105 @@ import java.sql.Timestamp;
|
||||
|
||||
public class DataBind {
|
||||
|
||||
private final PreparedStatement pstmt;
|
||||
private final PreparedStatement pstmt;
|
||||
|
||||
private int pos;
|
||||
private int pos;
|
||||
|
||||
public DataBind(PreparedStatement pstmt) {
|
||||
this.pstmt = pstmt;
|
||||
}
|
||||
public DataBind(PreparedStatement pstmt) {
|
||||
this.pstmt = pstmt;
|
||||
}
|
||||
|
||||
public void close() throws SQLException {
|
||||
pstmt.close();
|
||||
}
|
||||
|
||||
public int currentPos() {
|
||||
return pos;
|
||||
}
|
||||
|
||||
public void resetPos() {
|
||||
pos = 0;
|
||||
}
|
||||
public void close() throws SQLException {
|
||||
pstmt.close();
|
||||
}
|
||||
|
||||
public void setObject(Object value) throws SQLException {
|
||||
pstmt.setObject(++pos, value);
|
||||
}
|
||||
public int currentPos() {
|
||||
return pos;
|
||||
}
|
||||
|
||||
public void setObject(Object value, int sqlType) throws SQLException {
|
||||
pstmt.setObject(++pos, value, sqlType);
|
||||
}
|
||||
public void setObject(Object value) throws SQLException {
|
||||
pstmt.setObject(++pos, value);
|
||||
}
|
||||
|
||||
public void setNull(int jdbcType) throws SQLException {
|
||||
pstmt.setNull(++pos, jdbcType);
|
||||
}
|
||||
public void setObject(Object value, int sqlType) throws SQLException {
|
||||
pstmt.setObject(++pos, value, sqlType);
|
||||
}
|
||||
|
||||
public int nextPos() {
|
||||
return ++pos;
|
||||
}
|
||||
public void setNull(int jdbcType) throws SQLException {
|
||||
pstmt.setNull(++pos, jdbcType);
|
||||
}
|
||||
|
||||
public int decrementPos() {
|
||||
return ++pos;
|
||||
}
|
||||
|
||||
public int executeUpdate() throws SQLException {
|
||||
return pstmt.executeUpdate();
|
||||
}
|
||||
public int nextPos() {
|
||||
return ++pos;
|
||||
}
|
||||
|
||||
public PreparedStatement getPstmt() {
|
||||
return pstmt;
|
||||
}
|
||||
public int decrementPos() {
|
||||
return ++pos;
|
||||
}
|
||||
|
||||
public void setString(String s) throws SQLException {
|
||||
pstmt.setString(++pos, s);
|
||||
}
|
||||
public int executeUpdate() throws SQLException {
|
||||
return pstmt.executeUpdate();
|
||||
}
|
||||
|
||||
public void setInt(int i) throws SQLException {
|
||||
pstmt.setInt(++pos, i);
|
||||
}
|
||||
public PreparedStatement getPstmt() {
|
||||
return pstmt;
|
||||
}
|
||||
|
||||
public void setLong(long i) throws SQLException {
|
||||
pstmt.setLong(++pos, i);
|
||||
}
|
||||
public void setString(String s) throws SQLException {
|
||||
pstmt.setString(++pos, s);
|
||||
}
|
||||
|
||||
public void setShort(short i) throws SQLException {
|
||||
pstmt.setShort(++pos, i);
|
||||
}
|
||||
public void setInt(int i) throws SQLException {
|
||||
pstmt.setInt(++pos, i);
|
||||
}
|
||||
|
||||
public void setFloat(float i) throws SQLException {
|
||||
pstmt.setFloat(++pos, i);
|
||||
}
|
||||
public void setLong(long i) throws SQLException {
|
||||
pstmt.setLong(++pos, i);
|
||||
}
|
||||
|
||||
public void setDouble(double i) throws SQLException {
|
||||
pstmt.setDouble(++pos, i);
|
||||
}
|
||||
public void setShort(short i) throws SQLException {
|
||||
pstmt.setShort(++pos, i);
|
||||
}
|
||||
|
||||
public void setBigDecimal(BigDecimal v) throws SQLException {
|
||||
pstmt.setBigDecimal(++pos, v);
|
||||
}
|
||||
public void setFloat(float i) throws SQLException {
|
||||
pstmt.setFloat(++pos, i);
|
||||
}
|
||||
|
||||
public void setDate(java.sql.Date v) throws SQLException {
|
||||
pstmt.setDate(++pos, v);
|
||||
}
|
||||
public void setDouble(double i) throws SQLException {
|
||||
pstmt.setDouble(++pos, i);
|
||||
}
|
||||
|
||||
public void setTimestamp(Timestamp v) throws SQLException {
|
||||
pstmt.setTimestamp(++pos, v);
|
||||
}
|
||||
public void setBigDecimal(BigDecimal v) throws SQLException {
|
||||
pstmt.setBigDecimal(++pos, v);
|
||||
}
|
||||
|
||||
public void setTime(Time v) throws SQLException {
|
||||
pstmt.setTime(++pos, v);
|
||||
}
|
||||
public void setDate(java.sql.Date v) throws SQLException {
|
||||
pstmt.setDate(++pos, v);
|
||||
}
|
||||
|
||||
public void setBoolean(boolean v) throws SQLException {
|
||||
pstmt.setBoolean(++pos, v);
|
||||
}
|
||||
|
||||
public void setBytes(byte[] v) throws SQLException {
|
||||
pstmt.setBytes(++pos, v);
|
||||
}
|
||||
|
||||
public void setByte(byte v) throws SQLException {
|
||||
pstmt.setByte(++pos, v);
|
||||
}
|
||||
|
||||
public void setChar(char v) throws SQLException {
|
||||
pstmt.setString(++pos, String.valueOf(v));
|
||||
}
|
||||
public void setTimestamp(Timestamp v) throws SQLException {
|
||||
pstmt.setTimestamp(++pos, v);
|
||||
}
|
||||
|
||||
public void setTime(Time v) throws SQLException {
|
||||
pstmt.setTime(++pos, v);
|
||||
}
|
||||
|
||||
public void setBoolean(boolean v) throws SQLException {
|
||||
pstmt.setBoolean(++pos, v);
|
||||
}
|
||||
|
||||
public void setBytes(byte[] v) throws SQLException {
|
||||
pstmt.setBytes(++pos, v);
|
||||
}
|
||||
|
||||
public void setByte(byte v) throws SQLException {
|
||||
pstmt.setByte(++pos, v);
|
||||
}
|
||||
|
||||
public void setChar(char v) throws SQLException {
|
||||
pstmt.setString(++pos, String.valueOf(v));
|
||||
}
|
||||
|
||||
public void setBlob(InputStream inputStream, long length) throws SQLException {
|
||||
pstmt.setBlob(++pos, inputStream, length);
|
||||
@@ -124,14 +120,14 @@ public class DataBind {
|
||||
pstmt.setBlob(++pos, inputStream);
|
||||
}
|
||||
|
||||
public void setBlob(byte[] bytes) throws SQLException {
|
||||
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
|
||||
pstmt.setBinaryStream(++pos, is, bytes.length);
|
||||
}
|
||||
|
||||
public void setClob(String content) throws SQLException {
|
||||
Reader reader = new StringReader(content);
|
||||
pstmt.setCharacterStream(++pos, reader, content.length());
|
||||
}
|
||||
public void setBlob(byte[] bytes) throws SQLException {
|
||||
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
|
||||
pstmt.setBinaryStream(++pos, is, bytes.length);
|
||||
}
|
||||
|
||||
public void setClob(String content) throws SQLException {
|
||||
Reader reader = new StringReader(content);
|
||||
pstmt.setCharacterStream(++pos, reader, content.length());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,38 +6,38 @@ import com.avaje.ebean.config.Encryptor;
|
||||
|
||||
public class DataEncryptSupport {
|
||||
|
||||
private final EncryptKeyManager encryptKeyManager;
|
||||
private final Encryptor encryptor;
|
||||
private final String table;
|
||||
private final String column;
|
||||
|
||||
public DataEncryptSupport(EncryptKeyManager encryptKeyManager, Encryptor encryptor, String table, String column) {
|
||||
this.encryptKeyManager = encryptKeyManager;
|
||||
this.encryptor = encryptor;
|
||||
this.table = table;
|
||||
this.column = column;
|
||||
}
|
||||
|
||||
public byte[] encrypt(byte[] data){
|
||||
|
||||
EncryptKey key = encryptKeyManager.getEncryptKey(table, column);
|
||||
return encryptor.encrypt(data, key);
|
||||
}
|
||||
private final EncryptKeyManager encryptKeyManager;
|
||||
private final Encryptor encryptor;
|
||||
private final String table;
|
||||
private final String column;
|
||||
|
||||
public byte[] decrypt(byte[] data){
|
||||
|
||||
EncryptKey key = encryptKeyManager.getEncryptKey(table, column);
|
||||
return encryptor.decrypt(data, key);
|
||||
}
|
||||
|
||||
public String decryptObject(byte[] data) {
|
||||
EncryptKey key = encryptKeyManager.getEncryptKey(table, column);
|
||||
return encryptor.decryptString(data, key);
|
||||
}
|
||||
public DataEncryptSupport(EncryptKeyManager encryptKeyManager, Encryptor encryptor, String table, String column) {
|
||||
this.encryptKeyManager = encryptKeyManager;
|
||||
this.encryptor = encryptor;
|
||||
this.table = table;
|
||||
this.column = column;
|
||||
}
|
||||
|
||||
public <T> byte[] encryptObject(String formattedValue) {
|
||||
EncryptKey key = encryptKeyManager.getEncryptKey(table, column);
|
||||
return encryptor.encryptString(formattedValue, key);
|
||||
}
|
||||
public byte[] encrypt(byte[] data) {
|
||||
|
||||
EncryptKey key = encryptKeyManager.getEncryptKey(table, column);
|
||||
return encryptor.encrypt(data, key);
|
||||
}
|
||||
|
||||
public byte[] decrypt(byte[] data) {
|
||||
|
||||
EncryptKey key = encryptKeyManager.getEncryptKey(table, column);
|
||||
return encryptor.decrypt(data, key);
|
||||
}
|
||||
|
||||
public String decryptObject(byte[] data) {
|
||||
EncryptKey key = encryptKeyManager.getEncryptKey(table, column);
|
||||
return encryptor.decryptString(data, key);
|
||||
}
|
||||
|
||||
public byte[] encryptObject(String formattedValue) {
|
||||
EncryptKey key = encryptKeyManager.getEncryptKey(table, column);
|
||||
return encryptor.encryptString(formattedValue, key);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,51 +7,51 @@ import java.sql.SQLException;
|
||||
|
||||
public interface DataReader {
|
||||
|
||||
public void close() throws SQLException;
|
||||
void close() throws SQLException;
|
||||
|
||||
public boolean next() throws SQLException;
|
||||
boolean next() throws SQLException;
|
||||
|
||||
public void resetColumnPosition();
|
||||
|
||||
public void incrementPos(int increment);
|
||||
void resetColumnPosition();
|
||||
|
||||
public byte[] getBinaryBytes() throws SQLException;
|
||||
void incrementPos(int increment);
|
||||
|
||||
public byte[] getBlobBytes() throws SQLException;
|
||||
byte[] getBinaryBytes() throws SQLException;
|
||||
|
||||
public String getStringFromStream() throws SQLException;
|
||||
byte[] getBlobBytes() throws SQLException;
|
||||
|
||||
public String getStringClob() throws SQLException;
|
||||
|
||||
public String getString() throws SQLException;
|
||||
String getStringFromStream() throws SQLException;
|
||||
|
||||
public Boolean getBoolean() throws SQLException;
|
||||
String getStringClob() throws SQLException;
|
||||
|
||||
public Byte getByte() throws SQLException;
|
||||
String getString() throws SQLException;
|
||||
|
||||
public Short getShort() throws SQLException;
|
||||
Boolean getBoolean() throws SQLException;
|
||||
|
||||
public Integer getInt() throws SQLException;
|
||||
Byte getByte() throws SQLException;
|
||||
|
||||
public Long getLong() throws SQLException;
|
||||
Short getShort() throws SQLException;
|
||||
|
||||
public Float getFloat() throws SQLException;
|
||||
Integer getInt() throws SQLException;
|
||||
|
||||
public Double getDouble() throws SQLException;
|
||||
Long getLong() throws SQLException;
|
||||
|
||||
public byte[] getBytes() throws SQLException;
|
||||
Float getFloat() throws SQLException;
|
||||
|
||||
public java.sql.Date getDate() throws SQLException;
|
||||
Double getDouble() throws SQLException;
|
||||
|
||||
public java.sql.Time getTime() throws SQLException;
|
||||
byte[] getBytes() throws SQLException;
|
||||
|
||||
public java.sql.Timestamp getTimestamp() throws SQLException;
|
||||
java.sql.Date getDate() throws SQLException;
|
||||
|
||||
public BigDecimal getBigDecimal() throws SQLException;
|
||||
java.sql.Time getTime() throws SQLException;
|
||||
|
||||
public Array getArray() throws SQLException;
|
||||
|
||||
public Object getObject() throws SQLException;
|
||||
java.sql.Timestamp getTimestamp() throws SQLException;
|
||||
|
||||
public InputStream getBinaryStream() throws SQLException;
|
||||
BigDecimal getBigDecimal() throws SQLException;
|
||||
|
||||
Array getArray() throws SQLException;
|
||||
|
||||
Object getObject() throws SQLException;
|
||||
|
||||
InputStream getBinaryStream() throws SQLException;
|
||||
}
|
||||
|
||||
@@ -14,130 +14,128 @@ import com.avaje.ebeaninternal.server.core.BasicTypeConverter;
|
||||
*/
|
||||
public class DefaultTypeFactory {
|
||||
|
||||
private final ServerConfig serverConfig;
|
||||
private final ServerConfig serverConfig;
|
||||
|
||||
public DefaultTypeFactory(ServerConfig serverConfig) {
|
||||
this.serverConfig = serverConfig;
|
||||
public DefaultTypeFactory(ServerConfig serverConfig) {
|
||||
this.serverConfig = serverConfig;
|
||||
}
|
||||
|
||||
protected ScalarType<Boolean> createBoolean(String trueValue, String falseValue) {
|
||||
|
||||
try {
|
||||
// first try Integer based boolean
|
||||
Integer intTrue = BasicTypeConverter.toInteger(trueValue);
|
||||
Integer intFalse = BasicTypeConverter.toInteger(falseValue);
|
||||
|
||||
return new ScalarTypeBoolean.IntBoolean(intTrue, intFalse);
|
||||
|
||||
} catch (NumberFormatException e) {
|
||||
// treat as Varchar/String based boolean
|
||||
return new ScalarTypeBoolean.StringBoolean(trueValue, falseValue);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the ScalarType for mapping Booleans. For some databases this is a
|
||||
* native data type and for others Booleans will be converted to Y/N or 0/1
|
||||
* etc.
|
||||
*/
|
||||
public ScalarType<Boolean> createBoolean() {
|
||||
|
||||
if (serverConfig == null) {
|
||||
return new ScalarTypeBoolean.Native();
|
||||
}
|
||||
String trueValue = serverConfig.getDatabaseBooleanTrue();
|
||||
String falseValue = serverConfig.getDatabaseBooleanFalse();
|
||||
|
||||
if (falseValue != null && trueValue != null) {
|
||||
// explicit integer or string based booleans
|
||||
return createBoolean(trueValue, falseValue);
|
||||
}
|
||||
|
||||
private ScalarType<Boolean> createBoolean(String trueValue, String falseValue) {
|
||||
|
||||
try {
|
||||
// first try Integer based boolean
|
||||
Integer intTrue = BasicTypeConverter.toInteger(trueValue);
|
||||
Integer intFalse = BasicTypeConverter.toInteger(falseValue);
|
||||
|
||||
return new ScalarTypeBoolean.IntBoolean(intTrue, intFalse);
|
||||
|
||||
} catch (NumberFormatException e) {
|
||||
}
|
||||
|
||||
// treat as Varchar/String based boolean
|
||||
return new ScalarTypeBoolean.StringBoolean(trueValue, falseValue);
|
||||
// determine based on database platform configuration
|
||||
int booleanDbType = serverConfig.getDatabasePlatform().getBooleanDbType();
|
||||
|
||||
// Some dbs use BIT e.g. MySQL
|
||||
if (booleanDbType == Types.BIT) {
|
||||
return new ScalarTypeBoolean.BitBoolean();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the ScalarType for mapping Booleans. For some databases this is a
|
||||
* native data type and for others Booleans will be converted to Y/N or 0/1
|
||||
* etc.
|
||||
*/
|
||||
public ScalarType<Boolean> createBoolean() {
|
||||
|
||||
if (serverConfig == null) {
|
||||
return new ScalarTypeBoolean.Native();
|
||||
}
|
||||
String trueValue = serverConfig.getDatabaseBooleanTrue();
|
||||
String falseValue = serverConfig.getDatabaseBooleanFalse();
|
||||
|
||||
if (falseValue != null && trueValue != null) {
|
||||
// explicit integer or string based booleans
|
||||
return createBoolean(trueValue, falseValue);
|
||||
}
|
||||
|
||||
// determine based on database platform configuration
|
||||
int booleanDbType = serverConfig.getDatabasePlatform().getBooleanDbType();
|
||||
|
||||
// Some dbs use BIT e.g. MySQL
|
||||
if (booleanDbType == Types.BIT) {
|
||||
return new ScalarTypeBoolean.BitBoolean();
|
||||
}
|
||||
|
||||
if (booleanDbType == Types.INTEGER) {
|
||||
return new ScalarTypeBoolean.IntBoolean(1, 0);
|
||||
}
|
||||
if (booleanDbType == Types.VARCHAR) {
|
||||
return new ScalarTypeBoolean.StringBoolean("T", "F");
|
||||
}
|
||||
|
||||
if (booleanDbType == Types.BOOLEAN) {
|
||||
return new ScalarTypeBoolean.Native();
|
||||
}
|
||||
|
||||
// assume the JDBC driver can convert the type
|
||||
return new ScalarTypeBoolean.Native();
|
||||
if (booleanDbType == Types.INTEGER) {
|
||||
return new ScalarTypeBoolean.IntBoolean(1, 0);
|
||||
}
|
||||
if (booleanDbType == Types.VARCHAR) {
|
||||
return new ScalarTypeBoolean.StringBoolean("T", "F");
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the default ScalarType for java.util.Date.
|
||||
*/
|
||||
public ScalarType<java.util.Date> createUtilDate(JsonConfig.DateTime mode) {
|
||||
// by default map anonymous java.util.Date to java.sql.Timestamp.
|
||||
// String mapType =
|
||||
// properties.getProperty("type.mapping.java.util.Date","timestamp");
|
||||
int utilDateType = getTemporalMapType("timestamp");
|
||||
|
||||
return createUtilDate(mode, utilDateType);
|
||||
if (booleanDbType == Types.BOOLEAN) {
|
||||
return new ScalarTypeBoolean.Native();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a ScalarType for java.util.Date explicitly specifying the type to
|
||||
* map to.
|
||||
*/
|
||||
public ScalarType<java.util.Date> createUtilDate(JsonConfig.DateTime mode, int utilDateType) {
|
||||
// assume the JDBC driver can convert the type
|
||||
return new ScalarTypeBoolean.Native();
|
||||
}
|
||||
|
||||
switch (utilDateType) {
|
||||
case Types.DATE:
|
||||
return new ScalarTypeUtilDate.DateType();
|
||||
/**
|
||||
* Create the default ScalarType for java.util.Date.
|
||||
*/
|
||||
public ScalarType<java.util.Date> createUtilDate(JsonConfig.DateTime mode) {
|
||||
// by default map anonymous java.util.Date to java.sql.Timestamp.
|
||||
// String mapType =
|
||||
// properties.getProperty("type.mapping.java.util.Date","timestamp");
|
||||
int utilDateType = getTemporalMapType("timestamp");
|
||||
|
||||
case Types.TIMESTAMP:
|
||||
return new ScalarTypeUtilDate.TimestampType(mode);
|
||||
return createUtilDate(mode, utilDateType);
|
||||
}
|
||||
|
||||
default:
|
||||
throw new RuntimeException("Invalid type " + utilDateType);
|
||||
}
|
||||
/**
|
||||
* Create a ScalarType for java.util.Date explicitly specifying the type to
|
||||
* map to.
|
||||
*/
|
||||
public ScalarType<java.util.Date> createUtilDate(JsonConfig.DateTime mode, int utilDateType) {
|
||||
|
||||
switch (utilDateType) {
|
||||
case Types.DATE:
|
||||
return new ScalarTypeUtilDate.DateType();
|
||||
|
||||
case Types.TIMESTAMP:
|
||||
return new ScalarTypeUtilDate.TimestampType(mode);
|
||||
|
||||
default:
|
||||
throw new RuntimeException("Invalid type " + utilDateType);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the default ScalarType for java.util.Calendar.
|
||||
*/
|
||||
public ScalarType<Calendar> createCalendar(JsonConfig.DateTime mode) {
|
||||
/**
|
||||
* Create the default ScalarType for java.util.Calendar.
|
||||
*/
|
||||
public ScalarType<Calendar> createCalendar(JsonConfig.DateTime mode) {
|
||||
|
||||
int jdbcType = getTemporalMapType("timestamp");
|
||||
return createCalendar(mode, jdbcType);
|
||||
int jdbcType = getTemporalMapType("timestamp");
|
||||
return createCalendar(mode, jdbcType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a ScalarType for java.util.Calendar explicitly specifying the type
|
||||
* to map to.
|
||||
*/
|
||||
public ScalarType<Calendar> createCalendar(JsonConfig.DateTime mode, int jdbcType) {
|
||||
|
||||
return new ScalarTypeCalendar(mode, jdbcType);
|
||||
}
|
||||
|
||||
private int getTemporalMapType(String mapType) {
|
||||
if (mapType.equalsIgnoreCase("date")) {
|
||||
return java.sql.Types.DATE;
|
||||
}
|
||||
return java.sql.Types.TIMESTAMP;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a ScalarType for java.util.Calendar explicitly specifying the type
|
||||
* to map to.
|
||||
*/
|
||||
public ScalarType<Calendar> createCalendar(JsonConfig.DateTime mode, int jdbcType) {
|
||||
/**
|
||||
* Create a ScalarType for java.math.BigInteger.
|
||||
*/
|
||||
public ScalarType<BigInteger> createMathBigInteger() {
|
||||
|
||||
return new ScalarTypeCalendar(mode, jdbcType);
|
||||
}
|
||||
|
||||
private int getTemporalMapType(String mapType) {
|
||||
if (mapType.equalsIgnoreCase("date")) {
|
||||
return java.sql.Types.DATE;
|
||||
}
|
||||
return java.sql.Types.TIMESTAMP;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a ScalarType for java.math.BigInteger.
|
||||
*/
|
||||
public ScalarType<BigInteger> createMathBigInteger() {
|
||||
|
||||
return new ScalarTypeMathBigInteger();
|
||||
}
|
||||
return new ScalarTypeMathBigInteger();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.avaje.ebeaninternal.server.type;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
@@ -25,6 +26,7 @@ import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import com.avaje.ebean.config.*;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.joda.time.DateMidnight;
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.LocalDate;
|
||||
@@ -125,6 +127,8 @@ public final class DefaultTypeManager implements TypeManager, KnownImmutable {
|
||||
|
||||
private final JsonConfig.DateTime jsonDateTime;
|
||||
|
||||
private final boolean objectMapperPresent;
|
||||
|
||||
/**
|
||||
* Create the DefaultTypeManager.
|
||||
*/
|
||||
@@ -144,6 +148,8 @@ public final class DefaultTypeManager implements TypeManager, KnownImmutable {
|
||||
|
||||
this.customTypeMap.put(ScalarTypePostgresHstore.KEY, new ScalarTypePostgresHstore());
|
||||
|
||||
this.objectMapperPresent = ClassUtil.isPresent("com.fasterxml.jackson.databind.ObjectMapper", this.getClass());
|
||||
|
||||
this.extraTypeFactory = new DefaultTypeFactory(config);
|
||||
|
||||
initialiseStandard(jsonDateTime, clobType, blobType, config.isUuidStoreAsBinary());
|
||||
@@ -151,7 +157,7 @@ public final class DefaultTypeManager implements TypeManager, KnownImmutable {
|
||||
initialiseJodaTypes(jsonDateTime);
|
||||
|
||||
if (bootupClasses != null) {
|
||||
initialiseCustomScalarTypes(jsonDateTime, bootupClasses);
|
||||
initialiseCustomScalarTypes(jsonDateTime, bootupClasses, config);
|
||||
initialiseScalarConverters(bootupClasses);
|
||||
initialiseCompoundTypes(bootupClasses);
|
||||
}
|
||||
@@ -468,7 +474,7 @@ public final class DefaultTypeManager implements TypeManager, KnownImmutable {
|
||||
* interface and register it with this TypeManager.
|
||||
* </p>
|
||||
*/
|
||||
protected void initialiseCustomScalarTypes(JsonConfig.DateTime mode, BootupClasses bootupClasses) {
|
||||
protected void initialiseCustomScalarTypes(JsonConfig.DateTime mode, BootupClasses bootupClasses, ServerConfig serverConfig) {
|
||||
|
||||
ScalarTypeLongToTimestamp longToTimestamp = new ScalarTypeLongToTimestamp(mode);
|
||||
|
||||
@@ -480,9 +486,22 @@ public final class DefaultTypeManager implements TypeManager, KnownImmutable {
|
||||
Class<?> cls = foundTypes.get(i);
|
||||
try {
|
||||
|
||||
ScalarType<?> scalarType = (ScalarType<?>) cls.newInstance();
|
||||
add(scalarType);
|
||||
ScalarType<?> scalarType;
|
||||
if (!objectMapperPresent) {
|
||||
scalarType = (ScalarType<?>) cls.newInstance();
|
||||
|
||||
} else {
|
||||
try {
|
||||
// first try objectMapper constructor
|
||||
Constructor<?> constructor = cls.getConstructor(ObjectMapper.class);
|
||||
ObjectMapper objectMapper = getObjectMapper(serverConfig);
|
||||
scalarType = (ScalarType<?>)constructor.newInstance(objectMapper);
|
||||
} catch (NoSuchMethodException e) {
|
||||
scalarType = (ScalarType<?>) cls.newInstance();
|
||||
}
|
||||
}
|
||||
|
||||
add(scalarType);
|
||||
customScalarTypes.add(scalarType);
|
||||
|
||||
} catch (Exception e) {
|
||||
@@ -492,6 +511,16 @@ public final class DefaultTypeManager implements TypeManager, KnownImmutable {
|
||||
}
|
||||
}
|
||||
|
||||
private ObjectMapper getObjectMapper(ServerConfig serverConfig) {
|
||||
|
||||
ObjectMapper objectMapper = (ObjectMapper)serverConfig.getObjectMapper();
|
||||
if (objectMapper == null) {
|
||||
objectMapper = new ObjectMapper();
|
||||
serverConfig.setObjectMapper(objectMapper);
|
||||
}
|
||||
return objectMapper;
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
protected void initialiseScalarConverters(BootupClasses bootupClasses) {
|
||||
|
||||
|
||||
@@ -10,52 +10,52 @@ import javax.persistence.PersistenceException;
|
||||
*/
|
||||
public class EnumToDbIntegerMap extends EnumToDbValueMap<Integer> {
|
||||
|
||||
@Override
|
||||
public int getDbType() {
|
||||
return Types.INTEGER;
|
||||
}
|
||||
@Override
|
||||
public int getDbType() {
|
||||
return Types.INTEGER;
|
||||
}
|
||||
|
||||
public EnumToDbIntegerMap add(Object beanValue, Integer dbValue) {
|
||||
addInternal(beanValue, dbValue);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EnumToDbIntegerMap add(Object beanValue, String stringDbValue) {
|
||||
@Override
|
||||
public EnumToDbIntegerMap add(Object beanValue, String stringDbValue) {
|
||||
|
||||
try {
|
||||
Integer value = Integer.valueOf(stringDbValue);
|
||||
addInternal(beanValue, value);
|
||||
|
||||
return this;
|
||||
try {
|
||||
Integer value = Integer.valueOf(stringDbValue);
|
||||
addInternal(beanValue, value);
|
||||
|
||||
} catch (Exception e) {
|
||||
String msg = "Error converted enum type[" + beanValue.getClass().getName();
|
||||
msg += "] enum value[" + beanValue + "] string value [" + stringDbValue + "]";
|
||||
msg += " to an Integer.";
|
||||
throw new PersistenceException(msg, e);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
|
||||
@Override
|
||||
public void bind(DataBind b, Object value) throws SQLException {
|
||||
if (value == null) {
|
||||
b.setNull(Types.INTEGER);
|
||||
} else {
|
||||
Integer s = getDbValue(value);
|
||||
b.setInt(s);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
String msg = "Error converted enum type[" + beanValue.getClass().getName();
|
||||
msg += "] enum value[" + beanValue + "] string value [" + stringDbValue + "]";
|
||||
msg += " to an Integer.";
|
||||
throw new PersistenceException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@Override
|
||||
public void bind(DataBind b, Object value) throws SQLException {
|
||||
if (value == null) {
|
||||
b.setNull(Types.INTEGER);
|
||||
} else {
|
||||
Integer s = getDbValue(value);
|
||||
b.setInt(s);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object read(DataReader dataReader) throws SQLException {
|
||||
Integer i = dataReader.getInt();
|
||||
if (i == null) {
|
||||
return null;
|
||||
} else {
|
||||
return getBeanValue(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object read(DataReader dataReader) throws SQLException {
|
||||
Integer i = dataReader.getInt();
|
||||
if (i == null) {
|
||||
return null;
|
||||
} else {
|
||||
return getBeanValue(i);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -8,38 +8,37 @@ import java.sql.Types;
|
||||
*/
|
||||
public class EnumToDbStringMap extends EnumToDbValueMap<String> {
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public int getDbType() {
|
||||
return Types.VARCHAR;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EnumToDbStringMap add(Object beanValue, String dbValue) {
|
||||
addInternal(beanValue, dbValue);
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public int getDbType() {
|
||||
return Types.VARCHAR;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bind(DataBind b, Object value) throws SQLException {
|
||||
if (value == null){
|
||||
b.setNull(Types.VARCHAR);
|
||||
} else {
|
||||
String s = getDbValue(value);
|
||||
b.setString(s);
|
||||
}
|
||||
|
||||
}
|
||||
@Override
|
||||
public EnumToDbStringMap add(Object beanValue, String dbValue) {
|
||||
addInternal(beanValue, dbValue);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bind(DataBind b, Object value) throws SQLException {
|
||||
if (value == null) {
|
||||
b.setNull(Types.VARCHAR);
|
||||
} else {
|
||||
String s = getDbValue(value);
|
||||
b.setString(s);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object read(DataReader dataReader) throws SQLException {
|
||||
String s = dataReader.getString();
|
||||
if (s == null) {
|
||||
return null;
|
||||
} else {
|
||||
return getBeanValue(s);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object read(DataReader dataReader) throws SQLException {
|
||||
String s = dataReader.getString();
|
||||
if (s == null){
|
||||
return null;
|
||||
} else {
|
||||
return getBeanValue(s);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -13,122 +13,115 @@ import java.util.LinkedHashMap;
|
||||
*/
|
||||
public abstract class EnumToDbValueMap<T> {
|
||||
|
||||
public static EnumToDbValueMap<?> create(boolean integerType) {
|
||||
return integerType ? new EnumToDbIntegerMap() : new EnumToDbStringMap();
|
||||
}
|
||||
public static EnumToDbValueMap<?> create(boolean integerType) {
|
||||
return integerType ? new EnumToDbIntegerMap() : new EnumToDbStringMap();
|
||||
}
|
||||
|
||||
final LinkedHashMap<Object, T> keyMap;
|
||||
final LinkedHashMap<Object, T> keyMap;
|
||||
|
||||
final LinkedHashMap<T, Object> valueMap;
|
||||
final LinkedHashMap<T, Object> valueMap;
|
||||
|
||||
final boolean allowNulls;
|
||||
final boolean allowNulls;
|
||||
|
||||
final boolean isIntegerType;
|
||||
final boolean isIntegerType;
|
||||
|
||||
/**
|
||||
* Construct with allowNulls defaulting to false.
|
||||
*/
|
||||
public EnumToDbValueMap() {
|
||||
this(false, false);
|
||||
}
|
||||
/**
|
||||
* Construct with allowNulls defaulting to false.
|
||||
*/
|
||||
public EnumToDbValueMap() {
|
||||
this(false, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct with allowNulls setting.
|
||||
* <p>
|
||||
* If allowNulls is false then an IllegalArgumentException is thrown by
|
||||
* either the getDBValue or getBeanValue methods if not matching Bean or DB
|
||||
* value is found.
|
||||
* </p>
|
||||
*/
|
||||
public EnumToDbValueMap(boolean allowNulls, boolean isIntegerType) {
|
||||
this.allowNulls = allowNulls;
|
||||
this.isIntegerType = isIntegerType;
|
||||
keyMap = new LinkedHashMap<Object, T>();
|
||||
valueMap = new LinkedHashMap<T, Object>();
|
||||
}
|
||||
/**
|
||||
* Construct with allowNulls setting.
|
||||
* <p>
|
||||
* If allowNulls is false then an IllegalArgumentException is thrown by
|
||||
* either the getDBValue or getBeanValue methods if not matching Bean or DB
|
||||
* value is found.
|
||||
* </p>
|
||||
*/
|
||||
public EnumToDbValueMap(boolean allowNulls, boolean isIntegerType) {
|
||||
this.allowNulls = allowNulls;
|
||||
this.isIntegerType = isIntegerType;
|
||||
keyMap = new LinkedHashMap<Object, T>();
|
||||
valueMap = new LinkedHashMap<T, Object>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if this is mapping to integers, false
|
||||
* if mapping to Strings.
|
||||
*/
|
||||
public boolean isIntegerType() {
|
||||
return isIntegerType;
|
||||
}
|
||||
/**
|
||||
* Return true if this is mapping to integers, false
|
||||
* if mapping to Strings.
|
||||
*/
|
||||
public boolean isIntegerType() {
|
||||
return isIntegerType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the DB values.
|
||||
*/
|
||||
public Iterator<T> dbValues() {
|
||||
return valueMap.keySet().iterator();
|
||||
}
|
||||
/**
|
||||
* Return the DB values.
|
||||
*/
|
||||
public Iterator<T> dbValues() {
|
||||
return valueMap.keySet().iterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the bean 'key' value.
|
||||
*/
|
||||
public Iterator<Object> beanValues() {
|
||||
return valueMap.values().iterator();
|
||||
}
|
||||
/**
|
||||
* Bind using the correct database type.
|
||||
*/
|
||||
public abstract void bind(DataBind b, Object value) throws SQLException;
|
||||
|
||||
/**
|
||||
* Bind using the correct database type.
|
||||
*/
|
||||
public abstract void bind(DataBind b, Object value) throws SQLException;
|
||||
/**
|
||||
* Read using the correct database type.
|
||||
*/
|
||||
public abstract Object read(DataReader dataReader) throws SQLException;
|
||||
|
||||
/**
|
||||
* Read using the correct database type.
|
||||
*/
|
||||
public abstract Object read(DataReader dataReader) throws SQLException;
|
||||
/**
|
||||
* Return the database type.
|
||||
*/
|
||||
public abstract int getDbType();
|
||||
|
||||
/**
|
||||
* Return the database type.
|
||||
*/
|
||||
public abstract int getDbType();
|
||||
/**
|
||||
* Add name value pair where the dbValue is the raw string and may need to
|
||||
* be converted (to an Integer for example).
|
||||
*/
|
||||
public abstract EnumToDbValueMap<T> add(Object beanValue, String dbValue);
|
||||
|
||||
/**
|
||||
* Add name value pair where the dbValue is the raw string and may need to
|
||||
* be converted (to an Integer for example).
|
||||
*/
|
||||
public abstract EnumToDbValueMap<T> add(Object beanValue, String dbValue);
|
||||
/**
|
||||
* Add a bean value and DB value pair.
|
||||
* <p>
|
||||
* The dbValue will be converted to an Integer if isIntegerType is true;
|
||||
* </p>
|
||||
*/
|
||||
protected void addInternal(Object beanValue, T dbValue) {
|
||||
|
||||
/**
|
||||
* Add a bean value and DB value pair.
|
||||
* <p>
|
||||
* The dbValue will be converted to an Integer if isIntegerType is true;
|
||||
* </p>
|
||||
*/
|
||||
protected void addInternal(Object beanValue, T dbValue) {
|
||||
keyMap.put(beanValue, dbValue);
|
||||
valueMap.put(dbValue, beanValue);
|
||||
}
|
||||
|
||||
keyMap.put(beanValue, dbValue);
|
||||
valueMap.put(dbValue, beanValue);
|
||||
}
|
||||
/**
|
||||
* Return the DB value given the bean value.
|
||||
*/
|
||||
public T getDbValue(Object beanValue) {
|
||||
if (beanValue == null) {
|
||||
return null;
|
||||
}
|
||||
T dbValue = keyMap.get(beanValue);
|
||||
if (dbValue == null && !allowNulls) {
|
||||
String msg = "DB value for " + beanValue + " not found in " + keyMap;
|
||||
throw new IllegalArgumentException(msg);
|
||||
}
|
||||
return dbValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the DB value given the bean value.
|
||||
*/
|
||||
public T getDbValue(Object beanValue) {
|
||||
if (beanValue == null) {
|
||||
return null;
|
||||
}
|
||||
T dbValue = keyMap.get(beanValue);
|
||||
if (dbValue == null && !allowNulls) {
|
||||
String msg = "DB value for " + beanValue + " not found in " + keyMap;
|
||||
throw new IllegalArgumentException(msg);
|
||||
}
|
||||
return dbValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the Bean value given the DB value.
|
||||
*/
|
||||
public Object getBeanValue(T dbValue) {
|
||||
if (dbValue == null) {
|
||||
return null;
|
||||
}
|
||||
Object beanValue = valueMap.get(dbValue);
|
||||
if (beanValue == null && !allowNulls) {
|
||||
String msg = "Bean value for " + dbValue + " not found in " + valueMap;
|
||||
throw new IllegalArgumentException(msg);
|
||||
}
|
||||
return beanValue;
|
||||
}
|
||||
/**
|
||||
* Return the Bean value given the DB value.
|
||||
*/
|
||||
public Object getBeanValue(T dbValue) {
|
||||
if (dbValue == null) {
|
||||
return null;
|
||||
}
|
||||
Object beanValue = valueMap.get(dbValue);
|
||||
if (beanValue == null && !allowNulls) {
|
||||
String msg = "Bean value for " + dbValue + " not found in " + valueMap;
|
||||
throw new IllegalArgumentException(msg);
|
||||
}
|
||||
return beanValue;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,21 +4,21 @@ import java.sql.Timestamp;
|
||||
|
||||
import com.avaje.ebean.config.ScalarTypeConverter;
|
||||
|
||||
public class LongToTimestampConverter implements ScalarTypeConverter<Long, Timestamp>{
|
||||
|
||||
public Long getNullValue() {
|
||||
return null;
|
||||
}
|
||||
public class LongToTimestampConverter implements ScalarTypeConverter<Long, Timestamp> {
|
||||
|
||||
public Timestamp unwrapValue(Long beanType) {
|
||||
|
||||
return new Timestamp(beanType.longValue());
|
||||
}
|
||||
public Long getNullValue() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Timestamp unwrapValue(Long beanType) {
|
||||
|
||||
return new Timestamp(beanType.longValue());
|
||||
}
|
||||
|
||||
public Long wrapValue(Timestamp scalarType) {
|
||||
|
||||
return scalarType.getTime();
|
||||
}
|
||||
|
||||
public Long wrapValue(Timestamp scalarType) {
|
||||
|
||||
return scalarType.getTime();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ public class ModifyAwareCollection<E> implements Collection<E> {
|
||||
this.owner = owner;
|
||||
this.c = c;
|
||||
}
|
||||
|
||||
|
||||
public String toString() {
|
||||
return c.toString();
|
||||
}
|
||||
@@ -37,9 +37,7 @@ public class ModifyAwareCollection<E> implements Collection<E> {
|
||||
|
||||
public boolean addAll(Collection<? extends E> collection) {
|
||||
boolean changed = false;
|
||||
Iterator<? extends E> it = collection.iterator();
|
||||
while (it.hasNext()) {
|
||||
E o = it.next();
|
||||
for (E o : collection) {
|
||||
if (c.add(o)) {
|
||||
owner.markAsModified();
|
||||
changed = true;
|
||||
@@ -68,7 +66,7 @@ public class ModifyAwareCollection<E> implements Collection<E> {
|
||||
}
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
return new ModifyAwareIterator<E>(owner, c.iterator());
|
||||
return new ModifyAwareIterator<E>(owner, c.iterator());
|
||||
}
|
||||
|
||||
public boolean remove(Object o) {
|
||||
@@ -81,10 +79,8 @@ public class ModifyAwareCollection<E> implements Collection<E> {
|
||||
|
||||
public boolean removeAll(Collection<?> collection) {
|
||||
boolean changed = false;
|
||||
Iterator<?> it = collection.iterator();
|
||||
while (it.hasNext()) {
|
||||
Object o = (Object) it.next();
|
||||
if (c.remove(o)) {
|
||||
for (Object element : collection) {
|
||||
if (c.remove(element)) {
|
||||
owner.markAsModified();
|
||||
changed = true;
|
||||
}
|
||||
@@ -96,7 +92,7 @@ public class ModifyAwareCollection<E> implements Collection<E> {
|
||||
boolean changed = false;
|
||||
Iterator<?> it = c.iterator();
|
||||
while (it.hasNext()) {
|
||||
Object o = (Object) it.next();
|
||||
Object o = it.next();
|
||||
if (!collection.contains(o)) {
|
||||
it.remove();
|
||||
owner.markAsModified();
|
||||
|
||||
@@ -7,26 +7,26 @@ import java.util.Set;
|
||||
/**
|
||||
* Map that is wraps an underlying map for the purpose of detecting changes.
|
||||
*/
|
||||
public class ModifyAwareMap<K,V> implements Map<K,V>, ModifyAwareOwner {
|
||||
public class ModifyAwareMap<K, V> implements Map<K, V>, ModifyAwareOwner {
|
||||
|
||||
/**
|
||||
* Dirty flag set when the map has been modified.
|
||||
*/
|
||||
private boolean dirty;
|
||||
|
||||
|
||||
/**
|
||||
* The underlying map.
|
||||
*/
|
||||
private Map<K,V> map;
|
||||
|
||||
public ModifyAwareMap(Map<K,V> underyling) {
|
||||
private Map<K, V> map;
|
||||
|
||||
public ModifyAwareMap(Map<K, V> underyling) {
|
||||
this.map = underyling;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return map.toString();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isMarkedDirty() {
|
||||
return dirty;
|
||||
@@ -36,7 +36,7 @@ public class ModifyAwareMap<K,V> implements Map<K,V>, ModifyAwareOwner {
|
||||
public void markAsModified() {
|
||||
dirty = true;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return map.size();
|
||||
@@ -72,7 +72,7 @@ public class ModifyAwareMap<K,V> implements Map<K,V>, ModifyAwareOwner {
|
||||
public V remove(Object key) {
|
||||
V value = map.remove(key);
|
||||
if (value != null) {
|
||||
markAsModified();
|
||||
markAsModified();
|
||||
}
|
||||
return value;
|
||||
}
|
||||
@@ -107,5 +107,4 @@ public class ModifyAwareMap<K,V> implements Map<K,V>, ModifyAwareOwner {
|
||||
return new ModifyAwareSet<Map.Entry<K, V>>(this, map.entrySet());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -8,10 +8,10 @@ public interface ModifyAwareOwner {
|
||||
/**
|
||||
* Return true if the value is considered dirty.
|
||||
*/
|
||||
public boolean isMarkedDirty();
|
||||
|
||||
boolean isMarkedDirty();
|
||||
|
||||
/**
|
||||
* Marks the object as modified.
|
||||
*/
|
||||
public void markAsModified();
|
||||
void markAsModified();
|
||||
}
|
||||
|
||||
@@ -2,5 +2,5 @@ package com.avaje.ebeaninternal.server.type;
|
||||
|
||||
public interface ModifyAwareType {
|
||||
|
||||
public boolean isDirty();
|
||||
boolean isDirty();
|
||||
}
|
||||
|
||||
@@ -19,225 +19,224 @@ import com.avaje.ebeaninternal.server.core.Message;
|
||||
|
||||
public class RsetDataReader implements DataReader {
|
||||
|
||||
private static final int bufferSize = 512;
|
||||
private static final int bufferSize = 512;
|
||||
|
||||
static final int clobBufferSize = 512;
|
||||
|
||||
static final int stringInitialSize = 512;
|
||||
static final int clobBufferSize = 512;
|
||||
|
||||
private final ResultSet rset;
|
||||
|
||||
protected int pos;
|
||||
|
||||
public RsetDataReader(ResultSet rset) {
|
||||
this.rset = rset;
|
||||
static final int stringInitialSize = 512;
|
||||
|
||||
private final ResultSet rset;
|
||||
|
||||
protected int pos;
|
||||
|
||||
public RsetDataReader(ResultSet rset) {
|
||||
this.rset = rset;
|
||||
}
|
||||
|
||||
public void close() throws SQLException {
|
||||
rset.close();
|
||||
}
|
||||
|
||||
public boolean next() throws SQLException {
|
||||
return rset.next();
|
||||
}
|
||||
|
||||
public void resetColumnPosition() {
|
||||
pos = 0;
|
||||
}
|
||||
|
||||
public void incrementPos(int increment) {
|
||||
pos += increment;
|
||||
}
|
||||
|
||||
protected int pos() {
|
||||
return ++pos;
|
||||
}
|
||||
|
||||
public Array getArray() throws SQLException {
|
||||
return rset.getArray(pos());
|
||||
}
|
||||
|
||||
public InputStream getAsciiStream() throws SQLException {
|
||||
return rset.getAsciiStream(pos());
|
||||
}
|
||||
|
||||
public Object getObject() throws SQLException {
|
||||
return rset.getObject(pos());
|
||||
}
|
||||
|
||||
public BigDecimal getBigDecimal() throws SQLException {
|
||||
return rset.getBigDecimal(pos());
|
||||
}
|
||||
|
||||
|
||||
public InputStream getBinaryStream() throws SQLException {
|
||||
return rset.getBinaryStream(pos());
|
||||
}
|
||||
|
||||
public Boolean getBoolean() throws SQLException {
|
||||
boolean v = rset.getBoolean(pos());
|
||||
if (rset.wasNull()) {
|
||||
return null;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
public Byte getByte() throws SQLException {
|
||||
byte v = rset.getByte(pos());
|
||||
if (rset.wasNull()) {
|
||||
return null;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
public byte[] getBytes() throws SQLException {
|
||||
return rset.getBytes(pos());
|
||||
}
|
||||
|
||||
public Date getDate() throws SQLException {
|
||||
return rset.getDate(pos());
|
||||
}
|
||||
|
||||
public Double getDouble() throws SQLException {
|
||||
double v = rset.getDouble(pos());
|
||||
if (rset.wasNull()) {
|
||||
return null;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
public Float getFloat() throws SQLException {
|
||||
float v = rset.getFloat(pos());
|
||||
if (rset.wasNull()) {
|
||||
return null;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
public Integer getInt() throws SQLException {
|
||||
int v = rset.getInt(pos());
|
||||
if (rset.wasNull()) {
|
||||
return null;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
public Long getLong() throws SQLException {
|
||||
long v = rset.getLong(pos());
|
||||
if (rset.wasNull()) {
|
||||
return null;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
public Ref getRef() throws SQLException {
|
||||
return rset.getRef(pos());
|
||||
}
|
||||
|
||||
|
||||
public Short getShort() throws SQLException {
|
||||
short s = rset.getShort(pos());
|
||||
if (rset.wasNull()) {
|
||||
return null;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
public String getString() throws SQLException {
|
||||
return rset.getString(pos());
|
||||
}
|
||||
|
||||
|
||||
public Time getTime() throws SQLException {
|
||||
return rset.getTime(pos());
|
||||
}
|
||||
|
||||
|
||||
public Timestamp getTimestamp() throws SQLException {
|
||||
return rset.getTimestamp(pos());
|
||||
}
|
||||
|
||||
public String getStringFromStream() throws SQLException {
|
||||
Reader reader = rset.getCharacterStream(pos());
|
||||
if (reader == null) {
|
||||
return null;
|
||||
}
|
||||
return readStringLob(reader);
|
||||
}
|
||||
|
||||
public String getStringClob() throws SQLException {
|
||||
|
||||
Clob clob = rset.getClob(pos());
|
||||
if (clob == null) {
|
||||
return null;
|
||||
}
|
||||
Reader reader = clob.getCharacterStream();
|
||||
if (reader == null) {
|
||||
return null;
|
||||
}
|
||||
return readStringLob(reader);
|
||||
}
|
||||
|
||||
protected String readStringLob(Reader reader) throws SQLException {
|
||||
|
||||
char[] buffer = new char[clobBufferSize];
|
||||
int readLength;
|
||||
StringBuilder out = new StringBuilder(stringInitialSize);
|
||||
try {
|
||||
while ((readLength = reader.read(buffer)) != -1) {
|
||||
out.append(buffer, 0, readLength);
|
||||
}
|
||||
reader.close();
|
||||
} catch (IOException e) {
|
||||
throw new SQLException(Message.msg("persist.clob.io", e.getMessage()));
|
||||
}
|
||||
|
||||
public void close() throws SQLException {
|
||||
rset.close();
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
public byte[] getBinaryBytes() throws SQLException {
|
||||
InputStream in = rset.getBinaryStream(pos());
|
||||
return getBinaryLob(in);
|
||||
}
|
||||
|
||||
public byte[] getBlobBytes() throws SQLException {
|
||||
Blob blob = rset.getBlob(pos());
|
||||
if (blob == null) {
|
||||
return null;
|
||||
}
|
||||
InputStream in = blob.getBinaryStream();
|
||||
return getBinaryLob(in);
|
||||
}
|
||||
|
||||
public boolean next() throws SQLException {
|
||||
return rset.next();
|
||||
}
|
||||
|
||||
public void resetColumnPosition() {
|
||||
pos = 0;
|
||||
}
|
||||
|
||||
public void incrementPos(int increment){
|
||||
pos += increment;
|
||||
}
|
||||
|
||||
protected int pos() {
|
||||
return ++pos;
|
||||
}
|
||||
|
||||
public Array getArray() throws SQLException {
|
||||
return rset.getArray(pos());
|
||||
}
|
||||
|
||||
|
||||
public InputStream getAsciiStream() throws SQLException {
|
||||
return rset.getAsciiStream(pos());
|
||||
}
|
||||
|
||||
public Object getObject() throws SQLException {
|
||||
return rset.getObject(pos());
|
||||
}
|
||||
|
||||
public BigDecimal getBigDecimal() throws SQLException {
|
||||
return rset.getBigDecimal(pos());
|
||||
}
|
||||
|
||||
|
||||
public InputStream getBinaryStream() throws SQLException {
|
||||
return rset.getBinaryStream(pos());
|
||||
}
|
||||
|
||||
public Boolean getBoolean() throws SQLException {
|
||||
boolean v = rset.getBoolean(pos());
|
||||
if (rset.wasNull()){
|
||||
return null;
|
||||
}
|
||||
return Boolean.valueOf(v);
|
||||
}
|
||||
|
||||
public Byte getByte() throws SQLException {
|
||||
byte v = rset.getByte(pos());
|
||||
if (rset.wasNull()){
|
||||
return null;
|
||||
}
|
||||
return Byte.valueOf(v);
|
||||
}
|
||||
|
||||
public byte[] getBytes() throws SQLException {
|
||||
return rset.getBytes(pos());
|
||||
}
|
||||
|
||||
public Date getDate() throws SQLException {
|
||||
return rset.getDate(pos());
|
||||
}
|
||||
|
||||
public Double getDouble() throws SQLException {
|
||||
double v = rset.getDouble(pos());
|
||||
if (rset.wasNull()){
|
||||
return null;
|
||||
}
|
||||
return Double.valueOf(v);
|
||||
}
|
||||
|
||||
public Float getFloat() throws SQLException {
|
||||
float v = rset.getFloat(pos());
|
||||
if (rset.wasNull()){
|
||||
return null;
|
||||
}
|
||||
return Float.valueOf(v);
|
||||
}
|
||||
|
||||
public Integer getInt() throws SQLException {
|
||||
int v = rset.getInt(pos());
|
||||
if (rset.wasNull()){
|
||||
return null;
|
||||
}
|
||||
return Integer.valueOf(v);
|
||||
}
|
||||
|
||||
|
||||
public Long getLong() throws SQLException {
|
||||
long v = rset.getLong(pos());
|
||||
if (rset.wasNull()){
|
||||
return null;
|
||||
}
|
||||
return Long.valueOf(v);
|
||||
}
|
||||
|
||||
|
||||
public Ref getRef() throws SQLException {
|
||||
return rset.getRef(pos());
|
||||
}
|
||||
|
||||
|
||||
public Short getShort() throws SQLException {
|
||||
short s = rset.getShort(pos());
|
||||
if (rset.wasNull()){
|
||||
return null;
|
||||
}
|
||||
return Short.valueOf(s);
|
||||
}
|
||||
|
||||
|
||||
public String getString() throws SQLException {
|
||||
return rset.getString(pos());
|
||||
}
|
||||
|
||||
|
||||
public Time getTime() throws SQLException {
|
||||
return rset.getTime(pos());
|
||||
}
|
||||
|
||||
|
||||
public Timestamp getTimestamp() throws SQLException {
|
||||
return rset.getTimestamp(pos());
|
||||
}
|
||||
|
||||
public String getStringFromStream() throws SQLException {
|
||||
Reader reader = rset.getCharacterStream(pos());
|
||||
if (reader == null) {
|
||||
return null;
|
||||
}
|
||||
return readStringLob(reader);
|
||||
}
|
||||
|
||||
public String getStringClob() throws SQLException {
|
||||
|
||||
Clob clob = rset.getClob(pos());
|
||||
if (clob == null) {
|
||||
return null;
|
||||
}
|
||||
Reader reader = clob.getCharacterStream();
|
||||
if (reader == null) {
|
||||
return null;
|
||||
}
|
||||
return readStringLob(reader);
|
||||
}
|
||||
|
||||
protected String readStringLob(Reader reader) throws SQLException {
|
||||
|
||||
char[] buffer = new char[clobBufferSize];
|
||||
int readLength = 0;
|
||||
StringBuilder out = new StringBuilder(stringInitialSize);
|
||||
try {
|
||||
while ((readLength = reader.read(buffer)) != -1) {
|
||||
out.append(buffer, 0, readLength);
|
||||
}
|
||||
reader.close();
|
||||
} catch (IOException e) {
|
||||
throw new SQLException(Message.msg("persist.clob.io", e.getMessage()));
|
||||
}
|
||||
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
public byte[] getBinaryBytes() throws SQLException {
|
||||
InputStream in = rset.getBinaryStream(pos());
|
||||
return getBinaryLob(in);
|
||||
}
|
||||
|
||||
public byte[] getBlobBytes() throws SQLException {
|
||||
Blob blob = rset.getBlob(pos());
|
||||
if (blob == null) {
|
||||
return null;
|
||||
}
|
||||
InputStream in = blob.getBinaryStream();
|
||||
return getBinaryLob(in);
|
||||
}
|
||||
|
||||
protected byte[] getBinaryLob(InputStream in) throws SQLException {
|
||||
|
||||
try {
|
||||
if (in == null) {
|
||||
return null;
|
||||
}
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
|
||||
byte[] buf = new byte[bufferSize];
|
||||
int len;
|
||||
while ((len = in.read(buf, 0, buf.length)) != -1) {
|
||||
out.write(buf, 0, len);
|
||||
}
|
||||
byte[] data = out.toByteArray();
|
||||
|
||||
if (data.length == 0) {
|
||||
data = null;
|
||||
}
|
||||
in.close();
|
||||
out.close();
|
||||
return data;
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new SQLException(e.getClass().getName() + ":" + e.getMessage());
|
||||
}
|
||||
protected byte[] getBinaryLob(InputStream in) throws SQLException {
|
||||
|
||||
try {
|
||||
if (in == null) {
|
||||
return null;
|
||||
}
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
|
||||
byte[] buf = new byte[bufferSize];
|
||||
int len;
|
||||
while ((len = in.read(buf, 0, buf.length)) != -1) {
|
||||
out.write(buf, 0, len);
|
||||
}
|
||||
byte[] data = out.toByteArray();
|
||||
|
||||
if (data.length == 0) {
|
||||
data = null;
|
||||
}
|
||||
in.close();
|
||||
out.close();
|
||||
return data;
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new SQLException(e.getClass().getName() + ":" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,25 +4,25 @@ import java.sql.ResultSet;
|
||||
|
||||
public class RsetDataReaderIndexed extends RsetDataReader {
|
||||
|
||||
private final int[] rsetIndexPositions;
|
||||
|
||||
public RsetDataReaderIndexed(ResultSet rset, int[] rsetIndexPositions, boolean rowNumberIncluded) {
|
||||
super(rset);
|
||||
if (!rowNumberIncluded){
|
||||
this.rsetIndexPositions = rsetIndexPositions;
|
||||
} else {
|
||||
this.rsetIndexPositions = new int[rsetIndexPositions.length+1];
|
||||
for (int i = 0; i < rsetIndexPositions.length; i++) {
|
||||
// increment all the column indexes by 1
|
||||
this.rsetIndexPositions[i+1] = rsetIndexPositions[i]+1;
|
||||
}
|
||||
}
|
||||
}
|
||||
private final int[] rsetIndexPositions;
|
||||
|
||||
@Override
|
||||
protected int pos() {
|
||||
int i = pos++;
|
||||
return rsetIndexPositions[i];
|
||||
public RsetDataReaderIndexed(ResultSet rset, int[] rsetIndexPositions, boolean rowNumberIncluded) {
|
||||
super(rset);
|
||||
if (!rowNumberIncluded) {
|
||||
this.rsetIndexPositions = rsetIndexPositions;
|
||||
} else {
|
||||
this.rsetIndexPositions = new int[rsetIndexPositions.length + 1];
|
||||
for (int i = 0; i < rsetIndexPositions.length; i++) {
|
||||
// increment all the column indexes by 1
|
||||
this.rsetIndexPositions[i + 1] = rsetIndexPositions[i] + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int pos() {
|
||||
int i = pos++;
|
||||
return rsetIndexPositions[i];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,24 +7,24 @@ import java.sql.SQLException;
|
||||
*/
|
||||
public interface ScalarDataReader<T> {
|
||||
|
||||
/**
|
||||
* Read and return the appropriate value from the dataReader.
|
||||
*/
|
||||
public T read(DataReader dataReader) throws SQLException;
|
||||
/**
|
||||
* Read and return the appropriate value from the dataReader.
|
||||
*/
|
||||
T read(DataReader dataReader) throws SQLException;
|
||||
|
||||
/**
|
||||
* Ignore typically by moving the index position.
|
||||
*/
|
||||
public void loadIgnore(DataReader dataReader);
|
||||
/**
|
||||
* Ignore typically by moving the index position.
|
||||
*/
|
||||
void loadIgnore(DataReader dataReader);
|
||||
|
||||
/**
|
||||
* Bind the value to the underlying preparedStatement.
|
||||
*/
|
||||
public void bind(DataBind b, T value) throws SQLException;
|
||||
/**
|
||||
* Bind the value to the underlying preparedStatement.
|
||||
*/
|
||||
void bind(DataBind b, T value) throws SQLException;
|
||||
|
||||
/**
|
||||
* Accumulate all the scalar types used by an immutable compound value type.
|
||||
*/
|
||||
public void accumulateScalarTypes(String propName, CtCompoundTypeScalarList list);
|
||||
/**
|
||||
* Accumulate all the scalar types used by an immutable compound value type.
|
||||
*/
|
||||
void accumulateScalarTypes(String propName, CtCompoundTypeScalarList list);
|
||||
|
||||
}
|
||||
|
||||
@@ -38,143 +38,143 @@ public interface ScalarType<T> extends StringParser, StringFormatter, ScalarData
|
||||
/**
|
||||
* Return true if this is a mutable scalar type (like hstore).
|
||||
*/
|
||||
public boolean isMutable();
|
||||
|
||||
boolean isMutable();
|
||||
|
||||
/**
|
||||
* For mutable scalarType's return true if the value is dirty.
|
||||
* Non-dirty properties may be excluded from updates.
|
||||
*/
|
||||
public boolean isDirty(Object value);
|
||||
|
||||
/**
|
||||
* Return the default DB column length for this type.
|
||||
* <p>
|
||||
* If a BeanProperty has no explicit length defined then this length should
|
||||
* be assigned.
|
||||
* </p>
|
||||
* <p>
|
||||
* This is primarily to support defining a length on Enum types (to
|
||||
* supplement defining the length on the BeanProperty directly).
|
||||
* </p>
|
||||
*/
|
||||
public int getLength();
|
||||
boolean isDirty(Object value);
|
||||
|
||||
/**
|
||||
* Return true if the type is native to JDBC.
|
||||
* <p>
|
||||
* If it is native to JDBC then its values/instances do not need to be
|
||||
* converted to and from an associated JDBC type.
|
||||
* </p>
|
||||
*/
|
||||
public boolean isJdbcNative();
|
||||
/**
|
||||
* Return the default DB column length for this type.
|
||||
* <p>
|
||||
* If a BeanProperty has no explicit length defined then this length should
|
||||
* be assigned.
|
||||
* </p>
|
||||
* <p>
|
||||
* This is primarily to support defining a length on Enum types (to
|
||||
* supplement defining the length on the BeanProperty directly).
|
||||
* </p>
|
||||
*/
|
||||
int getLength();
|
||||
|
||||
/**
|
||||
* Return the type as per java.sql.Types that this maps to.
|
||||
* <p>
|
||||
* This type should be consistent with the toJdbcType() method in converting
|
||||
* the type to the appropriate type for binding to preparedStatements.
|
||||
* </p>
|
||||
*/
|
||||
public int getJdbcType();
|
||||
/**
|
||||
* Return true if the type is native to JDBC.
|
||||
* <p>
|
||||
* If it is native to JDBC then its values/instances do not need to be
|
||||
* converted to and from an associated JDBC type.
|
||||
* </p>
|
||||
*/
|
||||
boolean isJdbcNative();
|
||||
|
||||
/**
|
||||
* Return the type that matches the bean property type.
|
||||
* <p>
|
||||
* This represents the 'logical' type rather than the JDBC type this maps
|
||||
* to.
|
||||
* </p>
|
||||
*/
|
||||
public Class<T> getType();
|
||||
/**
|
||||
* Return the type as per java.sql.Types that this maps to.
|
||||
* <p>
|
||||
* This type should be consistent with the toJdbcType() method in converting
|
||||
* the type to the appropriate type for binding to preparedStatements.
|
||||
* </p>
|
||||
*/
|
||||
int getJdbcType();
|
||||
|
||||
/**
|
||||
* Read the value from the resultSet and convert if necessary to the logical
|
||||
* bean property value.
|
||||
*/
|
||||
public T read(DataReader dataReader) throws SQLException;
|
||||
/**
|
||||
* Return the type that matches the bean property type.
|
||||
* <p>
|
||||
* This represents the 'logical' type rather than the JDBC type this maps
|
||||
* to.
|
||||
* </p>
|
||||
*/
|
||||
Class<T> getType();
|
||||
|
||||
/**
|
||||
* Ignore the reading of this value. Typically this means moving the index
|
||||
* position in the ResultSet.
|
||||
*/
|
||||
public void loadIgnore(DataReader dataReader);
|
||||
/**
|
||||
* Read the value from the resultSet and convert if necessary to the logical
|
||||
* bean property value.
|
||||
*/
|
||||
T read(DataReader dataReader) throws SQLException;
|
||||
|
||||
/**
|
||||
* Convert (if necessary) and bind the value to the preparedStatement.
|
||||
* <p>
|
||||
* value may need to be converted from the logical bean property type to the
|
||||
* JDBC type.
|
||||
* </p>
|
||||
*/
|
||||
public void bind(DataBind b, T value) throws SQLException;
|
||||
/**
|
||||
* Ignore the reading of this value. Typically this means moving the index
|
||||
* position in the ResultSet.
|
||||
*/
|
||||
void loadIgnore(DataReader dataReader);
|
||||
|
||||
/**
|
||||
* Convert the value as necessary to the JDBC type.
|
||||
* <p>
|
||||
* Note that this should also match the type as per the getJdbcType()
|
||||
* method.
|
||||
* </p>
|
||||
* <p>
|
||||
* This is typically used when the matching type is used in a where clause
|
||||
* and we use this to ensure it is an appropriate jdbc type.
|
||||
* </p>
|
||||
*/
|
||||
public Object toJdbcType(Object value);
|
||||
/**
|
||||
* Convert (if necessary) and bind the value to the preparedStatement.
|
||||
* <p>
|
||||
* value may need to be converted from the logical bean property type to the
|
||||
* JDBC type.
|
||||
* </p>
|
||||
*/
|
||||
void bind(DataBind b, T value) throws SQLException;
|
||||
|
||||
/**
|
||||
* Convert the value as necessary to the logical Bean type.
|
||||
* <p>
|
||||
* The type as per the bean property.
|
||||
* </p>
|
||||
* <p>
|
||||
* This is used to automatically convert id values (typically from a string
|
||||
* to a int, long or UUID).
|
||||
* </p>
|
||||
*/
|
||||
public T toBeanType(Object value);
|
||||
/**
|
||||
* Convert the value as necessary to the JDBC type.
|
||||
* <p>
|
||||
* Note that this should also match the type as per the getJdbcType()
|
||||
* method.
|
||||
* </p>
|
||||
* <p>
|
||||
* This is typically used when the matching type is used in a where clause
|
||||
* and we use this to ensure it is an appropriate jdbc type.
|
||||
* </p>
|
||||
*/
|
||||
Object toJdbcType(Object value);
|
||||
|
||||
/**
|
||||
* Convert the type into a string representation.
|
||||
* <p>
|
||||
* Reciprocal of parse().
|
||||
* </p>
|
||||
*/
|
||||
public String formatValue(T v);
|
||||
/**
|
||||
* Convert the value as necessary to the logical Bean type.
|
||||
* <p>
|
||||
* The type as per the bean property.
|
||||
* </p>
|
||||
* <p>
|
||||
* This is used to automatically convert id values (typically from a string
|
||||
* to a int, long or UUID).
|
||||
* </p>
|
||||
*/
|
||||
T toBeanType(Object value);
|
||||
|
||||
/**
|
||||
* Convert the type into a string representation.
|
||||
* <p>
|
||||
* This assumes the value is of the correct type.
|
||||
* </p>
|
||||
* <p>
|
||||
* This is so that ScalarType also implements the StringFormatter interface.
|
||||
* </p>
|
||||
*/
|
||||
public String format(Object v);
|
||||
/**
|
||||
* Convert the type into a string representation.
|
||||
* <p>
|
||||
* Reciprocal of parse().
|
||||
* </p>
|
||||
*/
|
||||
String formatValue(T v);
|
||||
|
||||
/**
|
||||
* Convert the string value to the appropriate java object.
|
||||
* <p>
|
||||
* Mostly used to support CSV, JSON and XML parsing.
|
||||
* </p>
|
||||
* <p>
|
||||
* Reciprocal of formatValue().
|
||||
* </p>
|
||||
*/
|
||||
public T parse(String value);
|
||||
/**
|
||||
* Convert the type into a string representation.
|
||||
* <p>
|
||||
* This assumes the value is of the correct type.
|
||||
* </p>
|
||||
* <p>
|
||||
* This is so that ScalarType also implements the StringFormatter interface.
|
||||
* </p>
|
||||
*/
|
||||
String format(Object v);
|
||||
|
||||
/**
|
||||
* Return true if the type can accept long systemTimeMillis input.
|
||||
* <p>
|
||||
* This is used to determine if is is sensible to use the
|
||||
* {@link #convertFromMillis(long)} method.
|
||||
* </p>
|
||||
* <p>
|
||||
* This includes the Date, Calendar, sql Date, Time, Timestamp, JODA types
|
||||
* as well as Long, BigDecimal and String (although it generally is not
|
||||
* expected to parse systemTimeMillis to a String or BigDecimal).
|
||||
* </p>
|
||||
*/
|
||||
public boolean isDateTimeCapable();
|
||||
/**
|
||||
* Convert the string value to the appropriate java object.
|
||||
* <p>
|
||||
* Mostly used to support CSV, JSON and XML parsing.
|
||||
* </p>
|
||||
* <p>
|
||||
* Reciprocal of formatValue().
|
||||
* </p>
|
||||
*/
|
||||
T parse(String value);
|
||||
|
||||
/**
|
||||
* Return true if the type can accept long systemTimeMillis input.
|
||||
* <p>
|
||||
* This is used to determine if is is sensible to use the
|
||||
* {@link #convertFromMillis(long)} method.
|
||||
* </p>
|
||||
* <p>
|
||||
* This includes the Date, Calendar, sql Date, Time, Timestamp, JODA types
|
||||
* as well as Long, BigDecimal and String (although it generally is not
|
||||
* expected to parse systemTimeMillis to a String or BigDecimal).
|
||||
* </p>
|
||||
*/
|
||||
boolean isDateTimeCapable();
|
||||
|
||||
/**
|
||||
* Convert the systemTimeMillis into the appropriate java object.
|
||||
@@ -182,26 +182,26 @@ public interface ScalarType<T> extends StringParser, StringFormatter, ScalarData
|
||||
* For non dateTime types this will throw an exception.
|
||||
* </p>
|
||||
*/
|
||||
public T convertFromMillis(long dateTime);
|
||||
T convertFromMillis(long dateTime);
|
||||
|
||||
/**
|
||||
* Read the value from binary input.
|
||||
*/
|
||||
public T readData(DataInput dataInput) throws IOException;
|
||||
* Read the value from binary input.
|
||||
*/
|
||||
T readData(DataInput dataInput) throws IOException;
|
||||
|
||||
/**
|
||||
* Write the value to binary output.
|
||||
*/
|
||||
public void writeData(DataOutput dataOutput, T v) throws IOException;
|
||||
void writeData(DataOutput dataOutput, T v) throws IOException;
|
||||
|
||||
/**
|
||||
* Read the value from JsonParser.
|
||||
*/
|
||||
public T jsonRead(JsonParser ctx, JsonToken event) throws IOException;
|
||||
T jsonRead(JsonParser ctx, JsonToken event) throws IOException;
|
||||
|
||||
/**
|
||||
* Write the value to the JsonGenerator.
|
||||
*/
|
||||
public void jsonWrite(JsonGenerator ctx, String name, T value) throws IOException;
|
||||
|
||||
void jsonWrite(JsonGenerator ctx, String name, T value) throws IOException;
|
||||
|
||||
}
|
||||
|
||||
@@ -12,7 +12,6 @@ import java.math.BigDecimal;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Timestamp;
|
||||
import java.sql.Types;
|
||||
import java.time.Instant;
|
||||
|
||||
/**
|
||||
* Base type for DateTime types.
|
||||
@@ -98,7 +97,7 @@ public abstract class ScalarTypeBaseDateTime<T> extends ScalarTypeBase<T> {
|
||||
}
|
||||
default: {
|
||||
String jsonDateTime = ctx.getText();
|
||||
return convertFromTimestamp(dateTimeParser.parse(jsonDateTime));
|
||||
return convertFromTimestamp(dateTimeParser.parse(jsonDateTime));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,7 +89,7 @@ public abstract class ScalarTypeBaseVarchar<T> extends ScalarTypeBase<T> {
|
||||
public T convertFromMillis(long systemTimeMillis) {
|
||||
throw new TextException("Not Supported");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isDateTimeCapable() {
|
||||
return false;
|
||||
@@ -121,12 +121,12 @@ public abstract class ScalarTypeBaseVarchar<T> extends ScalarTypeBase<T> {
|
||||
dataOutput.writeUTF(s);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public T jsonRead(JsonParser ctx, JsonToken event) throws IOException {
|
||||
return parse(ctx.getValueAsString());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator ctx, String name, T value) throws IOException {
|
||||
ctx.writeStringField(name, format(value));
|
||||
|
||||
@@ -83,7 +83,7 @@ public class ScalarTypeBigDecimal extends ScalarTypeBase<BigDecimal> {
|
||||
}
|
||||
|
||||
public void jsonWrite(JsonGenerator ctx, String name, BigDecimal value) throws IOException {
|
||||
ctx.writeNumberField(name, (BigDecimal) value);
|
||||
ctx.writeNumberField(name, value);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ public class ScalarTypeBoolean {
|
||||
|
||||
/**
|
||||
* The Class BitBoolean converts a JDBC type BIT to a java boolean
|
||||
*
|
||||
* <p/>
|
||||
* <p>
|
||||
* Sometimes booleans may be mapped to the JDBC type BIT. To use the BitBoolean specify
|
||||
* type.boolean.dbtype="bit" in the ebean configuration
|
||||
@@ -143,11 +143,7 @@ public class ScalarTypeBoolean {
|
||||
return null;
|
||||
}
|
||||
Boolean b = (Boolean) value;
|
||||
if (b.booleanValue()) {
|
||||
return trueValue;
|
||||
} else {
|
||||
return falseValue;
|
||||
}
|
||||
return b ? trueValue : falseValue;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -222,11 +218,7 @@ public class ScalarTypeBoolean {
|
||||
return null;
|
||||
}
|
||||
Boolean b = (Boolean) value;
|
||||
if (b.booleanValue()) {
|
||||
return trueValue;
|
||||
} else {
|
||||
return falseValue;
|
||||
}
|
||||
return b ? trueValue : falseValue;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -293,7 +285,7 @@ public class ScalarTypeBoolean {
|
||||
}
|
||||
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Boolean value) throws IOException {
|
||||
ctx.writeBooleanField(name, (Boolean) value);
|
||||
ctx.writeBooleanField(name, value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,10 +20,6 @@ public abstract class ScalarTypeBytesBase extends ScalarTypeBase<byte[]> {
|
||||
super(byte[].class, jdbcNative, jdbcType);
|
||||
}
|
||||
|
||||
public Object convertFromBytes(byte[] bytes) {
|
||||
return bytes;
|
||||
}
|
||||
|
||||
public byte[] convertToBytes(Object value) {
|
||||
return (byte[]) value;
|
||||
}
|
||||
@@ -46,7 +42,7 @@ public abstract class ScalarTypeBytesBase extends ScalarTypeBase<byte[]> {
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator ctx, String name, byte[] value) throws IOException {
|
||||
ctx.writeBinaryField(name, (byte[]) value);
|
||||
ctx.writeBinaryField(name, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -12,9 +12,6 @@ import com.fasterxml.jackson.core.JsonToken;
|
||||
|
||||
/**
|
||||
* Encrypted ScalarType that wraps a byte[] types.
|
||||
*
|
||||
* @author rbygrave
|
||||
*
|
||||
*/
|
||||
public class ScalarTypeBytesEncrypted implements ScalarType<byte[]> {
|
||||
|
||||
@@ -68,7 +65,7 @@ public class ScalarTypeBytesEncrypted implements ScalarType<byte[]> {
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator ctx, String name, byte[] value) throws IOException {
|
||||
ctx.writeBinaryField(name, (byte[]) value);
|
||||
ctx.writeBinaryField(name, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -4,11 +4,11 @@ import javax.persistence.PersistenceException;
|
||||
|
||||
/**
|
||||
* ScalarType for Class that persists it to VARCHAR column.
|
||||
*
|
||||
*
|
||||
* @author emcgreal
|
||||
* @author rbygrave
|
||||
*/
|
||||
@SuppressWarnings({ "rawtypes" })
|
||||
@SuppressWarnings({"rawtypes"})
|
||||
public class ScalarTypeClass extends ScalarTypeBaseVarchar<Class> {
|
||||
|
||||
public ScalarTypeClass() {
|
||||
@@ -41,5 +41,5 @@ public class ScalarTypeClass extends ScalarTypeBaseVarchar<Class> {
|
||||
throw new PersistenceException("Unable to find Class " + value, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -9,11 +9,7 @@ import com.avaje.ebeaninternal.server.core.BasicTypeConverter;
|
||||
* ScalarType for String.
|
||||
*/
|
||||
public class ScalarTypeClob extends ScalarTypeBaseVarchar<String> {
|
||||
|
||||
static final int clobBufferSize = 512;
|
||||
|
||||
static final int stringInitialSize = 512;
|
||||
|
||||
|
||||
protected ScalarTypeClob(boolean jdbcNative, int jdbcType) {
|
||||
super(String.class, jdbcNative, jdbcType);
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ public class ScalarTypeCurrency extends ScalarTypeBaseVarchar<Currency> {
|
||||
|
||||
@Override
|
||||
public String convertToDbString(Currency beanValue) {
|
||||
return ((Currency) beanValue).getCurrencyCode();
|
||||
return beanValue.getCurrencyCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -25,7 +25,7 @@ public class ScalarTypeDouble extends ScalarTypeBase<Double> {
|
||||
if (value == null) {
|
||||
b.setNull(Types.DOUBLE);
|
||||
} else {
|
||||
b.setDouble(value.doubleValue());
|
||||
b.setDouble(value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ public class ScalarTypeDouble extends ScalarTypeBase<Double> {
|
||||
|
||||
@Override
|
||||
public Double convertFromMillis(long systemTimeMillis) {
|
||||
return Double.valueOf(systemTimeMillis);
|
||||
return (double) systemTimeMillis;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -52,8 +52,8 @@ public class ScalarTypeDuration extends ScalarTypeBase<Duration> {
|
||||
|
||||
@Override
|
||||
public Object toJdbcType(Object value) {
|
||||
if (value instanceof Long) return value;
|
||||
return ((Duration)value).getSeconds();
|
||||
if (value instanceof Long) return value;
|
||||
return ((Duration) value).getSeconds();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,14 +1,7 @@
|
||||
package com.avaje.ebeaninternal.server.type;
|
||||
|
||||
import com.avaje.ebean.text.TextException;
|
||||
import com.avaje.ebeaninternal.server.core.BasicTypeConverter;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonToken;
|
||||
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
@@ -17,7 +10,7 @@ import java.time.Duration;
|
||||
/**
|
||||
* ScalarType for java.time.Duration (with Nanos precision).
|
||||
* <p>
|
||||
* Stored in the DB as DECIMAL value.
|
||||
* Stored in the DB as DECIMAL value.
|
||||
* </p>
|
||||
*/
|
||||
public class ScalarTypeDurationWithNanos extends ScalarTypeDuration {
|
||||
@@ -42,8 +35,8 @@ public class ScalarTypeDurationWithNanos extends ScalarTypeDuration {
|
||||
|
||||
@Override
|
||||
public Object toJdbcType(Object value) {
|
||||
if (value instanceof BigDecimal) return value;
|
||||
return convertToBigDecimal((Duration)value);
|
||||
if (value instanceof BigDecimal) return value;
|
||||
return convertToBigDecimal((Duration) value);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -136,7 +136,7 @@ public class ScalarTypeEncryptedWrapper<T> implements ScalarType<T> {
|
||||
public T jsonRead(JsonParser ctx, JsonToken event) throws IOException {
|
||||
return wrapped.jsonRead(ctx, event);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator ctx, String name, T value) throws IOException {
|
||||
wrapped.jsonWrite(ctx, name, value);
|
||||
|
||||
@@ -5,9 +5,9 @@ package com.avaje.ebeaninternal.server.type;
|
||||
*/
|
||||
public interface ScalarTypeEnum {
|
||||
|
||||
/**
|
||||
* Return the IN values for DB constraint construction.
|
||||
*/
|
||||
public String getConstraintInValues();
|
||||
|
||||
/**
|
||||
* Return the IN values for DB constraint construction.
|
||||
*/
|
||||
String getConstraintInValues();
|
||||
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ import com.fasterxml.jackson.core.JsonToken;
|
||||
*/
|
||||
public class ScalarTypeEnumStandard {
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
public static class StringEnum extends EnumBase implements ScalarTypeEnum {
|
||||
|
||||
private final int length;
|
||||
@@ -113,7 +113,7 @@ public class ScalarTypeEnumStandard {
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
public static class OrdinalEnum extends EnumBase implements ScalarTypeEnum {
|
||||
|
||||
private final Object[] enumArray;
|
||||
@@ -196,7 +196,7 @@ public class ScalarTypeEnumStandard {
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
public abstract static class EnumBase extends ScalarTypeBase {
|
||||
|
||||
protected final Class enumType;
|
||||
|
||||
@@ -6,7 +6,7 @@ import java.util.Iterator;
|
||||
/**
|
||||
* Additional control over mapping to DB values.
|
||||
*/
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
public class ScalarTypeEnumWithMapping extends ScalarTypeEnumStandard.EnumBase implements ScalarType, ScalarTypeEnum {
|
||||
|
||||
private final EnumToDbValueMap beanDbMap;
|
||||
|
||||
@@ -25,7 +25,7 @@ public class ScalarTypeFloat extends ScalarTypeBase<Float> {
|
||||
if (value == null) {
|
||||
b.setNull(Types.REAL);
|
||||
} else {
|
||||
b.setFloat(value.floatValue());
|
||||
b.setFloat(value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ public class ScalarTypeFloat extends ScalarTypeBase<Float> {
|
||||
|
||||
@Override
|
||||
public Float convertFromMillis(long systemTimeMillis) {
|
||||
return Float.valueOf(systemTimeMillis);
|
||||
return (float) systemTimeMillis;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -69,8 +69,7 @@ public class ScalarTypeFloat extends ScalarTypeBase<Float> {
|
||||
if (!dataInput.readBoolean()) {
|
||||
return null;
|
||||
} else {
|
||||
float val = dataInput.readFloat();
|
||||
return Float.valueOf(val);
|
||||
return dataInput.readFloat();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,6 +91,6 @@ public class ScalarTypeFloat extends ScalarTypeBase<Float> {
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Float value) throws IOException {
|
||||
ctx.writeNumberField(name, (Float) value);
|
||||
ctx.writeNumberField(name, value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ public class ScalarTypeInteger extends ScalarTypeBase<Integer> {
|
||||
if (value == null) {
|
||||
b.setNull(Types.INTEGER);
|
||||
} else {
|
||||
b.setInt(value.intValue());
|
||||
b.setInt(value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ public class ScalarTypeInteger extends ScalarTypeBase<Integer> {
|
||||
if (!dataInput.readBoolean()) {
|
||||
return null;
|
||||
} else {
|
||||
return Integer.valueOf(dataInput.readInt());
|
||||
return dataInput.readInt();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
package com.avaje.ebeaninternal.server.type;
|
||||
|
||||
import com.avaje.ebean.config.JsonConfig;
|
||||
import com.avaje.ebeaninternal.server.core.BasicTypeConverter;
|
||||
import org.joda.time.DateTime;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.sql.Types;
|
||||
|
||||
import com.avaje.ebean.config.JsonConfig;
|
||||
import org.joda.time.DateTime;
|
||||
|
||||
import com.avaje.ebeaninternal.server.core.BasicTypeConverter;
|
||||
import org.joda.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* ScalarType for Joda DateTime. This maps to a JDBC Timestamp.
|
||||
*/
|
||||
|
||||
@@ -23,7 +23,7 @@ public class ScalarTypeJodaLocalDate extends ScalarTypeBaseDate<LocalDate> {
|
||||
|
||||
@Override
|
||||
public LocalDate convertFromDate(Date ts) {
|
||||
return new LocalDate(((java.util.Date) ts).getTime());
|
||||
return new LocalDate(ts.getTime());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -6,7 +6,6 @@ import java.sql.Timestamp;
|
||||
import java.sql.Types;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
/**
|
||||
|
||||
@@ -44,8 +44,8 @@ public class ScalarTypeLocalTime extends ScalarTypeBase<LocalTime> {
|
||||
|
||||
@Override
|
||||
public Object toJdbcType(Object value) {
|
||||
if (value instanceof Time) return value;
|
||||
return Time.valueOf((LocalTime)value);
|
||||
if (value instanceof Time) return value;
|
||||
return Time.valueOf((LocalTime) value);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -32,8 +32,8 @@ public class ScalarTypeLocalTimeWithNanos extends ScalarTypeLocalTime {
|
||||
|
||||
@Override
|
||||
public Object toJdbcType(Object value) {
|
||||
if (value instanceof Long) return value;
|
||||
return ((LocalTime)value).toNanoOfDay();
|
||||
if (value instanceof Long) return value;
|
||||
return ((LocalTime) value).toNanoOfDay();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -23,7 +23,7 @@ public class ScalarTypeLocale extends ScalarTypeBaseVarchar<Locale> {
|
||||
|
||||
@Override
|
||||
public String convertToDbString(Locale beanValue) {
|
||||
return ((Locale) beanValue).toString();
|
||||
return beanValue.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -25,7 +25,7 @@ public class ScalarTypeLong extends ScalarTypeBase<Long> {
|
||||
if (value == null) {
|
||||
b.setNull(Types.BIGINT);
|
||||
} else {
|
||||
b.setLong(value.longValue());
|
||||
b.setLong(value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,6 +91,6 @@ public class ScalarTypeLong extends ScalarTypeBase<Long> {
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Long value) throws IOException {
|
||||
ctx.writeNumberField(name, (Long) value);
|
||||
ctx.writeNumberField(name, value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import java.sql.Timestamp;
|
||||
|
||||
public class ScalarTypeLongToTimestamp extends ScalarTypeWrapper<Long, Timestamp> {
|
||||
|
||||
public ScalarTypeLongToTimestamp(JsonConfig.DateTime mode) {
|
||||
super(Long.class, new ScalarTypeTimestamp(mode), new LongToTimestampConverter());
|
||||
}
|
||||
public ScalarTypeLongToTimestamp(JsonConfig.DateTime mode) {
|
||||
super(Long.class, new ScalarTypeTimestamp(mode), new LongToTimestampConverter());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,17 @@
|
||||
package com.avaje.ebeaninternal.server.type;
|
||||
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
import java.time.Instant;
|
||||
|
||||
import com.avaje.ebeaninternal.server.core.BasicTypeConverter;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonToken;
|
||||
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
import java.math.BigInteger;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
|
||||
/**
|
||||
* ScalarType for java.math.BigInteger.
|
||||
*/
|
||||
@@ -100,7 +98,7 @@ public class ScalarTypeMathBigInteger extends ScalarTypeBase<BigInteger> {
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator ctx, String name, BigInteger value) throws IOException {
|
||||
ctx.writeNumberField(name, ((BigInteger) value).longValue());
|
||||
ctx.writeNumberField(name, value.longValue());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ public class ScalarTypeMonth extends ScalarTypeEnumWithMapping {
|
||||
b.setNull(Types.INTEGER);
|
||||
} else {
|
||||
// avoiding the map lookup
|
||||
b.setInt(((Month)value).getValue());
|
||||
b.setInt(((Month) value).getValue());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -50,12 +50,12 @@ public class ScalarTypeOffsetDateTime extends ScalarTypeBaseDateTime<OffsetDateT
|
||||
@Override
|
||||
public Object toJdbcType(Object value) {
|
||||
if (value instanceof Timestamp) return value;
|
||||
return convertToTimestamp((OffsetDateTime)value);
|
||||
return convertToTimestamp((OffsetDateTime) value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public OffsetDateTime toBeanType(Object value) {
|
||||
if (value instanceof OffsetDateTime) return (OffsetDateTime) value;
|
||||
return convertFromTimestamp((Timestamp)value);
|
||||
return convertFromTimestamp((Timestamp) value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,38 +20,35 @@ import com.fasterxml.jackson.core.JsonToken;
|
||||
public class ScalarTypePostgresHstore extends ScalarTypeBase<Map> {
|
||||
|
||||
public static final String KEY = "hstore";
|
||||
|
||||
|
||||
public static final int HSTORE_TYPE = PostgresPlatform.TYPE_HSTORE;
|
||||
|
||||
|
||||
public ScalarTypePostgresHstore() {
|
||||
super(Map.class, false, HSTORE_TYPE);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isMutable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isDirty(Object value) {
|
||||
if (value instanceof ModifyAwareOwner) {
|
||||
return ((ModifyAwareOwner)value).isMarkedDirty();
|
||||
}
|
||||
return true;
|
||||
return !(value instanceof ModifyAwareOwner) || ((ModifyAwareOwner) value).isMarkedDirty();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Map read(DataReader dataReader) throws SQLException {
|
||||
|
||||
|
||||
Object value = dataReader.getObject();
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
if (!(value instanceof Map)) {
|
||||
throw new RuntimeException("Expecting Hstore to return as Map but got type "+value.getClass());
|
||||
throw new RuntimeException("Expecting Hstore to return as Map but got type " + value.getClass());
|
||||
}
|
||||
return new ModifyAwareMap((Map)value);
|
||||
return new ModifyAwareMap((Map) value);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -66,7 +63,7 @@ public class ScalarTypePostgresHstore extends ScalarTypeBase<Map> {
|
||||
|
||||
@Override
|
||||
public Map toBeanType(Object value) {
|
||||
return (Map)value;
|
||||
return (Map) value;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -136,5 +133,5 @@ public class ScalarTypePostgresHstore extends ScalarTypeBase<Map> {
|
||||
// the EJson parsing so that it knows the first token has been read
|
||||
return EJson.parseObject(ctx, event);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ public class ScalarTypeShort extends ScalarTypeBase<Short> {
|
||||
if (value == null) {
|
||||
b.setNull(Types.SMALLINT);
|
||||
} else {
|
||||
b.setShort(value.shortValue());
|
||||
b.setShort(value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,6 +92,6 @@ public class ScalarTypeShort extends ScalarTypeBase<Short> {
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Short value) throws IOException {
|
||||
ctx.writeNumberField(name, (Short) value);
|
||||
ctx.writeNumberField(name, value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,6 +91,6 @@ public class ScalarTypeString extends ScalarTypeBase<String> {
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator ctx, String name, String value) throws IOException {
|
||||
ctx.writeStringField(name, (String) value);
|
||||
ctx.writeStringField(name, value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ public class ScalarTypeTimeZone extends ScalarTypeBaseVarchar<TimeZone> {
|
||||
|
||||
@Override
|
||||
public String convertToDbString(TimeZone beanValue) {
|
||||
return ((TimeZone) beanValue).getID();
|
||||
return beanValue.getID();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
package com.avaje.ebeaninternal.server.type;
|
||||
|
||||
import com.avaje.ebean.config.JsonConfig;
|
||||
import com.avaje.ebeaninternal.server.core.BasicTypeConverter;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Timestamp;
|
||||
import java.sql.Types;
|
||||
import java.time.OffsetDateTime;
|
||||
|
||||
import com.avaje.ebean.config.JsonConfig;
|
||||
import com.avaje.ebeaninternal.server.core.BasicTypeConverter;
|
||||
|
||||
/**
|
||||
* ScalarType for java.sql.Timestamp.
|
||||
@@ -48,7 +47,6 @@ public class ScalarTypeTimestamp extends ScalarTypeBaseDateTime<Timestamp> {
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void bind(DataBind b, Timestamp value) throws SQLException {
|
||||
if (value == null) {
|
||||
|
||||
@@ -46,7 +46,7 @@ public class ScalarTypeUtilDate {
|
||||
|
||||
@Override
|
||||
public void bind(DataBind b, java.util.Date value)
|
||||
throws SQLException {
|
||||
throws SQLException {
|
||||
if (value == null) {
|
||||
b.setNull(Types.TIMESTAMP);
|
||||
} else {
|
||||
|
||||
@@ -16,13 +16,9 @@ import com.fasterxml.jackson.core.JsonToken;
|
||||
* <p>
|
||||
* Enables the use of a simple interface to add additional scalarTypes.
|
||||
* </p>
|
||||
*
|
||||
* @author rbygrave
|
||||
*
|
||||
* @param <B>
|
||||
* the logical type
|
||||
* @param <S>
|
||||
* the underlying scalar type this is converted to
|
||||
*
|
||||
* @param <B> the logical type
|
||||
* @param <S> the underlying scalar type this is converted to
|
||||
*/
|
||||
public class ScalarTypeWrapper<B, S> implements ScalarType<B> {
|
||||
|
||||
|
||||
@@ -58,7 +58,7 @@ public class ScalarTypeYear extends ScalarTypeBase<Year> {
|
||||
|
||||
@Override
|
||||
public Object toJdbcType(Object value) {
|
||||
if (value instanceof Year) return ((Year)value).getValue();
|
||||
if (value instanceof Year) return ((Year) value).getValue();
|
||||
return BasicTypeConverter.toInteger(value);
|
||||
}
|
||||
|
||||
|
||||
@@ -53,7 +53,7 @@ public class ScalarTypeYearMonthDate extends ScalarTypeBaseDate<YearMonth> {
|
||||
public Object toJdbcType(Object value) {
|
||||
if (value instanceof Date) return value;
|
||||
if (value instanceof YearMonth) return Date.valueOf(toLocalDate((YearMonth) value));
|
||||
if (value instanceof LocalDate) return Date.valueOf((LocalDate)value);
|
||||
if (value instanceof LocalDate) return Date.valueOf((LocalDate) value);
|
||||
return BasicTypeConverter.toDate(value);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.avaje.ebeaninternal.server.type;
|
||||
|
||||
import java.time.OffsetTime;
|
||||
import java.time.ZoneId;
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,7 +5,6 @@ import com.avaje.ebean.config.JsonConfig;
|
||||
import java.sql.Timestamp;
|
||||
import java.sql.Types;
|
||||
import java.time.Instant;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
@@ -51,12 +50,12 @@ public class ScalarTypeZonedDateTime extends ScalarTypeBaseDateTime<ZonedDateTim
|
||||
@Override
|
||||
public Object toJdbcType(Object value) {
|
||||
if (value instanceof Timestamp) return value;
|
||||
return convertToTimestamp((ZonedDateTime)value);
|
||||
return convertToTimestamp((ZonedDateTime) value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZonedDateTime toBeanType(Object value) {
|
||||
if (value instanceof ZonedDateTime) return (ZonedDateTime) value;
|
||||
return convertFromTimestamp((Timestamp)value);
|
||||
return convertFromTimestamp((Timestamp) value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,118 +11,116 @@ import com.avaje.ebean.config.Encryptor;
|
||||
|
||||
/**
|
||||
* Simple AES based encryption and decryption.
|
||||
*
|
||||
* @author rbygrave
|
||||
*/
|
||||
public class SimpleAesEncryptor implements Encryptor {
|
||||
|
||||
private static final String AES_CIPHER = "AES/CBC/PKCS5Padding";
|
||||
private static final String AES_CIPHER = "AES/CBC/PKCS5Padding";
|
||||
|
||||
private static final String padding = "asldkalsdkadsdfkjsldfjl";
|
||||
private static final String padding = "asldkalsdkadsdfkjsldfjl";
|
||||
|
||||
public SimpleAesEncryptor() {
|
||||
public SimpleAesEncryptor() {
|
||||
}
|
||||
|
||||
private String paddKey(EncryptKey encryptKey) {
|
||||
|
||||
String key = encryptKey.getStringValue();
|
||||
int addChars = 16 - key.length();
|
||||
if (addChars < 0) {
|
||||
return key.substring(0, 16);
|
||||
} else if (addChars > 0) {
|
||||
return key + padding.substring(0, addChars);
|
||||
}
|
||||
return key;
|
||||
}
|
||||
|
||||
private byte[] getKeyBytes(String skey) {
|
||||
|
||||
try {
|
||||
return skey.getBytes("UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private IvParameterSpec getIvParameterSpec(String initialVector) {
|
||||
return new IvParameterSpec(initialVector.getBytes());
|
||||
}
|
||||
|
||||
public byte[] decrypt(byte[] data, EncryptKey encryptKey) {
|
||||
|
||||
if (data == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
private String paddKey(EncryptKey encryptKey) {
|
||||
String key = paddKey(encryptKey);
|
||||
|
||||
String key = encryptKey.getStringValue();
|
||||
int addChars = 16 - key.length();
|
||||
if (addChars < 0) {
|
||||
return key.substring(0, 16);
|
||||
} else if (addChars > 0) {
|
||||
return key + padding.substring(0, addChars);
|
||||
}
|
||||
return key;
|
||||
try {
|
||||
|
||||
byte[] keyBytes = getKeyBytes(key);
|
||||
IvParameterSpec iv = getIvParameterSpec(key);
|
||||
|
||||
SecretKeySpec sks = new SecretKeySpec(keyBytes, "AES");
|
||||
Cipher c = Cipher.getInstance(AES_CIPHER);
|
||||
|
||||
c.init(Cipher.DECRYPT_MODE, sks, iv);
|
||||
|
||||
return c.doFinal(data);
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] encrypt(byte[] data, EncryptKey encryptKey) {
|
||||
|
||||
if (data == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
private byte[] getKeyBytes(String skey) {
|
||||
String key = paddKey(encryptKey);
|
||||
|
||||
try {
|
||||
return skey.getBytes("UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
try {
|
||||
byte[] keyBytes = getKeyBytes(key);
|
||||
IvParameterSpec iv = getIvParameterSpec(key);
|
||||
|
||||
SecretKeySpec sks = new SecretKeySpec(keyBytes, "AES");
|
||||
Cipher c = Cipher.getInstance(AES_CIPHER);
|
||||
|
||||
c.init(Cipher.ENCRYPT_MODE, sks, iv);
|
||||
|
||||
return c.doFinal(data);
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public String decryptString(byte[] data, EncryptKey key) {
|
||||
if (data == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
private IvParameterSpec getIvParameterSpec(String initialVector) {
|
||||
return new IvParameterSpec(initialVector.getBytes());
|
||||
byte[] bytes = decrypt(data, key);
|
||||
try {
|
||||
return new String(bytes, "UTF-8");
|
||||
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] decrypt(byte[] data, EncryptKey encryptKey) {
|
||||
public byte[] encryptString(String valueFormatValue, EncryptKey key) {
|
||||
|
||||
if (data == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String key = paddKey(encryptKey);
|
||||
|
||||
try {
|
||||
|
||||
byte[] keyBytes = getKeyBytes(key);
|
||||
IvParameterSpec iv = getIvParameterSpec(key);
|
||||
|
||||
SecretKeySpec sks = new SecretKeySpec(keyBytes, "AES");
|
||||
Cipher c = Cipher.getInstance(AES_CIPHER);
|
||||
|
||||
c.init(Cipher.DECRYPT_MODE, sks, iv);
|
||||
|
||||
return c.doFinal(data);
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
if (valueFormatValue == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
byte[] d = valueFormatValue.getBytes("UTF-8");
|
||||
return encrypt(d, key);
|
||||
|
||||
public byte[] encrypt(byte[] data, EncryptKey encryptKey) {
|
||||
|
||||
if (data == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String key = paddKey(encryptKey);
|
||||
|
||||
try {
|
||||
byte[] keyBytes = getKeyBytes(key);
|
||||
IvParameterSpec iv = getIvParameterSpec(key);
|
||||
|
||||
SecretKeySpec sks = new SecretKeySpec(keyBytes, "AES");
|
||||
Cipher c = Cipher.getInstance(AES_CIPHER);
|
||||
|
||||
c.init(Cipher.ENCRYPT_MODE, sks, iv);
|
||||
|
||||
return c.doFinal(data);
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public String decryptString(byte[] data, EncryptKey key) {
|
||||
if (data == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
byte[] bytes = decrypt(data, key);
|
||||
try {
|
||||
return new String(bytes, "UTF-8");
|
||||
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] encryptString(String valueFormatValue, EncryptKey key) {
|
||||
|
||||
if (valueFormatValue == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
byte[] d = valueFormatValue.getBytes("UTF-8");
|
||||
return encrypt(d, key);
|
||||
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,39 +5,39 @@ import java.lang.reflect.Type;
|
||||
|
||||
public class TypeReflectHelper {
|
||||
|
||||
public static Class<?>[] getParams(Class<?> cls, Class<?> matchRawType) {
|
||||
public static Class<?>[] getParams(Class<?> cls, Class<?> matchRawType) {
|
||||
|
||||
Type[] types = getParamType(cls, matchRawType);
|
||||
Class<?>[] result = new Class<?>[types.length];
|
||||
for (int i = 0; i < result.length; i++) {
|
||||
result[i] = getClass(types[i]);
|
||||
}
|
||||
return result;
|
||||
Type[] types = getParamType(cls, matchRawType);
|
||||
Class<?>[] result = new Class<?>[types.length];
|
||||
for (int i = 0; i < result.length; i++) {
|
||||
result[i] = getClass(types[i]);
|
||||
}
|
||||
|
||||
public static Class<?> getClass(Type type){
|
||||
|
||||
if (type instanceof ParameterizedType){
|
||||
return getClass(((ParameterizedType)type).getRawType());
|
||||
}
|
||||
|
||||
return (Class<?>)type;
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Class<?> getClass(Type type) {
|
||||
|
||||
if (type instanceof ParameterizedType) {
|
||||
return getClass(((ParameterizedType) type).getRawType());
|
||||
}
|
||||
|
||||
private static Type[] getParamType(Class<?> cls, Class<?> matchRawType) {
|
||||
|
||||
Type[] gis = cls.getGenericInterfaces();
|
||||
for (int i = 0; i < gis.length; i++) {
|
||||
Type type = gis[i];
|
||||
if (type instanceof ParameterizedType) {
|
||||
ParameterizedType paramType = (ParameterizedType) type;
|
||||
Type rawType = paramType.getRawType();
|
||||
if (rawType.equals(matchRawType)) {
|
||||
|
||||
return paramType.getActualTypeArguments();
|
||||
}
|
||||
}
|
||||
|
||||
return (Class<?>) type;
|
||||
}
|
||||
|
||||
private static Type[] getParamType(Class<?> cls, Class<?> matchRawType) {
|
||||
|
||||
Type[] gis = cls.getGenericInterfaces();
|
||||
for (int i = 0; i < gis.length; i++) {
|
||||
Type type = gis[i];
|
||||
if (type instanceof ParameterizedType) {
|
||||
ParameterizedType paramType = (ParameterizedType) type;
|
||||
Type rawType = paramType.getRawType();
|
||||
if (rawType.equals(matchRawType)) {
|
||||
|
||||
return paramType.getActualTypeArguments();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user