diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/BeanToDbMap.java b/src/main/java/com/avaje/ebeaninternal/server/type/BeanToDbMap.java index 0339d5e2c..d7b07758f 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/BeanToDbMap.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/BeanToDbMap.java @@ -1,84 +1,84 @@ -package com.avaje.ebeaninternal.server.type; - -import java.util.HashMap; - -/** - * Used to map Bean values to DB values. - *

- * Useful for building Enum converters where you want to map the DB values an - * Enum gets converter to. - *

- * - * @param - * The Bean value type - * @param - * The DB value type - */ -public class BeanToDbMap { - - final HashMap keyMap; - - final HashMap valueMap; - - final boolean allowNulls; - - /** - * Construct with allowNulls defaulting to false. - */ - public BeanToDbMap() { - this(false); - } - - /** - * Construct with allowNulls setting. - *

- * 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. - *

- */ - public BeanToDbMap(boolean allowNulls) { - this.allowNulls = allowNulls; - keyMap = new HashMap(); - valueMap = new HashMap(); - } - - /** - * Add a bean value and DB value pair. - */ - public BeanToDbMap add(B beanValue, D dbValue) { - keyMap.put(beanValue, dbValue); - valueMap.put(dbValue, beanValue); - return this; - } - - /** - * Return the DB value given the bean value. - */ - public D getDbValue(B beanValue) { - if (beanValue == null){ - return null; - } - D dbValue = keyMap.get(beanValue); - if (dbValue == null && !allowNulls) { - String msg = "DB value for " + beanValue + " not found in "+valueMap; - throw new IllegalArgumentException(msg); - } - return dbValue; - } - - /** - * Return the Bean value given the DB value. - */ - public B getBeanValue(D dbValue) { - if (dbValue == null){ - return null; - } - B beanValue = valueMap.get(dbValue); - if (beanValue == null && !allowNulls) { - String msg = "Bean value for " + dbValue + " not found in "+valueMap; - throw new IllegalArgumentException(msg); - } - return beanValue; - } -} +package com.avaje.ebeaninternal.server.type; + +import java.util.HashMap; + +/** + * Used to map Bean values to DB values. + *

+ * Useful for building Enum converters where you want to map the DB values an + * Enum gets converter to. + *

+ * + * @param + * The Bean value type + * @param + * The DB value type + */ +public class BeanToDbMap { + + final HashMap keyMap; + + final HashMap valueMap; + + final boolean allowNulls; + + /** + * Construct with allowNulls defaulting to false. + */ + public BeanToDbMap() { + this(false); + } + + /** + * Construct with allowNulls setting. + *

+ * 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. + *

+ */ + public BeanToDbMap(boolean allowNulls) { + this.allowNulls = allowNulls; + keyMap = new HashMap(); + valueMap = new HashMap(); + } + + /** + * Add a bean value and DB value pair. + */ + public BeanToDbMap add(B beanValue, D dbValue) { + keyMap.put(beanValue, dbValue); + valueMap.put(dbValue, beanValue); + return this; + } + + /** + * Return the DB value given the bean value. + */ + public D getDbValue(B beanValue) { + if (beanValue == null){ + return null; + } + D dbValue = keyMap.get(beanValue); + if (dbValue == null && !allowNulls) { + String msg = "DB value for " + beanValue + " not found in "+valueMap; + throw new IllegalArgumentException(msg); + } + return dbValue; + } + + /** + * Return the Bean value given the DB value. + */ + public B getBeanValue(D dbValue) { + if (dbValue == null){ + return null; + } + B beanValue = valueMap.get(dbValue); + if (beanValue == null && !allowNulls) { + String msg = "Bean value for " + dbValue + " not found in "+valueMap; + throw new IllegalArgumentException(msg); + } + return beanValue; + } +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/CtCompoundProperty.java b/src/main/java/com/avaje/ebeaninternal/server/type/CtCompoundProperty.java index bc20eef20..c572bc5f8 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/CtCompoundProperty.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/CtCompoundProperty.java @@ -1,82 +1,82 @@ -package com.avaje.ebeaninternal.server.type; - -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 CtCompoundProperty parent; - - private final CtCompoundType compoundType; - - @SuppressWarnings({ "rawtypes" }) - private final CompoundTypeProperty property; - - public CtCompoundProperty(String relativeName, CtCompoundProperty parent, CtCompoundType ctType, - CompoundTypeProperty 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; - } - if (parent != null) { - valueObject = parent.getValue(valueObject); - } - return property.getValue(valueObject); - } - - /** - * Set a scalar value that is used to build the immutable compound value - * object. - *

- * When all the scalar values have been collected then the compound value - * object is built and this can be recursive for nested compound types. - *

- */ - 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; - } - } - -} +package com.avaje.ebeaninternal.server.type; + +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 CtCompoundProperty parent; + + private final CtCompoundType compoundType; + + @SuppressWarnings({ "rawtypes" }) + private final CompoundTypeProperty property; + + public CtCompoundProperty(String relativeName, CtCompoundProperty parent, CtCompoundType ctType, + CompoundTypeProperty 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; + } + if (parent != null) { + valueObject = parent.getValue(valueObject); + } + return property.getValue(valueObject); + } + + /** + * Set a scalar value that is used to build the immutable compound value + * object. + *

+ * When all the scalar values have been collected then the compound value + * object is built and this can be recursive for nested compound types. + *

+ */ + 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; + } + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/CtCompoundPropertyElAdapter.java b/src/main/java/com/avaje/ebeaninternal/server/type/CtCompoundPropertyElAdapter.java index 6e6557630..1a0d0763b 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/CtCompoundPropertyElAdapter.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/CtCompoundPropertyElAdapter.java @@ -1,145 +1,145 @@ -package com.avaje.ebeaninternal.server.type; - -import com.avaje.ebean.bean.EntityBean; -import com.avaje.ebean.text.StringFormatter; -import com.avaje.ebean.text.StringParser; -import com.avaje.ebeaninternal.server.deploy.BeanProperty; -import com.avaje.ebeaninternal.server.el.ElPropertyValue; - -/** - * Adapter for CtCompoundProperty to ElPropertyValue. - *

- * This is used for non-scalar properties of a Compound Value Object. These only - * occur in nested compound types. - *

- * - * @author rbygrave - */ -public class CtCompoundPropertyElAdapter implements ElPropertyValue { - - private final CtCompoundProperty prop; - - private int deployOrder; - - public CtCompoundPropertyElAdapter(CtCompoundProperty prop) { - this.prop = prop; - } - - public void setDeployOrder(int deployOrder) { - this.deployOrder = deployOrder; - } - - public Object elConvertType(Object value) { - return value; - } - - public Object elGetReference(EntityBean bean) { - return bean; - } - - public Object elGetValue(EntityBean bean) { - return prop.getValue(bean); - } - - public void elSetReference(EntityBean bean) { - // Do nothing - } - - public void elSetValue(EntityBean bean, Object value, boolean populate) { - prop.setValue(bean, value); - } - - public int getDeployOrder() { - return deployOrder; - } - - public String getAssocOneIdExpr(String prefix, String operator) { - throw new RuntimeException("Not Supported or Expected"); - } - - public Object[] getAssocOneIdValues(EntityBean bean) { - throw new RuntimeException("Not Supported or Expected"); - } - - public String getAssocIdInExpr(String prefix) { - throw new RuntimeException("Not Supported or Expected"); - } - - public String getAssocIdInValueExpr(int size) { - throw new RuntimeException("Not Supported or Expected"); - } - - public BeanProperty getBeanProperty() { - return null; - } - - public StringFormatter getStringFormatter() { - return null; - } - - public StringParser getStringParser() { - return null; - } - - public boolean isDbEncrypted() { - return false; - } - - public boolean isLocalEncrypted() { - return false; - } - - public boolean isAssocId() { - return false; - } - - public boolean isAssocProperty() { - return false; - } - - public boolean isDateTimeCapable() { - return false; - } - - public int getJdbcType() { - return 0; - } - - public Object parseDateTime(long systemTimeMillis) { - throw new RuntimeException("Not Supported or Expected"); - } - - @Override - public boolean containsFormulaWithJoin() { - return false; - } - - public boolean containsMany() { - return false; - } - - public boolean containsManySince(String sinceProperty) { - return containsMany(); - } - - public String getDbColumn() { - return null; - } - - public String getElPlaceholder(boolean encrypted) { - return null; - } - - public String getElPrefix() { - return null; - } - - public String getName() { - return prop.getPropertyName(); - } - - public String getElName() { - return prop.getPropertyName(); - } - -} +package com.avaje.ebeaninternal.server.type; + +import com.avaje.ebean.bean.EntityBean; +import com.avaje.ebean.text.StringFormatter; +import com.avaje.ebean.text.StringParser; +import com.avaje.ebeaninternal.server.deploy.BeanProperty; +import com.avaje.ebeaninternal.server.el.ElPropertyValue; + +/** + * Adapter for CtCompoundProperty to ElPropertyValue. + *

+ * This is used for non-scalar properties of a Compound Value Object. These only + * occur in nested compound types. + *

+ * + * @author rbygrave + */ +public class CtCompoundPropertyElAdapter implements ElPropertyValue { + + private final CtCompoundProperty prop; + + private int deployOrder; + + public CtCompoundPropertyElAdapter(CtCompoundProperty prop) { + this.prop = prop; + } + + public void setDeployOrder(int deployOrder) { + this.deployOrder = deployOrder; + } + + public Object elConvertType(Object value) { + return value; + } + + public Object elGetReference(EntityBean bean) { + return bean; + } + + public Object elGetValue(EntityBean bean) { + return prop.getValue(bean); + } + + public void elSetReference(EntityBean bean) { + // Do nothing + } + + public void elSetValue(EntityBean bean, Object value, boolean populate) { + prop.setValue(bean, value); + } + + public int getDeployOrder() { + return deployOrder; + } + + public String getAssocOneIdExpr(String prefix, String operator) { + throw new RuntimeException("Not Supported or Expected"); + } + + public Object[] getAssocOneIdValues(EntityBean bean) { + throw new RuntimeException("Not Supported or Expected"); + } + + public String getAssocIdInExpr(String prefix) { + throw new RuntimeException("Not Supported or Expected"); + } + + public String getAssocIdInValueExpr(int size) { + throw new RuntimeException("Not Supported or Expected"); + } + + public BeanProperty getBeanProperty() { + return null; + } + + public StringFormatter getStringFormatter() { + return null; + } + + public StringParser getStringParser() { + return null; + } + + public boolean isDbEncrypted() { + return false; + } + + public boolean isLocalEncrypted() { + return false; + } + + public boolean isAssocId() { + return false; + } + + public boolean isAssocProperty() { + return false; + } + + public boolean isDateTimeCapable() { + return false; + } + + public int getJdbcType() { + return 0; + } + + public Object parseDateTime(long systemTimeMillis) { + throw new RuntimeException("Not Supported or Expected"); + } + + @Override + public boolean containsFormulaWithJoin() { + return false; + } + + public boolean containsMany() { + return false; + } + + public boolean containsManySince(String sinceProperty) { + return containsMany(); + } + + public String getDbColumn() { + return null; + } + + public String getElPlaceholder(boolean encrypted) { + return null; + } + + public String getElPrefix() { + return null; + } + + public String getName() { + return prop.getPropertyName(); + } + + public String getElName() { + return prop.getPropertyName(); + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/CtCompoundType.java b/src/main/java/com/avaje/ebeaninternal/server/type/CtCompoundType.java index 5167aca71..356d976c4 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/CtCompoundType.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/CtCompoundType.java @@ -1,229 +1,229 @@ -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; - -/** - * The internal representation of a Compound Type (Immutable Compound Value Object). - * - * @param - * The Type of the "Immutable Compound Value Object". - */ -public final class CtCompoundType implements ScalarDataReader { - - private final Class cvoClass; - - private final CompoundType cvoType; - - private final Map> propertyMap; - - private final ScalarDataReader[] propReaders; - - private final CompoundTypeProperty[] properties; - - public CtCompoundType(Class cvoClass, CompoundType cvoType, ScalarDataReader[] propReaders) { - - this.cvoClass = cvoClass; - this.cvoType = cvoType; - this.properties = cvoType.getProperties(); - this.propReaders = propReaders; - - this.propertyMap = new LinkedHashMap>(); - for (CompoundTypeProperty cp: properties) { - propertyMap.put(cp.getName(), cp); - } - } - - public String toString() { - return cvoClass.toString(); - } - - public Class getCompoundTypeClass() { - return cvoClass; - } - - public V create(Object[] propertyValues) { - return cvoType.create(propertyValues); - } - - - public V create(Map 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); - } - - public CompoundTypeProperty[] getProperties() { - - return cvoType.getProperties(); - } - - 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; - } - - 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); - } - - 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[] 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). - *

- * This creates a flat list of scalars even when compound types are embedded - * inside compound types. - *

- */ - public void accumulateScalarTypes(String parent, CtCompoundTypeScalarList list) { - - CompoundTypeProperty[] 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 map) { - return readJsonElementObject(map); - } - - @SuppressWarnings("unchecked") - private Object readJsonElementObject(Map 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)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" }) - 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++) { - String propName = properties[i].getName(); - Object value = properties[i].getValue((V) valueObject); - if (propReaders[i] instanceof CtCompoundType) { - ((CtCompoundType) propReaders[i]).jsonWrite(ctx, value, propName); - - } else { - ((ScalarType) propReaders[i]).jsonWrite(ctx.gen(), propName, value); - //ctx.appendNameValue(propName, (ScalarType) propReaders[i], value); - } - } - - ctx.gen().writeEndObject(); - } - -} +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; + +/** + * The internal representation of a Compound Type (Immutable Compound Value Object). + * + * @param + * The Type of the "Immutable Compound Value Object". + */ +public final class CtCompoundType implements ScalarDataReader { + + private final Class cvoClass; + + private final CompoundType cvoType; + + private final Map> propertyMap; + + private final ScalarDataReader[] propReaders; + + private final CompoundTypeProperty[] properties; + + public CtCompoundType(Class cvoClass, CompoundType cvoType, ScalarDataReader[] propReaders) { + + this.cvoClass = cvoClass; + this.cvoType = cvoType; + this.properties = cvoType.getProperties(); + this.propReaders = propReaders; + + this.propertyMap = new LinkedHashMap>(); + for (CompoundTypeProperty cp: properties) { + propertyMap.put(cp.getName(), cp); + } + } + + public String toString() { + return cvoClass.toString(); + } + + public Class getCompoundTypeClass() { + return cvoClass; + } + + public V create(Object[] propertyValues) { + return cvoType.create(propertyValues); + } + + + public V create(Map 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); + } + + public CompoundTypeProperty[] getProperties() { + + return cvoType.getProperties(); + } + + 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; + } + + 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); + } + + 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[] 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). + *

+ * This creates a flat list of scalars even when compound types are embedded + * inside compound types. + *

+ */ + public void accumulateScalarTypes(String parent, CtCompoundTypeScalarList list) { + + CompoundTypeProperty[] 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 map) { + return readJsonElementObject(map); + } + + @SuppressWarnings("unchecked") + private Object readJsonElementObject(Map 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)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" }) + 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++) { + String propName = properties[i].getName(); + Object value = properties[i].getValue((V) valueObject); + if (propReaders[i] instanceof CtCompoundType) { + ((CtCompoundType) propReaders[i]).jsonWrite(ctx, value, propName); + + } else { + ((ScalarType) propReaders[i]).jsonWrite(ctx.gen(), propName, value); + //ctx.appendNameValue(propName, (ScalarType) propReaders[i], value); + } + } + + ctx.gen().writeEndObject(); + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/CtCompoundTypeScalarList.java b/src/main/java/com/avaje/ebeaninternal/server/type/CtCompoundTypeScalarList.java index 79ddedea4..2553c9217 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/CtCompoundTypeScalarList.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/CtCompoundTypeScalarList.java @@ -1,69 +1,69 @@ -package com.avaje.ebeaninternal.server.type; - -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map.Entry; -import java.util.Set; - -import com.avaje.ebean.config.CompoundTypeProperty; -import com.avaje.ebeaninternal.server.query.SplitName; - -/** - * Used to build a flat list of all the scalar types nested in a compound type. - * - * @author rbygrave - */ -public final class CtCompoundTypeScalarList { - - private final LinkedHashMap> scalarProps = new LinkedHashMap>(); - - private final LinkedHashMap compoundProperties = new LinkedHashMap(); - - /** - * Return the list of non-scalar properties. These occur when compound types are nested. - */ - public List getNonScalarProperties() { - - List nonScalarProps = new ArrayList(); - - for (String propKey: compoundProperties.keySet()) { - if (!scalarProps.containsKey(propKey)){ - nonScalarProps.add(compoundProperties.get(propKey)); - } - } - - return nonScalarProps; - } - - /** - * Register a property with it's associated compound type and relative name. - */ - public void addCompoundProperty(String propName, CtCompoundType t, CompoundTypeProperty prop) { - - CtCompoundProperty parent = null; - String[] split = SplitName.split(propName); - if (split[0] != null){ - parent = compoundProperties.get(split[0]); - } - - CtCompoundProperty p = new CtCompoundProperty(propName, parent, t, prop); - compoundProperties.put(propName, p); - } - - /** - * Register a scalarType used in the compound type with its given property name. - */ - public void addScalarType(String propName, ScalarType scalar){ - scalarProps.put(propName, scalar); - } - - public CtCompoundProperty getCompoundType(String propName) { - return compoundProperties.get(propName); - } - - public Set>> entries() { - return scalarProps.entrySet(); - } - -} +package com.avaje.ebeaninternal.server.type; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map.Entry; +import java.util.Set; + +import com.avaje.ebean.config.CompoundTypeProperty; +import com.avaje.ebeaninternal.server.query.SplitName; + +/** + * Used to build a flat list of all the scalar types nested in a compound type. + * + * @author rbygrave + */ +public final class CtCompoundTypeScalarList { + + private final LinkedHashMap> scalarProps = new LinkedHashMap>(); + + private final LinkedHashMap compoundProperties = new LinkedHashMap(); + + /** + * Return the list of non-scalar properties. These occur when compound types are nested. + */ + public List getNonScalarProperties() { + + List nonScalarProps = new ArrayList(); + + for (String propKey: compoundProperties.keySet()) { + if (!scalarProps.containsKey(propKey)){ + nonScalarProps.add(compoundProperties.get(propKey)); + } + } + + return nonScalarProps; + } + + /** + * Register a property with it's associated compound type and relative name. + */ + public void addCompoundProperty(String propName, CtCompoundType t, CompoundTypeProperty prop) { + + CtCompoundProperty parent = null; + String[] split = SplitName.split(propName); + if (split[0] != null){ + parent = compoundProperties.get(split[0]); + } + + CtCompoundProperty p = new CtCompoundProperty(propName, parent, t, prop); + compoundProperties.put(propName, p); + } + + /** + * Register a scalarType used in the compound type with its given property name. + */ + public void addScalarType(String propName, ScalarType scalar){ + scalarProps.put(propName, scalar); + } + + public CtCompoundProperty getCompoundType(String propName) { + return compoundProperties.get(propName); + } + + public Set>> entries() { + return scalarProps.entrySet(); + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/DataBind.java b/src/main/java/com/avaje/ebeaninternal/server/type/DataBind.java index acc20626e..4f55a95f4 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/DataBind.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/DataBind.java @@ -1,137 +1,137 @@ -package com.avaje.ebeaninternal.server.type; - -import java.io.ByteArrayInputStream; -import java.io.InputStream; -import java.io.Reader; -import java.io.StringReader; -import java.math.BigDecimal; -import java.sql.PreparedStatement; -import java.sql.SQLException; -import java.sql.Time; -import java.sql.Timestamp; - -public class DataBind { - - private final PreparedStatement pstmt; - - private int pos; - - 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 setObject(Object value) throws SQLException { - pstmt.setObject(++pos, value); - } - - public void setObject(Object value, int sqlType) throws SQLException { - pstmt.setObject(++pos, value, sqlType); - } - - public void setNull(int jdbcType) throws SQLException { - pstmt.setNull(++pos, jdbcType); - } - - public int nextPos() { - return ++pos; - } - - public int decrementPos() { - return ++pos; - } - - public int executeUpdate() throws SQLException { - return pstmt.executeUpdate(); - } - - public PreparedStatement getPstmt() { - return pstmt; - } - - public void setString(String s) throws SQLException { - pstmt.setString(++pos, s); - } - - public void setInt(int i) throws SQLException { - pstmt.setInt(++pos, i); - } - - public void setLong(long i) throws SQLException { - pstmt.setLong(++pos, i); - } - - public void setShort(short i) throws SQLException { - pstmt.setShort(++pos, i); - } - - public void setFloat(float i) throws SQLException { - pstmt.setFloat(++pos, i); - } - - public void setDouble(double i) throws SQLException { - pstmt.setDouble(++pos, i); - } - - public void setBigDecimal(BigDecimal v) throws SQLException { - pstmt.setBigDecimal(++pos, v); - } - - public void setDate(java.sql.Date v) throws SQLException { - pstmt.setDate(++pos, 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); - } - - public void setBlob(InputStream inputStream) throws SQLException { - 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()); - } - +package com.avaje.ebeaninternal.server.type; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.io.Reader; +import java.io.StringReader; +import java.math.BigDecimal; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.sql.Time; +import java.sql.Timestamp; + +public class DataBind { + + private final PreparedStatement pstmt; + + private int pos; + + 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 setObject(Object value) throws SQLException { + pstmt.setObject(++pos, value); + } + + public void setObject(Object value, int sqlType) throws SQLException { + pstmt.setObject(++pos, value, sqlType); + } + + public void setNull(int jdbcType) throws SQLException { + pstmt.setNull(++pos, jdbcType); + } + + public int nextPos() { + return ++pos; + } + + public int decrementPos() { + return ++pos; + } + + public int executeUpdate() throws SQLException { + return pstmt.executeUpdate(); + } + + public PreparedStatement getPstmt() { + return pstmt; + } + + public void setString(String s) throws SQLException { + pstmt.setString(++pos, s); + } + + public void setInt(int i) throws SQLException { + pstmt.setInt(++pos, i); + } + + public void setLong(long i) throws SQLException { + pstmt.setLong(++pos, i); + } + + public void setShort(short i) throws SQLException { + pstmt.setShort(++pos, i); + } + + public void setFloat(float i) throws SQLException { + pstmt.setFloat(++pos, i); + } + + public void setDouble(double i) throws SQLException { + pstmt.setDouble(++pos, i); + } + + public void setBigDecimal(BigDecimal v) throws SQLException { + pstmt.setBigDecimal(++pos, v); + } + + public void setDate(java.sql.Date v) throws SQLException { + pstmt.setDate(++pos, 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); + } + + public void setBlob(InputStream inputStream) throws SQLException { + 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()); + } + } \ No newline at end of file diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/DataEncryptSupport.java b/src/main/java/com/avaje/ebeaninternal/server/type/DataEncryptSupport.java index e2bb35eb8..b049f7f09 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/DataEncryptSupport.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/DataEncryptSupport.java @@ -1,43 +1,43 @@ -package com.avaje.ebeaninternal.server.type; - -import com.avaje.ebean.config.EncryptKey; -import com.avaje.ebean.config.EncryptKeyManager; -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); - } - - 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); - } - -} +package com.avaje.ebeaninternal.server.type; + +import com.avaje.ebean.config.EncryptKey; +import com.avaje.ebean.config.EncryptKeyManager; +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); + } + + 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); + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/DataReader.java b/src/main/java/com/avaje/ebeaninternal/server/type/DataReader.java index 2b92e13cc..7e8dfc33f 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/DataReader.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/DataReader.java @@ -1,57 +1,57 @@ -package com.avaje.ebeaninternal.server.type; - -import java.io.InputStream; -import java.math.BigDecimal; -import java.sql.Array; -import java.sql.SQLException; - -public interface DataReader { - - public void close() throws SQLException; - - public boolean next() throws SQLException; - - public void resetColumnPosition(); - - public void incrementPos(int increment); - - public byte[] getBinaryBytes() throws SQLException; - - public byte[] getBlobBytes() throws SQLException; - - public String getStringFromStream() throws SQLException; - - public String getStringClob() throws SQLException; - - public String getString() throws SQLException; - - public Boolean getBoolean() throws SQLException; - - public Byte getByte() throws SQLException; - - public Short getShort() throws SQLException; - - public Integer getInt() throws SQLException; - - public Long getLong() throws SQLException; - - public Float getFloat() throws SQLException; - - public Double getDouble() throws SQLException; - - public byte[] getBytes() throws SQLException; - - public java.sql.Date getDate() throws SQLException; - - public java.sql.Time getTime() throws SQLException; - - public java.sql.Timestamp getTimestamp() throws SQLException; - - public BigDecimal getBigDecimal() throws SQLException; - - public Array getArray() throws SQLException; - - public Object getObject() throws SQLException; - - public InputStream getBinaryStream() throws SQLException; -} +package com.avaje.ebeaninternal.server.type; + +import java.io.InputStream; +import java.math.BigDecimal; +import java.sql.Array; +import java.sql.SQLException; + +public interface DataReader { + + public void close() throws SQLException; + + public boolean next() throws SQLException; + + public void resetColumnPosition(); + + public void incrementPos(int increment); + + public byte[] getBinaryBytes() throws SQLException; + + public byte[] getBlobBytes() throws SQLException; + + public String getStringFromStream() throws SQLException; + + public String getStringClob() throws SQLException; + + public String getString() throws SQLException; + + public Boolean getBoolean() throws SQLException; + + public Byte getByte() throws SQLException; + + public Short getShort() throws SQLException; + + public Integer getInt() throws SQLException; + + public Long getLong() throws SQLException; + + public Float getFloat() throws SQLException; + + public Double getDouble() throws SQLException; + + public byte[] getBytes() throws SQLException; + + public java.sql.Date getDate() throws SQLException; + + public java.sql.Time getTime() throws SQLException; + + public java.sql.Timestamp getTimestamp() throws SQLException; + + public BigDecimal getBigDecimal() throws SQLException; + + public Array getArray() throws SQLException; + + public Object getObject() throws SQLException; + + public InputStream getBinaryStream() throws SQLException; +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/DateTimeJsonParser.java b/src/main/java/com/avaje/ebeaninternal/server/type/DateTimeJsonParser.java index df303c431..8224a06cf 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/DateTimeJsonParser.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/DateTimeJsonParser.java @@ -1,38 +1,38 @@ -package com.avaje.ebeaninternal.server.type; - -import java.sql.Timestamp; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.TimeZone; - -public class DateTimeJsonParser { - - private final SimpleDateFormat dateTimeProto; - - public DateTimeJsonParser() { - this("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); - } - - public DateTimeJsonParser(String dateTimeFormat) { - this.dateTimeProto = new SimpleDateFormat(dateTimeFormat); - this.dateTimeProto.setTimeZone(TimeZone.getTimeZone("UTC")); - } - - private SimpleDateFormat dtFormat() { - return (SimpleDateFormat) dateTimeProto.clone(); - } - - public Timestamp parse(String jsonDateTime) { - try { - java.util.Date d = dtFormat().parse(jsonDateTime); - return new Timestamp(d.getTime()); - } catch (ParseException e) { - throw new RuntimeException("Error parsing Datetime[" + jsonDateTime + "]", e); - } - } - - public String format(Date value) { - return dtFormat().format(value); - } -} +package com.avaje.ebeaninternal.server.type; + +import java.sql.Timestamp; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + +public class DateTimeJsonParser { + + private final SimpleDateFormat dateTimeProto; + + public DateTimeJsonParser() { + this("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); + } + + public DateTimeJsonParser(String dateTimeFormat) { + this.dateTimeProto = new SimpleDateFormat(dateTimeFormat); + this.dateTimeProto.setTimeZone(TimeZone.getTimeZone("UTC")); + } + + private SimpleDateFormat dtFormat() { + return (SimpleDateFormat) dateTimeProto.clone(); + } + + public Timestamp parse(String jsonDateTime) { + try { + java.util.Date d = dtFormat().parse(jsonDateTime); + return new Timestamp(d.getTime()); + } catch (ParseException e) { + throw new RuntimeException("Error parsing Datetime[" + jsonDateTime + "]", e); + } + } + + public String format(Date value) { + return dtFormat().format(value); + } +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/DefaultTypeFactory.java b/src/main/java/com/avaje/ebeaninternal/server/type/DefaultTypeFactory.java index ffbd27410..0baeef804 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/DefaultTypeFactory.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/DefaultTypeFactory.java @@ -1,143 +1,143 @@ -package com.avaje.ebeaninternal.server.type; - -import java.math.BigInteger; -import java.sql.Types; -import java.util.Calendar; - -import com.avaje.ebean.config.JsonConfig; -import com.avaje.ebean.config.ServerConfig; -import com.avaje.ebeaninternal.server.core.BasicTypeConverter; - -/** - * Helper to create some default ScalarType objects for Booleans, - * java.util.Date, java.util.Calendar etc. - */ -public class DefaultTypeFactory { - - private final ServerConfig serverConfig; - - public DefaultTypeFactory(ServerConfig serverConfig) { - this.serverConfig = serverConfig; - } - - private ScalarType 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 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(); - } - - /** - * Create the default ScalarType for java.util.Date. - */ - public ScalarType 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); - } - - /** - * Create a ScalarType for java.util.Date explicitly specifying the type to - * map to. - */ - public ScalarType 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 createCalendar(JsonConfig.DateTime mode) { - - int jdbcType = getTemporalMapType("timestamp"); - return createCalendar(mode, jdbcType); - } - - /** - * Create a ScalarType for java.util.Calendar explicitly specifying the type - * to map to. - */ - public ScalarType 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.math.BigInteger. - */ - public ScalarType createMathBigInteger() { - - return new ScalarTypeMathBigInteger(); - } -} +package com.avaje.ebeaninternal.server.type; + +import java.math.BigInteger; +import java.sql.Types; +import java.util.Calendar; + +import com.avaje.ebean.config.JsonConfig; +import com.avaje.ebean.config.ServerConfig; +import com.avaje.ebeaninternal.server.core.BasicTypeConverter; + +/** + * Helper to create some default ScalarType objects for Booleans, + * java.util.Date, java.util.Calendar etc. + */ +public class DefaultTypeFactory { + + private final ServerConfig serverConfig; + + public DefaultTypeFactory(ServerConfig serverConfig) { + this.serverConfig = serverConfig; + } + + private ScalarType 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 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(); + } + + /** + * Create the default ScalarType for java.util.Date. + */ + public ScalarType 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); + } + + /** + * Create a ScalarType for java.util.Date explicitly specifying the type to + * map to. + */ + public ScalarType 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 createCalendar(JsonConfig.DateTime mode) { + + int jdbcType = getTemporalMapType("timestamp"); + return createCalendar(mode, jdbcType); + } + + /** + * Create a ScalarType for java.util.Calendar explicitly specifying the type + * to map to. + */ + public ScalarType 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.math.BigInteger. + */ + public ScalarType createMathBigInteger() { + + return new ScalarTypeMathBigInteger(); + } +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/EnumToDbValueMap.java b/src/main/java/com/avaje/ebeaninternal/server/type/EnumToDbValueMap.java index 8b8857f92..edbf93592 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/EnumToDbValueMap.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/EnumToDbValueMap.java @@ -1,134 +1,134 @@ -package com.avaje.ebeaninternal.server.type; - -import java.sql.SQLException; -import java.util.Iterator; -import java.util.LinkedHashMap; - -/** - * Used to map Bean values to DB values. - *

- * Useful for building Enum converters where you want to map the DB values an - * Enum gets converter to. - *

- */ -public abstract class EnumToDbValueMap { - - public static EnumToDbValueMap create(boolean integerType) { - return integerType ? new EnumToDbIntegerMap() : new EnumToDbStringMap(); - } - - final LinkedHashMap keyMap; - - final LinkedHashMap valueMap; - - final boolean allowNulls; - - final boolean isIntegerType; - - /** - * Construct with allowNulls defaulting to false. - */ - public EnumToDbValueMap() { - this(false, false); - } - - /** - * Construct with allowNulls setting. - *

- * 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. - *

- */ - public EnumToDbValueMap(boolean allowNulls, boolean isIntegerType) { - this.allowNulls = allowNulls; - this.isIntegerType = isIntegerType; - keyMap = new LinkedHashMap(); - valueMap = new LinkedHashMap(); - } - - /** - * Return true if this is mapping to integers, false - * if mapping to Strings. - */ - public boolean isIntegerType() { - return isIntegerType; - } - - /** - * Return the DB values. - */ - public Iterator dbValues() { - return valueMap.keySet().iterator(); - } - - /** - * Return the bean 'key' value. - */ - public Iterator beanValues() { - return valueMap.values().iterator(); - } - - /** - * 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; - - /** - * 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 add(Object beanValue, String dbValue); - - /** - * Add a bean value and DB value pair. - *

- * The dbValue will be converted to an Integer if isIntegerType is true; - *

- */ - protected void addInternal(Object beanValue, T dbValue) { - - 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 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; - } -} +package com.avaje.ebeaninternal.server.type; + +import java.sql.SQLException; +import java.util.Iterator; +import java.util.LinkedHashMap; + +/** + * Used to map Bean values to DB values. + *

+ * Useful for building Enum converters where you want to map the DB values an + * Enum gets converter to. + *

+ */ +public abstract class EnumToDbValueMap { + + public static EnumToDbValueMap create(boolean integerType) { + return integerType ? new EnumToDbIntegerMap() : new EnumToDbStringMap(); + } + + final LinkedHashMap keyMap; + + final LinkedHashMap valueMap; + + final boolean allowNulls; + + final boolean isIntegerType; + + /** + * Construct with allowNulls defaulting to false. + */ + public EnumToDbValueMap() { + this(false, false); + } + + /** + * Construct with allowNulls setting. + *

+ * 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. + *

+ */ + public EnumToDbValueMap(boolean allowNulls, boolean isIntegerType) { + this.allowNulls = allowNulls; + this.isIntegerType = isIntegerType; + keyMap = new LinkedHashMap(); + valueMap = new LinkedHashMap(); + } + + /** + * Return true if this is mapping to integers, false + * if mapping to Strings. + */ + public boolean isIntegerType() { + return isIntegerType; + } + + /** + * Return the DB values. + */ + public Iterator dbValues() { + return valueMap.keySet().iterator(); + } + + /** + * Return the bean 'key' value. + */ + public Iterator beanValues() { + return valueMap.values().iterator(); + } + + /** + * 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; + + /** + * 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 add(Object beanValue, String dbValue); + + /** + * Add a bean value and DB value pair. + *

+ * The dbValue will be converted to an Integer if isIntegerType is true; + *

+ */ + protected void addInternal(Object beanValue, T dbValue) { + + 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 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; + } +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ImmutableCompoundTypeBuilder.java b/src/main/java/com/avaje/ebeaninternal/server/type/ImmutableCompoundTypeBuilder.java index bfb7feed9..37b337fbd 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ImmutableCompoundTypeBuilder.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ImmutableCompoundTypeBuilder.java @@ -1,98 +1,98 @@ -package com.avaje.ebeaninternal.server.type; - -import java.util.HashMap; -import java.util.Map; - -/** - * Used to build Immutable Compound Value objects. - *

- * The individual values are collected for a given type and when they have all - * been collected then the immutable compound value object is created and - * returned. - *

- * - * @author rbygrave - * - */ -public final class ImmutableCompoundTypeBuilder { - - private static ThreadLocal local = new ThreadLocal() { - protected synchronized ImmutableCompoundTypeBuilder initialValue() { - return new ImmutableCompoundTypeBuilder(); - } - }; - - private Map, Entry> entryMap = new HashMap, Entry>(); - - /** - * Clear the cache of partial compound objects. - */ - public static void clear() { - local.get().entryMap.clear(); - } - - /** - * Set the value for the property of a compound type. - *

- * If this is the last value required for the compound type then the - * compound type is created and returned, otherwise null is returned (and we - * need more values set). - *

- */ - public static Object set(CtCompoundType ct, String propName, Object value) { - return local.get().setValue(ct, propName, value); - } - - private Object setValue(CtCompoundType ct, String propName, Object value) { - - Entry e = getEntry(ct); - Object compoundValue = e.set(propName, value); - if (compoundValue != null) { - removeEntry(ct); - } - return compoundValue; - } - - /** - * Once we have built a compound value we remove the entry. - */ - private void removeEntry(CtCompoundType ct) { - entryMap.remove(ct.getCompoundTypeClass()); - } - - /** - * Get the Entry which contains the values collected so far for this type. - */ - private Entry getEntry(CtCompoundType ct) { - Entry e = entryMap.get(ct.getCompoundTypeClass()); - if (e == null) { - e = new Entry(ct); - entryMap.put(ct.getCompoundTypeClass(), e); - } - return e; - } - - /** - * Holds the values collected so far for a given compound type. - */ - private static class Entry { - - private final CtCompoundType ct; - - private final Map valueMap; - - private Entry(CtCompoundType ct) { - this.ct = ct; - this.valueMap = new HashMap(); - } - - private Object set(String propName, Object value) { - // collect the values... - valueMap.put(propName, value); - - // when got all the values this returns the - // compound value, otherwise null - return ct.create(valueMap); - } - } -} +package com.avaje.ebeaninternal.server.type; + +import java.util.HashMap; +import java.util.Map; + +/** + * Used to build Immutable Compound Value objects. + *

+ * The individual values are collected for a given type and when they have all + * been collected then the immutable compound value object is created and + * returned. + *

+ * + * @author rbygrave + * + */ +public final class ImmutableCompoundTypeBuilder { + + private static ThreadLocal local = new ThreadLocal() { + protected synchronized ImmutableCompoundTypeBuilder initialValue() { + return new ImmutableCompoundTypeBuilder(); + } + }; + + private Map, Entry> entryMap = new HashMap, Entry>(); + + /** + * Clear the cache of partial compound objects. + */ + public static void clear() { + local.get().entryMap.clear(); + } + + /** + * Set the value for the property of a compound type. + *

+ * If this is the last value required for the compound type then the + * compound type is created and returned, otherwise null is returned (and we + * need more values set). + *

+ */ + public static Object set(CtCompoundType ct, String propName, Object value) { + return local.get().setValue(ct, propName, value); + } + + private Object setValue(CtCompoundType ct, String propName, Object value) { + + Entry e = getEntry(ct); + Object compoundValue = e.set(propName, value); + if (compoundValue != null) { + removeEntry(ct); + } + return compoundValue; + } + + /** + * Once we have built a compound value we remove the entry. + */ + private void removeEntry(CtCompoundType ct) { + entryMap.remove(ct.getCompoundTypeClass()); + } + + /** + * Get the Entry which contains the values collected so far for this type. + */ + private Entry getEntry(CtCompoundType ct) { + Entry e = entryMap.get(ct.getCompoundTypeClass()); + if (e == null) { + e = new Entry(ct); + entryMap.put(ct.getCompoundTypeClass(), e); + } + return e; + } + + /** + * Holds the values collected so far for a given compound type. + */ + private static class Entry { + + private final CtCompoundType ct; + + private final Map valueMap; + + private Entry(CtCompoundType ct) { + this.ct = ct; + this.valueMap = new HashMap(); + } + + private Object set(String propName, Object value) { + // collect the values... + valueMap.put(propName, value); + + // when got all the values this returns the + // compound value, otherwise null + return ct.create(valueMap); + } + } +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/LongToTimestampConverter.java b/src/main/java/com/avaje/ebeaninternal/server/type/LongToTimestampConverter.java index 745d6f10d..10309d393 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/LongToTimestampConverter.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/LongToTimestampConverter.java @@ -1,24 +1,24 @@ -package com.avaje.ebeaninternal.server.type; - -import java.sql.Timestamp; - -import com.avaje.ebean.config.ScalarTypeConverter; - -public class LongToTimestampConverter implements ScalarTypeConverter{ - - public Long getNullValue() { - return null; - } - - public Timestamp unwrapValue(Long beanType) { - - return new Timestamp(beanType.longValue()); - } - - public Long wrapValue(Timestamp scalarType) { - - return scalarType.getTime(); - } - - -} +package com.avaje.ebeaninternal.server.type; + +import java.sql.Timestamp; + +import com.avaje.ebean.config.ScalarTypeConverter; + +public class LongToTimestampConverter implements ScalarTypeConverter{ + + public Long getNullValue() { + return null; + } + + public Timestamp unwrapValue(Long beanType) { + + return new Timestamp(beanType.longValue()); + } + + public Long wrapValue(Timestamp scalarType) { + + return scalarType.getTime(); + } + + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ModifyAwareCollection.java b/src/main/java/com/avaje/ebeaninternal/server/type/ModifyAwareCollection.java index 006ddb39e..174a47d24 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ModifyAwareCollection.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ModifyAwareCollection.java @@ -1,121 +1,121 @@ -package com.avaje.ebeaninternal.server.type; - -import java.util.Collection; -import java.util.Iterator; - -/** - * Wraps a collection for the purposes of detecting modifications. - */ -public class ModifyAwareCollection implements Collection { - - protected final ModifyAwareOwner owner; - - protected final Collection c; - - /** - * Create with an Owner and the underlying collection this wraps. - *

- * The owner is notified of the additions and removals. - *

- */ - public ModifyAwareCollection(ModifyAwareOwner owner, Collection c) { - this.owner = owner; - this.c = c; - } - - public String toString() { - return c.toString(); - } - - public boolean add(E o) { - if (c.add(o)) { - owner.markAsModified(); - return true; - } - return false; - } - - public boolean addAll(Collection collection) { - boolean changed = false; - Iterator it = collection.iterator(); - while (it.hasNext()) { - E o = it.next(); - if (c.add(o)) { - owner.markAsModified(); - changed = true; - } - } - return changed; - } - - public void clear() { - if (!c.isEmpty()) { - owner.markAsModified(); - } - c.clear(); - } - - public boolean contains(Object o) { - return c.contains(o); - } - - public boolean containsAll(Collection collection) { - return c.containsAll(collection); - } - - public boolean isEmpty() { - return c.isEmpty(); - } - - public Iterator iterator() { - return new ModifyAwareIterator(owner, c.iterator()); - } - - public boolean remove(Object o) { - if (c.remove(o)) { - owner.markAsModified(); - return true; - } - return false; - } - - public boolean removeAll(Collection collection) { - boolean changed = false; - Iterator it = collection.iterator(); - while (it.hasNext()) { - Object o = (Object) it.next(); - if (c.remove(o)) { - owner.markAsModified(); - changed = true; - } - } - return changed; - } - - public boolean retainAll(Collection collection) { - boolean changed = false; - Iterator it = c.iterator(); - while (it.hasNext()) { - Object o = (Object) it.next(); - if (!collection.contains(o)) { - it.remove(); - owner.markAsModified(); - changed = true; - } - } - return changed; - } - - public int size() { - return c.size(); - } - - public Object[] toArray() { - return c.toArray(); - } - - public T[] toArray(T[] a) { - return c.toArray(a); - } - -} +package com.avaje.ebeaninternal.server.type; + +import java.util.Collection; +import java.util.Iterator; + +/** + * Wraps a collection for the purposes of detecting modifications. + */ +public class ModifyAwareCollection implements Collection { + + protected final ModifyAwareOwner owner; + + protected final Collection c; + + /** + * Create with an Owner and the underlying collection this wraps. + *

+ * The owner is notified of the additions and removals. + *

+ */ + public ModifyAwareCollection(ModifyAwareOwner owner, Collection c) { + this.owner = owner; + this.c = c; + } + + public String toString() { + return c.toString(); + } + + public boolean add(E o) { + if (c.add(o)) { + owner.markAsModified(); + return true; + } + return false; + } + + public boolean addAll(Collection collection) { + boolean changed = false; + Iterator it = collection.iterator(); + while (it.hasNext()) { + E o = it.next(); + if (c.add(o)) { + owner.markAsModified(); + changed = true; + } + } + return changed; + } + + public void clear() { + if (!c.isEmpty()) { + owner.markAsModified(); + } + c.clear(); + } + + public boolean contains(Object o) { + return c.contains(o); + } + + public boolean containsAll(Collection collection) { + return c.containsAll(collection); + } + + public boolean isEmpty() { + return c.isEmpty(); + } + + public Iterator iterator() { + return new ModifyAwareIterator(owner, c.iterator()); + } + + public boolean remove(Object o) { + if (c.remove(o)) { + owner.markAsModified(); + return true; + } + return false; + } + + public boolean removeAll(Collection collection) { + boolean changed = false; + Iterator it = collection.iterator(); + while (it.hasNext()) { + Object o = (Object) it.next(); + if (c.remove(o)) { + owner.markAsModified(); + changed = true; + } + } + return changed; + } + + public boolean retainAll(Collection collection) { + boolean changed = false; + Iterator it = c.iterator(); + while (it.hasNext()) { + Object o = (Object) it.next(); + if (!collection.contains(o)) { + it.remove(); + owner.markAsModified(); + changed = true; + } + } + return changed; + } + + public int size() { + return c.size(); + } + + public Object[] toArray() { + return c.toArray(); + } + + public T[] toArray(T[] a) { + return c.toArray(a); + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ModifyAwareIterator.java b/src/main/java/com/avaje/ebeaninternal/server/type/ModifyAwareIterator.java index 90c7261e4..1b3151cb4 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ModifyAwareIterator.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ModifyAwareIterator.java @@ -1,38 +1,38 @@ -package com.avaje.ebeaninternal.server.type; - -import java.util.Iterator; - -/** - * Wraps an iterator for the purposes of detecting modifications. - */ -public class ModifyAwareIterator implements Iterator { - - private final ModifyAwareOwner owner; - - private final Iterator it; - - /** - * Create with an Owner and the underlying Iterator this wraps. - *

- * The owner is notified of the removals. - *

- */ - public ModifyAwareIterator(ModifyAwareOwner owner, Iterator it) { - this.owner = owner; - this.it = it; - } - - public boolean hasNext() { - return it.hasNext(); - } - - public E next() { - return it.next(); - } - - public void remove() { - owner.markAsModified(); - it.remove(); - } - -} +package com.avaje.ebeaninternal.server.type; + +import java.util.Iterator; + +/** + * Wraps an iterator for the purposes of detecting modifications. + */ +public class ModifyAwareIterator implements Iterator { + + private final ModifyAwareOwner owner; + + private final Iterator it; + + /** + * Create with an Owner and the underlying Iterator this wraps. + *

+ * The owner is notified of the removals. + *

+ */ + public ModifyAwareIterator(ModifyAwareOwner owner, Iterator it) { + this.owner = owner; + this.it = it; + } + + public boolean hasNext() { + return it.hasNext(); + } + + public E next() { + return it.next(); + } + + public void remove() { + owner.markAsModified(); + it.remove(); + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ModifyAwareSet.java b/src/main/java/com/avaje/ebeaninternal/server/type/ModifyAwareSet.java index 138b8d822..eb512cee1 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ModifyAwareSet.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ModifyAwareSet.java @@ -1,17 +1,17 @@ -package com.avaje.ebeaninternal.server.type; - -import java.util.Set; - -/** - * Wraps a Set for the purposes of detecting modifications. - */ -public class ModifyAwareSet extends ModifyAwareCollection implements Set { - - /** - * Create with an Owner that is notified of modifications. - */ - public ModifyAwareSet(ModifyAwareOwner owner, Set s) { - super(owner, s); - } - -} +package com.avaje.ebeaninternal.server.type; + +import java.util.Set; + +/** + * Wraps a Set for the purposes of detecting modifications. + */ +public class ModifyAwareSet extends ModifyAwareCollection implements Set { + + /** + * Create with an Owner that is notified of modifications. + */ + public ModifyAwareSet(ModifyAwareOwner owner, Set s) { + super(owner, s); + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/RsetDataReader.java b/src/main/java/com/avaje/ebeaninternal/server/type/RsetDataReader.java index 7da2384cd..9ab0f4587 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/RsetDataReader.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/RsetDataReader.java @@ -1,243 +1,243 @@ -package com.avaje.ebeaninternal.server.type; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.Reader; -import java.math.BigDecimal; -import java.sql.Array; -import java.sql.Blob; -import java.sql.Clob; -import java.sql.Date; -import java.sql.Ref; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Time; -import java.sql.Timestamp; - -import com.avaje.ebeaninternal.server.core.Message; - -public class RsetDataReader implements DataReader { - - private static final int bufferSize = 512; - - static final int clobBufferSize = 512; - - 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 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()); - } - } - -} +package com.avaje.ebeaninternal.server.type; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.Reader; +import java.math.BigDecimal; +import java.sql.Array; +import java.sql.Blob; +import java.sql.Clob; +import java.sql.Date; +import java.sql.Ref; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Time; +import java.sql.Timestamp; + +import com.avaje.ebeaninternal.server.core.Message; + +public class RsetDataReader implements DataReader { + + private static final int bufferSize = 512; + + static final int clobBufferSize = 512; + + 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 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()); + } + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/RsetDataReaderIndexed.java b/src/main/java/com/avaje/ebeaninternal/server/type/RsetDataReaderIndexed.java index 18f500e70..8d72e0db5 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/RsetDataReaderIndexed.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/RsetDataReaderIndexed.java @@ -1,28 +1,28 @@ -package com.avaje.ebeaninternal.server.type; - -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; - } - } - } - - @Override - protected int pos() { - int i = pos++; - return rsetIndexPositions[i]; - } - -} +package com.avaje.ebeaninternal.server.type; + +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; + } + } + } + + @Override + protected int pos() { + int i = pos++; + return rsetIndexPositions[i]; + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarDataReader.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarDataReader.java index ab0c1a12d..1140bfb10 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarDataReader.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarDataReader.java @@ -1,30 +1,30 @@ -package com.avaje.ebeaninternal.server.type; - -import java.sql.SQLException; - -/** - * Reads from and binds to database columns. - */ -public interface ScalarDataReader { - - /** - * Read and return the appropriate value from the dataReader. - */ - public T read(DataReader dataReader) throws SQLException; - - /** - * Ignore typically by moving the index position. - */ - public void loadIgnore(DataReader dataReader); - - /** - * Bind the value to the underlying preparedStatement. - */ - public 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); - -} +package com.avaje.ebeaninternal.server.type; + +import java.sql.SQLException; + +/** + * Reads from and binds to database columns. + */ +public interface ScalarDataReader { + + /** + * Read and return the appropriate value from the dataReader. + */ + public T read(DataReader dataReader) throws SQLException; + + /** + * Ignore typically by moving the index position. + */ + public void loadIgnore(DataReader dataReader); + + /** + * Bind the value to the underlying preparedStatement. + */ + public 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); + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarType.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarType.java index b59bb38f0..3ad716c72 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarType.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarType.java @@ -1,207 +1,207 @@ -package com.avaje.ebeaninternal.server.type; - -import java.io.DataInput; -import java.io.DataOutput; -import java.io.IOException; -import java.sql.SQLException; - -import com.avaje.ebean.text.StringFormatter; -import com.avaje.ebean.text.StringParser; -import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.core.JsonToken; - -/** - * Describes a scalar type. - *

- * Scalar in the sense that the types are not compound types. Scalar types only - * map to a single database column. - *

- *

- * These types fall into two categories. Types that are mapped natively to JDBC - * types and the rest. Types that map to native JDBC types do not require any - * data type conversion to be persisted to the database. These are java types - * that map via java.sql.Types. - *

- *

- * Types that are not native to JDBC require some conversion. These include some - * common java types such as java.util.Date, java.util.Calendar, - * java.math.BigInteger. - *

- *

- * Note that Booleans may be native for some databases and require conversion on - * other databases. - *

- */ -public interface ScalarType extends StringParser, StringFormatter, ScalarDataReader { - - /** - * Return true if this is a mutable scalar type (like hstore). - */ - public 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. - *

- * If a BeanProperty has no explicit length defined then this length should - * be assigned. - *

- *

- * This is primarily to support defining a length on Enum types (to - * supplement defining the length on the BeanProperty directly). - *

- */ - public int getLength(); - - /** - * Return true if the type is native to JDBC. - *

- * If it is native to JDBC then its values/instances do not need to be - * converted to and from an associated JDBC type. - *

- */ - public boolean isJdbcNative(); - - /** - * Return the type as per java.sql.Types that this maps to. - *

- * This type should be consistent with the toJdbcType() method in converting - * the type to the appropriate type for binding to preparedStatements. - *

- */ - public int getJdbcType(); - - /** - * Return the type that matches the bean property type. - *

- * This represents the 'logical' type rather than the JDBC type this maps - * to. - *

- */ - public Class getType(); - - /** - * Read the value from the resultSet and convert if necessary to the logical - * bean property value. - */ - public T read(DataReader dataReader) throws SQLException; - - /** - * Ignore the reading of this value. Typically this means moving the index - * position in the ResultSet. - */ - public void loadIgnore(DataReader dataReader); - - /** - * Convert (if necessary) and bind the value to the preparedStatement. - *

- * value may need to be converted from the logical bean property type to the - * JDBC type. - *

- */ - public void bind(DataBind b, T value) throws SQLException; - - /** - * Convert the value as necessary to the JDBC type. - *

- * Note that this should also match the type as per the getJdbcType() - * method. - *

- *

- * 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. - *

- */ - public Object toJdbcType(Object value); - - /** - * Convert the value as necessary to the logical Bean type. - *

- * The type as per the bean property. - *

- *

- * This is used to automatically convert id values (typically from a string - * to a int, long or UUID). - *

- */ - public T toBeanType(Object value); - - /** - * Convert the type into a string representation. - *

- * Reciprocal of parse(). - *

- */ - public String formatValue(T v); - - /** - * Convert the type into a string representation. - *

- * This assumes the value is of the correct type. - *

- *

- * This is so that ScalarType also implements the StringFormatter interface. - *

- */ - public String format(Object v); - - /** - * Convert the string value to the appropriate java object. - *

- * Mostly used to support CSV, JSON and XML parsing. - *

- *

- * Reciprocal of formatValue(). - *

- */ - public T parse(String value); - - /** - * Return true if the type can accept long systemTimeMillis input. - *

- * This is used to determine if is is sensible to use the - * {@link #convertFromMillis(long)} method. - *

- *

- * 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). - *

- */ - public boolean isDateTimeCapable(); - - /** - * Convert the systemTimeMillis into the appropriate java object. - *

- * For non dateTime types this will throw an exception. - *

- */ - public T convertFromMillis(long dateTime); - - /** - * Read the value from binary input. - */ - public T readData(DataInput dataInput) throws IOException; - - /** - * Write the value to binary output. - */ - public void writeData(DataOutput dataOutput, T v) throws IOException; - - /** - * Read the value from JsonParser. - */ - public 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; - -} +package com.avaje.ebeaninternal.server.type; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.sql.SQLException; + +import com.avaje.ebean.text.StringFormatter; +import com.avaje.ebean.text.StringParser; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonToken; + +/** + * Describes a scalar type. + *

+ * Scalar in the sense that the types are not compound types. Scalar types only + * map to a single database column. + *

+ *

+ * These types fall into two categories. Types that are mapped natively to JDBC + * types and the rest. Types that map to native JDBC types do not require any + * data type conversion to be persisted to the database. These are java types + * that map via java.sql.Types. + *

+ *

+ * Types that are not native to JDBC require some conversion. These include some + * common java types such as java.util.Date, java.util.Calendar, + * java.math.BigInteger. + *

+ *

+ * Note that Booleans may be native for some databases and require conversion on + * other databases. + *

+ */ +public interface ScalarType extends StringParser, StringFormatter, ScalarDataReader { + + /** + * Return true if this is a mutable scalar type (like hstore). + */ + public 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. + *

+ * If a BeanProperty has no explicit length defined then this length should + * be assigned. + *

+ *

+ * This is primarily to support defining a length on Enum types (to + * supplement defining the length on the BeanProperty directly). + *

+ */ + public int getLength(); + + /** + * Return true if the type is native to JDBC. + *

+ * If it is native to JDBC then its values/instances do not need to be + * converted to and from an associated JDBC type. + *

+ */ + public boolean isJdbcNative(); + + /** + * Return the type as per java.sql.Types that this maps to. + *

+ * This type should be consistent with the toJdbcType() method in converting + * the type to the appropriate type for binding to preparedStatements. + *

+ */ + public int getJdbcType(); + + /** + * Return the type that matches the bean property type. + *

+ * This represents the 'logical' type rather than the JDBC type this maps + * to. + *

+ */ + public Class getType(); + + /** + * Read the value from the resultSet and convert if necessary to the logical + * bean property value. + */ + public T read(DataReader dataReader) throws SQLException; + + /** + * Ignore the reading of this value. Typically this means moving the index + * position in the ResultSet. + */ + public void loadIgnore(DataReader dataReader); + + /** + * Convert (if necessary) and bind the value to the preparedStatement. + *

+ * value may need to be converted from the logical bean property type to the + * JDBC type. + *

+ */ + public void bind(DataBind b, T value) throws SQLException; + + /** + * Convert the value as necessary to the JDBC type. + *

+ * Note that this should also match the type as per the getJdbcType() + * method. + *

+ *

+ * 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. + *

+ */ + public Object toJdbcType(Object value); + + /** + * Convert the value as necessary to the logical Bean type. + *

+ * The type as per the bean property. + *

+ *

+ * This is used to automatically convert id values (typically from a string + * to a int, long or UUID). + *

+ */ + public T toBeanType(Object value); + + /** + * Convert the type into a string representation. + *

+ * Reciprocal of parse(). + *

+ */ + public String formatValue(T v); + + /** + * Convert the type into a string representation. + *

+ * This assumes the value is of the correct type. + *

+ *

+ * This is so that ScalarType also implements the StringFormatter interface. + *

+ */ + public String format(Object v); + + /** + * Convert the string value to the appropriate java object. + *

+ * Mostly used to support CSV, JSON and XML parsing. + *

+ *

+ * Reciprocal of formatValue(). + *

+ */ + public T parse(String value); + + /** + * Return true if the type can accept long systemTimeMillis input. + *

+ * This is used to determine if is is sensible to use the + * {@link #convertFromMillis(long)} method. + *

+ *

+ * 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). + *

+ */ + public boolean isDateTimeCapable(); + + /** + * Convert the systemTimeMillis into the appropriate java object. + *

+ * For non dateTime types this will throw an exception. + *

+ */ + public T convertFromMillis(long dateTime); + + /** + * Read the value from binary input. + */ + public T readData(DataInput dataInput) throws IOException; + + /** + * Write the value to binary output. + */ + public void writeData(DataOutput dataOutput, T v) throws IOException; + + /** + * Read the value from JsonParser. + */ + public 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; + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBase.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBase.java index 97dcff747..856e30334 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBase.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBase.java @@ -1,68 +1,68 @@ -package com.avaje.ebeaninternal.server.type; - -/** - * Base ScalarType object. - */ -public abstract class ScalarTypeBase implements ScalarType { - - protected final Class type; - - protected final boolean jdbcNative; - - protected final int jdbcType; - - public ScalarTypeBase(Class type, boolean jdbcNative, int jdbcType) { - this.type = type; - this.jdbcNative = jdbcNative; - this.jdbcType = jdbcType; - } - - /** - * Default implementation of mutable false. - */ - @Override - public boolean isMutable() { - return false; - } - - /** - * Default to true. - */ - @Override - public boolean isDirty(Object value) { - return true; - } - - /** - * Just return 0. - */ - public int getLength() { - return 0; - } - - public boolean isJdbcNative() { - return jdbcNative; - } - - public int getJdbcType() { - return jdbcType; - } - - public Class getType() { - return type; - } - - @SuppressWarnings("unchecked") - public String format(Object v) { - return formatValue((T) v); - } - - public void loadIgnore(DataReader dataReader) { - dataReader.incrementPos(1); - } - - public void accumulateScalarTypes(String propName, CtCompoundTypeScalarList list) { - list.addScalarType(propName, this); - } - -} +package com.avaje.ebeaninternal.server.type; + +/** + * Base ScalarType object. + */ +public abstract class ScalarTypeBase implements ScalarType { + + protected final Class type; + + protected final boolean jdbcNative; + + protected final int jdbcType; + + public ScalarTypeBase(Class type, boolean jdbcNative, int jdbcType) { + this.type = type; + this.jdbcNative = jdbcNative; + this.jdbcType = jdbcType; + } + + /** + * Default implementation of mutable false. + */ + @Override + public boolean isMutable() { + return false; + } + + /** + * Default to true. + */ + @Override + public boolean isDirty(Object value) { + return true; + } + + /** + * Just return 0. + */ + public int getLength() { + return 0; + } + + public boolean isJdbcNative() { + return jdbcNative; + } + + public int getJdbcType() { + return jdbcType; + } + + public Class getType() { + return type; + } + + @SuppressWarnings("unchecked") + public String format(Object v) { + return formatValue((T) v); + } + + public void loadIgnore(DataReader dataReader) { + dataReader.incrementPos(1); + } + + public void accumulateScalarTypes(String propName, CtCompoundTypeScalarList list) { + list.addScalarType(propName, this); + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBaseDate.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBaseDate.java index 7e485192d..105441148 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBaseDate.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBaseDate.java @@ -1,107 +1,107 @@ -package com.avaje.ebeaninternal.server.type; - -import java.io.DataInput; -import java.io.DataOutput; -import java.io.IOException; -import java.sql.Date; -import java.sql.SQLException; -import java.sql.Types; - -import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.core.JsonToken; - -/** - * Base class for Date types. - */ -public abstract class ScalarTypeBaseDate extends ScalarTypeBase { - - public ScalarTypeBaseDate(Class type, boolean jdbcNative, int jdbcType) { - super(type, jdbcNative, jdbcType); - } - - /** - * Convert the target value to millis. - */ - public abstract long convertToMillis(T value); - - /** - * Convert to java.sql.Date from the target Date type. - */ - public abstract java.sql.Date convertToDate(T t); - - /** - * Convert from java.sql.Date to the target Date type. - */ - public abstract T convertFromDate(java.sql.Date ts); - - public void bind(DataBind b, T value) throws SQLException { - if (value == null) { - b.setNull(Types.DATE); - } else { - b.setDate(convertToDate(value)); - } - } - - public T read(DataReader dataReader) throws SQLException { - - Date ts = dataReader.getDate(); - return ts == null ? null : convertFromDate(ts); - } - - public String formatValue(T t) { - Date date = convertToDate(t); - return date.toString(); - } - - public T parse(String value) { - Date date = Date.valueOf(value); - return convertFromDate(date); - } - - public T convertFromMillis(long systemTimeMillis) { - Date ts = new Date(systemTimeMillis); - return convertFromDate(ts); - } - - public boolean isDateTimeCapable() { - return true; - } - - @Override - public T jsonRead(JsonParser ctx, JsonToken event) throws IOException { - if (JsonToken.VALUE_NUMBER_INT == event) { - return convertFromMillis(ctx.getLongValue()); - } else { - return convertFromDate(Date.valueOf(ctx.getText())); - } - } - - public void jsonWrite(JsonGenerator ctx, String name, T value) throws IOException { - long millis = convertToMillis(value); - ctx.writeNumberField(name, millis); - } - - public T readData(DataInput dataInput) throws IOException { - if (!dataInput.readBoolean()) { - return null; - } else { - long val = dataInput.readLong(); - Date date = new Date(val); - return convertFromDate(date); - } - } - - @SuppressWarnings("unchecked") - public void writeData(DataOutput dataOutput, T value) throws IOException { - - if (value == null) { - dataOutput.writeBoolean(false); - } else { - dataOutput.writeBoolean(true); - Date date = convertToDate(value); - dataOutput.writeLong(date.getTime()); - } - } - -} +package com.avaje.ebeaninternal.server.type; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.sql.Date; +import java.sql.SQLException; +import java.sql.Types; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonToken; + +/** + * Base class for Date types. + */ +public abstract class ScalarTypeBaseDate extends ScalarTypeBase { + + public ScalarTypeBaseDate(Class type, boolean jdbcNative, int jdbcType) { + super(type, jdbcNative, jdbcType); + } + + /** + * Convert the target value to millis. + */ + public abstract long convertToMillis(T value); + + /** + * Convert to java.sql.Date from the target Date type. + */ + public abstract java.sql.Date convertToDate(T t); + + /** + * Convert from java.sql.Date to the target Date type. + */ + public abstract T convertFromDate(java.sql.Date ts); + + public void bind(DataBind b, T value) throws SQLException { + if (value == null) { + b.setNull(Types.DATE); + } else { + b.setDate(convertToDate(value)); + } + } + + public T read(DataReader dataReader) throws SQLException { + + Date ts = dataReader.getDate(); + return ts == null ? null : convertFromDate(ts); + } + + public String formatValue(T t) { + Date date = convertToDate(t); + return date.toString(); + } + + public T parse(String value) { + Date date = Date.valueOf(value); + return convertFromDate(date); + } + + public T convertFromMillis(long systemTimeMillis) { + Date ts = new Date(systemTimeMillis); + return convertFromDate(ts); + } + + public boolean isDateTimeCapable() { + return true; + } + + @Override + public T jsonRead(JsonParser ctx, JsonToken event) throws IOException { + if (JsonToken.VALUE_NUMBER_INT == event) { + return convertFromMillis(ctx.getLongValue()); + } else { + return convertFromDate(Date.valueOf(ctx.getText())); + } + } + + public void jsonWrite(JsonGenerator ctx, String name, T value) throws IOException { + long millis = convertToMillis(value); + ctx.writeNumberField(name, millis); + } + + public T readData(DataInput dataInput) throws IOException { + if (!dataInput.readBoolean()) { + return null; + } else { + long val = dataInput.readLong(); + Date date = new Date(val); + return convertFromDate(date); + } + } + + @SuppressWarnings("unchecked") + public void writeData(DataOutput dataOutput, T value) throws IOException { + + if (value == null) { + dataOutput.writeBoolean(false); + } else { + dataOutput.writeBoolean(true); + Date date = convertToDate(value); + dataOutput.writeLong(date.getTime()); + } + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBaseDateTime.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBaseDateTime.java index a4f02d489..708f2d36b 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBaseDateTime.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBaseDateTime.java @@ -1,162 +1,162 @@ -package com.avaje.ebeaninternal.server.type; - -import com.avaje.ebean.config.JsonConfig; -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.Timestamp; -import java.sql.Types; -import java.time.Instant; - -/** - * Base type for DateTime types. - */ -public abstract class ScalarTypeBaseDateTime extends ScalarTypeBase { - - - protected final DateTimeJsonParser dateTimeParser = new DateTimeJsonParser(); - - protected final JsonConfig.DateTime mode; - - public ScalarTypeBaseDateTime(JsonConfig.DateTime mode, Class type, boolean jdbcNative, int jdbcType) { - super(type, jdbcNative, jdbcType); - this.mode = mode; - } - - /** - * Convert the value to a Timestamp. - */ - public abstract Timestamp convertToTimestamp(T t); - - /** - * Convert to the value from a Timestamp. - */ - public abstract T convertFromTimestamp(Timestamp ts); - - /** - * Convert from epoch millis to the value. - */ - public abstract T convertFromMillis(long systemTimeMillis); - - /** - * Convert from the value to epoch millis. - */ - public abstract long convertToMillis(T value); - - /** - * Convert the value to time with nanos format. - */ - protected abstract String toJsonNanos(T value); - - /** - * Convert the value to ISO8601 format. - */ - protected abstract String toJsonISO8601(T value); - - public void bind(DataBind b, T value) throws SQLException { - if (value == null) { - b.setNull(Types.TIMESTAMP); - } else { - b.setTimestamp(convertToTimestamp(value)); - } - } - - public T read(DataReader dataReader) throws SQLException { - - Timestamp ts = dataReader.getTimestamp(); - if (ts == null) { - return null; - } else { - return convertFromTimestamp(ts); - } - } - - /** - * Helper method that given epoch seconds and nanos return a JSON nanos formatted string. - */ - protected String toJsonNanos(long epochSecs, int nanos) { - return DecimalUtils.toDecimal(epochSecs, nanos); - } - - @Override - public T jsonRead(JsonParser ctx, JsonToken event) throws IOException { - - switch (event) { - case VALUE_NUMBER_INT: { - return convertFromMillis(ctx.getLongValue()); - } - case VALUE_NUMBER_FLOAT: { - BigDecimal value = ctx.getDecimalValue(); - Timestamp timestamp = DecimalUtils.toTimestamp(value); - return convertFromTimestamp(timestamp); - } - default: { - String jsonDateTime = ctx.getText(); - return convertFromTimestamp(dateTimeParser.parse(jsonDateTime)); - } - } - } - - @Override - public void jsonWrite(JsonGenerator generator, String name, T value) throws IOException { - - switch (mode) { - case ISO8601: { - generator.writeFieldName(name); - generator.writeString(toJsonISO8601(value)); - break; - } - case NANOS: { - generator.writeFieldName(name); - generator.writeNumber(toJsonNanos(value)); - break; - } - default: { - generator.writeNumberField(name, convertToMillis(value)); - } - } - } - - public String formatValue(T t) { - Timestamp ts = convertToTimestamp(t); - return ts.toString(); - } - - public T parse(String value) { - Timestamp ts = Timestamp.valueOf(value); - return convertFromTimestamp(ts); - } - - - public boolean isDateTimeCapable() { - return true; - } - - public T readData(DataInput dataInput) throws IOException { - if (!dataInput.readBoolean()) { - return null; - } else { - long val = dataInput.readLong(); - Timestamp ts = new Timestamp(val); - return convertFromTimestamp(ts); - } - } - - public void writeData(DataOutput dataOutput, T value) throws IOException { - - if (value == null) { - dataOutput.writeBoolean(false); - } else { - dataOutput.writeBoolean(true); - Timestamp ts = convertToTimestamp(value); - dataOutput.writeLong(ts.getTime()); - } - } - -} +package com.avaje.ebeaninternal.server.type; + +import com.avaje.ebean.config.JsonConfig; +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.Timestamp; +import java.sql.Types; +import java.time.Instant; + +/** + * Base type for DateTime types. + */ +public abstract class ScalarTypeBaseDateTime extends ScalarTypeBase { + + + protected final DateTimeJsonParser dateTimeParser = new DateTimeJsonParser(); + + protected final JsonConfig.DateTime mode; + + public ScalarTypeBaseDateTime(JsonConfig.DateTime mode, Class type, boolean jdbcNative, int jdbcType) { + super(type, jdbcNative, jdbcType); + this.mode = mode; + } + + /** + * Convert the value to a Timestamp. + */ + public abstract Timestamp convertToTimestamp(T t); + + /** + * Convert to the value from a Timestamp. + */ + public abstract T convertFromTimestamp(Timestamp ts); + + /** + * Convert from epoch millis to the value. + */ + public abstract T convertFromMillis(long systemTimeMillis); + + /** + * Convert from the value to epoch millis. + */ + public abstract long convertToMillis(T value); + + /** + * Convert the value to time with nanos format. + */ + protected abstract String toJsonNanos(T value); + + /** + * Convert the value to ISO8601 format. + */ + protected abstract String toJsonISO8601(T value); + + public void bind(DataBind b, T value) throws SQLException { + if (value == null) { + b.setNull(Types.TIMESTAMP); + } else { + b.setTimestamp(convertToTimestamp(value)); + } + } + + public T read(DataReader dataReader) throws SQLException { + + Timestamp ts = dataReader.getTimestamp(); + if (ts == null) { + return null; + } else { + return convertFromTimestamp(ts); + } + } + + /** + * Helper method that given epoch seconds and nanos return a JSON nanos formatted string. + */ + protected String toJsonNanos(long epochSecs, int nanos) { + return DecimalUtils.toDecimal(epochSecs, nanos); + } + + @Override + public T jsonRead(JsonParser ctx, JsonToken event) throws IOException { + + switch (event) { + case VALUE_NUMBER_INT: { + return convertFromMillis(ctx.getLongValue()); + } + case VALUE_NUMBER_FLOAT: { + BigDecimal value = ctx.getDecimalValue(); + Timestamp timestamp = DecimalUtils.toTimestamp(value); + return convertFromTimestamp(timestamp); + } + default: { + String jsonDateTime = ctx.getText(); + return convertFromTimestamp(dateTimeParser.parse(jsonDateTime)); + } + } + } + + @Override + public void jsonWrite(JsonGenerator generator, String name, T value) throws IOException { + + switch (mode) { + case ISO8601: { + generator.writeFieldName(name); + generator.writeString(toJsonISO8601(value)); + break; + } + case NANOS: { + generator.writeFieldName(name); + generator.writeNumber(toJsonNanos(value)); + break; + } + default: { + generator.writeNumberField(name, convertToMillis(value)); + } + } + } + + public String formatValue(T t) { + Timestamp ts = convertToTimestamp(t); + return ts.toString(); + } + + public T parse(String value) { + Timestamp ts = Timestamp.valueOf(value); + return convertFromTimestamp(ts); + } + + + public boolean isDateTimeCapable() { + return true; + } + + public T readData(DataInput dataInput) throws IOException { + if (!dataInput.readBoolean()) { + return null; + } else { + long val = dataInput.readLong(); + Timestamp ts = new Timestamp(val); + return convertFromTimestamp(ts); + } + } + + public void writeData(DataOutput dataOutput, T value) throws IOException { + + if (value == null) { + dataOutput.writeBoolean(false); + } else { + dataOutput.writeBoolean(true); + Timestamp ts = convertToTimestamp(value); + dataOutput.writeLong(ts.getTime()); + } + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBaseVarchar.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBaseVarchar.java index 750400b95..f3009983e 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBaseVarchar.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBaseVarchar.java @@ -1,134 +1,134 @@ -package com.avaje.ebeaninternal.server.type; - -import java.io.DataInput; -import java.io.DataOutput; -import java.io.IOException; -import java.sql.SQLException; -import java.sql.Types; - -import com.avaje.ebean.text.TextException; -import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.core.JsonToken; - -/** - * Base ScalarType for types which converts to and from a VARCHAR database - * column. - */ -public abstract class ScalarTypeBaseVarchar extends ScalarTypeBase { - - public ScalarTypeBaseVarchar(Class type) { - super(type, false, Types.VARCHAR); - } - - public ScalarTypeBaseVarchar(Class type, boolean jdbcNative, int jdbcType) { - super(type, jdbcNative, jdbcType); - } - - /** - * Format the target type to a string. - */ - public abstract String formatValue(T v); - - /** - * Parse from a formatted string value. - */ - public abstract T parse(String value); - - /** - * Convert from DB string value to the target type. - */ - public abstract T convertFromDbString(String dbValue); - - /** - * Convert to DB string from the target type. - */ - public abstract String convertToDbString(T beanValue); - - @Override - public void bind(DataBind b, T value) throws SQLException { - if (value == null) { - b.setNull(Types.VARCHAR); - - } else { - b.setString(convertToDbString(value)); - } - } - - @Override - public T read(DataReader dataReader) throws SQLException { - String s = dataReader.getString(); - if (s == null) { - return null; - } else { - return convertFromDbString(s); - } - } - - @Override - @SuppressWarnings("unchecked") - public T toBeanType(Object value) { - if (value == null) { - return null; - } - if (value instanceof String) { - return parse((String) value); - } - return (T) value; - } - - @Override - public Object toJdbcType(Object value) { - if (value instanceof String) { - return parse((String) value); - } - return value; - } - - @Override - public T convertFromMillis(long systemTimeMillis) { - throw new TextException("Not Supported"); - } - - @Override - public boolean isDateTimeCapable() { - return false; - } - - @Override - @SuppressWarnings("unchecked") - public String format(Object v) { - return formatValue((T) v); - } - - @Override - public T readData(DataInput dataInput) throws IOException { - if (!dataInput.readBoolean()) { - return null; - } else { - String val = dataInput.readUTF(); - return convertFromDbString(val); - } - } - - public void writeData(DataOutput dataOutput, T value) throws IOException { - - if (value == null) { - dataOutput.writeBoolean(false); - } else { - dataOutput.writeBoolean(true); - String s = convertToDbString(value); - 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)); - } -} +package com.avaje.ebeaninternal.server.type; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.sql.SQLException; +import java.sql.Types; + +import com.avaje.ebean.text.TextException; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonToken; + +/** + * Base ScalarType for types which converts to and from a VARCHAR database + * column. + */ +public abstract class ScalarTypeBaseVarchar extends ScalarTypeBase { + + public ScalarTypeBaseVarchar(Class type) { + super(type, false, Types.VARCHAR); + } + + public ScalarTypeBaseVarchar(Class type, boolean jdbcNative, int jdbcType) { + super(type, jdbcNative, jdbcType); + } + + /** + * Format the target type to a string. + */ + public abstract String formatValue(T v); + + /** + * Parse from a formatted string value. + */ + public abstract T parse(String value); + + /** + * Convert from DB string value to the target type. + */ + public abstract T convertFromDbString(String dbValue); + + /** + * Convert to DB string from the target type. + */ + public abstract String convertToDbString(T beanValue); + + @Override + public void bind(DataBind b, T value) throws SQLException { + if (value == null) { + b.setNull(Types.VARCHAR); + + } else { + b.setString(convertToDbString(value)); + } + } + + @Override + public T read(DataReader dataReader) throws SQLException { + String s = dataReader.getString(); + if (s == null) { + return null; + } else { + return convertFromDbString(s); + } + } + + @Override + @SuppressWarnings("unchecked") + public T toBeanType(Object value) { + if (value == null) { + return null; + } + if (value instanceof String) { + return parse((String) value); + } + return (T) value; + } + + @Override + public Object toJdbcType(Object value) { + if (value instanceof String) { + return parse((String) value); + } + return value; + } + + @Override + public T convertFromMillis(long systemTimeMillis) { + throw new TextException("Not Supported"); + } + + @Override + public boolean isDateTimeCapable() { + return false; + } + + @Override + @SuppressWarnings("unchecked") + public String format(Object v) { + return formatValue((T) v); + } + + @Override + public T readData(DataInput dataInput) throws IOException { + if (!dataInput.readBoolean()) { + return null; + } else { + String val = dataInput.readUTF(); + return convertFromDbString(val); + } + } + + public void writeData(DataOutput dataOutput, T value) throws IOException { + + if (value == null) { + dataOutput.writeBoolean(false); + } else { + dataOutput.writeBoolean(true); + String s = convertToDbString(value); + 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)); + } +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBigDecimal.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBigDecimal.java index 23d4fad29..c45e960db 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBigDecimal.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBigDecimal.java @@ -1,89 +1,89 @@ -package com.avaje.ebeaninternal.server.type; - -import java.io.DataInput; -import java.io.DataOutput; -import java.io.IOException; -import java.math.BigDecimal; -import java.sql.SQLException; -import java.sql.Types; - -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; - -/** - * ScalarType for BigDecimal. - */ -public class ScalarTypeBigDecimal extends ScalarTypeBase { - - public ScalarTypeBigDecimal() { - super(BigDecimal.class, true, Types.DECIMAL); - } - - public BigDecimal readData(DataInput dataInput) throws IOException { - if (!dataInput.readBoolean()) { - return null; - } else { - double val = dataInput.readDouble(); - return new BigDecimal(val); - } - } - - public void writeData(DataOutput dataOutput, BigDecimal b) throws IOException { - - if (b == null) { - dataOutput.writeBoolean(false); - } else { - dataOutput.writeBoolean(true); - dataOutput.writeDouble(b.doubleValue()); - } - } - - public void bind(DataBind b, BigDecimal value) throws SQLException { - if (value == null) { - b.setNull(Types.DECIMAL); - } else { - b.setBigDecimal(value); - } - } - - public BigDecimal read(DataReader dataReader) throws SQLException { - - return dataReader.getBigDecimal(); - } - - public Object toJdbcType(Object value) { - return BasicTypeConverter.toBigDecimal(value); - } - - public BigDecimal toBeanType(Object value) { - return BasicTypeConverter.toBigDecimal(value); - } - - public String formatValue(BigDecimal t) { - return t.toPlainString(); - } - - public BigDecimal parse(String value) { - return new BigDecimal(value); - } - - public BigDecimal convertFromMillis(long systemTimeMillis) { - return BigDecimal.valueOf(systemTimeMillis); - } - - public boolean isDateTimeCapable() { - return true; - } - - @Override - public BigDecimal jsonRead(JsonParser ctx, JsonToken event) throws IOException { - return ctx.getDecimalValue(); - } - - public void jsonWrite(JsonGenerator ctx, String name, BigDecimal value) throws IOException { - ctx.writeNumberField(name, (BigDecimal) value); - } - -} +package com.avaje.ebeaninternal.server.type; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.math.BigDecimal; +import java.sql.SQLException; +import java.sql.Types; + +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; + +/** + * ScalarType for BigDecimal. + */ +public class ScalarTypeBigDecimal extends ScalarTypeBase { + + public ScalarTypeBigDecimal() { + super(BigDecimal.class, true, Types.DECIMAL); + } + + public BigDecimal readData(DataInput dataInput) throws IOException { + if (!dataInput.readBoolean()) { + return null; + } else { + double val = dataInput.readDouble(); + return new BigDecimal(val); + } + } + + public void writeData(DataOutput dataOutput, BigDecimal b) throws IOException { + + if (b == null) { + dataOutput.writeBoolean(false); + } else { + dataOutput.writeBoolean(true); + dataOutput.writeDouble(b.doubleValue()); + } + } + + public void bind(DataBind b, BigDecimal value) throws SQLException { + if (value == null) { + b.setNull(Types.DECIMAL); + } else { + b.setBigDecimal(value); + } + } + + public BigDecimal read(DataReader dataReader) throws SQLException { + + return dataReader.getBigDecimal(); + } + + public Object toJdbcType(Object value) { + return BasicTypeConverter.toBigDecimal(value); + } + + public BigDecimal toBeanType(Object value) { + return BasicTypeConverter.toBigDecimal(value); + } + + public String formatValue(BigDecimal t) { + return t.toPlainString(); + } + + public BigDecimal parse(String value) { + return new BigDecimal(value); + } + + public BigDecimal convertFromMillis(long systemTimeMillis) { + return BigDecimal.valueOf(systemTimeMillis); + } + + public boolean isDateTimeCapable() { + return true; + } + + @Override + public BigDecimal jsonRead(JsonParser ctx, JsonToken event) throws IOException { + return ctx.getDecimalValue(); + } + + public void jsonWrite(JsonGenerator ctx, String name, BigDecimal value) throws IOException { + ctx.writeNumberField(name, (BigDecimal) value); + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBoolean.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBoolean.java index 67780c3bb..4d76e3805 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBoolean.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBoolean.java @@ -1,300 +1,300 @@ -package com.avaje.ebeaninternal.server.type; - -import java.io.DataInput; -import java.io.DataOutput; -import java.io.IOException; -import java.sql.SQLException; -import java.sql.Types; - -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; - -/** - * ScalarType for Boolean and boolean. - *

- * This may or may not be a native jdbc type depending on the database and jdbc driver. - *

- */ -public class ScalarTypeBoolean { - - public static class Native extends BooleanBase { - - /** - * Native Boolean database type. - */ - public Native() { - super(true, Types.BOOLEAN); - } - - public Boolean toBeanType(Object value) { - return BasicTypeConverter.toBoolean(value); - } - - public Object toJdbcType(Object value) { - return BasicTypeConverter.convert(value, jdbcType); - } - - public void bind(DataBind b, Boolean value) throws SQLException { - if (value == null) { - b.setNull(Types.BOOLEAN); - } else { - b.setBoolean(value); - } - - } - - public Boolean read(DataReader dataReader) throws SQLException { - return dataReader.getBoolean(); - } - } - - /** - * The Class BitBoolean converts a JDBC type BIT to a java boolean - * - *

- * Sometimes booleans may be mapped to the JDBC type BIT. To use the BitBoolean specify - * type.boolean.dbtype="bit" in the ebean configuration - *

- */ - public static class BitBoolean extends BooleanBase { - - /** - * Native Boolean database type. - */ - public BitBoolean() { - super(true, Types.BIT); - } - - public Boolean toBeanType(Object value) { - return BasicTypeConverter.toBoolean(value); - } - - public Object toJdbcType(Object value) { - // use JDBC driver to convert boolean to bit - return BasicTypeConverter.toBoolean(value); - } - - public void bind(DataBind b, Boolean value) throws SQLException { - if (value == null) { - b.setNull(Types.BIT); - } else { - // use JDBC driver to convert boolean to bit - b.setBoolean(value); - } - } - - public Boolean read(DataReader dataReader) throws SQLException { - return dataReader.getBoolean(); - } - - } - - /** - * Converted to/from an Integer in the Database. - */ - public static class IntBoolean extends BooleanBase { - - private final Integer trueValue; - private final Integer falseValue; - - public IntBoolean(Integer trueValue, Integer falseValue) { - super(false, Types.INTEGER); - this.trueValue = trueValue; - this.falseValue = falseValue; - } - - @Override - public int getLength() { - return 1; - } - - public void bind(DataBind b, Boolean value) throws SQLException { - if (value == null) { - b.setNull(Types.INTEGER); - } else { - b.setInt(toInteger(value)); - } - } - - public Boolean read(DataReader dataReader) throws SQLException { - Integer i = dataReader.getInt(); - if (i == null) { - return null; - } - if (i.equals(trueValue)) { - return Boolean.TRUE; - } else { - return Boolean.FALSE; - } - } - - public Object toJdbcType(Object value) { - return toInteger(value); - } - - /** - * Convert the Boolean value to the db value. - */ - public Integer toInteger(Object value) { - if (value == null) { - return null; - } - Boolean b = (Boolean) value; - if (b.booleanValue()) { - return trueValue; - } else { - return falseValue; - } - } - - /** - * Convert the db value to the Boolean value. - */ - public Boolean toBeanType(Object value) { - if (value == null) { - return null; - } - if (value instanceof Boolean) { - return (Boolean) value; - } - if (trueValue.equals(value)) { - return Boolean.TRUE; - } else { - return Boolean.FALSE; - } - } - - } - - /** - * Converted to/from an Integer in the Database. - */ - public static class StringBoolean extends BooleanBase { - - private final String trueValue; - private final String falseValue; - - public StringBoolean(String trueValue, String falseValue) { - super(false, Types.VARCHAR); - this.trueValue = trueValue; - this.falseValue = falseValue; - } - - @Override - public int getLength() { - // typically this will return 1 - return Math.max(trueValue.length(), falseValue.length()); - } - - public void bind(DataBind b, Boolean value) throws SQLException { - if (value == null) { - b.setNull(Types.VARCHAR); - } else { - b.setString(toString(value)); - } - } - - public Boolean read(DataReader dataReader) throws SQLException { - String string = dataReader.getString(); - if (string == null) { - return null; - } - - if (string.equals(trueValue)) { - return Boolean.TRUE; - } else { - return Boolean.FALSE; - } - } - - public Object toJdbcType(Object value) { - return toString(value); - } - - /** - * Convert the Boolean value to the db value. - */ - public String toString(Object value) { - if (value == null) { - return null; - } - Boolean b = (Boolean) value; - if (b.booleanValue()) { - return trueValue; - } else { - return falseValue; - } - } - - /** - * Convert the db value to the Boolean value. - */ - public Boolean toBeanType(Object value) { - if (value == null) { - return null; - } - if (value instanceof Boolean) { - return (Boolean) value; - } - if (trueValue.equals(value)) { - return Boolean.TRUE; - } else { - return Boolean.FALSE; - } - } - } - - public static abstract class BooleanBase extends ScalarTypeBase { - - public BooleanBase(boolean jdbcNative, int jdbcType) { - super(Boolean.class, jdbcNative, jdbcType); - } - - public String formatValue(Boolean t) { - return t.toString(); - } - - public Boolean parse(String value) { - return Boolean.valueOf(value); - } - - public Boolean convertFromMillis(long systemTimeMillis) { - throw new TextException("Not Supported"); - } - - public boolean isDateTimeCapable() { - return false; - } - - public Boolean readData(DataInput dataInput) throws IOException { - if (!dataInput.readBoolean()) { - return null; - } else { - return dataInput.readBoolean(); - } - } - - public void writeData(DataOutput dataOutput, Boolean val) throws IOException { - - if (val == null) { - dataOutput.writeBoolean(false); - } else { - dataOutput.writeBoolean(true); - dataOutput.writeBoolean(val); - } - } - - @Override - public Boolean jsonRead(JsonParser ctx, JsonToken event) { - return JsonToken.VALUE_TRUE == event ? Boolean.TRUE : Boolean.FALSE; - } - - public void jsonWrite(JsonGenerator ctx, String name, Boolean value) throws IOException { - ctx.writeBooleanField(name, (Boolean) value); - } - } - -} +package com.avaje.ebeaninternal.server.type; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.sql.SQLException; +import java.sql.Types; + +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; + +/** + * ScalarType for Boolean and boolean. + *

+ * This may or may not be a native jdbc type depending on the database and jdbc driver. + *

+ */ +public class ScalarTypeBoolean { + + public static class Native extends BooleanBase { + + /** + * Native Boolean database type. + */ + public Native() { + super(true, Types.BOOLEAN); + } + + public Boolean toBeanType(Object value) { + return BasicTypeConverter.toBoolean(value); + } + + public Object toJdbcType(Object value) { + return BasicTypeConverter.convert(value, jdbcType); + } + + public void bind(DataBind b, Boolean value) throws SQLException { + if (value == null) { + b.setNull(Types.BOOLEAN); + } else { + b.setBoolean(value); + } + + } + + public Boolean read(DataReader dataReader) throws SQLException { + return dataReader.getBoolean(); + } + } + + /** + * The Class BitBoolean converts a JDBC type BIT to a java boolean + * + *

+ * Sometimes booleans may be mapped to the JDBC type BIT. To use the BitBoolean specify + * type.boolean.dbtype="bit" in the ebean configuration + *

+ */ + public static class BitBoolean extends BooleanBase { + + /** + * Native Boolean database type. + */ + public BitBoolean() { + super(true, Types.BIT); + } + + public Boolean toBeanType(Object value) { + return BasicTypeConverter.toBoolean(value); + } + + public Object toJdbcType(Object value) { + // use JDBC driver to convert boolean to bit + return BasicTypeConverter.toBoolean(value); + } + + public void bind(DataBind b, Boolean value) throws SQLException { + if (value == null) { + b.setNull(Types.BIT); + } else { + // use JDBC driver to convert boolean to bit + b.setBoolean(value); + } + } + + public Boolean read(DataReader dataReader) throws SQLException { + return dataReader.getBoolean(); + } + + } + + /** + * Converted to/from an Integer in the Database. + */ + public static class IntBoolean extends BooleanBase { + + private final Integer trueValue; + private final Integer falseValue; + + public IntBoolean(Integer trueValue, Integer falseValue) { + super(false, Types.INTEGER); + this.trueValue = trueValue; + this.falseValue = falseValue; + } + + @Override + public int getLength() { + return 1; + } + + public void bind(DataBind b, Boolean value) throws SQLException { + if (value == null) { + b.setNull(Types.INTEGER); + } else { + b.setInt(toInteger(value)); + } + } + + public Boolean read(DataReader dataReader) throws SQLException { + Integer i = dataReader.getInt(); + if (i == null) { + return null; + } + if (i.equals(trueValue)) { + return Boolean.TRUE; + } else { + return Boolean.FALSE; + } + } + + public Object toJdbcType(Object value) { + return toInteger(value); + } + + /** + * Convert the Boolean value to the db value. + */ + public Integer toInteger(Object value) { + if (value == null) { + return null; + } + Boolean b = (Boolean) value; + if (b.booleanValue()) { + return trueValue; + } else { + return falseValue; + } + } + + /** + * Convert the db value to the Boolean value. + */ + public Boolean toBeanType(Object value) { + if (value == null) { + return null; + } + if (value instanceof Boolean) { + return (Boolean) value; + } + if (trueValue.equals(value)) { + return Boolean.TRUE; + } else { + return Boolean.FALSE; + } + } + + } + + /** + * Converted to/from an Integer in the Database. + */ + public static class StringBoolean extends BooleanBase { + + private final String trueValue; + private final String falseValue; + + public StringBoolean(String trueValue, String falseValue) { + super(false, Types.VARCHAR); + this.trueValue = trueValue; + this.falseValue = falseValue; + } + + @Override + public int getLength() { + // typically this will return 1 + return Math.max(trueValue.length(), falseValue.length()); + } + + public void bind(DataBind b, Boolean value) throws SQLException { + if (value == null) { + b.setNull(Types.VARCHAR); + } else { + b.setString(toString(value)); + } + } + + public Boolean read(DataReader dataReader) throws SQLException { + String string = dataReader.getString(); + if (string == null) { + return null; + } + + if (string.equals(trueValue)) { + return Boolean.TRUE; + } else { + return Boolean.FALSE; + } + } + + public Object toJdbcType(Object value) { + return toString(value); + } + + /** + * Convert the Boolean value to the db value. + */ + public String toString(Object value) { + if (value == null) { + return null; + } + Boolean b = (Boolean) value; + if (b.booleanValue()) { + return trueValue; + } else { + return falseValue; + } + } + + /** + * Convert the db value to the Boolean value. + */ + public Boolean toBeanType(Object value) { + if (value == null) { + return null; + } + if (value instanceof Boolean) { + return (Boolean) value; + } + if (trueValue.equals(value)) { + return Boolean.TRUE; + } else { + return Boolean.FALSE; + } + } + } + + public static abstract class BooleanBase extends ScalarTypeBase { + + public BooleanBase(boolean jdbcNative, int jdbcType) { + super(Boolean.class, jdbcNative, jdbcType); + } + + public String formatValue(Boolean t) { + return t.toString(); + } + + public Boolean parse(String value) { + return Boolean.valueOf(value); + } + + public Boolean convertFromMillis(long systemTimeMillis) { + throw new TextException("Not Supported"); + } + + public boolean isDateTimeCapable() { + return false; + } + + public Boolean readData(DataInput dataInput) throws IOException { + if (!dataInput.readBoolean()) { + return null; + } else { + return dataInput.readBoolean(); + } + } + + public void writeData(DataOutput dataOutput, Boolean val) throws IOException { + + if (val == null) { + dataOutput.writeBoolean(false); + } else { + dataOutput.writeBoolean(true); + dataOutput.writeBoolean(val); + } + } + + @Override + public Boolean jsonRead(JsonParser ctx, JsonToken event) { + return JsonToken.VALUE_TRUE == event ? Boolean.TRUE : Boolean.FALSE; + } + + public void jsonWrite(JsonGenerator ctx, String name, Boolean value) throws IOException { + ctx.writeBooleanField(name, (Boolean) value); + } + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeByte.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeByte.java index 1ac6dfcf2..bc6872eaf 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeByte.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeByte.java @@ -1,88 +1,88 @@ -package com.avaje.ebeaninternal.server.type; - -import java.io.DataInput; -import java.io.DataOutput; -import java.io.IOException; -import java.sql.SQLException; -import java.sql.Types; - -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; - -/** - * ScalarType for Byte. - */ -public class ScalarTypeByte extends ScalarTypeBase { - - public ScalarTypeByte() { - super(Byte.class, true, Types.TINYINT); - } - - public void bind(DataBind b, Byte value) throws SQLException { - if (value == null) { - b.setNull(Types.TINYINT); - } else { - b.setByte(value); - } - } - - public Byte read(DataReader dataReader) throws SQLException { - return dataReader.getByte(); - } - - public Object toJdbcType(Object value) { - return BasicTypeConverter.toByte(value); - } - - public Byte toBeanType(Object value) { - return BasicTypeConverter.toByte(value); - } - - @Override - public void jsonWrite(JsonGenerator ctx, String name, Byte value) throws IOException { - throw new IOException("Not supported"); - } - - @Override - public Byte jsonRead(JsonParser ctx, JsonToken event) throws IOException { - throw new IOException("Not supported"); - } - - public String formatValue(Byte t) { - return t.toString(); - } - - public Byte parse(String value) { - throw new TextException("Not supported"); - } - - public Byte convertFromMillis(long systemTimeMillis) { - throw new TextException("Not Supported"); - } - - public boolean isDateTimeCapable() { - return false; - } - - public Byte readData(DataInput dataInput) throws IOException { - if (!dataInput.readBoolean()) { - return null; - } else { - return dataInput.readByte(); - } - } - - public void writeData(DataOutput dataOutput, Byte val) throws IOException { - - if (val == null) { - dataOutput.writeBoolean(false); - } else { - dataOutput.writeBoolean(true); - dataOutput.writeByte(val); - } - } - -} +package com.avaje.ebeaninternal.server.type; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.sql.SQLException; +import java.sql.Types; + +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; + +/** + * ScalarType for Byte. + */ +public class ScalarTypeByte extends ScalarTypeBase { + + public ScalarTypeByte() { + super(Byte.class, true, Types.TINYINT); + } + + public void bind(DataBind b, Byte value) throws SQLException { + if (value == null) { + b.setNull(Types.TINYINT); + } else { + b.setByte(value); + } + } + + public Byte read(DataReader dataReader) throws SQLException { + return dataReader.getByte(); + } + + public Object toJdbcType(Object value) { + return BasicTypeConverter.toByte(value); + } + + public Byte toBeanType(Object value) { + return BasicTypeConverter.toByte(value); + } + + @Override + public void jsonWrite(JsonGenerator ctx, String name, Byte value) throws IOException { + throw new IOException("Not supported"); + } + + @Override + public Byte jsonRead(JsonParser ctx, JsonToken event) throws IOException { + throw new IOException("Not supported"); + } + + public String formatValue(Byte t) { + return t.toString(); + } + + public Byte parse(String value) { + throw new TextException("Not supported"); + } + + public Byte convertFromMillis(long systemTimeMillis) { + throw new TextException("Not Supported"); + } + + public boolean isDateTimeCapable() { + return false; + } + + public Byte readData(DataInput dataInput) throws IOException { + if (!dataInput.readBoolean()) { + return null; + } else { + return dataInput.readByte(); + } + } + + public void writeData(DataOutput dataOutput, Byte val) throws IOException { + + if (val == null) { + dataOutput.writeBoolean(false); + } else { + dataOutput.writeBoolean(true); + dataOutput.writeByte(val); + } + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBytesBase.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBytesBase.java index 2f03c15aa..0fa8c65a0 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBytesBase.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBytesBase.java @@ -1,97 +1,97 @@ -package com.avaje.ebeaninternal.server.type; - -import java.io.ByteArrayOutputStream; -import java.io.DataInput; -import java.io.DataOutput; -import java.io.IOException; -import java.sql.SQLException; - -import com.avaje.ebean.text.TextException; -import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.core.JsonToken; - -/** - * Base type for binary types. - */ -public abstract class ScalarTypeBytesBase extends ScalarTypeBase { - - protected ScalarTypeBytesBase(boolean jdbcNative, int jdbcType) { - super(byte[].class, jdbcNative, jdbcType); - } - - public Object convertFromBytes(byte[] bytes) { - return bytes; - } - - public byte[] convertToBytes(Object value) { - return (byte[]) value; - } - - public void bind(DataBind b, byte[] value) throws SQLException { - if (value == null) { - b.setNull(jdbcType); - } else { - b.setBytes(value); - } - } - - public Object toJdbcType(Object value) { - return value; - } - - public byte[] toBeanType(Object value) { - return (byte[]) value; - } - - @Override - public void jsonWrite(JsonGenerator ctx, String name, byte[] value) throws IOException { - ctx.writeBinaryField(name, (byte[]) value); - } - - @Override - public byte[] jsonRead(JsonParser ctx, JsonToken event) throws IOException { - ByteArrayOutputStream out = new ByteArrayOutputStream(500); - ctx.readBinaryValue(out); - return out.toByteArray(); - } - - public String formatValue(byte[] t) { - throw new TextException("Not supported"); - } - - public byte[] parse(String value) { - throw new TextException("Not supported"); - } - - public byte[] convertFromMillis(long systemTimeMillis) { - throw new TextException("Not supported"); - } - - public boolean isDateTimeCapable() { - return false; - } - - public byte[] readData(DataInput dataInput) throws IOException { - if (!dataInput.readBoolean()) { - return null; - } else { - int len = dataInput.readInt(); - byte[] buf = new byte[len]; - dataInput.readFully(buf, 0, buf.length); - return buf; - } - } - - public void writeData(DataOutput dataOutput, byte[] v) throws IOException { - if (v == null) { - dataOutput.writeBoolean(false); - } else { - dataOutput.writeBoolean(true); - byte[] bytes = convertToBytes(v); - dataOutput.writeInt(bytes.length); - dataOutput.write(bytes); - } - } - -} +package com.avaje.ebeaninternal.server.type; + +import java.io.ByteArrayOutputStream; +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.sql.SQLException; + +import com.avaje.ebean.text.TextException; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonToken; + +/** + * Base type for binary types. + */ +public abstract class ScalarTypeBytesBase extends ScalarTypeBase { + + protected ScalarTypeBytesBase(boolean jdbcNative, int jdbcType) { + super(byte[].class, jdbcNative, jdbcType); + } + + public Object convertFromBytes(byte[] bytes) { + return bytes; + } + + public byte[] convertToBytes(Object value) { + return (byte[]) value; + } + + public void bind(DataBind b, byte[] value) throws SQLException { + if (value == null) { + b.setNull(jdbcType); + } else { + b.setBytes(value); + } + } + + public Object toJdbcType(Object value) { + return value; + } + + public byte[] toBeanType(Object value) { + return (byte[]) value; + } + + @Override + public void jsonWrite(JsonGenerator ctx, String name, byte[] value) throws IOException { + ctx.writeBinaryField(name, (byte[]) value); + } + + @Override + public byte[] jsonRead(JsonParser ctx, JsonToken event) throws IOException { + ByteArrayOutputStream out = new ByteArrayOutputStream(500); + ctx.readBinaryValue(out); + return out.toByteArray(); + } + + public String formatValue(byte[] t) { + throw new TextException("Not supported"); + } + + public byte[] parse(String value) { + throw new TextException("Not supported"); + } + + public byte[] convertFromMillis(long systemTimeMillis) { + throw new TextException("Not supported"); + } + + public boolean isDateTimeCapable() { + return false; + } + + public byte[] readData(DataInput dataInput) throws IOException { + if (!dataInput.readBoolean()) { + return null; + } else { + int len = dataInput.readInt(); + byte[] buf = new byte[len]; + dataInput.readFully(buf, 0, buf.length); + return buf; + } + } + + public void writeData(DataOutput dataOutput, byte[] v) throws IOException { + if (v == null) { + dataOutput.writeBoolean(false); + } else { + dataOutput.writeBoolean(true); + byte[] bytes = convertToBytes(v); + dataOutput.writeInt(bytes.length); + dataOutput.write(bytes); + } + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBytesBinary.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBytesBinary.java index b83cf3c05..22809599a 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBytesBinary.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBytesBinary.java @@ -1,19 +1,19 @@ -package com.avaje.ebeaninternal.server.type; - -import java.sql.SQLException; -import java.sql.Types; - -/** - * ScalarType for Types.BINARY to byte[]. - */ -public class ScalarTypeBytesBinary extends ScalarTypeBytesBase { - - public ScalarTypeBytesBinary() { - super(true, Types.BINARY); - } - - public byte[] read(DataReader dataReader) throws SQLException { - return dataReader.getBytes(); - } - -} +package com.avaje.ebeaninternal.server.type; + +import java.sql.SQLException; +import java.sql.Types; + +/** + * ScalarType for Types.BINARY to byte[]. + */ +public class ScalarTypeBytesBinary extends ScalarTypeBytesBase { + + public ScalarTypeBytesBinary() { + super(true, Types.BINARY); + } + + public byte[] read(DataReader dataReader) throws SQLException { + return dataReader.getBytes(); + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBytesBlob.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBytesBlob.java index 11ff9f465..31dc90637 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBytesBlob.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBytesBlob.java @@ -1,20 +1,20 @@ -package com.avaje.ebeaninternal.server.type; - -import java.sql.SQLException; -import java.sql.Types; - -/** - * ScalarType for BLOB. - */ -public class ScalarTypeBytesBlob extends ScalarTypeBytesBase { - - public ScalarTypeBytesBlob() { - super(true, Types.BLOB); - } - - public byte[] read(DataReader dataReader) throws SQLException { - - return dataReader.getBlobBytes(); - } - -} +package com.avaje.ebeaninternal.server.type; + +import java.sql.SQLException; +import java.sql.Types; + +/** + * ScalarType for BLOB. + */ +public class ScalarTypeBytesBlob extends ScalarTypeBytesBase { + + public ScalarTypeBytesBlob() { + super(true, Types.BLOB); + } + + public byte[] read(DataReader dataReader) throws SQLException { + + return dataReader.getBlobBytes(); + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBytesEncrypted.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBytesEncrypted.java index 4dfbc2b1f..d739a24f6 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBytesEncrypted.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBytesEncrypted.java @@ -1,138 +1,138 @@ -package com.avaje.ebeaninternal.server.type; - -import java.io.ByteArrayOutputStream; -import java.io.DataInput; -import java.io.DataOutput; -import java.io.IOException; -import java.sql.SQLException; - -import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.core.JsonToken; - -/** - * Encrypted ScalarType that wraps a byte[] types. - * - * @author rbygrave - * - */ -public class ScalarTypeBytesEncrypted implements ScalarType { - - private final ScalarTypeBytesBase baseType; - - private final DataEncryptSupport dataEncryptSupport; - - public ScalarTypeBytesEncrypted(ScalarTypeBytesBase baseType, DataEncryptSupport dataEncryptSupport) { - this.baseType = baseType; - this.dataEncryptSupport = dataEncryptSupport; - } - - @Override - public boolean isMutable() { - return false; - } - - @Override - public boolean isDirty(Object value) { - return false; - } - - public void bind(DataBind b, byte[] value) throws SQLException { - value = dataEncryptSupport.encrypt(value); - baseType.bind(b, value); - } - - public int getJdbcType() { - return baseType.getJdbcType(); - } - - public int getLength() { - return baseType.getLength(); - } - - public Class getType() { - return byte[].class; - } - - public boolean isDateTimeCapable() { - return baseType.isDateTimeCapable(); - } - - public boolean isJdbcNative() { - return baseType.isJdbcNative(); - } - - public void loadIgnore(DataReader dataReader) { - baseType.loadIgnore(dataReader); - } - - @Override - public void jsonWrite(JsonGenerator ctx, String name, byte[] value) throws IOException { - ctx.writeBinaryField(name, (byte[]) value); - } - - @Override - public byte[] jsonRead(JsonParser ctx, JsonToken event) throws IOException { - ByteArrayOutputStream out = new ByteArrayOutputStream(500); - ctx.readBinaryValue(out); - return out.toByteArray(); - } - - public String format(Object v) { - throw new RuntimeException("Not used"); - } - - public String formatValue(byte[] v) { - throw new RuntimeException("Not used"); - } - - public byte[] parse(String value) { - return baseType.parse(value); - } - - public byte[] convertFromMillis(long systemTimeMillis) { - return baseType.convertFromMillis(systemTimeMillis); - } - - public byte[] read(DataReader dataReader) throws SQLException { - - byte[] data = baseType.read(dataReader); - data = dataEncryptSupport.decrypt(data); - return data; - } - - public byte[] toBeanType(Object value) { - return baseType.toBeanType(value); - } - - public Object toJdbcType(Object value) { - return baseType.toJdbcType(value); - } - - public void accumulateScalarTypes(String propName, CtCompoundTypeScalarList list) { - baseType.accumulateScalarTypes(propName, list); - } - - public byte[] readData(DataInput dataInput) throws IOException { - if (!dataInput.readBoolean()) { - return null; - } else { - int len = dataInput.readInt(); - byte[] value = new byte[len]; - dataInput.readFully(value); - return value; - } - } - - public void writeData(DataOutput dataOutput, byte[] value) throws IOException { - - if (value == null) { - dataOutput.writeBoolean(false); - } else { - dataOutput.writeBoolean(true); - dataOutput.writeInt(value.length); - dataOutput.write(value); - } - } - -} +package com.avaje.ebeaninternal.server.type; + +import java.io.ByteArrayOutputStream; +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.sql.SQLException; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonToken; + +/** + * Encrypted ScalarType that wraps a byte[] types. + * + * @author rbygrave + * + */ +public class ScalarTypeBytesEncrypted implements ScalarType { + + private final ScalarTypeBytesBase baseType; + + private final DataEncryptSupport dataEncryptSupport; + + public ScalarTypeBytesEncrypted(ScalarTypeBytesBase baseType, DataEncryptSupport dataEncryptSupport) { + this.baseType = baseType; + this.dataEncryptSupport = dataEncryptSupport; + } + + @Override + public boolean isMutable() { + return false; + } + + @Override + public boolean isDirty(Object value) { + return false; + } + + public void bind(DataBind b, byte[] value) throws SQLException { + value = dataEncryptSupport.encrypt(value); + baseType.bind(b, value); + } + + public int getJdbcType() { + return baseType.getJdbcType(); + } + + public int getLength() { + return baseType.getLength(); + } + + public Class getType() { + return byte[].class; + } + + public boolean isDateTimeCapable() { + return baseType.isDateTimeCapable(); + } + + public boolean isJdbcNative() { + return baseType.isJdbcNative(); + } + + public void loadIgnore(DataReader dataReader) { + baseType.loadIgnore(dataReader); + } + + @Override + public void jsonWrite(JsonGenerator ctx, String name, byte[] value) throws IOException { + ctx.writeBinaryField(name, (byte[]) value); + } + + @Override + public byte[] jsonRead(JsonParser ctx, JsonToken event) throws IOException { + ByteArrayOutputStream out = new ByteArrayOutputStream(500); + ctx.readBinaryValue(out); + return out.toByteArray(); + } + + public String format(Object v) { + throw new RuntimeException("Not used"); + } + + public String formatValue(byte[] v) { + throw new RuntimeException("Not used"); + } + + public byte[] parse(String value) { + return baseType.parse(value); + } + + public byte[] convertFromMillis(long systemTimeMillis) { + return baseType.convertFromMillis(systemTimeMillis); + } + + public byte[] read(DataReader dataReader) throws SQLException { + + byte[] data = baseType.read(dataReader); + data = dataEncryptSupport.decrypt(data); + return data; + } + + public byte[] toBeanType(Object value) { + return baseType.toBeanType(value); + } + + public Object toJdbcType(Object value) { + return baseType.toJdbcType(value); + } + + public void accumulateScalarTypes(String propName, CtCompoundTypeScalarList list) { + baseType.accumulateScalarTypes(propName, list); + } + + public byte[] readData(DataInput dataInput) throws IOException { + if (!dataInput.readBoolean()) { + return null; + } else { + int len = dataInput.readInt(); + byte[] value = new byte[len]; + dataInput.readFully(value); + return value; + } + } + + public void writeData(DataOutput dataOutput, byte[] value) throws IOException { + + if (value == null) { + dataOutput.writeBoolean(false); + } else { + dataOutput.writeBoolean(true); + dataOutput.writeInt(value.length); + dataOutput.write(value); + } + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBytesLongVarbinary.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBytesLongVarbinary.java index 3a40e3ee9..6aa95f9fe 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBytesLongVarbinary.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBytesLongVarbinary.java @@ -1,18 +1,18 @@ -package com.avaje.ebeaninternal.server.type; - -import java.sql.SQLException; -import java.sql.Types; - -/** - * ScalarType for Longvarbinary. - */ -public class ScalarTypeBytesLongVarbinary extends ScalarTypeBytesBase { - - public ScalarTypeBytesLongVarbinary() { - super(true, Types.LONGVARBINARY); - } - - public byte[] read(DataReader dataReader) throws SQLException { - return dataReader.getBinaryBytes(); - } -} +package com.avaje.ebeaninternal.server.type; + +import java.sql.SQLException; +import java.sql.Types; + +/** + * ScalarType for Longvarbinary. + */ +public class ScalarTypeBytesLongVarbinary extends ScalarTypeBytesBase { + + public ScalarTypeBytesLongVarbinary() { + super(true, Types.LONGVARBINARY); + } + + public byte[] read(DataReader dataReader) throws SQLException { + return dataReader.getBinaryBytes(); + } +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBytesVarbinary.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBytesVarbinary.java index 7ea8f4a5f..e1ee46889 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBytesVarbinary.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeBytesVarbinary.java @@ -1,19 +1,19 @@ -package com.avaje.ebeaninternal.server.type; - -import java.sql.SQLException; -import java.sql.Types; - -/** - * ScalarType for Types.VARBINARY to byte[]. - */ -public class ScalarTypeBytesVarbinary extends ScalarTypeBytesBase { - - public ScalarTypeBytesVarbinary() { - super(true, Types.VARBINARY); - } - - public byte[] read(DataReader dataReader) throws SQLException { - return dataReader.getBytes(); - } - -} +package com.avaje.ebeaninternal.server.type; + +import java.sql.SQLException; +import java.sql.Types; + +/** + * ScalarType for Types.VARBINARY to byte[]. + */ +public class ScalarTypeBytesVarbinary extends ScalarTypeBytesBase { + + public ScalarTypeBytesVarbinary() { + super(true, Types.VARBINARY); + } + + public byte[] read(DataReader dataReader) throws SQLException { + return dataReader.getBytes(); + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeCalendar.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeCalendar.java index ee790cc87..1df451c41 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeCalendar.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeCalendar.java @@ -1,80 +1,80 @@ -package com.avaje.ebeaninternal.server.type; - -import java.sql.Date; -import java.sql.SQLException; -import java.sql.Timestamp; -import java.sql.Types; -import java.util.Calendar; - -import com.avaje.ebean.config.JsonConfig; -import com.avaje.ebeaninternal.server.core.BasicTypeConverter; - -/** - * ScalarType for java.util.Calendar. - */ -public class ScalarTypeCalendar extends ScalarTypeBaseDateTime { - - public ScalarTypeCalendar(JsonConfig.DateTime mode, int jdbcType) { - super(mode, Calendar.class, false, jdbcType); - } - - public void bind(DataBind b, Calendar value) throws SQLException { - if (value == null) { - b.setNull(Types.TIMESTAMP); - } else { - - if (jdbcType == Types.TIMESTAMP) { - Timestamp timestamp = new Timestamp(value.getTimeInMillis()); - b.setTimestamp(timestamp); - } else { - Date d = new Date(value.getTimeInMillis()); - b.setDate(d); - } - } - } - - @Override - public Calendar convertFromMillis(long systemTimeMillis) { - Calendar calendar = Calendar.getInstance(); - calendar.setTimeInMillis(systemTimeMillis); - return calendar; - } - - @Override - public Calendar convertFromTimestamp(Timestamp ts) { - Calendar calendar = Calendar.getInstance(); - calendar.setTimeInMillis(ts.getTime()); - return calendar; - } - - @Override - protected String toJsonNanos(Calendar value) { - return String.valueOf(value.getTime()); - } - - @Override - protected String toJsonISO8601(Calendar value) { - return dateTimeParser.format(value.getTime()); - } - - @Override - public long convertToMillis(Calendar value) { - return value.getTimeInMillis(); - } - - @Override - public Timestamp convertToTimestamp(Calendar t) { - return new Timestamp(t.getTimeInMillis()); - } - - @Override - public Object toJdbcType(Object value) { - return BasicTypeConverter.convert(value, jdbcType); - } - - @Override - public Calendar toBeanType(Object value) { - return BasicTypeConverter.toCalendar(value); - } - -} +package com.avaje.ebeaninternal.server.type; + +import java.sql.Date; +import java.sql.SQLException; +import java.sql.Timestamp; +import java.sql.Types; +import java.util.Calendar; + +import com.avaje.ebean.config.JsonConfig; +import com.avaje.ebeaninternal.server.core.BasicTypeConverter; + +/** + * ScalarType for java.util.Calendar. + */ +public class ScalarTypeCalendar extends ScalarTypeBaseDateTime { + + public ScalarTypeCalendar(JsonConfig.DateTime mode, int jdbcType) { + super(mode, Calendar.class, false, jdbcType); + } + + public void bind(DataBind b, Calendar value) throws SQLException { + if (value == null) { + b.setNull(Types.TIMESTAMP); + } else { + + if (jdbcType == Types.TIMESTAMP) { + Timestamp timestamp = new Timestamp(value.getTimeInMillis()); + b.setTimestamp(timestamp); + } else { + Date d = new Date(value.getTimeInMillis()); + b.setDate(d); + } + } + } + + @Override + public Calendar convertFromMillis(long systemTimeMillis) { + Calendar calendar = Calendar.getInstance(); + calendar.setTimeInMillis(systemTimeMillis); + return calendar; + } + + @Override + public Calendar convertFromTimestamp(Timestamp ts) { + Calendar calendar = Calendar.getInstance(); + calendar.setTimeInMillis(ts.getTime()); + return calendar; + } + + @Override + protected String toJsonNanos(Calendar value) { + return String.valueOf(value.getTime()); + } + + @Override + protected String toJsonISO8601(Calendar value) { + return dateTimeParser.format(value.getTime()); + } + + @Override + public long convertToMillis(Calendar value) { + return value.getTimeInMillis(); + } + + @Override + public Timestamp convertToTimestamp(Calendar t) { + return new Timestamp(t.getTimeInMillis()); + } + + @Override + public Object toJdbcType(Object value) { + return BasicTypeConverter.convert(value, jdbcType); + } + + @Override + public Calendar toBeanType(Object value) { + return BasicTypeConverter.toCalendar(value); + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeChar.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeChar.java index 7abe7c09c..567e3bd38 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeChar.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeChar.java @@ -1,62 +1,62 @@ -package com.avaje.ebeaninternal.server.type; - -import java.sql.SQLException; -import java.sql.Types; - -import com.avaje.ebeaninternal.server.core.BasicTypeConverter; - -/** - * ScalarType for char. - */ -public class ScalarTypeChar extends ScalarTypeBaseVarchar { - - public ScalarTypeChar() { - super(char.class, false, Types.VARCHAR); - } - - @Override - public Character convertFromDbString(String dbValue) { - return dbValue.charAt(0); - } - - @Override - public String convertToDbString(Character beanValue) { - return beanValue.toString(); - } - - public void bind(DataBind b, Character value) throws SQLException { - if (value == null) { - b.setNull(Types.VARCHAR); - } else { - String s = BasicTypeConverter.toString(value); - b.setString(s); - } - } - - public Character read(DataReader dataReader) throws SQLException { - String string = dataReader.getString(); - if (string == null || string.length() == 0) { - return null; - } else { - return string.charAt(0); - } - } - - public Object toJdbcType(Object value) { - return BasicTypeConverter.toString(value); - } - - public Character toBeanType(Object value) { - String s = BasicTypeConverter.toString(value); - return s.charAt(0); - } - - public String formatValue(Character t) { - return t.toString(); - } - - public Character parse(String value) { - return value.charAt(0); - } - -} +package com.avaje.ebeaninternal.server.type; + +import java.sql.SQLException; +import java.sql.Types; + +import com.avaje.ebeaninternal.server.core.BasicTypeConverter; + +/** + * ScalarType for char. + */ +public class ScalarTypeChar extends ScalarTypeBaseVarchar { + + public ScalarTypeChar() { + super(char.class, false, Types.VARCHAR); + } + + @Override + public Character convertFromDbString(String dbValue) { + return dbValue.charAt(0); + } + + @Override + public String convertToDbString(Character beanValue) { + return beanValue.toString(); + } + + public void bind(DataBind b, Character value) throws SQLException { + if (value == null) { + b.setNull(Types.VARCHAR); + } else { + String s = BasicTypeConverter.toString(value); + b.setString(s); + } + } + + public Character read(DataReader dataReader) throws SQLException { + String string = dataReader.getString(); + if (string == null || string.length() == 0) { + return null; + } else { + return string.charAt(0); + } + } + + public Object toJdbcType(Object value) { + return BasicTypeConverter.toString(value); + } + + public Character toBeanType(Object value) { + String s = BasicTypeConverter.toString(value); + return s.charAt(0); + } + + public String formatValue(Character t) { + return t.toString(); + } + + public Character parse(String value) { + return value.charAt(0); + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeCharArray.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeCharArray.java index ec947202f..1394e5d86 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeCharArray.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeCharArray.java @@ -1,74 +1,74 @@ -package com.avaje.ebeaninternal.server.type; - -import java.io.IOException; -import java.sql.SQLException; -import java.sql.Types; - -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; - -/** - * ScalarType for char[]. - */ -public class ScalarTypeCharArray extends ScalarTypeBaseVarchar { - - public ScalarTypeCharArray() { - super(char[].class, false, Types.VARCHAR); - } - - @Override - public char[] convertFromDbString(String dbValue) { - return dbValue.toCharArray(); - } - - @Override - public String convertToDbString(char[] beanValue) { - return new String(beanValue); - } - - public void bind(DataBind b, char[] value) throws SQLException { - if (value == null) { - b.setNull(Types.VARCHAR); - } else { - String s = BasicTypeConverter.toString(value); - b.setString(s); - } - } - - public char[] read(DataReader dataReader) throws SQLException { - String string = dataReader.getString(); - if (string == null) { - return null; - } else { - return string.toCharArray(); - } - } - - public Object toJdbcType(Object value) { - return BasicTypeConverter.toString(value); - } - - public char[] toBeanType(Object value) { - String s = BasicTypeConverter.toString(value); - return s.toCharArray(); - } - - public String formatValue(char[] t) { - return String.valueOf(t); - } - - public char[] parse(String value) { - return value.toCharArray(); - } - - @Override - public char[] jsonRead(JsonParser ctx, JsonToken event) throws IOException { - return ctx.getValueAsString().toCharArray(); - } - - public void jsonWrite(JsonGenerator ctx, String name, char[] value) throws IOException { - ctx.writeStringField(name, String.valueOf(value)); - } -} +package com.avaje.ebeaninternal.server.type; + +import java.io.IOException; +import java.sql.SQLException; +import java.sql.Types; + +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; + +/** + * ScalarType for char[]. + */ +public class ScalarTypeCharArray extends ScalarTypeBaseVarchar { + + public ScalarTypeCharArray() { + super(char[].class, false, Types.VARCHAR); + } + + @Override + public char[] convertFromDbString(String dbValue) { + return dbValue.toCharArray(); + } + + @Override + public String convertToDbString(char[] beanValue) { + return new String(beanValue); + } + + public void bind(DataBind b, char[] value) throws SQLException { + if (value == null) { + b.setNull(Types.VARCHAR); + } else { + String s = BasicTypeConverter.toString(value); + b.setString(s); + } + } + + public char[] read(DataReader dataReader) throws SQLException { + String string = dataReader.getString(); + if (string == null) { + return null; + } else { + return string.toCharArray(); + } + } + + public Object toJdbcType(Object value) { + return BasicTypeConverter.toString(value); + } + + public char[] toBeanType(Object value) { + String s = BasicTypeConverter.toString(value); + return s.toCharArray(); + } + + public String formatValue(char[] t) { + return String.valueOf(t); + } + + public char[] parse(String value) { + return value.toCharArray(); + } + + @Override + public char[] jsonRead(JsonParser ctx, JsonToken event) throws IOException { + return ctx.getValueAsString().toCharArray(); + } + + public void jsonWrite(JsonGenerator ctx, String name, char[] value) throws IOException { + ctx.writeStringField(name, String.valueOf(value)); + } +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeClass.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeClass.java index 1a2a6683b..a1227e719 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeClass.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeClass.java @@ -1,45 +1,45 @@ -package com.avaje.ebeaninternal.server.type; - -import javax.persistence.PersistenceException; - -/** - * ScalarType for Class that persists it to VARCHAR column. - * - * @author emcgreal - * @author rbygrave - */ -@SuppressWarnings({ "rawtypes" }) -public class ScalarTypeClass extends ScalarTypeBaseVarchar { - - public ScalarTypeClass() { - super(Class.class); - } - - @Override - public int getLength() { - return 255; - } - - @Override - public Class convertFromDbString(String dbValue) { - return parse(dbValue); - } - - @Override - public String convertToDbString(Class beanValue) { - return beanValue.getCanonicalName(); - } - - public String formatValue(Class v) { - return v.getCanonicalName(); - } - - public Class parse(String value) { - try { - return Class.forName(value); - } catch (Exception e) { - throw new PersistenceException("Unable to find Class " + value, e); - } - } - -} +package com.avaje.ebeaninternal.server.type; + +import javax.persistence.PersistenceException; + +/** + * ScalarType for Class that persists it to VARCHAR column. + * + * @author emcgreal + * @author rbygrave + */ +@SuppressWarnings({ "rawtypes" }) +public class ScalarTypeClass extends ScalarTypeBaseVarchar { + + public ScalarTypeClass() { + super(Class.class); + } + + @Override + public int getLength() { + return 255; + } + + @Override + public Class convertFromDbString(String dbValue) { + return parse(dbValue); + } + + @Override + public String convertToDbString(Class beanValue) { + return beanValue.getCanonicalName(); + } + + public String formatValue(Class v) { + return v.getCanonicalName(); + } + + public Class parse(String value) { + try { + return Class.forName(value); + } catch (Exception e) { + throw new PersistenceException("Unable to find Class " + value, e); + } + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeClob.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeClob.java index c81469c66..d052373d5 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeClob.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeClob.java @@ -1,70 +1,70 @@ -package com.avaje.ebeaninternal.server.type; - -import java.sql.SQLException; -import java.sql.Types; - -import com.avaje.ebeaninternal.server.core.BasicTypeConverter; - -/** - * ScalarType for String. - */ -public class ScalarTypeClob extends ScalarTypeBaseVarchar { - - static final int clobBufferSize = 512; - - static final int stringInitialSize = 512; - - protected ScalarTypeClob(boolean jdbcNative, int jdbcType) { - super(String.class, jdbcNative, jdbcType); - } - - public ScalarTypeClob() { - super(String.class, true, Types.CLOB); - } - - @Override - public String convertFromDbString(String dbValue) { - return dbValue; - } - - @Override - public String convertToDbString(String beanValue) { - return beanValue; - } - - @Override - public void bind(DataBind b, String value) throws SQLException { - if (value == null) { - b.setNull(Types.VARCHAR); - } else { - b.setString(value); - } - } - - @Override - public String read(DataReader dataReader) throws SQLException { - - return dataReader.getStringClob(); - } - - @Override - public Object toJdbcType(Object value) { - return BasicTypeConverter.toString(value); - } - - @Override - public String toBeanType(Object value) { - return BasicTypeConverter.toString(value); - } - - @Override - public String formatValue(String t) { - return t; - } - - @Override - public String parse(String value) { - return value; - } - -} +package com.avaje.ebeaninternal.server.type; + +import java.sql.SQLException; +import java.sql.Types; + +import com.avaje.ebeaninternal.server.core.BasicTypeConverter; + +/** + * ScalarType for String. + */ +public class ScalarTypeClob extends ScalarTypeBaseVarchar { + + static final int clobBufferSize = 512; + + static final int stringInitialSize = 512; + + protected ScalarTypeClob(boolean jdbcNative, int jdbcType) { + super(String.class, jdbcNative, jdbcType); + } + + public ScalarTypeClob() { + super(String.class, true, Types.CLOB); + } + + @Override + public String convertFromDbString(String dbValue) { + return dbValue; + } + + @Override + public String convertToDbString(String beanValue) { + return beanValue; + } + + @Override + public void bind(DataBind b, String value) throws SQLException { + if (value == null) { + b.setNull(Types.VARCHAR); + } else { + b.setString(value); + } + } + + @Override + public String read(DataReader dataReader) throws SQLException { + + return dataReader.getStringClob(); + } + + @Override + public Object toJdbcType(Object value) { + return BasicTypeConverter.toString(value); + } + + @Override + public String toBeanType(Object value) { + return BasicTypeConverter.toString(value); + } + + @Override + public String formatValue(String t) { + return t; + } + + @Override + public String parse(String value) { + return value; + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeCurrency.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeCurrency.java index e70d11d87..076c65c21 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeCurrency.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeCurrency.java @@ -1,39 +1,39 @@ -package com.avaje.ebeaninternal.server.type; - -import java.util.Currency; - -/** - * ScalarType for java.util.Currency which converts to and from a VARCHAR database column. - */ -public class ScalarTypeCurrency extends ScalarTypeBaseVarchar { - - public ScalarTypeCurrency() { - super(Currency.class); - } - - @Override - public int getLength() { - return 3; - } - - @Override - public Currency convertFromDbString(String dbValue) { - return Currency.getInstance(dbValue); - } - - @Override - public String convertToDbString(Currency beanValue) { - return ((Currency) beanValue).getCurrencyCode(); - } - - @Override - public String formatValue(Currency v) { - return v.toString(); - } - - @Override - public Currency parse(String value) { - return Currency.getInstance(value); - } - -} +package com.avaje.ebeaninternal.server.type; + +import java.util.Currency; + +/** + * ScalarType for java.util.Currency which converts to and from a VARCHAR database column. + */ +public class ScalarTypeCurrency extends ScalarTypeBaseVarchar { + + public ScalarTypeCurrency() { + super(Currency.class); + } + + @Override + public int getLength() { + return 3; + } + + @Override + public Currency convertFromDbString(String dbValue) { + return Currency.getInstance(dbValue); + } + + @Override + public String convertToDbString(Currency beanValue) { + return ((Currency) beanValue).getCurrencyCode(); + } + + @Override + public String formatValue(Currency v) { + return v.toString(); + } + + @Override + public Currency parse(String value) { + return Currency.getInstance(value); + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeDate.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeDate.java index 21b373007..986c273aa 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeDate.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeDate.java @@ -1,53 +1,53 @@ -package com.avaje.ebeaninternal.server.type; - -import java.sql.Date; -import java.sql.SQLException; -import java.sql.Types; - -import com.avaje.ebeaninternal.server.core.BasicTypeConverter; - -/** - * ScalarType for java.sql.Date. - */ -public class ScalarTypeDate extends ScalarTypeBaseDate { - - public ScalarTypeDate() { - super(Date.class, true, Types.DATE); - } - - @Override - public long convertToMillis(Date value) { - return value.getTime(); - } - - @Override - public Date convertFromDate(Date date) { - return date; - } - - @Override - public Date convertToDate(Date t) { - return t; - } - - public void bind(DataBind b, java.sql.Date value) throws SQLException { - if (value == null) { - b.setNull(Types.DATE); - } else { - b.setDate(value); - } - } - - public java.sql.Date read(DataReader dataReader) throws SQLException { - return dataReader.getDate(); - } - - public Object toJdbcType(Object value) { - return BasicTypeConverter.toDate(value); - } - - public java.sql.Date toBeanType(Object value) { - return BasicTypeConverter.toDate(value); - } - -} +package com.avaje.ebeaninternal.server.type; + +import java.sql.Date; +import java.sql.SQLException; +import java.sql.Types; + +import com.avaje.ebeaninternal.server.core.BasicTypeConverter; + +/** + * ScalarType for java.sql.Date. + */ +public class ScalarTypeDate extends ScalarTypeBaseDate { + + public ScalarTypeDate() { + super(Date.class, true, Types.DATE); + } + + @Override + public long convertToMillis(Date value) { + return value.getTime(); + } + + @Override + public Date convertFromDate(Date date) { + return date; + } + + @Override + public Date convertToDate(Date t) { + return t; + } + + public void bind(DataBind b, java.sql.Date value) throws SQLException { + if (value == null) { + b.setNull(Types.DATE); + } else { + b.setDate(value); + } + } + + public java.sql.Date read(DataReader dataReader) throws SQLException { + return dataReader.getDate(); + } + + public Object toJdbcType(Object value) { + return BasicTypeConverter.toDate(value); + } + + public java.sql.Date toBeanType(Object value) { + return BasicTypeConverter.toDate(value); + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeDouble.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeDouble.java index 10b5c97fe..ce8c4f3ad 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeDouble.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeDouble.java @@ -1,96 +1,96 @@ -package com.avaje.ebeaninternal.server.type; - -import java.io.DataInput; -import java.io.DataOutput; -import java.io.IOException; -import java.sql.SQLException; -import java.sql.Types; - -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; - -/** - * ScalarType for Double and double. - */ -public class ScalarTypeDouble extends ScalarTypeBase { - - public ScalarTypeDouble() { - super(Double.class, true, Types.DOUBLE); - } - - @Override - public void bind(DataBind b, Double value) throws SQLException { - if (value == null) { - b.setNull(Types.DOUBLE); - } else { - b.setDouble(value.doubleValue()); - } - } - - @Override - public Double read(DataReader dataReader) throws SQLException { - return dataReader.getDouble(); - } - - @Override - public Object toJdbcType(Object value) { - return BasicTypeConverter.toDouble(value); - } - - @Override - public Double toBeanType(Object value) { - return BasicTypeConverter.toDouble(value); - } - - @Override - public String formatValue(Double t) { - return t.toString(); - } - - @Override - public Double parse(String value) { - return Double.valueOf(value); - } - - @Override - public Double convertFromMillis(long systemTimeMillis) { - return Double.valueOf(systemTimeMillis); - } - - @Override - public boolean isDateTimeCapable() { - return true; - } - - @Override - public Double readData(DataInput dataInput) throws IOException { - if (!dataInput.readBoolean()) { - return null; - } else { - return dataInput.readDouble(); - } - } - - @Override - public void writeData(DataOutput dataOutput, Double value) throws IOException { - - if (value == null) { - dataOutput.writeBoolean(false); - } else { - dataOutput.writeBoolean(true); - dataOutput.writeDouble(value); - } - } - - @Override - public Double jsonRead(JsonParser ctx, JsonToken event) throws IOException { - return ctx.getDoubleValue(); - } - - @Override - public void jsonWrite(JsonGenerator ctx, String name, Double value) throws IOException { - ctx.writeNumberField(name, value); - } -} +package com.avaje.ebeaninternal.server.type; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.sql.SQLException; +import java.sql.Types; + +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; + +/** + * ScalarType for Double and double. + */ +public class ScalarTypeDouble extends ScalarTypeBase { + + public ScalarTypeDouble() { + super(Double.class, true, Types.DOUBLE); + } + + @Override + public void bind(DataBind b, Double value) throws SQLException { + if (value == null) { + b.setNull(Types.DOUBLE); + } else { + b.setDouble(value.doubleValue()); + } + } + + @Override + public Double read(DataReader dataReader) throws SQLException { + return dataReader.getDouble(); + } + + @Override + public Object toJdbcType(Object value) { + return BasicTypeConverter.toDouble(value); + } + + @Override + public Double toBeanType(Object value) { + return BasicTypeConverter.toDouble(value); + } + + @Override + public String formatValue(Double t) { + return t.toString(); + } + + @Override + public Double parse(String value) { + return Double.valueOf(value); + } + + @Override + public Double convertFromMillis(long systemTimeMillis) { + return Double.valueOf(systemTimeMillis); + } + + @Override + public boolean isDateTimeCapable() { + return true; + } + + @Override + public Double readData(DataInput dataInput) throws IOException { + if (!dataInput.readBoolean()) { + return null; + } else { + return dataInput.readDouble(); + } + } + + @Override + public void writeData(DataOutput dataOutput, Double value) throws IOException { + + if (value == null) { + dataOutput.writeBoolean(false); + } else { + dataOutput.writeBoolean(true); + dataOutput.writeDouble(value); + } + } + + @Override + public Double jsonRead(JsonParser ctx, JsonToken event) throws IOException { + return ctx.getDoubleValue(); + } + + @Override + public void jsonWrite(JsonGenerator ctx, String name, Double value) throws IOException { + ctx.writeNumberField(name, value); + } +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeDuration.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeDuration.java index 7e5937b45..5cd093ed3 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeDuration.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeDuration.java @@ -1,113 +1,113 @@ -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; -import java.time.Duration; - -/** - * ScalarType for java.time.Duration (with seconds precision). - */ -public class ScalarTypeDuration extends ScalarTypeBase { - - public ScalarTypeDuration() { - super(Duration.class, false, Types.BIGINT); - } - - protected ScalarTypeDuration(int jdbcType) { - super(Duration.class, false, jdbcType); - } - - public BigDecimal convertToBigDecimal(Duration value) { - return (value == null) ? null : DecimalUtils.toDecimal(value); - } - - public Duration convertFromBigDecimal(BigDecimal value) { - return (value == null) ? null : DecimalUtils.toDuration(value); - } - - @Override - public void bind(DataBind bind, Duration value) throws SQLException { - if (value == null) { - bind.setNull(Types.BIGINT); - } else { - bind.setLong(value.getSeconds()); - } - } - - @Override - public Duration read(DataReader dataReader) throws SQLException { - Long value = dataReader.getLong(); - return (value == null) ? null : Duration.ofSeconds(value); - } - - @Override - public Object toJdbcType(Object value) { - if (value instanceof Long) return value; - return ((Duration)value).getSeconds(); - } - - @Override - public Duration toBeanType(Object value) { - if (value instanceof Duration) return (Duration) value; - return Duration.ofSeconds(BasicTypeConverter.toLong(value)); - } - - @Override - public Duration readData(DataInput dataInput) throws IOException { - if (!dataInput.readBoolean()) { - return null; - } else { - return convertFromBigDecimal(new BigDecimal(dataInput.readUTF())); - } - } - - @Override - public void writeData(DataOutput dataOutput, Duration value) throws IOException { - if (value == null) { - dataOutput.writeBoolean(false); - } else { - dataOutput.writeBoolean(true); - dataOutput.writeUTF(convertToBigDecimal(value).toString()); - } - } - - @Override - public String formatValue(Duration v) { - return v.toString(); - } - - @Override - public Duration parse(String value) { - return Duration.parse(value); - } - - @Override - public boolean isDateTimeCapable() { - return false; - } - - @Override - public Duration convertFromMillis(long systemTimeMillis) { - throw new TextException("Not Supported"); - } - - @Override - public Duration jsonRead(JsonParser ctx, JsonToken event) throws IOException { - return Duration.parse(ctx.getValueAsString()); - } - - @Override - public void jsonWrite(JsonGenerator ctx, String name, Duration value) throws IOException { - ctx.writeStringField(name, value.toString()); - } -} +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; +import java.time.Duration; + +/** + * ScalarType for java.time.Duration (with seconds precision). + */ +public class ScalarTypeDuration extends ScalarTypeBase { + + public ScalarTypeDuration() { + super(Duration.class, false, Types.BIGINT); + } + + protected ScalarTypeDuration(int jdbcType) { + super(Duration.class, false, jdbcType); + } + + public BigDecimal convertToBigDecimal(Duration value) { + return (value == null) ? null : DecimalUtils.toDecimal(value); + } + + public Duration convertFromBigDecimal(BigDecimal value) { + return (value == null) ? null : DecimalUtils.toDuration(value); + } + + @Override + public void bind(DataBind bind, Duration value) throws SQLException { + if (value == null) { + bind.setNull(Types.BIGINT); + } else { + bind.setLong(value.getSeconds()); + } + } + + @Override + public Duration read(DataReader dataReader) throws SQLException { + Long value = dataReader.getLong(); + return (value == null) ? null : Duration.ofSeconds(value); + } + + @Override + public Object toJdbcType(Object value) { + if (value instanceof Long) return value; + return ((Duration)value).getSeconds(); + } + + @Override + public Duration toBeanType(Object value) { + if (value instanceof Duration) return (Duration) value; + return Duration.ofSeconds(BasicTypeConverter.toLong(value)); + } + + @Override + public Duration readData(DataInput dataInput) throws IOException { + if (!dataInput.readBoolean()) { + return null; + } else { + return convertFromBigDecimal(new BigDecimal(dataInput.readUTF())); + } + } + + @Override + public void writeData(DataOutput dataOutput, Duration value) throws IOException { + if (value == null) { + dataOutput.writeBoolean(false); + } else { + dataOutput.writeBoolean(true); + dataOutput.writeUTF(convertToBigDecimal(value).toString()); + } + } + + @Override + public String formatValue(Duration v) { + return v.toString(); + } + + @Override + public Duration parse(String value) { + return Duration.parse(value); + } + + @Override + public boolean isDateTimeCapable() { + return false; + } + + @Override + public Duration convertFromMillis(long systemTimeMillis) { + throw new TextException("Not Supported"); + } + + @Override + public Duration jsonRead(JsonParser ctx, JsonToken event) throws IOException { + return Duration.parse(ctx.getValueAsString()); + } + + @Override + public void jsonWrite(JsonGenerator ctx, String name, Duration value) throws IOException { + ctx.writeStringField(name, value.toString()); + } +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeDurationWithNanos.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeDurationWithNanos.java index 1ff246375..42f05ffb8 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeDurationWithNanos.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeDurationWithNanos.java @@ -1,55 +1,55 @@ -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; -import java.time.Duration; - -/** - * ScalarType for java.time.Duration (with Nanos precision). - *

- * Stored in the DB as DECIMAL value. - *

- */ -public class ScalarTypeDurationWithNanos extends ScalarTypeDuration { - - public ScalarTypeDurationWithNanos() { - super(Types.DECIMAL); - } - - @Override - public void bind(DataBind bind, Duration value) throws SQLException { - if (value == null) { - bind.setNull(Types.DECIMAL); - } else { - bind.setBigDecimal(convertToBigDecimal(value)); - } - } - - @Override - public Duration read(DataReader dataReader) throws SQLException { - return convertFromBigDecimal(dataReader.getBigDecimal()); - } - - @Override - public Object toJdbcType(Object value) { - if (value instanceof BigDecimal) return value; - return convertToBigDecimal((Duration)value); - } - - @Override - public Duration toBeanType(Object value) { - if (value instanceof Duration) return (Duration) value; - return convertFromBigDecimal(BasicTypeConverter.toBigDecimal(value)); - } - -} +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; +import java.time.Duration; + +/** + * ScalarType for java.time.Duration (with Nanos precision). + *

+ * Stored in the DB as DECIMAL value. + *

+ */ +public class ScalarTypeDurationWithNanos extends ScalarTypeDuration { + + public ScalarTypeDurationWithNanos() { + super(Types.DECIMAL); + } + + @Override + public void bind(DataBind bind, Duration value) throws SQLException { + if (value == null) { + bind.setNull(Types.DECIMAL); + } else { + bind.setBigDecimal(convertToBigDecimal(value)); + } + } + + @Override + public Duration read(DataReader dataReader) throws SQLException { + return convertFromBigDecimal(dataReader.getBigDecimal()); + } + + @Override + public Object toJdbcType(Object value) { + if (value instanceof BigDecimal) return value; + return convertToBigDecimal((Duration)value); + } + + @Override + public Duration toBeanType(Object value) { + if (value instanceof Duration) return (Duration) value; + return convertFromBigDecimal(BasicTypeConverter.toBigDecimal(value)); + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeEncryptedWrapper.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeEncryptedWrapper.java index 041fd9471..49153679c 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeEncryptedWrapper.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeEncryptedWrapper.java @@ -1,144 +1,144 @@ -package com.avaje.ebeaninternal.server.type; - -import java.io.DataInput; -import java.io.DataOutput; -import java.io.IOException; -import java.sql.SQLException; - -import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.core.JsonToken; - -public class ScalarTypeEncryptedWrapper implements ScalarType { - - private final ScalarType wrapped; - - private final DataEncryptSupport dataEncryptSupport; - - private final ScalarTypeBytesBase byteArrayType; - - public ScalarTypeEncryptedWrapper(ScalarType wrapped, ScalarTypeBytesBase byteArrayType, DataEncryptSupport dataEncryptSupport) { - this.wrapped = wrapped; - this.byteArrayType = byteArrayType; - this.dataEncryptSupport = dataEncryptSupport; - } - - @Override - public boolean isMutable() { - return wrapped.isMutable(); - } - - @Override - public boolean isDirty(Object value) { - return wrapped.isDirty(value); - } - - @Override - public T readData(DataInput dataInput) throws IOException { - return wrapped.readData(dataInput); - } - - @Override - public void writeData(DataOutput dataOutput, T v) throws IOException { - wrapped.writeData(dataOutput, v); - } - - @Override - public T read(DataReader dataReader) throws SQLException { - - byte[] data = dataReader.getBytes(); - String formattedValue = dataEncryptSupport.decryptObject(data); - if (formattedValue == null) { - return null; - } - return wrapped.parse(formattedValue); - } - - private byte[] encrypt(T value) { - String formatValue = wrapped.formatValue(value); - return dataEncryptSupport.encryptObject(formatValue); - } - - @Override - public void bind(DataBind b, T value) throws SQLException { - - byte[] encryptedValue = encrypt(value); - byteArrayType.bind(b, encryptedValue); - } - - @Override - public int getJdbcType() { - return byteArrayType.getJdbcType(); - } - - @Override - public int getLength() { - return byteArrayType.getLength(); - } - - @Override - public Class getType() { - return wrapped.getType(); - } - - @Override - public boolean isDateTimeCapable() { - return wrapped.isDateTimeCapable(); - } - - @Override - public boolean isJdbcNative() { - return false; - } - - @Override - public void loadIgnore(DataReader dataReader) { - wrapped.loadIgnore(dataReader); - } - - @Override - @SuppressWarnings("unchecked") - public String format(Object v) { - return formatValue((T) v); - } - - @Override - public String formatValue(T v) { - return wrapped.formatValue(v); - } - - @Override - public T parse(String value) { - return wrapped.parse(value); - } - - @Override - public T convertFromMillis(long systemTimeMillis) { - return wrapped.convertFromMillis(systemTimeMillis); - } - - @Override - public T toBeanType(Object value) { - return wrapped.toBeanType(value); - } - - @Override - public Object toJdbcType(Object value) { - return wrapped.toJdbcType(value); - } - - @Override - public void accumulateScalarTypes(String propName, CtCompoundTypeScalarList list) { - wrapped.accumulateScalarTypes(propName, list); - } - - @Override - 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); - } -} +package com.avaje.ebeaninternal.server.type; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.sql.SQLException; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonToken; + +public class ScalarTypeEncryptedWrapper implements ScalarType { + + private final ScalarType wrapped; + + private final DataEncryptSupport dataEncryptSupport; + + private final ScalarTypeBytesBase byteArrayType; + + public ScalarTypeEncryptedWrapper(ScalarType wrapped, ScalarTypeBytesBase byteArrayType, DataEncryptSupport dataEncryptSupport) { + this.wrapped = wrapped; + this.byteArrayType = byteArrayType; + this.dataEncryptSupport = dataEncryptSupport; + } + + @Override + public boolean isMutable() { + return wrapped.isMutable(); + } + + @Override + public boolean isDirty(Object value) { + return wrapped.isDirty(value); + } + + @Override + public T readData(DataInput dataInput) throws IOException { + return wrapped.readData(dataInput); + } + + @Override + public void writeData(DataOutput dataOutput, T v) throws IOException { + wrapped.writeData(dataOutput, v); + } + + @Override + public T read(DataReader dataReader) throws SQLException { + + byte[] data = dataReader.getBytes(); + String formattedValue = dataEncryptSupport.decryptObject(data); + if (formattedValue == null) { + return null; + } + return wrapped.parse(formattedValue); + } + + private byte[] encrypt(T value) { + String formatValue = wrapped.formatValue(value); + return dataEncryptSupport.encryptObject(formatValue); + } + + @Override + public void bind(DataBind b, T value) throws SQLException { + + byte[] encryptedValue = encrypt(value); + byteArrayType.bind(b, encryptedValue); + } + + @Override + public int getJdbcType() { + return byteArrayType.getJdbcType(); + } + + @Override + public int getLength() { + return byteArrayType.getLength(); + } + + @Override + public Class getType() { + return wrapped.getType(); + } + + @Override + public boolean isDateTimeCapable() { + return wrapped.isDateTimeCapable(); + } + + @Override + public boolean isJdbcNative() { + return false; + } + + @Override + public void loadIgnore(DataReader dataReader) { + wrapped.loadIgnore(dataReader); + } + + @Override + @SuppressWarnings("unchecked") + public String format(Object v) { + return formatValue((T) v); + } + + @Override + public String formatValue(T v) { + return wrapped.formatValue(v); + } + + @Override + public T parse(String value) { + return wrapped.parse(value); + } + + @Override + public T convertFromMillis(long systemTimeMillis) { + return wrapped.convertFromMillis(systemTimeMillis); + } + + @Override + public T toBeanType(Object value) { + return wrapped.toBeanType(value); + } + + @Override + public Object toJdbcType(Object value) { + return wrapped.toJdbcType(value); + } + + @Override + public void accumulateScalarTypes(String propName, CtCompoundTypeScalarList list) { + wrapped.accumulateScalarTypes(propName, list); + } + + @Override + 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); + } +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeEnumStandard.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeEnumStandard.java index b8e7014b5..4180250bb 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeEnumStandard.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeEnumStandard.java @@ -1,265 +1,265 @@ -package com.avaje.ebeaninternal.server.type; - -import java.io.DataInput; -import java.io.DataOutput; -import java.io.IOException; -import java.sql.SQLException; -import java.sql.Types; -import java.util.EnumSet; - -import com.avaje.ebean.text.TextException; -import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.core.JsonToken; - -/** - * JPA standard based Enum scalar type. - *

- * Converts between bean Enum types to database string or integer columns. - *

- *

- * The limitation of this class is that it converts the Enum to either the ordinal or string value - * of the Enum. If you wish to convert the Enum to some other value then you should look at the - * Ebean specific @EnumMapping. - *

- */ -public class ScalarTypeEnumStandard { - - @SuppressWarnings({ "rawtypes", "unchecked" }) - public static class StringEnum extends EnumBase implements ScalarTypeEnum { - - private final int length; - - /** - * Create a ScalarTypeEnum. - */ - public StringEnum(Class enumType) { - super(enumType, false, Types.VARCHAR); - this.length = maxValueLength(enumType); - } - - /** - * Return the IN values for DB constraint construction. - */ - public String getConstraintInValues() { - - StringBuilder sb = new StringBuilder(); - - sb.append("("); - Object[] ea = enumType.getEnumConstants(); - for (int i = 0; i < ea.length; i++) { - Enum e = (Enum) ea[i]; - if (i > 0) { - sb.append(","); - } - sb.append("'").append(e.name()).append("'"); - } - sb.append(")"); - - return sb.toString(); - } - - private int maxValueLength(Class enumType) { - - int maxLen = 0; - - Object[] ea = enumType.getEnumConstants(); - for (int i = 0; i < ea.length; i++) { - Enum e = (Enum) ea[i]; - maxLen = Math.max(maxLen, e.toString().length()); - } - - return maxLen; - } - - public int getLength() { - return length; - } - - public void bind(DataBind b, Object value) throws SQLException { - if (value == null) { - b.setNull(Types.VARCHAR); - } else { - b.setString(format(value)); - } - } - - public Object read(DataReader dataReader) throws SQLException { - - String string = dataReader.getString(); - if (string == null) { - return null; - } else { - return parse(string); - } - } - - /** - * Convert the Boolean value to the db value. - */ - public Object toJdbcType(Object beanValue) { - if (beanValue == null) { - return null; - } - return beanValue.toString(); - } - - public Object toBeanType(Object dbValue) { - if (dbValue == null || dbValue instanceof Enum) { - return dbValue; - } - return Enum.valueOf(enumType, (String) dbValue); - } - - } - - @SuppressWarnings({ "rawtypes", "unchecked" }) - public static class OrdinalEnum extends EnumBase implements ScalarTypeEnum { - - private final Object[] enumArray; - - /** - * Create a ScalarTypeEnum. - */ - public OrdinalEnum(Class enumType) { - super(enumType, false, Types.INTEGER); - this.enumArray = EnumSet.allOf(enumType).toArray(); - } - - /** - * Return the IN values for DB constraint construction. - */ - public String getConstraintInValues() { - - StringBuilder sb = new StringBuilder(); - - sb.append("("); - for (int i = 0; i < enumArray.length; i++) { - Enum e = (Enum) enumArray[i]; - if (i > 0) { - sb.append(","); - } - sb.append(e.ordinal()); - } - sb.append(")"); - - return sb.toString(); - } - - public void bind(DataBind b, Object value) throws SQLException { - if (value == null) { - b.setNull(Types.INTEGER); - } else { - b.setInt(((Enum) value).ordinal()); - } - } - - public Object read(DataReader dataReader) throws SQLException { - - Integer ordinal = dataReader.getInt(); - if (ordinal == null) { - return null; - } else { - if (ordinal < 0 || ordinal >= enumArray.length) { - String m = "Unexpected ordinal [" + ordinal + "] out of range [" + enumArray.length + "]"; - throw new IllegalStateException(m); - } - return enumArray[ordinal]; - } - } - - /** - * Convert the Enum value to the db value. - */ - public Object toJdbcType(Object beanValue) { - if (beanValue == null) { - return null; - } - return ((Enum) beanValue).ordinal(); - } - - /** - * Convert the db value to the Enum value. - */ - public Object toBeanType(Object dbValue) { - if (dbValue == null || dbValue instanceof Enum) { - return dbValue; - } - - int ordinal = (Integer) dbValue; - if (ordinal < 0 || ordinal >= enumArray.length) { - String m = "Unexpected ordinal [" + ordinal + "] out of range [" + enumArray.length + "]"; - throw new IllegalStateException(m); - } - return enumArray[ordinal]; - } - - } - - @SuppressWarnings({ "rawtypes", "unchecked" }) - public abstract static class EnumBase extends ScalarTypeBase { - - protected final Class enumType; - - public EnumBase(Class type, boolean jdbcNative, int jdbcType) { - super(type, jdbcNative, jdbcType); - this.enumType = type; - } - - @Override - public String format(Object t) { - return ((Enum) t).name(); - } - - @Override - public String formatValue(Object t) { - return ((Enum) t).name(); - } - - @Override - public Object parse(String value) { - return Enum.valueOf(enumType, value); - } - - @Override - public Object convertFromMillis(long systemTimeMillis) { - throw new TextException("Not Supported"); - } - - @Override - public boolean isDateTimeCapable() { - return false; - } - - @Override - public Object jsonRead(JsonParser ctx, JsonToken event) throws IOException { - return parse(ctx.getValueAsString()); - } - - @Override - public void jsonWrite(JsonGenerator ctx, String name, Object value) throws IOException { - ctx.writeStringField(name, formatValue(value)); - } - - @Override - public Object readData(DataInput dataInput) throws IOException { - if (!dataInput.readBoolean()) { - return null; - } else { - String s = dataInput.readUTF(); - return parse(s); - } - } - - @Override - public void writeData(DataOutput dataOutput, Object v) throws IOException { - if (v == null) { - dataOutput.writeBoolean(false); - } else { - dataOutput.writeBoolean(true); - dataOutput.writeUTF(format(v)); - } - } - - } -} +package com.avaje.ebeaninternal.server.type; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.sql.SQLException; +import java.sql.Types; +import java.util.EnumSet; + +import com.avaje.ebean.text.TextException; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonToken; + +/** + * JPA standard based Enum scalar type. + *

+ * Converts between bean Enum types to database string or integer columns. + *

+ *

+ * The limitation of this class is that it converts the Enum to either the ordinal or string value + * of the Enum. If you wish to convert the Enum to some other value then you should look at the + * Ebean specific @EnumMapping. + *

+ */ +public class ScalarTypeEnumStandard { + + @SuppressWarnings({ "rawtypes", "unchecked" }) + public static class StringEnum extends EnumBase implements ScalarTypeEnum { + + private final int length; + + /** + * Create a ScalarTypeEnum. + */ + public StringEnum(Class enumType) { + super(enumType, false, Types.VARCHAR); + this.length = maxValueLength(enumType); + } + + /** + * Return the IN values for DB constraint construction. + */ + public String getConstraintInValues() { + + StringBuilder sb = new StringBuilder(); + + sb.append("("); + Object[] ea = enumType.getEnumConstants(); + for (int i = 0; i < ea.length; i++) { + Enum e = (Enum) ea[i]; + if (i > 0) { + sb.append(","); + } + sb.append("'").append(e.name()).append("'"); + } + sb.append(")"); + + return sb.toString(); + } + + private int maxValueLength(Class enumType) { + + int maxLen = 0; + + Object[] ea = enumType.getEnumConstants(); + for (int i = 0; i < ea.length; i++) { + Enum e = (Enum) ea[i]; + maxLen = Math.max(maxLen, e.toString().length()); + } + + return maxLen; + } + + public int getLength() { + return length; + } + + public void bind(DataBind b, Object value) throws SQLException { + if (value == null) { + b.setNull(Types.VARCHAR); + } else { + b.setString(format(value)); + } + } + + public Object read(DataReader dataReader) throws SQLException { + + String string = dataReader.getString(); + if (string == null) { + return null; + } else { + return parse(string); + } + } + + /** + * Convert the Boolean value to the db value. + */ + public Object toJdbcType(Object beanValue) { + if (beanValue == null) { + return null; + } + return beanValue.toString(); + } + + public Object toBeanType(Object dbValue) { + if (dbValue == null || dbValue instanceof Enum) { + return dbValue; + } + return Enum.valueOf(enumType, (String) dbValue); + } + + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + public static class OrdinalEnum extends EnumBase implements ScalarTypeEnum { + + private final Object[] enumArray; + + /** + * Create a ScalarTypeEnum. + */ + public OrdinalEnum(Class enumType) { + super(enumType, false, Types.INTEGER); + this.enumArray = EnumSet.allOf(enumType).toArray(); + } + + /** + * Return the IN values for DB constraint construction. + */ + public String getConstraintInValues() { + + StringBuilder sb = new StringBuilder(); + + sb.append("("); + for (int i = 0; i < enumArray.length; i++) { + Enum e = (Enum) enumArray[i]; + if (i > 0) { + sb.append(","); + } + sb.append(e.ordinal()); + } + sb.append(")"); + + return sb.toString(); + } + + public void bind(DataBind b, Object value) throws SQLException { + if (value == null) { + b.setNull(Types.INTEGER); + } else { + b.setInt(((Enum) value).ordinal()); + } + } + + public Object read(DataReader dataReader) throws SQLException { + + Integer ordinal = dataReader.getInt(); + if (ordinal == null) { + return null; + } else { + if (ordinal < 0 || ordinal >= enumArray.length) { + String m = "Unexpected ordinal [" + ordinal + "] out of range [" + enumArray.length + "]"; + throw new IllegalStateException(m); + } + return enumArray[ordinal]; + } + } + + /** + * Convert the Enum value to the db value. + */ + public Object toJdbcType(Object beanValue) { + if (beanValue == null) { + return null; + } + return ((Enum) beanValue).ordinal(); + } + + /** + * Convert the db value to the Enum value. + */ + public Object toBeanType(Object dbValue) { + if (dbValue == null || dbValue instanceof Enum) { + return dbValue; + } + + int ordinal = (Integer) dbValue; + if (ordinal < 0 || ordinal >= enumArray.length) { + String m = "Unexpected ordinal [" + ordinal + "] out of range [" + enumArray.length + "]"; + throw new IllegalStateException(m); + } + return enumArray[ordinal]; + } + + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + public abstract static class EnumBase extends ScalarTypeBase { + + protected final Class enumType; + + public EnumBase(Class type, boolean jdbcNative, int jdbcType) { + super(type, jdbcNative, jdbcType); + this.enumType = type; + } + + @Override + public String format(Object t) { + return ((Enum) t).name(); + } + + @Override + public String formatValue(Object t) { + return ((Enum) t).name(); + } + + @Override + public Object parse(String value) { + return Enum.valueOf(enumType, value); + } + + @Override + public Object convertFromMillis(long systemTimeMillis) { + throw new TextException("Not Supported"); + } + + @Override + public boolean isDateTimeCapable() { + return false; + } + + @Override + public Object jsonRead(JsonParser ctx, JsonToken event) throws IOException { + return parse(ctx.getValueAsString()); + } + + @Override + public void jsonWrite(JsonGenerator ctx, String name, Object value) throws IOException { + ctx.writeStringField(name, formatValue(value)); + } + + @Override + public Object readData(DataInput dataInput) throws IOException { + if (!dataInput.readBoolean()) { + return null; + } else { + String s = dataInput.readUTF(); + return parse(s); + } + } + + @Override + public void writeData(DataOutput dataOutput, Object v) throws IOException { + if (v == null) { + dataOutput.writeBoolean(false); + } else { + dataOutput.writeBoolean(true); + dataOutput.writeUTF(format(v)); + } + } + + } +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeEnumWithMapping.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeEnumWithMapping.java index a7f26f86e..cedb8e767 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeEnumWithMapping.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeEnumWithMapping.java @@ -1,85 +1,85 @@ -package com.avaje.ebeaninternal.server.type; - -import java.sql.SQLException; -import java.util.Iterator; - -/** - * Additional control over mapping to DB values. - */ -@SuppressWarnings({ "unchecked", "rawtypes" }) -public class ScalarTypeEnumWithMapping extends ScalarTypeEnumStandard.EnumBase implements ScalarType, ScalarTypeEnum { - - private final EnumToDbValueMap beanDbMap; - - private final int length; - - /** - * Create with an explicit mapping of bean to database values. - */ - public ScalarTypeEnumWithMapping(EnumToDbValueMap beanDbMap, Class enumType, int length) { - super(enumType, false, beanDbMap.getDbType()); - this.beanDbMap = beanDbMap; - this.length = length; - } - - /** - * Return the IN values for DB constraint construction. - */ - public String getConstraintInValues() { - - StringBuilder sb = new StringBuilder(); - - int i = 0; - - sb.append("("); - - Iterator it = beanDbMap.dbValues(); - while (it.hasNext()) { - Object dbValue = it.next(); - if (i++ > 0) { - sb.append(","); - } - if (!beanDbMap.isIntegerType()) { - sb.append("'"); - } - sb.append(dbValue.toString()); - if (!beanDbMap.isIntegerType()) { - sb.append("'"); - } - } - - sb.append(")"); - - return sb.toString(); - } - - /** - * Return the DB column length for storing the enum value. - *

- * This is for enum's mapped to strings. - *

- */ - public int getLength() { - return length; - } - - public void bind(DataBind b, Object value) throws SQLException { - beanDbMap.bind(b, value); - } - - public Object read(DataReader dataReader) throws SQLException { - return beanDbMap.read(dataReader); - } - - public Object toBeanType(Object dbValue) { - if (dbValue == null || dbValue instanceof Enum) { - return dbValue; - } - return beanDbMap.getBeanValue(dbValue); - } - - public Object toJdbcType(Object beanValue) { - return beanDbMap.getDbValue(beanValue); - } - -} +package com.avaje.ebeaninternal.server.type; + +import java.sql.SQLException; +import java.util.Iterator; + +/** + * Additional control over mapping to DB values. + */ +@SuppressWarnings({ "unchecked", "rawtypes" }) +public class ScalarTypeEnumWithMapping extends ScalarTypeEnumStandard.EnumBase implements ScalarType, ScalarTypeEnum { + + private final EnumToDbValueMap beanDbMap; + + private final int length; + + /** + * Create with an explicit mapping of bean to database values. + */ + public ScalarTypeEnumWithMapping(EnumToDbValueMap beanDbMap, Class enumType, int length) { + super(enumType, false, beanDbMap.getDbType()); + this.beanDbMap = beanDbMap; + this.length = length; + } + + /** + * Return the IN values for DB constraint construction. + */ + public String getConstraintInValues() { + + StringBuilder sb = new StringBuilder(); + + int i = 0; + + sb.append("("); + + Iterator it = beanDbMap.dbValues(); + while (it.hasNext()) { + Object dbValue = it.next(); + if (i++ > 0) { + sb.append(","); + } + if (!beanDbMap.isIntegerType()) { + sb.append("'"); + } + sb.append(dbValue.toString()); + if (!beanDbMap.isIntegerType()) { + sb.append("'"); + } + } + + sb.append(")"); + + return sb.toString(); + } + + /** + * Return the DB column length for storing the enum value. + *

+ * This is for enum's mapped to strings. + *

+ */ + public int getLength() { + return length; + } + + public void bind(DataBind b, Object value) throws SQLException { + beanDbMap.bind(b, value); + } + + public Object read(DataReader dataReader) throws SQLException { + return beanDbMap.read(dataReader); + } + + public Object toBeanType(Object dbValue) { + if (dbValue == null || dbValue instanceof Enum) { + return dbValue; + } + return beanDbMap.getBeanValue(dbValue); + } + + public Object toJdbcType(Object beanValue) { + return beanDbMap.getDbValue(beanValue); + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeFile.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeFile.java index 074f4ece1..ca6a6a6fe 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeFile.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeFile.java @@ -1,193 +1,193 @@ -package com.avaje.ebeaninternal.server.type; - -import com.avaje.ebean.text.TextException; -import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.core.JsonToken; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.*; -import java.sql.SQLException; -import java.sql.Types; - -/** - * ScalarType for streaming between a File and the database. - */ -public class ScalarTypeFile extends ScalarTypeBase { - - private static Logger logger = LoggerFactory.getLogger(ScalarTypeFile.class); - - private final String prefix; - - private final String suffix; - - private final File directory; - - private final int bufferSize; - - /** - * Construct with reasonable defaults of Blob and 8096 buffer size. - */ - public ScalarTypeFile() { - this(Types.BLOB, "db-", null, null, 8096); - } - - /** - * Create the ScalarTypeFile. - */ - public ScalarTypeFile(int jdbcType, String prefix, String suffix, File directory, int bufferSize) { - super(File.class, false, jdbcType); - this.prefix = prefix; - this.suffix = suffix; - this.directory = directory; - this.bufferSize = bufferSize; - } - - private InputStream getInputStream(File value) throws IOException { - FileInputStream fi = new FileInputStream(value); - return new BufferedInputStream(fi, bufferSize); - } - - private OutputStream getOutputStream(File value) throws IOException { - FileOutputStream fi = new FileOutputStream(value); - return new BufferedOutputStream(fi, bufferSize); - } - - @Override - public File read(DataReader dataReader) throws SQLException { - - InputStream is = dataReader.getBinaryStream(); - if (is == null) { - return null; - } - - try { - // stream from db into our temp file - File tempFile = File.createTempFile(prefix, suffix, directory); - OutputStream os = getOutputStream(tempFile); - pump(is, os); - - return tempFile; - - } catch (IOException e) { - throw new SQLException("Error reading db file inputStream", e); - } - } - - - @Override - public void bind(DataBind b, File value) throws SQLException { - if (value == null) { - b.setNull(jdbcType); - } else { - try { - // stream from our file to the db - InputStream fi = getInputStream(value); - b.setBlob(fi, value.length()); - } catch (IOException e) { - throw new SQLException("Error trying to set file inputStream", e); - } - } - } - - @Override - public Object toJdbcType(Object value) { - return value; - } - - @Override - public File toBeanType(Object value) { - return (File) value; - } - - @Override - public void jsonWrite(JsonGenerator ctx, String name, File value) throws IOException { - ctx.writeFieldName(name); - InputStream is = getInputStream(value); - ctx.writeBinary(is, (int) value.length()); - } - - @Override - public File jsonRead(JsonParser ctx, JsonToken event) throws IOException { - File tempFile = File.createTempFile(prefix, suffix, directory); - OutputStream os = getOutputStream(tempFile); - ctx.readBinaryValue(os); - os.flush(); - os.close(); - return tempFile; - } - - @Override - public String formatValue(File file) { - throw new TextException("Not supported"); - } - - @Override - public File parse(String value) { - throw new TextException("Not supported"); - } - - @Override - public File convertFromMillis(long systemTimeMillis) { - throw new TextException("Not supported"); - } - - @Override - public boolean isDateTimeCapable() { - return false; - } - - @Override - public File readData(DataInput dataInput) throws IOException { - // skip reading large file - return null; - } - - public void writeData(DataOutput dataOutput, File file) throws IOException { - // skip writing large file - } - - /** - * Helper method to pump bytes from input to output. - */ - public long pump(InputStream is, OutputStream out) throws IOException { - - long totalBytes = 0; - InputStream input = null; - OutputStream output = null; - - try { - input = new BufferedInputStream(is, bufferSize); - output = new BufferedOutputStream(out, bufferSize); - - byte[] buffer = new byte[bufferSize]; - int length; - while (((length = input.read(buffer)) > 0)) { - output.write(buffer, 0, length); - totalBytes += length; - } - - output.flush(); - - return totalBytes; - - } finally { - if (output != null) { - try { - output.close(); - } catch (IOException e) { - logger.error("Error when closing outputstream", e); - } - } - if (input != null) { - try { - input.close(); - } catch (IOException e) { - logger.error("Error when closing inputstream ", e); - } - } - } - } - -} +package com.avaje.ebeaninternal.server.type; + +import com.avaje.ebean.text.TextException; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonToken; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.*; +import java.sql.SQLException; +import java.sql.Types; + +/** + * ScalarType for streaming between a File and the database. + */ +public class ScalarTypeFile extends ScalarTypeBase { + + private static Logger logger = LoggerFactory.getLogger(ScalarTypeFile.class); + + private final String prefix; + + private final String suffix; + + private final File directory; + + private final int bufferSize; + + /** + * Construct with reasonable defaults of Blob and 8096 buffer size. + */ + public ScalarTypeFile() { + this(Types.BLOB, "db-", null, null, 8096); + } + + /** + * Create the ScalarTypeFile. + */ + public ScalarTypeFile(int jdbcType, String prefix, String suffix, File directory, int bufferSize) { + super(File.class, false, jdbcType); + this.prefix = prefix; + this.suffix = suffix; + this.directory = directory; + this.bufferSize = bufferSize; + } + + private InputStream getInputStream(File value) throws IOException { + FileInputStream fi = new FileInputStream(value); + return new BufferedInputStream(fi, bufferSize); + } + + private OutputStream getOutputStream(File value) throws IOException { + FileOutputStream fi = new FileOutputStream(value); + return new BufferedOutputStream(fi, bufferSize); + } + + @Override + public File read(DataReader dataReader) throws SQLException { + + InputStream is = dataReader.getBinaryStream(); + if (is == null) { + return null; + } + + try { + // stream from db into our temp file + File tempFile = File.createTempFile(prefix, suffix, directory); + OutputStream os = getOutputStream(tempFile); + pump(is, os); + + return tempFile; + + } catch (IOException e) { + throw new SQLException("Error reading db file inputStream", e); + } + } + + + @Override + public void bind(DataBind b, File value) throws SQLException { + if (value == null) { + b.setNull(jdbcType); + } else { + try { + // stream from our file to the db + InputStream fi = getInputStream(value); + b.setBlob(fi, value.length()); + } catch (IOException e) { + throw new SQLException("Error trying to set file inputStream", e); + } + } + } + + @Override + public Object toJdbcType(Object value) { + return value; + } + + @Override + public File toBeanType(Object value) { + return (File) value; + } + + @Override + public void jsonWrite(JsonGenerator ctx, String name, File value) throws IOException { + ctx.writeFieldName(name); + InputStream is = getInputStream(value); + ctx.writeBinary(is, (int) value.length()); + } + + @Override + public File jsonRead(JsonParser ctx, JsonToken event) throws IOException { + File tempFile = File.createTempFile(prefix, suffix, directory); + OutputStream os = getOutputStream(tempFile); + ctx.readBinaryValue(os); + os.flush(); + os.close(); + return tempFile; + } + + @Override + public String formatValue(File file) { + throw new TextException("Not supported"); + } + + @Override + public File parse(String value) { + throw new TextException("Not supported"); + } + + @Override + public File convertFromMillis(long systemTimeMillis) { + throw new TextException("Not supported"); + } + + @Override + public boolean isDateTimeCapable() { + return false; + } + + @Override + public File readData(DataInput dataInput) throws IOException { + // skip reading large file + return null; + } + + public void writeData(DataOutput dataOutput, File file) throws IOException { + // skip writing large file + } + + /** + * Helper method to pump bytes from input to output. + */ + public long pump(InputStream is, OutputStream out) throws IOException { + + long totalBytes = 0; + InputStream input = null; + OutputStream output = null; + + try { + input = new BufferedInputStream(is, bufferSize); + output = new BufferedOutputStream(out, bufferSize); + + byte[] buffer = new byte[bufferSize]; + int length; + while (((length = input.read(buffer)) > 0)) { + output.write(buffer, 0, length); + totalBytes += length; + } + + output.flush(); + + return totalBytes; + + } finally { + if (output != null) { + try { + output.close(); + } catch (IOException e) { + logger.error("Error when closing outputstream", e); + } + } + if (input != null) { + try { + input.close(); + } catch (IOException e) { + logger.error("Error when closing inputstream ", e); + } + } + } + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeFloat.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeFloat.java index 96807eab4..06f4f3c1a 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeFloat.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeFloat.java @@ -1,97 +1,97 @@ -package com.avaje.ebeaninternal.server.type; - -import java.io.DataInput; -import java.io.DataOutput; -import java.io.IOException; -import java.sql.SQLException; -import java.sql.Types; - -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; - -/** - * ScalarType for Float and float. - */ -public class ScalarTypeFloat extends ScalarTypeBase { - - public ScalarTypeFloat() { - super(Float.class, true, Types.REAL); - } - - @Override - public void bind(DataBind b, Float value) throws SQLException { - if (value == null) { - b.setNull(Types.REAL); - } else { - b.setFloat(value.floatValue()); - } - } - - @Override - public Float read(DataReader dataReader) throws SQLException { - return dataReader.getFloat(); - } - - @Override - public Object toJdbcType(Object value) { - return BasicTypeConverter.toFloat(value); - } - - @Override - public Float toBeanType(Object value) { - return BasicTypeConverter.toFloat(value); - } - - @Override - public String formatValue(Float t) { - return t.toString(); - } - - @Override - public Float parse(String value) { - return Float.valueOf(value); - } - - @Override - public Float convertFromMillis(long systemTimeMillis) { - return Float.valueOf(systemTimeMillis); - } - - @Override - public boolean isDateTimeCapable() { - return true; - } - - @Override - public Float readData(DataInput dataInput) throws IOException { - if (!dataInput.readBoolean()) { - return null; - } else { - float val = dataInput.readFloat(); - return Float.valueOf(val); - } - } - - @Override - public void writeData(DataOutput dataOutput, Float value) throws IOException { - - if (value == null) { - dataOutput.writeBoolean(false); - } else { - dataOutput.writeBoolean(true); - dataOutput.writeFloat(value); - } - } - - @Override - public Float jsonRead(JsonParser ctx, JsonToken event) throws IOException { - return ctx.getFloatValue(); - } - - @Override - public void jsonWrite(JsonGenerator ctx, String name, Float value) throws IOException { - ctx.writeNumberField(name, (Float) value); - } -} +package com.avaje.ebeaninternal.server.type; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.sql.SQLException; +import java.sql.Types; + +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; + +/** + * ScalarType for Float and float. + */ +public class ScalarTypeFloat extends ScalarTypeBase { + + public ScalarTypeFloat() { + super(Float.class, true, Types.REAL); + } + + @Override + public void bind(DataBind b, Float value) throws SQLException { + if (value == null) { + b.setNull(Types.REAL); + } else { + b.setFloat(value.floatValue()); + } + } + + @Override + public Float read(DataReader dataReader) throws SQLException { + return dataReader.getFloat(); + } + + @Override + public Object toJdbcType(Object value) { + return BasicTypeConverter.toFloat(value); + } + + @Override + public Float toBeanType(Object value) { + return BasicTypeConverter.toFloat(value); + } + + @Override + public String formatValue(Float t) { + return t.toString(); + } + + @Override + public Float parse(String value) { + return Float.valueOf(value); + } + + @Override + public Float convertFromMillis(long systemTimeMillis) { + return Float.valueOf(systemTimeMillis); + } + + @Override + public boolean isDateTimeCapable() { + return true; + } + + @Override + public Float readData(DataInput dataInput) throws IOException { + if (!dataInput.readBoolean()) { + return null; + } else { + float val = dataInput.readFloat(); + return Float.valueOf(val); + } + } + + @Override + public void writeData(DataOutput dataOutput, Float value) throws IOException { + + if (value == null) { + dataOutput.writeBoolean(false); + } else { + dataOutput.writeBoolean(true); + dataOutput.writeFloat(value); + } + } + + @Override + public Float jsonRead(JsonParser ctx, JsonToken event) throws IOException { + return ctx.getFloatValue(); + } + + @Override + public void jsonWrite(JsonGenerator ctx, String name, Float value) throws IOException { + ctx.writeNumberField(name, (Float) value); + } +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeInetAddress.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeInetAddress.java index 3b83d3f17..a1ce30a37 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeInetAddress.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeInetAddress.java @@ -1,48 +1,48 @@ -package com.avaje.ebeaninternal.server.type; - -import java.net.InetAddress; - -import com.avaje.ebean.text.TextException; - -/** - * ScalarType for java.net.URI which converts to and from a VARCHAR database column. - */ -public class ScalarTypeInetAddress extends ScalarTypeBaseVarchar { - - public ScalarTypeInetAddress() { - super(InetAddress.class); - } - - @Override - public int getLength() { - return 50; - } - - @Override - public InetAddress convertFromDbString(String dbValue) { - try { - return parse(dbValue); - } catch (IllegalArgumentException e) { - throw new RuntimeException("Error with InetAddresses [" + dbValue + "] " + e); - } - } - - @Override - public String convertToDbString(InetAddress beanValue) { - return ConvertInetAddresses.toUriString(beanValue); - } - - @Override - public String formatValue(InetAddress v) { - return ConvertInetAddresses.toUriString(v); - } - - @Override - public InetAddress parse(String value) { - try { - return ConvertInetAddresses.forUriString(value); - } catch (IllegalArgumentException e) { - throw new TextException("Error with InetAddresses [" + value + "] ", e); - } - } -} +package com.avaje.ebeaninternal.server.type; + +import java.net.InetAddress; + +import com.avaje.ebean.text.TextException; + +/** + * ScalarType for java.net.URI which converts to and from a VARCHAR database column. + */ +public class ScalarTypeInetAddress extends ScalarTypeBaseVarchar { + + public ScalarTypeInetAddress() { + super(InetAddress.class); + } + + @Override + public int getLength() { + return 50; + } + + @Override + public InetAddress convertFromDbString(String dbValue) { + try { + return parse(dbValue); + } catch (IllegalArgumentException e) { + throw new RuntimeException("Error with InetAddresses [" + dbValue + "] " + e); + } + } + + @Override + public String convertToDbString(InetAddress beanValue) { + return ConvertInetAddresses.toUriString(beanValue); + } + + @Override + public String formatValue(InetAddress v) { + return ConvertInetAddresses.toUriString(v); + } + + @Override + public InetAddress parse(String value) { + try { + return ConvertInetAddresses.forUriString(value); + } catch (IllegalArgumentException e) { + throw new TextException("Error with InetAddresses [" + value + "] ", e); + } + } +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeInstant.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeInstant.java index 00997022d..5e309a9c8 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeInstant.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeInstant.java @@ -1,59 +1,59 @@ -package com.avaje.ebeaninternal.server.type; - -import com.avaje.ebean.config.JsonConfig; - -import java.sql.Timestamp; -import java.sql.Types; -import java.time.Instant; - -/** - * ScalarType for java.sql.Timestamp. - */ -public class ScalarTypeInstant extends ScalarTypeBaseDateTime { - - public ScalarTypeInstant(JsonConfig.DateTime mode) { - super(mode, Instant.class, true, Types.TIMESTAMP); - } - - @Override - protected String toJsonNanos(Instant value) { - return toJsonNanos(value.getEpochSecond(), value.getNano()); - } - - @Override - protected String toJsonISO8601(Instant value) { - return value.toString(); - } - - @Override - public long convertToMillis(Instant value) { - return value.toEpochMilli(); - } - - @Override - public Instant convertFromMillis(long systemTimeMillis) { - return Instant.ofEpochMilli(systemTimeMillis); - } - - @Override - public Instant convertFromTimestamp(Timestamp ts) { - return ts.toInstant(); - } - - @Override - public Timestamp convertToTimestamp(Instant t) { - return Timestamp.from(t); - } - - @Override - public Object toJdbcType(Object value) { - if (value instanceof Timestamp) return value; - return convertToTimestamp((Instant) value); - } - - @Override - public Instant toBeanType(Object value) { - if (value instanceof Instant) return (Instant) value; - return convertFromTimestamp((Timestamp) value); - } -} +package com.avaje.ebeaninternal.server.type; + +import com.avaje.ebean.config.JsonConfig; + +import java.sql.Timestamp; +import java.sql.Types; +import java.time.Instant; + +/** + * ScalarType for java.sql.Timestamp. + */ +public class ScalarTypeInstant extends ScalarTypeBaseDateTime { + + public ScalarTypeInstant(JsonConfig.DateTime mode) { + super(mode, Instant.class, true, Types.TIMESTAMP); + } + + @Override + protected String toJsonNanos(Instant value) { + return toJsonNanos(value.getEpochSecond(), value.getNano()); + } + + @Override + protected String toJsonISO8601(Instant value) { + return value.toString(); + } + + @Override + public long convertToMillis(Instant value) { + return value.toEpochMilli(); + } + + @Override + public Instant convertFromMillis(long systemTimeMillis) { + return Instant.ofEpochMilli(systemTimeMillis); + } + + @Override + public Instant convertFromTimestamp(Timestamp ts) { + return ts.toInstant(); + } + + @Override + public Timestamp convertToTimestamp(Instant t) { + return Timestamp.from(t); + } + + @Override + public Object toJdbcType(Object value) { + if (value instanceof Timestamp) return value; + return convertToTimestamp((Instant) value); + } + + @Override + public Instant toBeanType(Object value) { + if (value instanceof Instant) return (Instant) value; + return convertFromTimestamp((Timestamp) value); + } +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeInteger.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeInteger.java index 3199fb609..40d851867 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeInteger.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeInteger.java @@ -1,96 +1,96 @@ -package com.avaje.ebeaninternal.server.type; - -import java.io.DataInput; -import java.io.DataOutput; -import java.io.IOException; -import java.sql.SQLException; -import java.sql.Types; - -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; - -/** - * ScalarType for Integer and int. - */ -public class ScalarTypeInteger extends ScalarTypeBase { - - public ScalarTypeInteger() { - super(Integer.class, true, Types.INTEGER); - } - - @Override - public void bind(DataBind b, Integer value) throws SQLException { - if (value == null) { - b.setNull(Types.INTEGER); - } else { - b.setInt(value.intValue()); - } - } - - @Override - public Integer read(DataReader dataReader) throws SQLException { - return dataReader.getInt(); - } - - @Override - public Integer readData(DataInput dataInput) throws IOException { - if (!dataInput.readBoolean()) { - return null; - } else { - return Integer.valueOf(dataInput.readInt()); - } - } - - @Override - public void writeData(DataOutput dataOutput, Integer value) throws IOException { - if (value == null) { - dataOutput.writeBoolean(false); - } else { - dataOutput.writeBoolean(true); - dataOutput.writeInt(value); - } - } - - @Override - public Object toJdbcType(Object value) { - return BasicTypeConverter.toInteger(value); - } - - @Override - public Integer toBeanType(Object value) { - return BasicTypeConverter.toInteger(value); - } - - @Override - public String formatValue(Integer v) { - return v.toString(); - } - - @Override - public Integer parse(String value) { - return Integer.valueOf(value); - } - - @Override - public Integer convertFromMillis(long systemTimeMillis) { - throw new TextException("Not Supported"); - } - - @Override - public boolean isDateTimeCapable() { - return false; - } - - @Override - public Integer jsonRead(JsonParser ctx, JsonToken event) throws IOException { - return ctx.getIntValue(); - } - - @Override - public void jsonWrite(JsonGenerator ctx, String name, Integer value) throws IOException { - ctx.writeNumberField(name, value); - } -} +package com.avaje.ebeaninternal.server.type; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.sql.SQLException; +import java.sql.Types; + +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; + +/** + * ScalarType for Integer and int. + */ +public class ScalarTypeInteger extends ScalarTypeBase { + + public ScalarTypeInteger() { + super(Integer.class, true, Types.INTEGER); + } + + @Override + public void bind(DataBind b, Integer value) throws SQLException { + if (value == null) { + b.setNull(Types.INTEGER); + } else { + b.setInt(value.intValue()); + } + } + + @Override + public Integer read(DataReader dataReader) throws SQLException { + return dataReader.getInt(); + } + + @Override + public Integer readData(DataInput dataInput) throws IOException { + if (!dataInput.readBoolean()) { + return null; + } else { + return Integer.valueOf(dataInput.readInt()); + } + } + + @Override + public void writeData(DataOutput dataOutput, Integer value) throws IOException { + if (value == null) { + dataOutput.writeBoolean(false); + } else { + dataOutput.writeBoolean(true); + dataOutput.writeInt(value); + } + } + + @Override + public Object toJdbcType(Object value) { + return BasicTypeConverter.toInteger(value); + } + + @Override + public Integer toBeanType(Object value) { + return BasicTypeConverter.toInteger(value); + } + + @Override + public String formatValue(Integer v) { + return v.toString(); + } + + @Override + public Integer parse(String value) { + return Integer.valueOf(value); + } + + @Override + public Integer convertFromMillis(long systemTimeMillis) { + throw new TextException("Not Supported"); + } + + @Override + public boolean isDateTimeCapable() { + return false; + } + + @Override + public Integer jsonRead(JsonParser ctx, JsonToken event) throws IOException { + return ctx.getIntValue(); + } + + @Override + public void jsonWrite(JsonGenerator ctx, String name, Integer value) throws IOException { + ctx.writeNumberField(name, value); + } +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeJodaDateMidnight.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeJodaDateMidnight.java index 244b8c127..5a0c66c77 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeJodaDateMidnight.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeJodaDateMidnight.java @@ -1,52 +1,52 @@ -package com.avaje.ebeaninternal.server.type; - -import java.sql.Date; -import java.sql.Types; - -import org.joda.time.DateMidnight; - -import com.avaje.ebeaninternal.server.core.BasicTypeConverter; - -/** - * ScalarType for Joda DateMidnight. This maps to a JDBC Date. - */ -public class ScalarTypeJodaDateMidnight extends ScalarTypeBaseDate { - - /** - * Instantiates a new scalar type joda date midnight. - */ - public ScalarTypeJodaDateMidnight() { - super(DateMidnight.class, false, Types.DATE); - } - - @Override - public long convertToMillis(DateMidnight value) { - return value.getMillis(); - } - - @Override - public DateMidnight convertFromDate(Date ts) { - return new DateMidnight(ts.getTime()); - } - - @Override - public Date convertToDate(DateMidnight t) { - return new Date(t.getMillis()); - } - - @Override - public Object toJdbcType(Object value) { - if (value instanceof DateMidnight) { - return new Date(((DateMidnight) value).getMillis()); - } - return BasicTypeConverter.toDate(value); - } - - @Override - public DateMidnight toBeanType(Object value) { - if (value instanceof java.util.Date) { - return new DateMidnight(((java.util.Date) value).getTime()); - } - return (DateMidnight) value; - } -} +package com.avaje.ebeaninternal.server.type; + +import java.sql.Date; +import java.sql.Types; + +import org.joda.time.DateMidnight; + +import com.avaje.ebeaninternal.server.core.BasicTypeConverter; + +/** + * ScalarType for Joda DateMidnight. This maps to a JDBC Date. + */ +public class ScalarTypeJodaDateMidnight extends ScalarTypeBaseDate { + + /** + * Instantiates a new scalar type joda date midnight. + */ + public ScalarTypeJodaDateMidnight() { + super(DateMidnight.class, false, Types.DATE); + } + + @Override + public long convertToMillis(DateMidnight value) { + return value.getMillis(); + } + + @Override + public DateMidnight convertFromDate(Date ts) { + return new DateMidnight(ts.getTime()); + } + + @Override + public Date convertToDate(DateMidnight t) { + return new Date(t.getMillis()); + } + + @Override + public Object toJdbcType(Object value) { + if (value instanceof DateMidnight) { + return new Date(((DateMidnight) value).getMillis()); + } + return BasicTypeConverter.toDate(value); + } + + @Override + public DateMidnight toBeanType(Object value) { + if (value instanceof java.util.Date) { + return new DateMidnight(((java.util.Date) value).getTime()); + } + return (DateMidnight) value; + } +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeJodaDateTime.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeJodaDateTime.java index 9f0d5de86..797ce69f9 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeJodaDateTime.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeJodaDateTime.java @@ -1,67 +1,67 @@ -package com.avaje.ebeaninternal.server.type; - -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. - */ -public class ScalarTypeJodaDateTime extends ScalarTypeBaseDateTime { - - public ScalarTypeJodaDateTime(JsonConfig.DateTime mode) { - super(mode, DateTime.class, false, Types.TIMESTAMP); - } - - @Override - public long convertToMillis(DateTime value) { - return value.getMillis(); - } - - @Override - protected String toJsonNanos(DateTime value) { - return String.valueOf(value.toDateTime().getMillis()); - } - - @Override - protected String toJsonISO8601(DateTime value) { - return value.toString(); - } - - @Override - public DateTime convertFromMillis(long systemTimeMillis) { - return new DateTime(systemTimeMillis); - } - - @Override - public DateTime convertFromTimestamp(Timestamp ts) { - return new DateTime(ts.getTime()); - } - - @Override - public Timestamp convertToTimestamp(DateTime t) { - return new Timestamp(t.getMillis()); - } - - @Override - public Object toJdbcType(Object value) { - if (value instanceof DateTime) { - return new Timestamp(((DateTime) value).getMillis()); - } - return BasicTypeConverter.toTimestamp(value); - } - - @Override - public DateTime toBeanType(Object value) { - if (value instanceof java.util.Date) { - return new DateTime(((java.util.Date) value).getTime()); - } - return (DateTime) value; - } - -} +package com.avaje.ebeaninternal.server.type; + +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. + */ +public class ScalarTypeJodaDateTime extends ScalarTypeBaseDateTime { + + public ScalarTypeJodaDateTime(JsonConfig.DateTime mode) { + super(mode, DateTime.class, false, Types.TIMESTAMP); + } + + @Override + public long convertToMillis(DateTime value) { + return value.getMillis(); + } + + @Override + protected String toJsonNanos(DateTime value) { + return String.valueOf(value.toDateTime().getMillis()); + } + + @Override + protected String toJsonISO8601(DateTime value) { + return value.toString(); + } + + @Override + public DateTime convertFromMillis(long systemTimeMillis) { + return new DateTime(systemTimeMillis); + } + + @Override + public DateTime convertFromTimestamp(Timestamp ts) { + return new DateTime(ts.getTime()); + } + + @Override + public Timestamp convertToTimestamp(DateTime t) { + return new Timestamp(t.getMillis()); + } + + @Override + public Object toJdbcType(Object value) { + if (value instanceof DateTime) { + return new Timestamp(((DateTime) value).getMillis()); + } + return BasicTypeConverter.toTimestamp(value); + } + + @Override + public DateTime toBeanType(Object value) { + if (value instanceof java.util.Date) { + return new DateTime(((java.util.Date) value).getTime()); + } + return (DateTime) value; + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeJodaLocalDate.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeJodaLocalDate.java index f06639ec4..30f39d4b9 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeJodaLocalDate.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeJodaLocalDate.java @@ -1,55 +1,55 @@ -package com.avaje.ebeaninternal.server.type; - -import java.sql.Date; -import java.sql.Types; - -import org.joda.time.LocalDate; - -import com.avaje.ebeaninternal.server.core.BasicTypeConverter; - -/** - * ScalarType for Joda LocalDate. This maps to a JDBC Date. - */ -public class ScalarTypeJodaLocalDate extends ScalarTypeBaseDate { - - public ScalarTypeJodaLocalDate() { - super(LocalDate.class, false, Types.DATE); - } - - @Override - public long convertToMillis(LocalDate value) { - return value.toDateMidnight().getMillis(); - } - - @Override - public LocalDate convertFromDate(Date ts) { - return new LocalDate(((java.util.Date) ts).getTime()); - } - - @Override - public Date convertToDate(LocalDate t) { - return new java.sql.Date(t.toDateMidnight().getMillis()); - } - - @Override - public Object toJdbcType(Object value) { - if (value instanceof LocalDate) { - return new java.sql.Date(((LocalDate) value).toDateMidnight().getMillis()); - } - return BasicTypeConverter.toDate(value); - } - - @Override - public LocalDate toBeanType(Object value) { - if (value instanceof java.util.Date) { - return new LocalDate(((java.util.Date) value).getTime()); - } - return (LocalDate) value; - } - - @Override - public LocalDate convertFromMillis(long systemTimeMillis) { - return new LocalDate(systemTimeMillis); - } - -} +package com.avaje.ebeaninternal.server.type; + +import java.sql.Date; +import java.sql.Types; + +import org.joda.time.LocalDate; + +import com.avaje.ebeaninternal.server.core.BasicTypeConverter; + +/** + * ScalarType for Joda LocalDate. This maps to a JDBC Date. + */ +public class ScalarTypeJodaLocalDate extends ScalarTypeBaseDate { + + public ScalarTypeJodaLocalDate() { + super(LocalDate.class, false, Types.DATE); + } + + @Override + public long convertToMillis(LocalDate value) { + return value.toDateMidnight().getMillis(); + } + + @Override + public LocalDate convertFromDate(Date ts) { + return new LocalDate(((java.util.Date) ts).getTime()); + } + + @Override + public Date convertToDate(LocalDate t) { + return new java.sql.Date(t.toDateMidnight().getMillis()); + } + + @Override + public Object toJdbcType(Object value) { + if (value instanceof LocalDate) { + return new java.sql.Date(((LocalDate) value).toDateMidnight().getMillis()); + } + return BasicTypeConverter.toDate(value); + } + + @Override + public LocalDate toBeanType(Object value) { + if (value instanceof java.util.Date) { + return new LocalDate(((java.util.Date) value).getTime()); + } + return (LocalDate) value; + } + + @Override + public LocalDate convertFromMillis(long systemTimeMillis) { + return new LocalDate(systemTimeMillis); + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeJodaLocalDateTime.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeJodaLocalDateTime.java index e0bc36ce3..cda901d66 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeJodaLocalDateTime.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeJodaLocalDateTime.java @@ -1,67 +1,67 @@ -package com.avaje.ebeaninternal.server.type; - -import java.sql.Timestamp; -import java.sql.Types; - -import com.avaje.ebean.config.JsonConfig; -import org.joda.time.LocalDateTime; - -import com.avaje.ebeaninternal.server.core.BasicTypeConverter; - -/** - * ScalarType for Joda LocalDateTime. This maps to a JDBC Timestamp. - */ -public class ScalarTypeJodaLocalDateTime extends ScalarTypeBaseDateTime { - - public ScalarTypeJodaLocalDateTime(JsonConfig.DateTime mode) { - super(mode, LocalDateTime.class, false, Types.TIMESTAMP); - } - - @Override - protected String toJsonNanos(LocalDateTime value) { - return String.valueOf(value.toDateTime().getMillis()); - } - - @Override - protected String toJsonISO8601(LocalDateTime value) { - return value.toString(); - } - - @Override - public long convertToMillis(LocalDateTime value) { - return value.toDateTime().getMillis(); - } - - - @Override - public LocalDateTime convertFromMillis(long systemTimeMillis) { - return new LocalDateTime(systemTimeMillis); - } - - @Override - public LocalDateTime convertFromTimestamp(Timestamp ts) { - return new LocalDateTime(ts.getTime()); - } - - @Override - public Timestamp convertToTimestamp(LocalDateTime t) { - return new Timestamp(t.toDateTime().getMillis()); - } - - @Override - public Object toJdbcType(Object value) { - if (value instanceof LocalDateTime) { - return new Timestamp(((LocalDateTime) value).toDateTime().getMillis()); - } - return BasicTypeConverter.toTimestamp(value); - } - - @Override - public LocalDateTime toBeanType(Object value) { - if (value instanceof java.util.Date) { - return new LocalDateTime(((java.util.Date) value).getTime()); - } - return (LocalDateTime) value; - } - -} +package com.avaje.ebeaninternal.server.type; + +import java.sql.Timestamp; +import java.sql.Types; + +import com.avaje.ebean.config.JsonConfig; +import org.joda.time.LocalDateTime; + +import com.avaje.ebeaninternal.server.core.BasicTypeConverter; + +/** + * ScalarType for Joda LocalDateTime. This maps to a JDBC Timestamp. + */ +public class ScalarTypeJodaLocalDateTime extends ScalarTypeBaseDateTime { + + public ScalarTypeJodaLocalDateTime(JsonConfig.DateTime mode) { + super(mode, LocalDateTime.class, false, Types.TIMESTAMP); + } + + @Override + protected String toJsonNanos(LocalDateTime value) { + return String.valueOf(value.toDateTime().getMillis()); + } + + @Override + protected String toJsonISO8601(LocalDateTime value) { + return value.toString(); + } + + @Override + public long convertToMillis(LocalDateTime value) { + return value.toDateTime().getMillis(); + } + + + @Override + public LocalDateTime convertFromMillis(long systemTimeMillis) { + return new LocalDateTime(systemTimeMillis); + } + + @Override + public LocalDateTime convertFromTimestamp(Timestamp ts) { + return new LocalDateTime(ts.getTime()); + } + + @Override + public Timestamp convertToTimestamp(LocalDateTime t) { + return new Timestamp(t.toDateTime().getMillis()); + } + + @Override + public Object toJdbcType(Object value) { + if (value instanceof LocalDateTime) { + return new Timestamp(((LocalDateTime) value).toDateTime().getMillis()); + } + return BasicTypeConverter.toTimestamp(value); + } + + @Override + public LocalDateTime toBeanType(Object value) { + if (value instanceof java.util.Date) { + return new LocalDateTime(((java.util.Date) value).getTime()); + } + return (LocalDateTime) value; + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeJodaLocalTime.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeJodaLocalTime.java index 3b5e6c97b..c3cd766c6 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeJodaLocalTime.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeJodaLocalTime.java @@ -1,120 +1,120 @@ -package com.avaje.ebeaninternal.server.type; - -import java.io.DataInput; -import java.io.DataOutput; -import java.io.IOException; -import java.sql.SQLException; -import java.sql.Time; -import java.sql.Types; - -import org.joda.time.DateTimeZone; -import org.joda.time.LocalTime; - -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; - -/** - * ScalarType for Joda LocalTime. This maps to a JDBC Time. - */ -public class ScalarTypeJodaLocalTime extends ScalarTypeBase { - - public ScalarTypeJodaLocalTime() { - super(LocalTime.class, false, Types.TIME); - } - - @Override - public void bind(DataBind b, LocalTime value) throws SQLException { - if (value == null) { - b.setNull(Types.TIME); - } else { - Time sqlTime = new Time(value.getMillisOfDay()); - b.setTime(sqlTime); - } - } - - @Override - public LocalTime read(DataReader dataReader) throws SQLException { - - Time sqlTime = dataReader.getTime(); - if (sqlTime == null) { - return null; - } else { - return new LocalTime(sqlTime, DateTimeZone.UTC); - } - } - - @Override - public Object toJdbcType(Object value) { - if (value instanceof LocalTime) { - return new Time(((LocalTime) value).getMillisOfDay()); - } - return BasicTypeConverter.toTime(value); - } - - @Override - public LocalTime toBeanType(Object value) { - if (value instanceof java.util.Date) { - return new LocalTime(value, DateTimeZone.UTC); - } - return (LocalTime) value; - } - - @Override - public String formatValue(LocalTime v) { - return v.toString(); - } - - @Override - public LocalTime parse(String value) { - return new LocalTime(value); - } - - @Override - public void jsonWrite(JsonGenerator ctx, String name, LocalTime value) throws IOException { - ctx.writeStringField(name, value.toString()); - } - - @Override - public LocalTime jsonRead(JsonParser ctx, JsonToken event) throws IOException { - if (JsonToken.VALUE_NUMBER_INT == event) { - long millis = ctx.getLongValue(); - return convertFromMillis(millis); - } else { - String string = ctx.getValueAsString(); - throw new RuntimeException("convert " + string); - } - } - - @Override - public LocalTime convertFromMillis(long systemTimeMillis) { - return new LocalTime(systemTimeMillis); - } - - @Override - public boolean isDateTimeCapable() { - return true; - } - - @Override - public LocalTime readData(DataInput dataInput) throws IOException { - if (!dataInput.readBoolean()) { - return null; - } else { - String val = dataInput.readUTF(); - return parse(val); - } - } - - @Override - public void writeData(DataOutput dataOutput, LocalTime value) throws IOException { - - if (value == null) { - dataOutput.writeBoolean(false); - } else { - dataOutput.writeBoolean(true); - dataOutput.writeUTF(format(value)); - } - } -} +package com.avaje.ebeaninternal.server.type; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.sql.SQLException; +import java.sql.Time; +import java.sql.Types; + +import org.joda.time.DateTimeZone; +import org.joda.time.LocalTime; + +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; + +/** + * ScalarType for Joda LocalTime. This maps to a JDBC Time. + */ +public class ScalarTypeJodaLocalTime extends ScalarTypeBase { + + public ScalarTypeJodaLocalTime() { + super(LocalTime.class, false, Types.TIME); + } + + @Override + public void bind(DataBind b, LocalTime value) throws SQLException { + if (value == null) { + b.setNull(Types.TIME); + } else { + Time sqlTime = new Time(value.getMillisOfDay()); + b.setTime(sqlTime); + } + } + + @Override + public LocalTime read(DataReader dataReader) throws SQLException { + + Time sqlTime = dataReader.getTime(); + if (sqlTime == null) { + return null; + } else { + return new LocalTime(sqlTime, DateTimeZone.UTC); + } + } + + @Override + public Object toJdbcType(Object value) { + if (value instanceof LocalTime) { + return new Time(((LocalTime) value).getMillisOfDay()); + } + return BasicTypeConverter.toTime(value); + } + + @Override + public LocalTime toBeanType(Object value) { + if (value instanceof java.util.Date) { + return new LocalTime(value, DateTimeZone.UTC); + } + return (LocalTime) value; + } + + @Override + public String formatValue(LocalTime v) { + return v.toString(); + } + + @Override + public LocalTime parse(String value) { + return new LocalTime(value); + } + + @Override + public void jsonWrite(JsonGenerator ctx, String name, LocalTime value) throws IOException { + ctx.writeStringField(name, value.toString()); + } + + @Override + public LocalTime jsonRead(JsonParser ctx, JsonToken event) throws IOException { + if (JsonToken.VALUE_NUMBER_INT == event) { + long millis = ctx.getLongValue(); + return convertFromMillis(millis); + } else { + String string = ctx.getValueAsString(); + throw new RuntimeException("convert " + string); + } + } + + @Override + public LocalTime convertFromMillis(long systemTimeMillis) { + return new LocalTime(systemTimeMillis); + } + + @Override + public boolean isDateTimeCapable() { + return true; + } + + @Override + public LocalTime readData(DataInput dataInput) throws IOException { + if (!dataInput.readBoolean()) { + return null; + } else { + String val = dataInput.readUTF(); + return parse(val); + } + } + + @Override + public void writeData(DataOutput dataOutput, LocalTime value) throws IOException { + + if (value == null) { + dataOutput.writeBoolean(false); + } else { + dataOutput.writeBoolean(true); + dataOutput.writeUTF(format(value)); + } + } +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLocalDate.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLocalDate.java index 5ffef6b3f..76f7fc33a 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLocalDate.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLocalDate.java @@ -1,59 +1,59 @@ -package com.avaje.ebeaninternal.server.type; - -import com.avaje.ebeaninternal.server.core.BasicTypeConverter; - -import java.sql.Date; -import java.sql.Timestamp; -import java.sql.Types; -import java.time.LocalDate; -import java.time.ZoneId; -import java.time.ZonedDateTime; - -/** - * ScalarType for java.time.LocalDate. This maps to a JDBC Date. - */ -public class ScalarTypeLocalDate extends ScalarTypeBaseDate { - - public ScalarTypeLocalDate() { - super(LocalDate.class, false, Types.DATE); - } - - @Override - public long convertToMillis(LocalDate value) { - ZonedDateTime zonedDateTime = value.atStartOfDay(ZoneId.systemDefault()); - return zonedDateTime.toInstant().toEpochMilli(); - } - - @Override - public LocalDate convertFromDate(Date ts) { - return ts.toLocalDate(); - } - - @Override - public Date convertToDate(LocalDate t) { - return Date.valueOf(t); - } - - @Override - public Object toJdbcType(Object value) { - if (value instanceof LocalDate) { - return Date.valueOf((LocalDate) value); - } - return BasicTypeConverter.toDate(value); - } - - @Override - public LocalDate toBeanType(Object value) { - if (value instanceof java.sql.Date) { - return ((java.sql.Date) value).toLocalDate(); - } - return (LocalDate) value; - } - - @Override - public LocalDate convertFromMillis(long systemTimeMillis) { - return new Timestamp(systemTimeMillis).toLocalDateTime().toLocalDate(); - } - - -} +package com.avaje.ebeaninternal.server.type; + +import com.avaje.ebeaninternal.server.core.BasicTypeConverter; + +import java.sql.Date; +import java.sql.Timestamp; +import java.sql.Types; +import java.time.LocalDate; +import java.time.ZoneId; +import java.time.ZonedDateTime; + +/** + * ScalarType for java.time.LocalDate. This maps to a JDBC Date. + */ +public class ScalarTypeLocalDate extends ScalarTypeBaseDate { + + public ScalarTypeLocalDate() { + super(LocalDate.class, false, Types.DATE); + } + + @Override + public long convertToMillis(LocalDate value) { + ZonedDateTime zonedDateTime = value.atStartOfDay(ZoneId.systemDefault()); + return zonedDateTime.toInstant().toEpochMilli(); + } + + @Override + public LocalDate convertFromDate(Date ts) { + return ts.toLocalDate(); + } + + @Override + public Date convertToDate(LocalDate t) { + return Date.valueOf(t); + } + + @Override + public Object toJdbcType(Object value) { + if (value instanceof LocalDate) { + return Date.valueOf((LocalDate) value); + } + return BasicTypeConverter.toDate(value); + } + + @Override + public LocalDate toBeanType(Object value) { + if (value instanceof java.sql.Date) { + return ((java.sql.Date) value).toLocalDate(); + } + return (LocalDate) value; + } + + @Override + public LocalDate convertFromMillis(long systemTimeMillis) { + return new Timestamp(systemTimeMillis).toLocalDateTime().toLocalDate(); + } + + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLocalDateTime.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLocalDateTime.java index b0452f95e..e745480ad 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLocalDateTime.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLocalDateTime.java @@ -1,64 +1,64 @@ -package com.avaje.ebeaninternal.server.type; - -import com.avaje.ebean.config.JsonConfig; - -import java.sql.Timestamp; -import java.sql.Types; -import java.time.LocalDateTime; -import java.time.ZoneId; -import java.time.ZoneOffset; -import java.time.ZonedDateTime; - -/** - * ScalarType for java.sql.Timestamp. - */ -public class ScalarTypeLocalDateTime extends ScalarTypeBaseDateTime { - - public ScalarTypeLocalDateTime(JsonConfig.DateTime mode) { - super(mode, LocalDateTime.class, true, Types.TIMESTAMP); - } - - @Override - public LocalDateTime convertFromMillis(long systemTimeMillis) { - return new Timestamp(systemTimeMillis).toLocalDateTime(); - } - - @Override - public long convertToMillis(LocalDateTime value) { - return value.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); - } - - @Override - protected String toJsonNanos(LocalDateTime value) { - return String.valueOf(convertToMillis(value)); - } - - @Override - protected String toJsonISO8601(LocalDateTime value) { - return value.atZone(ZoneId.systemDefault()).toInstant().toString(); - } - - @Override - public LocalDateTime convertFromTimestamp(Timestamp ts) { - return ts.toLocalDateTime(); - } - - @Override - public Timestamp convertToTimestamp(LocalDateTime t) { - - ZonedDateTime zonedDateTime = t.atZone(ZoneId.systemDefault()); - return Timestamp.from(zonedDateTime.toInstant()); - } - - @Override - public Object toJdbcType(Object value) { - if (value instanceof Timestamp) return value; - return convertToTimestamp((LocalDateTime)value); - } - - @Override - public LocalDateTime toBeanType(Object value) { - if (value instanceof LocalDateTime) return (LocalDateTime)value; - return convertFromTimestamp((Timestamp)value); - } -} +package com.avaje.ebeaninternal.server.type; + +import com.avaje.ebean.config.JsonConfig; + +import java.sql.Timestamp; +import java.sql.Types; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; + +/** + * ScalarType for java.sql.Timestamp. + */ +public class ScalarTypeLocalDateTime extends ScalarTypeBaseDateTime { + + public ScalarTypeLocalDateTime(JsonConfig.DateTime mode) { + super(mode, LocalDateTime.class, true, Types.TIMESTAMP); + } + + @Override + public LocalDateTime convertFromMillis(long systemTimeMillis) { + return new Timestamp(systemTimeMillis).toLocalDateTime(); + } + + @Override + public long convertToMillis(LocalDateTime value) { + return value.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); + } + + @Override + protected String toJsonNanos(LocalDateTime value) { + return String.valueOf(convertToMillis(value)); + } + + @Override + protected String toJsonISO8601(LocalDateTime value) { + return value.atZone(ZoneId.systemDefault()).toInstant().toString(); + } + + @Override + public LocalDateTime convertFromTimestamp(Timestamp ts) { + return ts.toLocalDateTime(); + } + + @Override + public Timestamp convertToTimestamp(LocalDateTime t) { + + ZonedDateTime zonedDateTime = t.atZone(ZoneId.systemDefault()); + return Timestamp.from(zonedDateTime.toInstant()); + } + + @Override + public Object toJdbcType(Object value) { + if (value instanceof Timestamp) return value; + return convertToTimestamp((LocalDateTime)value); + } + + @Override + public LocalDateTime toBeanType(Object value) { + if (value instanceof LocalDateTime) return (LocalDateTime)value; + return convertFromTimestamp((Timestamp)value); + } +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLocalTime.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLocalTime.java index 40a45f0a3..32971086e 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLocalTime.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLocalTime.java @@ -1,105 +1,105 @@ -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.sql.SQLException; -import java.sql.Time; -import java.sql.Types; -import java.time.LocalTime; - -/** - * ScalarType for java.time.LocalTime stored as JDBC Time. - */ -public class ScalarTypeLocalTime extends ScalarTypeBase { - - public ScalarTypeLocalTime() { - super(LocalTime.class, false, Types.TIME); - } - - protected ScalarTypeLocalTime(int jdbcTtype) { - super(LocalTime.class, false, jdbcTtype); - } - - @Override - public void bind(DataBind bind, LocalTime value) throws SQLException { - if (value == null) { - bind.setNull(Types.TIME); - } else { - bind.setTime(Time.valueOf(value)); - } - } - - @Override - public LocalTime read(DataReader dataReader) throws SQLException { - Time time = dataReader.getTime(); - return (time == null) ? null : time.toLocalTime(); - } - - @Override - public Object toJdbcType(Object value) { - if (value instanceof Time) return value; - return Time.valueOf((LocalTime)value); - } - - @Override - public LocalTime toBeanType(Object value) { - if (value instanceof LocalTime) return (LocalTime) value; - return BasicTypeConverter.toTime(value).toLocalTime(); - } - - @Override - public LocalTime readData(DataInput dataInput) throws IOException { - if (!dataInput.readBoolean()) { - return null; - } else { - return LocalTime.ofNanoOfDay(dataInput.readLong()); - } - } - - @Override - public void writeData(DataOutput dataOutput, LocalTime value) throws IOException { - if (value == null) { - dataOutput.writeBoolean(false); - } else { - dataOutput.writeBoolean(true); - dataOutput.writeLong(value.toNanoOfDay()); - } - } - - @Override - public String formatValue(LocalTime v) { - return v.toString(); - } - - @Override - public LocalTime parse(String value) { - return LocalTime.parse(value); - } - - @Override - public boolean isDateTimeCapable() { - return false; - } - - @Override - public LocalTime convertFromMillis(long systemTimeMillis) { - throw new TextException("Not Supported"); - } - - @Override - public LocalTime jsonRead(JsonParser ctx, JsonToken event) throws IOException { - return LocalTime.parse(ctx.getValueAsString()); - } - - @Override - public void jsonWrite(JsonGenerator ctx, String name, LocalTime value) throws IOException { - ctx.writeStringField(name, value.toString()); - } -} +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.sql.SQLException; +import java.sql.Time; +import java.sql.Types; +import java.time.LocalTime; + +/** + * ScalarType for java.time.LocalTime stored as JDBC Time. + */ +public class ScalarTypeLocalTime extends ScalarTypeBase { + + public ScalarTypeLocalTime() { + super(LocalTime.class, false, Types.TIME); + } + + protected ScalarTypeLocalTime(int jdbcTtype) { + super(LocalTime.class, false, jdbcTtype); + } + + @Override + public void bind(DataBind bind, LocalTime value) throws SQLException { + if (value == null) { + bind.setNull(Types.TIME); + } else { + bind.setTime(Time.valueOf(value)); + } + } + + @Override + public LocalTime read(DataReader dataReader) throws SQLException { + Time time = dataReader.getTime(); + return (time == null) ? null : time.toLocalTime(); + } + + @Override + public Object toJdbcType(Object value) { + if (value instanceof Time) return value; + return Time.valueOf((LocalTime)value); + } + + @Override + public LocalTime toBeanType(Object value) { + if (value instanceof LocalTime) return (LocalTime) value; + return BasicTypeConverter.toTime(value).toLocalTime(); + } + + @Override + public LocalTime readData(DataInput dataInput) throws IOException { + if (!dataInput.readBoolean()) { + return null; + } else { + return LocalTime.ofNanoOfDay(dataInput.readLong()); + } + } + + @Override + public void writeData(DataOutput dataOutput, LocalTime value) throws IOException { + if (value == null) { + dataOutput.writeBoolean(false); + } else { + dataOutput.writeBoolean(true); + dataOutput.writeLong(value.toNanoOfDay()); + } + } + + @Override + public String formatValue(LocalTime v) { + return v.toString(); + } + + @Override + public LocalTime parse(String value) { + return LocalTime.parse(value); + } + + @Override + public boolean isDateTimeCapable() { + return false; + } + + @Override + public LocalTime convertFromMillis(long systemTimeMillis) { + throw new TextException("Not Supported"); + } + + @Override + public LocalTime jsonRead(JsonParser ctx, JsonToken event) throws IOException { + return LocalTime.parse(ctx.getValueAsString()); + } + + @Override + public void jsonWrite(JsonGenerator ctx, String name, LocalTime value) throws IOException { + ctx.writeStringField(name, value.toString()); + } +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLocalTimeWithNanos.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLocalTimeWithNanos.java index d72f36222..deab02e3e 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLocalTimeWithNanos.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLocalTimeWithNanos.java @@ -1,45 +1,45 @@ -package com.avaje.ebeaninternal.server.type; - -import com.avaje.ebeaninternal.server.core.BasicTypeConverter; - -import java.sql.SQLException; -import java.sql.Types; -import java.time.LocalTime; - -/** - * ScalarType for java.time.LocalTime stored with Nanos as DB BIGINT type. - */ -public class ScalarTypeLocalTimeWithNanos extends ScalarTypeLocalTime { - - public ScalarTypeLocalTimeWithNanos() { - super(Types.BIGINT); - } - - @Override - public void bind(DataBind bind, LocalTime value) throws SQLException { - if (value == null) { - bind.setNull(Types.BIGINT); - } else { - bind.setLong(value.toNanoOfDay()); - } - } - - @Override - public LocalTime read(DataReader dataReader) throws SQLException { - Long value = dataReader.getLong(); - return (value == null) ? null : LocalTime.ofNanoOfDay(value); - } - - @Override - public Object toJdbcType(Object value) { - if (value instanceof Long) return value; - return ((LocalTime)value).toNanoOfDay(); - } - - @Override - public LocalTime toBeanType(Object value) { - if (value instanceof LocalTime) return (LocalTime) value; - return LocalTime.ofNanoOfDay(BasicTypeConverter.toLong(value)); - } - -} +package com.avaje.ebeaninternal.server.type; + +import com.avaje.ebeaninternal.server.core.BasicTypeConverter; + +import java.sql.SQLException; +import java.sql.Types; +import java.time.LocalTime; + +/** + * ScalarType for java.time.LocalTime stored with Nanos as DB BIGINT type. + */ +public class ScalarTypeLocalTimeWithNanos extends ScalarTypeLocalTime { + + public ScalarTypeLocalTimeWithNanos() { + super(Types.BIGINT); + } + + @Override + public void bind(DataBind bind, LocalTime value) throws SQLException { + if (value == null) { + bind.setNull(Types.BIGINT); + } else { + bind.setLong(value.toNanoOfDay()); + } + } + + @Override + public LocalTime read(DataReader dataReader) throws SQLException { + Long value = dataReader.getLong(); + return (value == null) ? null : LocalTime.ofNanoOfDay(value); + } + + @Override + public Object toJdbcType(Object value) { + if (value instanceof Long) return value; + return ((LocalTime)value).toNanoOfDay(); + } + + @Override + public LocalTime toBeanType(Object value) { + if (value instanceof LocalTime) return (LocalTime) value; + return LocalTime.ofNanoOfDay(BasicTypeConverter.toLong(value)); + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLocale.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLocale.java index 26c04c70d..62ec137ca 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLocale.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLocale.java @@ -1,65 +1,65 @@ -package com.avaje.ebeaninternal.server.type; - -import java.util.Locale; - -/** - * ScalarType for java.util.Currency which converts to and from a VARCHAR database column. - */ -public class ScalarTypeLocale extends ScalarTypeBaseVarchar { - - public ScalarTypeLocale() { - super(Locale.class); - } - - @Override - public int getLength() { - return 20; - } - - @Override - public Locale convertFromDbString(String dbValue) { - return parse(dbValue); - } - - @Override - public String convertToDbString(Locale beanValue) { - return ((Locale) beanValue).toString(); - } - - @Override - public String formatValue(Locale t) { - return t.toString(); - } - - @Override - public Locale parse(String value) { - - int pos1 = -1; - int pos2 = -1; - - for (int i = 0; i < value.length(); i++) { - char c = value.charAt(i); - if (c == '_') { - if (pos1 > -1) { - pos2 = i; - break; - } else { - pos1 = i; - } - } - } - if (pos1 == -1) { - return new Locale(value); - } - String language = value.substring(0, pos1); - if (pos2 == -1) { - String country = value.substring(pos1 + 1); - return new Locale(language, country); - } else { - String country = value.substring(pos1 + 1, pos2); - String variant = value.substring(pos2 + 1); - return new Locale(language, country, variant); - } - } - -} +package com.avaje.ebeaninternal.server.type; + +import java.util.Locale; + +/** + * ScalarType for java.util.Currency which converts to and from a VARCHAR database column. + */ +public class ScalarTypeLocale extends ScalarTypeBaseVarchar { + + public ScalarTypeLocale() { + super(Locale.class); + } + + @Override + public int getLength() { + return 20; + } + + @Override + public Locale convertFromDbString(String dbValue) { + return parse(dbValue); + } + + @Override + public String convertToDbString(Locale beanValue) { + return ((Locale) beanValue).toString(); + } + + @Override + public String formatValue(Locale t) { + return t.toString(); + } + + @Override + public Locale parse(String value) { + + int pos1 = -1; + int pos2 = -1; + + for (int i = 0; i < value.length(); i++) { + char c = value.charAt(i); + if (c == '_') { + if (pos1 > -1) { + pos2 = i; + break; + } else { + pos1 = i; + } + } + } + if (pos1 == -1) { + return new Locale(value); + } + String language = value.substring(0, pos1); + if (pos2 == -1) { + String country = value.substring(pos1 + 1); + return new Locale(language, country); + } else { + String country = value.substring(pos1 + 1, pos2); + String variant = value.substring(pos2 + 1); + return new Locale(language, country, variant); + } + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLong.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLong.java index 1bf541144..f14190777 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLong.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLong.java @@ -1,96 +1,96 @@ -package com.avaje.ebeaninternal.server.type; - -import java.io.DataInput; -import java.io.DataOutput; -import java.io.IOException; -import java.sql.SQLException; -import java.sql.Types; - -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; - -/** - * ScalarType for Long and long. - */ -public class ScalarTypeLong extends ScalarTypeBase { - - public ScalarTypeLong() { - super(Long.class, true, Types.BIGINT); - } - - @Override - public void bind(DataBind b, Long value) throws SQLException { - if (value == null) { - b.setNull(Types.BIGINT); - } else { - b.setLong(value.longValue()); - } - } - - @Override - public Long read(DataReader dataReader) throws SQLException { - return dataReader.getLong(); - } - - @Override - public Object toJdbcType(Object value) { - return BasicTypeConverter.toLong(value); - } - - @Override - public Long toBeanType(Object value) { - return BasicTypeConverter.toLong(value); - } - - @Override - public String formatValue(Long t) { - return t.toString(); - } - - @Override - public Long parse(String value) { - return Long.valueOf(value); - } - - @Override - public Long convertFromMillis(long systemTimeMillis) { - return systemTimeMillis; - } - - @Override - public boolean isDateTimeCapable() { - return true; - } - - @Override - public Long readData(DataInput dataInput) throws IOException { - if (!dataInput.readBoolean()) { - return null; - } else { - return dataInput.readLong(); - } - } - - @Override - public void writeData(DataOutput dataOutput, Long value) throws IOException { - - if (value == null) { - dataOutput.writeBoolean(false); - } else { - dataOutput.writeBoolean(true); - dataOutput.writeLong(value); - } - } - - @Override - public Long jsonRead(JsonParser ctx, JsonToken event) throws IOException { - return ctx.getLongValue(); - } - - @Override - public void jsonWrite(JsonGenerator ctx, String name, Long value) throws IOException { - ctx.writeNumberField(name, (Long) value); - } -} +package com.avaje.ebeaninternal.server.type; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.sql.SQLException; +import java.sql.Types; + +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; + +/** + * ScalarType for Long and long. + */ +public class ScalarTypeLong extends ScalarTypeBase { + + public ScalarTypeLong() { + super(Long.class, true, Types.BIGINT); + } + + @Override + public void bind(DataBind b, Long value) throws SQLException { + if (value == null) { + b.setNull(Types.BIGINT); + } else { + b.setLong(value.longValue()); + } + } + + @Override + public Long read(DataReader dataReader) throws SQLException { + return dataReader.getLong(); + } + + @Override + public Object toJdbcType(Object value) { + return BasicTypeConverter.toLong(value); + } + + @Override + public Long toBeanType(Object value) { + return BasicTypeConverter.toLong(value); + } + + @Override + public String formatValue(Long t) { + return t.toString(); + } + + @Override + public Long parse(String value) { + return Long.valueOf(value); + } + + @Override + public Long convertFromMillis(long systemTimeMillis) { + return systemTimeMillis; + } + + @Override + public boolean isDateTimeCapable() { + return true; + } + + @Override + public Long readData(DataInput dataInput) throws IOException { + if (!dataInput.readBoolean()) { + return null; + } else { + return dataInput.readLong(); + } + } + + @Override + public void writeData(DataOutput dataOutput, Long value) throws IOException { + + if (value == null) { + dataOutput.writeBoolean(false); + } else { + dataOutput.writeBoolean(true); + dataOutput.writeLong(value); + } + } + + @Override + public Long jsonRead(JsonParser ctx, JsonToken event) throws IOException { + return ctx.getLongValue(); + } + + @Override + public void jsonWrite(JsonGenerator ctx, String name, Long value) throws IOException { + ctx.writeNumberField(name, (Long) value); + } +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLongToTimestamp.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLongToTimestamp.java index 7a9b119d2..b6444a772 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLongToTimestamp.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLongToTimestamp.java @@ -1,12 +1,12 @@ -package com.avaje.ebeaninternal.server.type; - -import com.avaje.ebean.config.JsonConfig; - -import java.sql.Timestamp; - -public class ScalarTypeLongToTimestamp extends ScalarTypeWrapper { - - public ScalarTypeLongToTimestamp(JsonConfig.DateTime mode) { - super(Long.class, new ScalarTypeTimestamp(mode), new LongToTimestampConverter()); - } -} +package com.avaje.ebeaninternal.server.type; + +import com.avaje.ebean.config.JsonConfig; + +import java.sql.Timestamp; + +public class ScalarTypeLongToTimestamp extends ScalarTypeWrapper { + + public ScalarTypeLongToTimestamp(JsonConfig.DateTime mode) { + super(Long.class, new ScalarTypeTimestamp(mode), new LongToTimestampConverter()); + } +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLongVarchar.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLongVarchar.java index cfcecb7bc..ee56eefe4 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLongVarchar.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeLongVarchar.java @@ -1,19 +1,19 @@ -package com.avaje.ebeaninternal.server.type; - -import java.sql.SQLException; -import java.sql.Types; - -/** - * ScalarType for String. - */ -public class ScalarTypeLongVarchar extends ScalarTypeClob { - - public ScalarTypeLongVarchar() { - super(true, Types.LONGVARCHAR); - } - - @Override - public String read(DataReader dataReader) throws SQLException { - return dataReader.getStringFromStream(); - } -} +package com.avaje.ebeaninternal.server.type; + +import java.sql.SQLException; +import java.sql.Types; + +/** + * ScalarType for String. + */ +public class ScalarTypeLongVarchar extends ScalarTypeClob { + + public ScalarTypeLongVarchar() { + super(true, Types.LONGVARCHAR); + } + + @Override + public String read(DataReader dataReader) throws SQLException { + return dataReader.getStringFromStream(); + } +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeMathBigInteger.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeMathBigInteger.java index 521462b65..d32e1150b 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeMathBigInteger.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeMathBigInteger.java @@ -1,106 +1,106 @@ -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; - -/** - * ScalarType for java.math.BigInteger. - */ -public class ScalarTypeMathBigInteger extends ScalarTypeBase { - - public ScalarTypeMathBigInteger() { - super(BigInteger.class, false, Types.BIGINT); - } - - @Override - public void bind(DataBind b, BigInteger value) throws SQLException { - if (value == null) { - b.setNull(Types.BIGINT); - } else { - b.setLong(value.longValue()); - } - } - - @Override - public BigInteger read(DataReader dataReader) throws SQLException { - - Long l = dataReader.getLong(); - if (l == null) { - return null; - } - return new BigInteger(String.valueOf(l)); - } - - @Override - public Object toJdbcType(Object value) { - return BasicTypeConverter.toLong(value); - } - - @Override - public BigInteger toBeanType(Object value) { - return BasicTypeConverter.toMathBigInteger(value); - } - - @Override - public String formatValue(BigInteger v) { - return v.toString(); - } - - @Override - public BigInteger parse(String value) { - return new BigInteger(value); - } - - @Override - public BigInteger convertFromMillis(long systemTimeMillis) { - return BigInteger.valueOf(systemTimeMillis); - } - - @Override - public boolean isDateTimeCapable() { - return true; - } - - @Override - public BigInteger readData(DataInput dataInput) throws IOException { - if (!dataInput.readBoolean()) { - return null; - } else { - long val = dataInput.readLong(); - return BigInteger.valueOf(val); - } - } - - @Override - public void writeData(DataOutput dataOutput, BigInteger value) throws IOException { - - if (value == null) { - dataOutput.writeBoolean(false); - } else { - dataOutput.writeBoolean(true); - dataOutput.writeLong(value.longValue()); - } - } - - @Override - public BigInteger jsonRead(JsonParser ctx, JsonToken event) throws IOException { - return ctx.getDecimalValue().toBigInteger(); - } - - @Override - public void jsonWrite(JsonGenerator ctx, String name, BigInteger value) throws IOException { - ctx.writeNumberField(name, ((BigInteger) value).longValue()); - } - -} +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; + +/** + * ScalarType for java.math.BigInteger. + */ +public class ScalarTypeMathBigInteger extends ScalarTypeBase { + + public ScalarTypeMathBigInteger() { + super(BigInteger.class, false, Types.BIGINT); + } + + @Override + public void bind(DataBind b, BigInteger value) throws SQLException { + if (value == null) { + b.setNull(Types.BIGINT); + } else { + b.setLong(value.longValue()); + } + } + + @Override + public BigInteger read(DataReader dataReader) throws SQLException { + + Long l = dataReader.getLong(); + if (l == null) { + return null; + } + return new BigInteger(String.valueOf(l)); + } + + @Override + public Object toJdbcType(Object value) { + return BasicTypeConverter.toLong(value); + } + + @Override + public BigInteger toBeanType(Object value) { + return BasicTypeConverter.toMathBigInteger(value); + } + + @Override + public String formatValue(BigInteger v) { + return v.toString(); + } + + @Override + public BigInteger parse(String value) { + return new BigInteger(value); + } + + @Override + public BigInteger convertFromMillis(long systemTimeMillis) { + return BigInteger.valueOf(systemTimeMillis); + } + + @Override + public boolean isDateTimeCapable() { + return true; + } + + @Override + public BigInteger readData(DataInput dataInput) throws IOException { + if (!dataInput.readBoolean()) { + return null; + } else { + long val = dataInput.readLong(); + return BigInteger.valueOf(val); + } + } + + @Override + public void writeData(DataOutput dataOutput, BigInteger value) throws IOException { + + if (value == null) { + dataOutput.writeBoolean(false); + } else { + dataOutput.writeBoolean(true); + dataOutput.writeLong(value.longValue()); + } + } + + @Override + public BigInteger jsonRead(JsonParser ctx, JsonToken event) throws IOException { + return ctx.getDecimalValue().toBigInteger(); + } + + @Override + public void jsonWrite(JsonGenerator ctx, String name, BigInteger value) throws IOException { + ctx.writeNumberField(name, ((BigInteger) value).longValue()); + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeOffsetDateTime.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeOffsetDateTime.java index 7071f52cc..0f6dc1ec3 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeOffsetDateTime.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeOffsetDateTime.java @@ -1,61 +1,61 @@ -package com.avaje.ebeaninternal.server.type; - -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; - -/** - * ScalarType for java.sql.Timestamp. - */ -public class ScalarTypeOffsetDateTime extends ScalarTypeBaseDateTime { - - public ScalarTypeOffsetDateTime(JsonConfig.DateTime mode) { - super(mode, OffsetDateTime.class, true, Types.TIMESTAMP); - } - - @Override - protected String toJsonNanos(OffsetDateTime value) { - return toJsonNanos(value.toEpochSecond(), value.getNano()); - } - - @Override - protected String toJsonISO8601(OffsetDateTime value) { - return value.toInstant().toString(); - } - - @Override - public long convertToMillis(OffsetDateTime value) { - return value.toInstant().toEpochMilli(); - } - - @Override - public OffsetDateTime convertFromMillis(long systemTimeMillis) { - return OffsetDateTime.ofInstant(Instant.ofEpochMilli(systemTimeMillis), ZoneId.systemDefault()); - } - - @Override - public OffsetDateTime convertFromTimestamp(Timestamp ts) { - return OffsetDateTime.ofInstant(ts.toInstant(), ZoneId.systemDefault()); - } - - @Override - public Timestamp convertToTimestamp(OffsetDateTime t) { - return Timestamp.from(t.toInstant()); - } - - @Override - public Object toJdbcType(Object value) { - if (value instanceof Timestamp) return value; - return convertToTimestamp((OffsetDateTime)value); - } - - @Override - public OffsetDateTime toBeanType(Object value) { - if (value instanceof OffsetDateTime) return (OffsetDateTime) value; - return convertFromTimestamp((Timestamp)value); - } -} +package com.avaje.ebeaninternal.server.type; + +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; + +/** + * ScalarType for java.sql.Timestamp. + */ +public class ScalarTypeOffsetDateTime extends ScalarTypeBaseDateTime { + + public ScalarTypeOffsetDateTime(JsonConfig.DateTime mode) { + super(mode, OffsetDateTime.class, true, Types.TIMESTAMP); + } + + @Override + protected String toJsonNanos(OffsetDateTime value) { + return toJsonNanos(value.toEpochSecond(), value.getNano()); + } + + @Override + protected String toJsonISO8601(OffsetDateTime value) { + return value.toInstant().toString(); + } + + @Override + public long convertToMillis(OffsetDateTime value) { + return value.toInstant().toEpochMilli(); + } + + @Override + public OffsetDateTime convertFromMillis(long systemTimeMillis) { + return OffsetDateTime.ofInstant(Instant.ofEpochMilli(systemTimeMillis), ZoneId.systemDefault()); + } + + @Override + public OffsetDateTime convertFromTimestamp(Timestamp ts) { + return OffsetDateTime.ofInstant(ts.toInstant(), ZoneId.systemDefault()); + } + + @Override + public Timestamp convertToTimestamp(OffsetDateTime t) { + return Timestamp.from(t.toInstant()); + } + + @Override + public Object toJdbcType(Object value) { + if (value instanceof Timestamp) return value; + return convertToTimestamp((OffsetDateTime)value); + } + + @Override + public OffsetDateTime toBeanType(Object value) { + if (value instanceof OffsetDateTime) return (OffsetDateTime) value; + return convertFromTimestamp((Timestamp)value); + } +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeOffsetTime.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeOffsetTime.java index b36d8c553..9a7eb403a 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeOffsetTime.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeOffsetTime.java @@ -1,39 +1,39 @@ -package com.avaje.ebeaninternal.server.type; - -import java.time.OffsetTime; - -/** - * ScalarType for java.time.OffsetTime stored as DB VARCHAR - */ -public class ScalarTypeOffsetTime extends ScalarTypeBaseVarchar { - - public ScalarTypeOffsetTime() { - super(OffsetTime.class); - } - - @Override - public int getLength() { - return 25; - } - - @Override - public String formatValue(OffsetTime v) { - return v.toString(); - } - - @Override - public OffsetTime parse(String value) { - return OffsetTime.parse(value); - } - - @Override - public OffsetTime convertFromDbString(String dbValue) { - return OffsetTime.parse(dbValue); - } - - @Override - public String convertToDbString(OffsetTime beanValue) { - return beanValue.toString(); - } - -} +package com.avaje.ebeaninternal.server.type; + +import java.time.OffsetTime; + +/** + * ScalarType for java.time.OffsetTime stored as DB VARCHAR + */ +public class ScalarTypeOffsetTime extends ScalarTypeBaseVarchar { + + public ScalarTypeOffsetTime() { + super(OffsetTime.class); + } + + @Override + public int getLength() { + return 25; + } + + @Override + public String formatValue(OffsetTime v) { + return v.toString(); + } + + @Override + public OffsetTime parse(String value) { + return OffsetTime.parse(value); + } + + @Override + public OffsetTime convertFromDbString(String dbValue) { + return OffsetTime.parse(dbValue); + } + + @Override + public String convertToDbString(OffsetTime beanValue) { + return beanValue.toString(); + } + +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeShort.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeShort.java index 9c9871251..43f7a7b3f 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeShort.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeShort.java @@ -1,97 +1,97 @@ -package com.avaje.ebeaninternal.server.type; - -import java.io.DataInput; -import java.io.DataOutput; -import java.io.IOException; -import java.sql.SQLException; -import java.sql.Types; - -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; - -/** - * ScalarType for Short and short. - */ -public class ScalarTypeShort extends ScalarTypeBase { - - public ScalarTypeShort() { - super(Short.class, true, Types.SMALLINT); - } - - @Override - public void bind(DataBind b, Short value) throws SQLException { - if (value == null) { - b.setNull(Types.SMALLINT); - } else { - b.setShort(value.shortValue()); - } - } - - @Override - public Short read(DataReader dataReader) throws SQLException { - return dataReader.getShort(); - } - - @Override - public Object toJdbcType(Object value) { - return BasicTypeConverter.toShort(value); - } - - @Override - public Short toBeanType(Object value) { - return BasicTypeConverter.toShort(value); - } - - @Override - public String formatValue(Short v) { - return v.toString(); - } - - @Override - public Short parse(String value) { - return Short.valueOf(value); - } - - @Override - public Short convertFromMillis(long systemTimeMillis) { - throw new TextException("Not Supported"); - } - - @Override - public boolean isDateTimeCapable() { - return false; - } - - @Override - public Short readData(DataInput dataInput) throws IOException { - if (!dataInput.readBoolean()) { - return null; - } else { - return dataInput.readShort(); - } - } - - @Override - public void writeData(DataOutput dataOutput, Short value) throws IOException { - - if (value == null) { - dataOutput.writeBoolean(false); - } else { - dataOutput.writeBoolean(true); - dataOutput.writeShort(value); - } - } - - @Override - public Short jsonRead(JsonParser ctx, JsonToken event) throws IOException { - return ctx.getShortValue(); - } - - @Override - public void jsonWrite(JsonGenerator ctx, String name, Short value) throws IOException { - ctx.writeNumberField(name, (Short) value); - } -} +package com.avaje.ebeaninternal.server.type; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.sql.SQLException; +import java.sql.Types; + +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; + +/** + * ScalarType for Short and short. + */ +public class ScalarTypeShort extends ScalarTypeBase { + + public ScalarTypeShort() { + super(Short.class, true, Types.SMALLINT); + } + + @Override + public void bind(DataBind b, Short value) throws SQLException { + if (value == null) { + b.setNull(Types.SMALLINT); + } else { + b.setShort(value.shortValue()); + } + } + + @Override + public Short read(DataReader dataReader) throws SQLException { + return dataReader.getShort(); + } + + @Override + public Object toJdbcType(Object value) { + return BasicTypeConverter.toShort(value); + } + + @Override + public Short toBeanType(Object value) { + return BasicTypeConverter.toShort(value); + } + + @Override + public String formatValue(Short v) { + return v.toString(); + } + + @Override + public Short parse(String value) { + return Short.valueOf(value); + } + + @Override + public Short convertFromMillis(long systemTimeMillis) { + throw new TextException("Not Supported"); + } + + @Override + public boolean isDateTimeCapable() { + return false; + } + + @Override + public Short readData(DataInput dataInput) throws IOException { + if (!dataInput.readBoolean()) { + return null; + } else { + return dataInput.readShort(); + } + } + + @Override + public void writeData(DataOutput dataOutput, Short value) throws IOException { + + if (value == null) { + dataOutput.writeBoolean(false); + } else { + dataOutput.writeBoolean(true); + dataOutput.writeShort(value); + } + } + + @Override + public Short jsonRead(JsonParser ctx, JsonToken event) throws IOException { + return ctx.getShortValue(); + } + + @Override + public void jsonWrite(JsonGenerator ctx, String name, Short value) throws IOException { + ctx.writeNumberField(name, (Short) value); + } +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeString.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeString.java index 9584d26a7..3608c14bd 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeString.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeString.java @@ -1,96 +1,96 @@ -package com.avaje.ebeaninternal.server.type; - -import java.io.DataInput; -import java.io.DataOutput; -import java.io.IOException; -import java.sql.SQLException; -import java.sql.Types; - -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; - -/** - * ScalarType for String. - */ -public class ScalarTypeString extends ScalarTypeBase { - - public ScalarTypeString() { - super(String.class, true, Types.VARCHAR); - } - - @Override - public void bind(DataBind b, String value) throws SQLException { - if (value == null) { - b.setNull(Types.VARCHAR); - } else { - b.setString(value); - } - } - - @Override - public String read(DataReader dataReader) throws SQLException { - return dataReader.getString(); - } - - @Override - public Object toJdbcType(Object value) { - return BasicTypeConverter.toString(value); - } - - @Override - public String toBeanType(Object value) { - return BasicTypeConverter.toString(value); - } - - @Override - public String formatValue(String t) { - return t; - } - - @Override - public String parse(String value) { - return value; - } - - @Override - public String convertFromMillis(long systemTimeMillis) { - return String.valueOf(systemTimeMillis); - } - - @Override - public boolean isDateTimeCapable() { - return true; - } - - @Override - public String readData(DataInput dataInput) throws IOException { - if (!dataInput.readBoolean()) { - return null; - } else { - return dataInput.readUTF(); - } - } - - @Override - public void writeData(DataOutput dataOutput, String value) throws IOException { - - if (value == null) { - dataOutput.writeBoolean(false); - } else { - dataOutput.writeBoolean(true); - dataOutput.writeUTF(value); - } - } - - @Override - public String jsonRead(JsonParser ctx, JsonToken event) throws IOException { - return ctx.getValueAsString(); - } - - @Override - public void jsonWrite(JsonGenerator ctx, String name, String value) throws IOException { - ctx.writeStringField(name, (String) value); - } -} +package com.avaje.ebeaninternal.server.type; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.sql.SQLException; +import java.sql.Types; + +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; + +/** + * ScalarType for String. + */ +public class ScalarTypeString extends ScalarTypeBase { + + public ScalarTypeString() { + super(String.class, true, Types.VARCHAR); + } + + @Override + public void bind(DataBind b, String value) throws SQLException { + if (value == null) { + b.setNull(Types.VARCHAR); + } else { + b.setString(value); + } + } + + @Override + public String read(DataReader dataReader) throws SQLException { + return dataReader.getString(); + } + + @Override + public Object toJdbcType(Object value) { + return BasicTypeConverter.toString(value); + } + + @Override + public String toBeanType(Object value) { + return BasicTypeConverter.toString(value); + } + + @Override + public String formatValue(String t) { + return t; + } + + @Override + public String parse(String value) { + return value; + } + + @Override + public String convertFromMillis(long systemTimeMillis) { + return String.valueOf(systemTimeMillis); + } + + @Override + public boolean isDateTimeCapable() { + return true; + } + + @Override + public String readData(DataInput dataInput) throws IOException { + if (!dataInput.readBoolean()) { + return null; + } else { + return dataInput.readUTF(); + } + } + + @Override + public void writeData(DataOutput dataOutput, String value) throws IOException { + + if (value == null) { + dataOutput.writeBoolean(false); + } else { + dataOutput.writeBoolean(true); + dataOutput.writeUTF(value); + } + } + + @Override + public String jsonRead(JsonParser ctx, JsonToken event) throws IOException { + return ctx.getValueAsString(); + } + + @Override + public void jsonWrite(JsonGenerator ctx, String name, String value) throws IOException { + ctx.writeStringField(name, (String) value); + } +} diff --git a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeTime.java b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeTime.java index 955430f65..eb0b51d2f 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeTime.java +++ b/src/main/java/com/avaje/ebeaninternal/server/type/ScalarTypeTime.java @@ -1,99 +1,99 @@ -package com.avaje.ebeaninternal.server.type; - -import java.io.DataInput; -import java.io.DataOutput; -import java.io.IOException; -import java.sql.SQLException; -import java.sql.Time; -import java.sql.Types; - -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; - -/** - * ScalarType for java.sql.Time. - */ -public class ScalarTypeTime extends ScalarTypeBase