mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-10-24 23:56:36 +00:00
WIP JSON refactor - tidy up ScalarTypes
This commit is contained in:
@@ -2,6 +2,7 @@ package com.avaje.ebean.json;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -13,19 +14,19 @@ import com.fasterxml.jackson.core.JsonParser;
|
||||
*/
|
||||
public class EJson {
|
||||
|
||||
// /**
|
||||
// * Write the nested Map/List as json.
|
||||
// */
|
||||
// public static String write(Object object) {
|
||||
// return EJsonWriter.write(object);
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * Write the nested Map/List as json to the writer.
|
||||
// */
|
||||
// public static void write(Object object, Writer writer) {
|
||||
// EJsonWriter.write(object, writer);
|
||||
// }
|
||||
/**
|
||||
* Write the nested Map/List as json.
|
||||
*/
|
||||
public static String write(Object object) throws IOException {
|
||||
return EJsonWriter.write(object);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the nested Map/List as json to the writer.
|
||||
*/
|
||||
public static void write(Object object, Writer writer) throws IOException {
|
||||
EJsonWriter.write(object, writer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the nested Map/List as json to the jsonGenerator.
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.avaje.ebean.json;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.Collection;
|
||||
@@ -9,21 +11,27 @@ import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonFactory;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
|
||||
class EJsonWriter {
|
||||
|
||||
// static String write(Object object) {
|
||||
// StringWriter writer = new StringWriter(200);
|
||||
// write(object, writer);
|
||||
// return writer.toString();
|
||||
// }
|
||||
//
|
||||
// static void write(Object object, Writer writer) {
|
||||
// JsonGenerator generator = Json.createGenerator(writer);
|
||||
// write(object, generator);
|
||||
// generator.close();
|
||||
// }
|
||||
/**
|
||||
* Base jsonFactory implementation used when it is not passed in.
|
||||
*/
|
||||
static JsonFactory jsonFactory = new JsonFactory();
|
||||
|
||||
static String write(Object object) throws IOException {
|
||||
StringWriter writer = new StringWriter(200);
|
||||
write(object, writer);
|
||||
return writer.toString();
|
||||
}
|
||||
|
||||
static void write(Object object, Writer writer) throws IOException {
|
||||
JsonGenerator generator = jsonFactory.createGenerator(writer);
|
||||
write(object, generator);
|
||||
generator.close();
|
||||
}
|
||||
|
||||
static void write(Object object, JsonGenerator jsonGenerator) {
|
||||
new EJsonWriter(jsonGenerator).writeJson(object);
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.avaje.ebeaninternal.server.type;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,100 +0,0 @@
|
||||
package com.avaje.ebeaninternal.server.type;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.avaje.ebean.text.TextException;
|
||||
import com.avaje.ebean.util.StringHelper;
|
||||
|
||||
public class EscapeJson {
|
||||
|
||||
/**
|
||||
* Unescape the JSON escaped slash character.
|
||||
*/
|
||||
public static String unescapeSlash(String value) {
|
||||
return StringHelper.replaceString(value, "\\/","/");
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape and quote the string value.
|
||||
*/
|
||||
public static String escapeQuote(String value) {
|
||||
if (value == null) {
|
||||
return "null";
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder(value.length() + 2);
|
||||
sb.append("\"");
|
||||
escapeAppend(value, sb);
|
||||
sb.append("\"");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape quotes, \, /, \r, \n, \b, \f, \t and characters (U+0000 through
|
||||
* U+001F).
|
||||
*/
|
||||
public static String escape(String s) {
|
||||
if (s == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
escapeAppend(s, sb);
|
||||
return sb.toString();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape quotes, \, /, \r, \n, \b, \f, \t and characters (U+0000 through
|
||||
* U+001F).
|
||||
*/
|
||||
public static void escapeAppend(String s, Appendable sb) {
|
||||
|
||||
try {
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
char ch = s.charAt(i);
|
||||
switch (ch) {
|
||||
case '"':
|
||||
sb.append("\\\"");
|
||||
break;
|
||||
case '\\':
|
||||
sb.append("\\\\");
|
||||
break;
|
||||
case '\b':
|
||||
sb.append("\\b");
|
||||
break;
|
||||
case '\f':
|
||||
sb.append("\\f");
|
||||
break;
|
||||
case '\n':
|
||||
sb.append("\\n");
|
||||
break;
|
||||
case '\r':
|
||||
sb.append("\\r");
|
||||
break;
|
||||
case '\t':
|
||||
sb.append("\\t");
|
||||
break;
|
||||
case '/':
|
||||
sb.append("\\/");
|
||||
break;
|
||||
default:
|
||||
if ((ch >= '\u0000' && ch <= '\u001F') || (ch >= '\u007F' && ch <= '\u009F')
|
||||
|| (ch >= '\u2000' && ch <= '\u20FF')) {
|
||||
|
||||
String hs = Integer.toHexString(ch);
|
||||
sb.append("\\u");
|
||||
for (int j = 0; j < 4 - hs.length(); j++) {
|
||||
sb.append('0');
|
||||
}
|
||||
sb.append(hs.toUpperCase());
|
||||
} else {
|
||||
sb.append(ch);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new TextException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,8 +4,6 @@ import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* Reads from and binds to database columns.
|
||||
*
|
||||
* @author rbygrave
|
||||
*/
|
||||
public interface ScalarDataReader<T> {
|
||||
|
||||
|
||||
@@ -184,12 +184,24 @@ public interface ScalarType<T> extends StringParser, StringFormatter, ScalarData
|
||||
*/
|
||||
public boolean isDateTimeCapable();
|
||||
|
||||
/**
|
||||
* Read the value from binary input.
|
||||
*/
|
||||
public Object readData(DataInput dataInput) throws IOException;
|
||||
|
||||
/**
|
||||
* Write the value to binary output.
|
||||
*/
|
||||
public void writeData(DataOutput dataOutput, Object v) throws IOException;
|
||||
|
||||
/**
|
||||
* Read the value from JsonParser.
|
||||
*/
|
||||
public Object jsonRead(JsonParser ctx, JsonToken event) throws IOException;
|
||||
|
||||
/**
|
||||
* Write the value to the JsonGenerator.
|
||||
*/
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) throws IOException;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,76 +1,75 @@
|
||||
package com.avaje.ebeaninternal.server.type;
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Base ScalarType object.
|
||||
*/
|
||||
public abstract class ScalarTypeBase<T> implements ScalarType<T> {
|
||||
|
||||
protected final Class<T> type;
|
||||
protected final boolean jdbcNative;
|
||||
protected final int jdbcType;
|
||||
|
||||
public ScalarTypeBase(Class<T> type, boolean jdbcNative, int jdbcType) {
|
||||
this.type = type;
|
||||
this.jdbcNative = jdbcNative;
|
||||
this.jdbcType = jdbcType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Default implementation of mutable false.
|
||||
*/
|
||||
@Override
|
||||
protected final Class<T> type;
|
||||
|
||||
protected final boolean jdbcNative;
|
||||
|
||||
protected final int jdbcType;
|
||||
|
||||
public ScalarTypeBase(Class<T> 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.
|
||||
*/
|
||||
/**
|
||||
* Default to true.
|
||||
*/
|
||||
@Override
|
||||
public boolean isDirty(Object value) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Just return 0.
|
||||
*/
|
||||
public int getLength() {
|
||||
return 0;
|
||||
}
|
||||
* Just return 0.
|
||||
*/
|
||||
public int getLength() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public boolean isJdbcNative() {
|
||||
return jdbcNative;
|
||||
}
|
||||
|
||||
public int getJdbcType() {
|
||||
return jdbcType;
|
||||
}
|
||||
|
||||
public Class<T> getType() {
|
||||
return type;
|
||||
}
|
||||
public boolean isJdbcNative() {
|
||||
return jdbcNative;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public String format(Object v) {
|
||||
return formatValue((T)v);
|
||||
}
|
||||
public int getJdbcType() {
|
||||
return jdbcType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the value is null.
|
||||
*/
|
||||
public boolean isDbNull(Object value) {
|
||||
return value == null;
|
||||
}
|
||||
public Class<T> getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value that was passed in.
|
||||
*/
|
||||
public Object getDbNullValue(Object value) {
|
||||
return value;
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
public String format(Object v) {
|
||||
return formatValue((T) v);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the value is null.
|
||||
*/
|
||||
public boolean isDbNull(Object value) {
|
||||
return value == null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value that was passed in.
|
||||
*/
|
||||
public Object getDbNullValue(Object value) {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void loadIgnore(DataReader dataReader) {
|
||||
dataReader.incrementPos(1);
|
||||
@@ -79,5 +78,5 @@ public abstract class ScalarTypeBase<T> implements ScalarType<T> {
|
||||
public void accumulateScalarTypes(String propName, CtCompoundTypeScalarList list) {
|
||||
list.addScalarType(propName, this);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -16,91 +16,98 @@ import com.fasterxml.jackson.core.JsonToken;
|
||||
*/
|
||||
public abstract class ScalarTypeBaseDate<T> extends ScalarTypeBase<T> {
|
||||
|
||||
public ScalarTypeBaseDate(Class<T> type, boolean jdbcNative, int jdbcType) {
|
||||
super(type, jdbcNative, jdbcType);
|
||||
}
|
||||
|
||||
public abstract java.sql.Date convertToDate(T t);
|
||||
|
||||
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 {
|
||||
Date date = convertToDate(value);
|
||||
b.setDate(date);
|
||||
}
|
||||
}
|
||||
public ScalarTypeBaseDate(Class<T> type, boolean jdbcNative, int jdbcType) {
|
||||
super(type, jdbcNative, jdbcType);
|
||||
}
|
||||
|
||||
public T read(DataReader dataReader) throws SQLException {
|
||||
|
||||
Date ts = dataReader.getDate();
|
||||
if (ts == null){
|
||||
return null;
|
||||
} else {
|
||||
return convertFromDate(ts);
|
||||
}
|
||||
}
|
||||
|
||||
public String formatValue(T t) {
|
||||
Date date = convertToDate(t);
|
||||
return date.toString();
|
||||
}
|
||||
/**
|
||||
* Convert the target value to millis.
|
||||
*/
|
||||
public abstract long convertToMillis(Object value);
|
||||
|
||||
public T parse(String value) {
|
||||
Date date = Date.valueOf(value);
|
||||
return convertFromDate(date);
|
||||
}
|
||||
|
||||
public T parseDateTime(long systemTimeMillis) {
|
||||
Date ts = new Date(systemTimeMillis);
|
||||
return convertFromDate(ts);
|
||||
}
|
||||
/**
|
||||
* Convert to java.sql.Date from the target Date type.
|
||||
*/
|
||||
public abstract java.sql.Date convertToDate(T t);
|
||||
|
||||
public boolean isDateTimeCapable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, JsonToken event) throws IOException {
|
||||
if (JsonToken.VALUE_NUMBER_INT == event) {
|
||||
return parseDateTime(ctx.getLongValue());
|
||||
} else {
|
||||
String string = ctx.getText();
|
||||
throw new RuntimeException("convert " + string);
|
||||
}
|
||||
}
|
||||
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) throws IOException {
|
||||
long millis = convertToMillis(value);
|
||||
ctx.writeNumberField(name, millis);
|
||||
}
|
||||
|
||||
public abstract long convertToMillis(Object value);
|
||||
/**
|
||||
* Convert from java.sql.Date to the target Date type.
|
||||
*/
|
||||
public abstract T convertFromDate(java.sql.Date ts);
|
||||
|
||||
|
||||
public Object readData(DataInput dataInput) throws IOException {
|
||||
if (!dataInput.readBoolean()) {
|
||||
return null;
|
||||
} else {
|
||||
long val = dataInput.readLong();
|
||||
Date date = new Date(val);
|
||||
return convertFromDate(date);
|
||||
}
|
||||
public void bind(DataBind b, T value) throws SQLException {
|
||||
if (value == null) {
|
||||
b.setNull(Types.DATE);
|
||||
} else {
|
||||
Date date = convertToDate(value);
|
||||
b.setDate(date);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void writeData(DataOutput dataOutput, Object v) throws IOException {
|
||||
|
||||
T value = (T)v;
|
||||
if (value == null){
|
||||
dataOutput.writeBoolean(false);
|
||||
} else {
|
||||
dataOutput.writeBoolean(true);
|
||||
Date date = convertToDate(value);
|
||||
dataOutput.writeLong(date.getTime());
|
||||
}
|
||||
public T read(DataReader dataReader) throws SQLException {
|
||||
|
||||
Date ts = dataReader.getDate();
|
||||
if (ts == null) {
|
||||
return null;
|
||||
} else {
|
||||
return 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 parseDateTime(long systemTimeMillis) {
|
||||
Date ts = new Date(systemTimeMillis);
|
||||
return convertFromDate(ts);
|
||||
}
|
||||
|
||||
public boolean isDateTimeCapable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, JsonToken event) throws IOException {
|
||||
if (JsonToken.VALUE_NUMBER_INT == event) {
|
||||
return parseDateTime(ctx.getLongValue());
|
||||
} else {
|
||||
return convertFromDate(Date.valueOf(ctx.getText()));
|
||||
}
|
||||
}
|
||||
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) throws IOException {
|
||||
long millis = convertToMillis(value);
|
||||
ctx.writeNumberField(name, millis);
|
||||
}
|
||||
|
||||
public Object 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, Object v) throws IOException {
|
||||
|
||||
T value = (T) v;
|
||||
if (value == null) {
|
||||
dataOutput.writeBoolean(false);
|
||||
} else {
|
||||
dataOutput.writeBoolean(true);
|
||||
Date date = convertToDate(value);
|
||||
dataOutput.writeLong(date.getTime());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,94 +16,96 @@ import com.fasterxml.jackson.core.JsonToken;
|
||||
*/
|
||||
public abstract class ScalarTypeBaseDateTime<T> extends ScalarTypeBase<T> {
|
||||
|
||||
public ScalarTypeBaseDateTime(Class<T> type, boolean jdbcNative, int jdbcType) {
|
||||
super(type, jdbcNative, jdbcType);
|
||||
}
|
||||
|
||||
public abstract long convertToMillis(Object value);
|
||||
|
||||
public abstract Timestamp convertToTimestamp(T t);
|
||||
|
||||
public abstract T convertFromTimestamp(Timestamp ts);
|
||||
|
||||
public void bind(DataBind b, T value) throws SQLException {
|
||||
if (value == null){
|
||||
b.setNull(Types.TIMESTAMP);
|
||||
} else {
|
||||
Timestamp ts = convertToTimestamp(value);
|
||||
b.setTimestamp(ts);
|
||||
}
|
||||
}
|
||||
protected DateTimeJsonParser dateTimeParser = new DateTimeJsonParser();
|
||||
|
||||
public ScalarTypeBaseDateTime(Class<T> type, boolean jdbcNative, int jdbcType) {
|
||||
super(type, jdbcNative, jdbcType);
|
||||
}
|
||||
|
||||
public T read(DataReader dataReader) throws SQLException {
|
||||
|
||||
Timestamp ts = dataReader.getTimestamp();
|
||||
if (ts == null){
|
||||
return null;
|
||||
} else {
|
||||
return convertFromTimestamp(ts);
|
||||
}
|
||||
public abstract long convertToMillis(Object value);
|
||||
|
||||
public abstract Timestamp convertToTimestamp(T t);
|
||||
|
||||
public abstract T convertFromTimestamp(Timestamp ts);
|
||||
|
||||
public void bind(DataBind b, T value) throws SQLException {
|
||||
if (value == null) {
|
||||
b.setNull(Types.TIMESTAMP);
|
||||
} else {
|
||||
Timestamp ts = convertToTimestamp(value);
|
||||
b.setTimestamp(ts);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public T read(DataReader dataReader) throws SQLException {
|
||||
|
||||
Timestamp ts = dataReader.getTimestamp();
|
||||
if (ts == null) {
|
||||
return null;
|
||||
} else {
|
||||
return convertFromTimestamp(ts);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, JsonToken event) throws IOException {
|
||||
|
||||
|
||||
if (JsonToken.VALUE_NUMBER_INT == event) {
|
||||
long millis = ctx.getLongValue();
|
||||
return parseDateTime(millis);
|
||||
|
||||
|
||||
} else {
|
||||
String string = ctx.getText();
|
||||
throw new RuntimeException("convert " + string);
|
||||
String jsonDateTime = ctx.getText();
|
||||
return convertFromTimestamp(dateTimeParser.parse(jsonDateTime));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) throws IOException {
|
||||
long millis = convertToMillis(value);
|
||||
ctx.writeNumberField(name, millis);
|
||||
}
|
||||
|
||||
public String formatValue(T t) {
|
||||
Timestamp ts = convertToTimestamp(t);
|
||||
return ts.toString();
|
||||
}
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) throws IOException {
|
||||
long millis = convertToMillis(value);
|
||||
ctx.writeNumberField(name, millis);
|
||||
}
|
||||
|
||||
public T parse(String value) {
|
||||
Timestamp ts = Timestamp.valueOf(value);
|
||||
return convertFromTimestamp(ts);
|
||||
}
|
||||
|
||||
public T parseDateTime(long systemTimeMillis) {
|
||||
Timestamp ts = new Timestamp(systemTimeMillis);
|
||||
return convertFromTimestamp(ts);
|
||||
}
|
||||
public String formatValue(T t) {
|
||||
Timestamp ts = convertToTimestamp(t);
|
||||
return ts.toString();
|
||||
}
|
||||
|
||||
public boolean isDateTimeCapable() {
|
||||
return true;
|
||||
}
|
||||
public T parse(String value) {
|
||||
Timestamp ts = Timestamp.valueOf(value);
|
||||
return convertFromTimestamp(ts);
|
||||
}
|
||||
|
||||
public Object readData(DataInput dataInput) throws IOException {
|
||||
if (!dataInput.readBoolean()) {
|
||||
return null;
|
||||
} else {
|
||||
long val = dataInput.readLong();
|
||||
Timestamp ts = new Timestamp(val);
|
||||
return convertFromTimestamp(ts);
|
||||
}
|
||||
}
|
||||
public T parseDateTime(long systemTimeMillis) {
|
||||
Timestamp ts = new Timestamp(systemTimeMillis);
|
||||
return convertFromTimestamp(ts);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void writeData(DataOutput dataOutput, Object v) throws IOException {
|
||||
|
||||
T value = (T)v;
|
||||
if (value == null){
|
||||
dataOutput.writeBoolean(false);
|
||||
} else {
|
||||
dataOutput.writeBoolean(true);
|
||||
Timestamp ts = convertToTimestamp(value);
|
||||
dataOutput.writeLong(ts.getTime());
|
||||
}
|
||||
public boolean isDateTimeCapable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public Object readData(DataInput dataInput) throws IOException {
|
||||
if (!dataInput.readBoolean()) {
|
||||
return null;
|
||||
} else {
|
||||
long val = dataInput.readLong();
|
||||
Timestamp ts = new Timestamp(val);
|
||||
return convertFromTimestamp(ts);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void writeData(DataOutput dataOutput, Object v) throws IOException {
|
||||
|
||||
T value = (T) v;
|
||||
if (value == null) {
|
||||
dataOutput.writeBoolean(false);
|
||||
} else {
|
||||
dataOutput.writeBoolean(true);
|
||||
Timestamp ts = convertToTimestamp(value);
|
||||
dataOutput.writeLong(ts.getTime());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -25,14 +25,27 @@ public abstract class ScalarTypeBaseVarchar<T> extends ScalarTypeBase<T> {
|
||||
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);
|
||||
@@ -42,6 +55,7 @@ public abstract class ScalarTypeBaseVarchar<T> extends ScalarTypeBase<T> {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public T read(DataReader dataReader) throws SQLException {
|
||||
String s = dataReader.getString();
|
||||
if (s == null) {
|
||||
@@ -51,6 +65,7 @@ public abstract class ScalarTypeBaseVarchar<T> extends ScalarTypeBase<T> {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public T toBeanType(Object value) {
|
||||
if (value == null) {
|
||||
@@ -62,6 +77,7 @@ public abstract class ScalarTypeBaseVarchar<T> extends ScalarTypeBase<T> {
|
||||
return (T) value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object toJdbcType(Object value) {
|
||||
if (value instanceof String) {
|
||||
return parse((String) value);
|
||||
@@ -69,19 +85,23 @@ public abstract class ScalarTypeBaseVarchar<T> extends ScalarTypeBase<T> {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T parseDateTime(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 Object readData(DataInput dataInput) throws IOException {
|
||||
if (!dataInput.readBoolean()) {
|
||||
return null;
|
||||
@@ -109,6 +129,7 @@ public abstract class ScalarTypeBaseVarchar<T> extends ScalarTypeBase<T> {
|
||||
return parse(ctx.getValueAsString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) throws IOException {
|
||||
ctx.writeStringField(name, format(value));
|
||||
}
|
||||
|
||||
@@ -15,289 +15,288 @@ import com.fasterxml.jackson.core.JsonToken;
|
||||
/**
|
||||
* ScalarType for Boolean and boolean.
|
||||
* <p>
|
||||
* This may or may not be a native jdbc type depending on the database and jdbc
|
||||
* driver.
|
||||
* This may or may not be a native jdbc type depending on the database and jdbc driver.
|
||||
* </p>
|
||||
*/
|
||||
public class ScalarTypeBoolean {
|
||||
|
||||
public static class Native extends BooleanBase {
|
||||
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
|
||||
*
|
||||
* <p>
|
||||
* Sometimes booleans may be mapped to the JDBC type BIT. To use
|
||||
* the BitBoolean specify type.boolean.dbtype="bit" in the ebean configuration
|
||||
* </p>
|
||||
*/
|
||||
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<Boolean> {
|
||||
|
||||
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 parseDateTime(long systemTimeMillis) {
|
||||
throw new TextException("Not Supported");
|
||||
}
|
||||
|
||||
public boolean isDateTimeCapable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object readData(DataInput dataInput) throws IOException {
|
||||
if (!dataInput.readBoolean()) {
|
||||
return null;
|
||||
} else {
|
||||
boolean val = dataInput.readBoolean();
|
||||
return Boolean.valueOf(val);
|
||||
}
|
||||
}
|
||||
|
||||
public void writeData(DataOutput dataOutput, Object v) throws IOException {
|
||||
|
||||
Boolean val = (Boolean) v;
|
||||
if (val == null) {
|
||||
dataOutput.writeBoolean(false);
|
||||
} else {
|
||||
dataOutput.writeBoolean(true);
|
||||
dataOutput.writeBoolean(val.booleanValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, JsonToken event) {
|
||||
return JsonToken.VALUE_TRUE == event ? Boolean.TRUE : Boolean.FALSE;
|
||||
}
|
||||
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) throws IOException {
|
||||
ctx.writeBooleanField(name, (Boolean)value);
|
||||
}
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
* <p>
|
||||
* Sometimes booleans may be mapped to the JDBC type BIT. To use the BitBoolean specify
|
||||
* type.boolean.dbtype="bit" in the ebean configuration
|
||||
* </p>
|
||||
*/
|
||||
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<Boolean> {
|
||||
|
||||
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 parseDateTime(long systemTimeMillis) {
|
||||
throw new TextException("Not Supported");
|
||||
}
|
||||
|
||||
public boolean isDateTimeCapable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object readData(DataInput dataInput) throws IOException {
|
||||
if (!dataInput.readBoolean()) {
|
||||
return null;
|
||||
} else {
|
||||
boolean val = dataInput.readBoolean();
|
||||
return Boolean.valueOf(val);
|
||||
}
|
||||
}
|
||||
|
||||
public void writeData(DataOutput dataOutput, Object v) throws IOException {
|
||||
|
||||
Boolean val = (Boolean) v;
|
||||
if (val == null) {
|
||||
dataOutput.writeBoolean(false);
|
||||
} else {
|
||||
dataOutput.writeBoolean(true);
|
||||
dataOutput.writeBoolean(val.booleanValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, JsonToken event) {
|
||||
return JsonToken.VALUE_TRUE == event ? Boolean.TRUE : Boolean.FALSE;
|
||||
}
|
||||
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) throws IOException {
|
||||
ctx.writeBooleanField(name, (Boolean) value);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -17,74 +17,74 @@ import com.fasterxml.jackson.core.JsonToken;
|
||||
*/
|
||||
public class ScalarTypeByte extends ScalarTypeBase<Byte> {
|
||||
|
||||
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 ScalarTypeByte() {
|
||||
super(Byte.class, true, Types.TINYINT);
|
||||
}
|
||||
|
||||
public Byte read(DataReader dataReader) throws SQLException {
|
||||
return dataReader.getByte();
|
||||
}
|
||||
public void bind(DataBind b, Byte value) throws SQLException {
|
||||
if (value == null) {
|
||||
b.setNull(Types.TINYINT);
|
||||
} else {
|
||||
b.setByte(value);
|
||||
}
|
||||
}
|
||||
|
||||
public Object toJdbcType(Object value) {
|
||||
return BasicTypeConverter.toByte(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);
|
||||
}
|
||||
|
||||
public Byte toBeanType(Object value) {
|
||||
return BasicTypeConverter.toByte(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) {
|
||||
throw new TextException("Not supported");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, JsonToken event) {
|
||||
throw new TextException("Not supported");
|
||||
}
|
||||
|
||||
public String formatValue(Byte t) {
|
||||
return t.toString();
|
||||
}
|
||||
return t.toString();
|
||||
}
|
||||
|
||||
public Byte parse(String value) {
|
||||
throw new TextException("Not supported");
|
||||
}
|
||||
|
||||
public Byte parseDateTime(long systemTimeMillis) {
|
||||
throw new TextException("Not Supported");
|
||||
}
|
||||
|
||||
public boolean isDateTimeCapable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object readData(DataInput dataInput) throws IOException {
|
||||
if (!dataInput.readBoolean()) {
|
||||
return null;
|
||||
} else {
|
||||
byte val = dataInput.readByte();
|
||||
return Byte.valueOf(val);
|
||||
}
|
||||
}
|
||||
public Byte parse(String value) {
|
||||
throw new TextException("Not supported");
|
||||
}
|
||||
|
||||
public void writeData(DataOutput dataOutput, Object v) throws IOException {
|
||||
public Byte parseDateTime(long systemTimeMillis) {
|
||||
throw new TextException("Not Supported");
|
||||
}
|
||||
|
||||
Byte val = (Byte) v;
|
||||
if (val == null) {
|
||||
dataOutput.writeBoolean(false);
|
||||
} else {
|
||||
dataOutput.writeBoolean(true);
|
||||
dataOutput.writeByte(val);
|
||||
}
|
||||
public boolean isDateTimeCapable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object readData(DataInput dataInput) throws IOException {
|
||||
if (!dataInput.readBoolean()) {
|
||||
return null;
|
||||
} else {
|
||||
byte val = dataInput.readByte();
|
||||
return Byte.valueOf(val);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void writeData(DataOutput dataOutput, Object v) throws IOException {
|
||||
|
||||
Byte val = (Byte) v;
|
||||
if (val == null) {
|
||||
dataOutput.writeBoolean(false);
|
||||
} else {
|
||||
dataOutput.writeBoolean(true);
|
||||
dataOutput.writeByte(val);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.avaje.ebeaninternal.server.type;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
@@ -14,83 +15,82 @@ import com.fasterxml.jackson.core.JsonToken;
|
||||
* Base type for binary types.
|
||||
*/
|
||||
public abstract class ScalarTypeBytesBase extends ScalarTypeBase<byte[]> {
|
||||
|
||||
protected ScalarTypeBytesBase(boolean jdbcNative, int jdbcType) {
|
||||
super(byte[].class, jdbcNative, jdbcType);
|
||||
}
|
||||
|
||||
public Object convertFromBytes(byte[] bytes) {
|
||||
return bytes;
|
||||
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 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 Object toJdbcType(Object value) {
|
||||
return value;
|
||||
}
|
||||
|
||||
public byte[] toBeanType(Object value) {
|
||||
return (byte[])value;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) {
|
||||
throw new TextException("Not supported");
|
||||
public byte[] toBeanType(Object value) {
|
||||
return (byte[]) value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, JsonToken event) {
|
||||
throw new TextException("Not supported");
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) throws IOException {
|
||||
ctx.writeBinaryField(name, (byte[]) value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object 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");
|
||||
throw new TextException("Not supported");
|
||||
}
|
||||
|
||||
public byte[] parse(String value) {
|
||||
throw new TextException("Not supported");
|
||||
}
|
||||
|
||||
public byte[] parseDateTime(long systemTimeMillis) {
|
||||
throw new TextException("Not supported");
|
||||
}
|
||||
|
||||
public boolean isDateTimeCapable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object 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 byte[] parse(String value) {
|
||||
throw new TextException("Not supported");
|
||||
}
|
||||
|
||||
public byte[] parseDateTime(long systemTimeMillis) {
|
||||
throw new TextException("Not supported");
|
||||
}
|
||||
|
||||
public boolean isDateTimeCapable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object 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, Object v) throws IOException {
|
||||
if (v == null) {
|
||||
dataOutput.writeBoolean(false);
|
||||
} else {
|
||||
byte[] bytes = convertToBytes(v);
|
||||
dataOutput.writeInt(bytes.length);
|
||||
dataOutput.write(bytes);
|
||||
}
|
||||
}
|
||||
|
||||
public void writeData(DataOutput dataOutput, Object v) throws IOException {
|
||||
if (v == null){
|
||||
dataOutput.writeBoolean(false);
|
||||
} else {
|
||||
byte[] bytes = convertToBytes(v);
|
||||
dataOutput.writeInt(bytes.length);
|
||||
dataOutput.write(bytes);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -8,12 +8,12 @@ import java.sql.Types;
|
||||
*/
|
||||
public class ScalarTypeBytesBinary extends ScalarTypeBytesBase {
|
||||
|
||||
public ScalarTypeBytesBinary() {
|
||||
super(true, Types.BINARY);
|
||||
}
|
||||
|
||||
public byte[] read(DataReader dataReader) throws SQLException {
|
||||
return dataReader.getBytes();
|
||||
}
|
||||
|
||||
public ScalarTypeBytesBinary() {
|
||||
super(true, Types.BINARY);
|
||||
}
|
||||
|
||||
public byte[] read(DataReader dataReader) throws SQLException {
|
||||
return dataReader.getBytes();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,14 +7,14 @@ 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();
|
||||
}
|
||||
|
||||
public ScalarTypeBytesBlob() {
|
||||
super(true, Types.BLOB);
|
||||
}
|
||||
|
||||
public byte[] read(DataReader dataReader) throws SQLException {
|
||||
|
||||
return dataReader.getBlobBytes();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
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;
|
||||
@@ -14,115 +14,116 @@ import com.fasterxml.jackson.core.JsonToken;
|
||||
* Encrypted ScalarType that wraps a byte[] types.
|
||||
*
|
||||
* @author rbygrave
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class ScalarTypeBytesEncrypted implements ScalarType<byte[]> {
|
||||
|
||||
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;
|
||||
}
|
||||
private final ScalarTypeBytesBase baseType;
|
||||
|
||||
@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);
|
||||
}
|
||||
private final DataEncryptSupport dataEncryptSupport;
|
||||
|
||||
public int getJdbcType() {
|
||||
return baseType.getJdbcType();
|
||||
}
|
||||
public ScalarTypeBytesEncrypted(ScalarTypeBytesBase baseType, DataEncryptSupport dataEncryptSupport) {
|
||||
this.baseType = baseType;
|
||||
this.dataEncryptSupport = dataEncryptSupport;
|
||||
}
|
||||
|
||||
public int getLength() {
|
||||
return baseType.getLength();
|
||||
}
|
||||
@Override
|
||||
public boolean isMutable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public Class<byte[]> getType() {
|
||||
return byte[].class;
|
||||
}
|
||||
@Override
|
||||
public boolean isDirty(Object value) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isDateTimeCapable() {
|
||||
return baseType.isDateTimeCapable();
|
||||
}
|
||||
public void bind(DataBind b, byte[] value) throws SQLException {
|
||||
value = dataEncryptSupport.encrypt(value);
|
||||
baseType.bind(b, value);
|
||||
}
|
||||
|
||||
public boolean isJdbcNative() {
|
||||
return baseType.isJdbcNative();
|
||||
}
|
||||
public int getJdbcType() {
|
||||
return baseType.getJdbcType();
|
||||
}
|
||||
|
||||
public void loadIgnore(DataReader dataReader) {
|
||||
baseType.loadIgnore(dataReader);
|
||||
}
|
||||
public int getLength() {
|
||||
return baseType.getLength();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) {
|
||||
throw new TextException("Not supported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, JsonToken event) {
|
||||
throw new TextException("Not supported");
|
||||
}
|
||||
|
||||
public String format(Object v) {
|
||||
throw new RuntimeException("Not used");
|
||||
}
|
||||
|
||||
public String formatValue(byte[] v) {
|
||||
throw new RuntimeException("Not used");
|
||||
}
|
||||
public Class<byte[]> getType() {
|
||||
return byte[].class;
|
||||
}
|
||||
|
||||
public byte[] parse(String value) {
|
||||
return baseType.parse(value);
|
||||
}
|
||||
public boolean isDateTimeCapable() {
|
||||
return baseType.isDateTimeCapable();
|
||||
}
|
||||
|
||||
public byte[] parseDateTime(long systemTimeMillis) {
|
||||
return baseType.parseDateTime(systemTimeMillis);
|
||||
}
|
||||
public boolean isJdbcNative() {
|
||||
return baseType.isJdbcNative();
|
||||
}
|
||||
|
||||
public byte[] read(DataReader dataReader) throws SQLException {
|
||||
|
||||
byte[] data = baseType.read(dataReader);
|
||||
data = dataEncryptSupport.decrypt(data);
|
||||
return data;
|
||||
}
|
||||
public void loadIgnore(DataReader dataReader) {
|
||||
baseType.loadIgnore(dataReader);
|
||||
}
|
||||
|
||||
public byte[] toBeanType(Object value) {
|
||||
return baseType.toBeanType(value);
|
||||
}
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) throws IOException {
|
||||
ctx.writeBinaryField(name, (byte[]) value);
|
||||
}
|
||||
|
||||
public Object toJdbcType(Object value) {
|
||||
return baseType.toJdbcType(value);
|
||||
}
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, JsonToken event) throws IOException {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream(500);
|
||||
ctx.readBinaryValue(out);
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
public void accumulateScalarTypes(String propName, CtCompoundTypeScalarList list) {
|
||||
baseType.accumulateScalarTypes(propName, list);
|
||||
}
|
||||
public String format(Object v) {
|
||||
throw new RuntimeException("Not used");
|
||||
}
|
||||
|
||||
public Object readData(DataInput dataInput) throws IOException {
|
||||
int len = dataInput.readInt();
|
||||
byte[] value = new byte[len];
|
||||
dataInput.readFully(value);
|
||||
return value;
|
||||
}
|
||||
public String formatValue(byte[] v) {
|
||||
throw new RuntimeException("Not used");
|
||||
}
|
||||
|
||||
public byte[] parse(String value) {
|
||||
return baseType.parse(value);
|
||||
}
|
||||
|
||||
public byte[] parseDateTime(long systemTimeMillis) {
|
||||
return baseType.parseDateTime(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 Object readData(DataInput dataInput) throws IOException {
|
||||
int len = dataInput.readInt();
|
||||
byte[] value = new byte[len];
|
||||
dataInput.readFully(value);
|
||||
return value;
|
||||
}
|
||||
|
||||
public void writeData(DataOutput dataOutput, Object v) throws IOException {
|
||||
byte[] value = (byte[]) v;
|
||||
dataOutput.writeInt(value.length);
|
||||
dataOutput.write(value);
|
||||
}
|
||||
|
||||
public void writeData(DataOutput dataOutput, Object v) throws IOException {
|
||||
byte[] value = (byte[])v;
|
||||
dataOutput.writeInt(value.length);
|
||||
dataOutput.write(value);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,12 +7,12 @@ 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();
|
||||
}
|
||||
|
||||
public ScalarTypeBytesLongVarbinary() {
|
||||
super(true, Types.LONGVARBINARY);
|
||||
}
|
||||
|
||||
public byte[] read(DataReader dataReader) throws SQLException {
|
||||
return dataReader.getBinaryBytes();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,12 +8,12 @@ import java.sql.Types;
|
||||
*/
|
||||
public class ScalarTypeBytesVarbinary extends ScalarTypeBytesBase {
|
||||
|
||||
public ScalarTypeBytesVarbinary() {
|
||||
super(true, Types.VARBINARY);
|
||||
}
|
||||
|
||||
public byte[] read(DataReader dataReader) throws SQLException {
|
||||
return dataReader.getBytes();
|
||||
}
|
||||
|
||||
public ScalarTypeBytesVarbinary() {
|
||||
super(true, Types.VARBINARY);
|
||||
}
|
||||
|
||||
public byte[] read(DataReader dataReader) throws SQLException {
|
||||
return dataReader.getBytes();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -12,49 +12,49 @@ import com.avaje.ebeaninternal.server.core.BasicTypeConverter;
|
||||
* ScalarType for java.util.Calendar.
|
||||
*/
|
||||
public class ScalarTypeCalendar extends ScalarTypeBaseDateTime<Calendar> {
|
||||
|
||||
public ScalarTypeCalendar(int jdbcType) {
|
||||
super(Calendar.class, false, jdbcType);
|
||||
}
|
||||
|
||||
public void bind(DataBind b, Calendar value) throws SQLException {
|
||||
if (value == null){
|
||||
b.setNull(Types.TIMESTAMP);
|
||||
} else {
|
||||
Calendar date = (Calendar)value;
|
||||
if (jdbcType == Types.TIMESTAMP){
|
||||
Timestamp timestamp = new Timestamp(date.getTimeInMillis());
|
||||
b.setTimestamp(timestamp);
|
||||
} else {
|
||||
Date d = new Date(date.getTimeInMillis());
|
||||
b.setDate(d);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Calendar convertFromTimestamp(Timestamp ts) {
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTimeInMillis(ts.getTime());
|
||||
return calendar;
|
||||
|
||||
public ScalarTypeCalendar(int jdbcType) {
|
||||
super(Calendar.class, false, jdbcType);
|
||||
}
|
||||
|
||||
public void bind(DataBind b, Calendar value) throws SQLException {
|
||||
if (value == null) {
|
||||
b.setNull(Types.TIMESTAMP);
|
||||
} else {
|
||||
Calendar date = (Calendar) value;
|
||||
if (jdbcType == Types.TIMESTAMP) {
|
||||
Timestamp timestamp = new Timestamp(date.getTimeInMillis());
|
||||
b.setTimestamp(timestamp);
|
||||
} else {
|
||||
Date d = new Date(date.getTimeInMillis());
|
||||
b.setDate(d);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Calendar convertFromTimestamp(Timestamp ts) {
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTimeInMillis(ts.getTime());
|
||||
return calendar;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long convertToMillis(Object value) {
|
||||
return ((Calendar) value).getTimeInMillis();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Timestamp convertToTimestamp(Calendar t) {
|
||||
return new Timestamp(t.getTimeInMillis());
|
||||
}
|
||||
@Override
|
||||
public Timestamp convertToTimestamp(Calendar t) {
|
||||
return new Timestamp(t.getTimeInMillis());
|
||||
}
|
||||
|
||||
public Object toJdbcType(Object value) {
|
||||
return BasicTypeConverter.convert(value, jdbcType);
|
||||
}
|
||||
public Object toJdbcType(Object value) {
|
||||
return BasicTypeConverter.convert(value, jdbcType);
|
||||
}
|
||||
|
||||
public Calendar toBeanType(Object value) {
|
||||
return BasicTypeConverter.toCalendar(value);
|
||||
}
|
||||
|
||||
public Calendar toBeanType(Object value) {
|
||||
return BasicTypeConverter.toCalendar(value);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,70 +1,62 @@
|
||||
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.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonToken;
|
||||
|
||||
/**
|
||||
* ScalarType for char.
|
||||
*/
|
||||
public class ScalarTypeChar extends ScalarTypeBaseVarchar<Character> {
|
||||
|
||||
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);
|
||||
}
|
||||
public ScalarTypeChar() {
|
||||
super(char.class, false, Types.VARCHAR);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, JsonToken event) throws IOException {
|
||||
return ctx.getValueAsString();
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -12,63 +12,63 @@ import com.fasterxml.jackson.core.JsonToken;
|
||||
/**
|
||||
* ScalarType for char[].
|
||||
*/
|
||||
public class ScalarTypeCharArray extends ScalarTypeBaseVarchar<char[]>{
|
||||
public class ScalarTypeCharArray extends ScalarTypeBaseVarchar<char[]> {
|
||||
|
||||
public ScalarTypeCharArray() {
|
||||
super(char[].class, false, Types.VARCHAR);
|
||||
}
|
||||
|
||||
@Override
|
||||
public char[] convertFromDbString(String dbValue) {
|
||||
return dbValue.toCharArray();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String convertToDbString(char[] beanValue) {
|
||||
return new String(beanValue);
|
||||
public char[] read(DataReader dataReader) throws SQLException {
|
||||
String string = dataReader.getString();
|
||||
if (string == null) {
|
||||
return null;
|
||||
} else {
|
||||
return string.toCharArray();
|
||||
}
|
||||
}
|
||||
|
||||
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 Object toJdbcType(Object value) {
|
||||
return BasicTypeConverter.toString(value);
|
||||
}
|
||||
|
||||
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 char[] toBeanType(Object value) {
|
||||
String s = BasicTypeConverter.toString(value);
|
||||
return s.toCharArray();
|
||||
}
|
||||
|
||||
public String formatValue(char[] t) {
|
||||
return String.valueOf(t);
|
||||
}
|
||||
public String formatValue(char[] t) {
|
||||
return String.valueOf(t);
|
||||
}
|
||||
|
||||
public char[] parse(String value) {
|
||||
return value.toCharArray();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, JsonToken event) throws IOException {
|
||||
return ctx.getValueAsString().toCharArray();
|
||||
}
|
||||
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) throws IOException {
|
||||
ctx.writeStringField(name, String.valueOf(value));
|
||||
}
|
||||
public char[] parse(String value) {
|
||||
return value.toCharArray();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, JsonToken event) throws IOException {
|
||||
return ctx.getValueAsString().toCharArray();
|
||||
}
|
||||
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) throws IOException {
|
||||
ctx.writeStringField(name, String.valueOf(value));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
package com.avaje.ebeaninternal.server.type;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
|
||||
/**
|
||||
* ScalarType for Class that persists it to VARCHAR column.
|
||||
*
|
||||
@@ -14,42 +10,36 @@ import com.fasterxml.jackson.core.JsonGenerator;
|
||||
*/
|
||||
@SuppressWarnings({ "rawtypes" })
|
||||
public class ScalarTypeClass extends ScalarTypeBaseVarchar<Class> {
|
||||
|
||||
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 ScalarTypeClass() {
|
||||
super(Class.class);
|
||||
}
|
||||
|
||||
public String formatValue(Class v) {
|
||||
return v.getCanonicalName();
|
||||
}
|
||||
@Override
|
||||
public int getLength() {
|
||||
return 255;
|
||||
}
|
||||
|
||||
public Class<?> parse(String value) {
|
||||
try {
|
||||
return Class.forName(value);
|
||||
} catch (Exception e) {
|
||||
String msg = "Unable to find Class "+value;
|
||||
throw new PersistenceException(msg, e);
|
||||
}
|
||||
@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);
|
||||
}
|
||||
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) throws IOException {
|
||||
ctx.writeStringField(name, formatValue((Class<?>)value));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@ public class ScalarTypeClob extends ScalarTypeBaseVarchar<String> {
|
||||
return beanValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bind(DataBind b, String value) throws SQLException {
|
||||
if (value == null) {
|
||||
b.setNull(Types.VARCHAR);
|
||||
@@ -40,23 +41,28 @@ public class ScalarTypeClob extends ScalarTypeBaseVarchar<String> {
|
||||
}
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
@@ -1,50 +1,39 @@
|
||||
package com.avaje.ebeaninternal.server.type;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Currency;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonToken;
|
||||
|
||||
/**
|
||||
* ScalarType for java.util.Currency which converts to and from a VARCHAR database column.
|
||||
*/
|
||||
public class ScalarTypeCurrency extends ScalarTypeBaseVarchar<Currency> {
|
||||
|
||||
public ScalarTypeCurrency() {
|
||||
super(Currency.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLength() {
|
||||
return 3;
|
||||
}
|
||||
public ScalarTypeCurrency() {
|
||||
super(Currency.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Currency convertFromDbString(String dbValue) {
|
||||
return Currency.getInstance(dbValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String convertToDbString(Currency beanValue) {
|
||||
return ((Currency)beanValue).getCurrencyCode();
|
||||
}
|
||||
|
||||
public String formatValue(Currency v) {
|
||||
return v.toString();
|
||||
}
|
||||
|
||||
public Currency parse(String value) {
|
||||
return Currency.getInstance(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, JsonToken event) throws IOException {
|
||||
return parse(ctx.getValueAsString());
|
||||
public int getLength() {
|
||||
return 3;
|
||||
}
|
||||
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) throws IOException {
|
||||
ctx.writeStringField(name, formatValue((Currency)value));
|
||||
@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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -10,44 +10,44 @@ import com.avaje.ebeaninternal.server.core.BasicTypeConverter;
|
||||
* ScalarType for java.sql.Date.
|
||||
*/
|
||||
public class ScalarTypeDate extends ScalarTypeBaseDate<java.sql.Date> {
|
||||
|
||||
public ScalarTypeDate() {
|
||||
super(Date.class, true, Types.DATE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long convertToMillis(Object value) {
|
||||
return ((Date)value).getTime();
|
||||
|
||||
public ScalarTypeDate() {
|
||||
super(Date.class, true, Types.DATE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Date convertFromDate(Date date) {
|
||||
return date;
|
||||
public long convertToMillis(Object value) {
|
||||
return ((Date) 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);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Date convertToDate(Date t) {
|
||||
return t;
|
||||
}
|
||||
public java.sql.Date read(DataReader dataReader) throws SQLException {
|
||||
return dataReader.getDate();
|
||||
}
|
||||
|
||||
public void bind(DataBind b, java.sql.Date value) throws SQLException {
|
||||
if (value == null){
|
||||
b.setNull(Types.DATE);
|
||||
} else {
|
||||
b.setDate(value);
|
||||
}
|
||||
}
|
||||
public Object toJdbcType(Object value) {
|
||||
return BasicTypeConverter.toDate(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);
|
||||
}
|
||||
public java.sql.Date toBeanType(Object value) {
|
||||
return BasicTypeConverter.toDate(value);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,83 +15,84 @@ import com.fasterxml.jackson.core.JsonToken;
|
||||
* ScalarType for Double and double.
|
||||
*/
|
||||
public class ScalarTypeDouble extends ScalarTypeBase<Double> {
|
||||
|
||||
public ScalarTypeDouble() {
|
||||
super(Double.class, true, Types.DOUBLE);
|
||||
}
|
||||
|
||||
public void bind(DataBind b, Double value) throws SQLException {
|
||||
if (value == null){
|
||||
b.setNull(Types.DOUBLE);
|
||||
} else {
|
||||
b.setDouble(value.doubleValue());
|
||||
}
|
||||
}
|
||||
|
||||
public Double read(DataReader dataReader) throws SQLException {
|
||||
|
||||
return dataReader.getDouble();
|
||||
}
|
||||
|
||||
public Object toJdbcType(Object value) {
|
||||
return BasicTypeConverter.toDouble(value);
|
||||
}
|
||||
public ScalarTypeDouble() {
|
||||
super(Double.class, true, Types.DOUBLE);
|
||||
}
|
||||
|
||||
public Double toBeanType(Object value) {
|
||||
return BasicTypeConverter.toDouble(value);
|
||||
}
|
||||
|
||||
|
||||
public String formatValue(Double t) {
|
||||
return t.toString();
|
||||
@Override
|
||||
public void bind(DataBind b, Double value) throws SQLException {
|
||||
if (value == null) {
|
||||
b.setNull(Types.DOUBLE);
|
||||
} else {
|
||||
b.setDouble(value.doubleValue());
|
||||
}
|
||||
}
|
||||
|
||||
public Double parse(String value) {
|
||||
return Double.valueOf(value);
|
||||
}
|
||||
@Override
|
||||
public Double read(DataReader dataReader) throws SQLException {
|
||||
return dataReader.getDouble();
|
||||
}
|
||||
|
||||
public Double parseDateTime(long systemTimeMillis) {
|
||||
return Double.valueOf(systemTimeMillis);
|
||||
}
|
||||
@Override
|
||||
public Object toJdbcType(Object value) {
|
||||
return BasicTypeConverter.toDouble(value);
|
||||
}
|
||||
|
||||
public boolean isDateTimeCapable() {
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
public Double toBeanType(Object value) {
|
||||
return BasicTypeConverter.toDouble(value);
|
||||
}
|
||||
|
||||
public String toJsonString(Double value) {
|
||||
if(value.isInfinite() || value.isNaN()) {
|
||||
return "null";
|
||||
} else {
|
||||
return value.toString();
|
||||
}
|
||||
}
|
||||
|
||||
public Object readData(DataInput dataInput) throws IOException {
|
||||
if (!dataInput.readBoolean()) {
|
||||
return null;
|
||||
} else {
|
||||
double val = dataInput.readDouble();
|
||||
return Double.valueOf(val);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public String formatValue(Double t) {
|
||||
return t.toString();
|
||||
}
|
||||
|
||||
public void writeData(DataOutput dataOutput, Object v) throws IOException {
|
||||
|
||||
Double value = (Double)v;
|
||||
if (value == null){
|
||||
dataOutput.writeBoolean(false);
|
||||
} else {
|
||||
dataOutput.writeBoolean(true);
|
||||
dataOutput.writeDouble(value.doubleValue());
|
||||
}
|
||||
@Override
|
||||
public Double parse(String value) {
|
||||
return Double.valueOf(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double parseDateTime(long systemTimeMillis) {
|
||||
return Double.valueOf(systemTimeMillis);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDateTimeCapable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object readData(DataInput dataInput) throws IOException {
|
||||
if (!dataInput.readBoolean()) {
|
||||
return null;
|
||||
} else {
|
||||
double val = dataInput.readDouble();
|
||||
return Double.valueOf(val);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, JsonToken event) throws IOException {
|
||||
return ctx.getDoubleValue();
|
||||
}
|
||||
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) throws IOException {
|
||||
ctx.writeNumberField(name, (Double)value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeData(DataOutput dataOutput, Object v) throws IOException {
|
||||
|
||||
Double value = (Double) v;
|
||||
if (value == null) {
|
||||
dataOutput.writeBoolean(false);
|
||||
} else {
|
||||
dataOutput.writeBoolean(true);
|
||||
dataOutput.writeDouble(value.doubleValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, JsonToken event) throws IOException {
|
||||
return ctx.getDoubleValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) throws IOException {
|
||||
ctx.writeNumberField(name, (Double) value);
|
||||
}
|
||||
}
|
||||
|
||||
+107
-89
@@ -11,115 +11,133 @@ import com.fasterxml.jackson.core.JsonToken;
|
||||
|
||||
public class ScalarTypeEncryptedWrapper<T> implements ScalarType<T> {
|
||||
|
||||
private final ScalarType<T> wrapped;
|
||||
|
||||
private final DataEncryptSupport dataEncryptSupport;
|
||||
|
||||
private final ScalarTypeBytesBase byteArrayType;
|
||||
|
||||
public ScalarTypeEncryptedWrapper(ScalarType<T> wrapped, ScalarTypeBytesBase byteArrayType, DataEncryptSupport dataEncryptSupport) {
|
||||
this.wrapped = wrapped;
|
||||
this.byteArrayType = byteArrayType;
|
||||
this.dataEncryptSupport = dataEncryptSupport;
|
||||
}
|
||||
private final ScalarType<T> wrapped;
|
||||
|
||||
@Override
|
||||
public boolean isMutable() {
|
||||
return wrapped.isMutable();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDirty(Object value) {
|
||||
return wrapped.isDirty(value);
|
||||
}
|
||||
private final DataEncryptSupport dataEncryptSupport;
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, JsonToken event) throws IOException {
|
||||
return wrapped.jsonRead(ctx, event);
|
||||
}
|
||||
private final ScalarTypeBytesBase byteArrayType;
|
||||
|
||||
public Object readData(DataInput dataInput) throws IOException {
|
||||
return wrapped.readData(dataInput);
|
||||
}
|
||||
public ScalarTypeEncryptedWrapper(ScalarType<T> wrapped, ScalarTypeBytesBase byteArrayType, DataEncryptSupport dataEncryptSupport) {
|
||||
this.wrapped = wrapped;
|
||||
this.byteArrayType = byteArrayType;
|
||||
this.dataEncryptSupport = dataEncryptSupport;
|
||||
}
|
||||
|
||||
public void writeData(DataOutput dataOutput, Object v) throws IOException {
|
||||
wrapped.writeData(dataOutput, v);
|
||||
}
|
||||
@Override
|
||||
public boolean isMutable() {
|
||||
return wrapped.isMutable();
|
||||
}
|
||||
|
||||
public T read(DataReader dataReader) throws SQLException {
|
||||
@Override
|
||||
public boolean isDirty(Object value) {
|
||||
return wrapped.isDirty(value);
|
||||
}
|
||||
|
||||
byte[] data = dataReader.getBytes();
|
||||
String formattedValue = dataEncryptSupport.decryptObject(data);
|
||||
if (formattedValue == null){
|
||||
return null;
|
||||
}
|
||||
return wrapped.parse(formattedValue);
|
||||
}
|
||||
@Override
|
||||
public Object readData(DataInput dataInput) throws IOException {
|
||||
return wrapped.readData(dataInput);
|
||||
}
|
||||
|
||||
private byte[] encrypt(T value){
|
||||
String formatValue = wrapped.formatValue(value);
|
||||
return dataEncryptSupport.encryptObject(formatValue);
|
||||
}
|
||||
|
||||
public void bind(DataBind b, T value) throws SQLException {
|
||||
|
||||
byte[] encryptedValue = encrypt(value);
|
||||
byteArrayType.bind(b, encryptedValue);
|
||||
}
|
||||
@Override
|
||||
public void writeData(DataOutput dataOutput, Object v) throws IOException {
|
||||
wrapped.writeData(dataOutput, v);
|
||||
}
|
||||
|
||||
public int getJdbcType() {
|
||||
return byteArrayType.getJdbcType();
|
||||
}
|
||||
@Override
|
||||
public T read(DataReader dataReader) throws SQLException {
|
||||
|
||||
public int getLength() {
|
||||
return byteArrayType.getLength();
|
||||
byte[] data = dataReader.getBytes();
|
||||
String formattedValue = dataEncryptSupport.decryptObject(data);
|
||||
if (formattedValue == null) {
|
||||
return null;
|
||||
}
|
||||
return wrapped.parse(formattedValue);
|
||||
}
|
||||
|
||||
public Class<T> getType() {
|
||||
return wrapped.getType();
|
||||
}
|
||||
private byte[] encrypt(T value) {
|
||||
String formatValue = wrapped.formatValue(value);
|
||||
return dataEncryptSupport.encryptObject(formatValue);
|
||||
}
|
||||
|
||||
public boolean isDateTimeCapable() {
|
||||
return wrapped.isDateTimeCapable();
|
||||
}
|
||||
@Override
|
||||
public void bind(DataBind b, T value) throws SQLException {
|
||||
|
||||
public boolean isJdbcNative() {
|
||||
return false;
|
||||
}
|
||||
byte[] encryptedValue = encrypt(value);
|
||||
byteArrayType.bind(b, encryptedValue);
|
||||
}
|
||||
|
||||
public void loadIgnore(DataReader dataReader) {
|
||||
wrapped.loadIgnore(dataReader);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public String format(Object v) {
|
||||
return formatValue((T)v);
|
||||
}
|
||||
@Override
|
||||
public int getJdbcType() {
|
||||
return byteArrayType.getJdbcType();
|
||||
}
|
||||
|
||||
public String formatValue(T v) {
|
||||
return wrapped.formatValue(v);
|
||||
}
|
||||
@Override
|
||||
public int getLength() {
|
||||
return byteArrayType.getLength();
|
||||
}
|
||||
|
||||
public T parse(String value) {
|
||||
return wrapped.parse(value);
|
||||
}
|
||||
@Override
|
||||
public Class<T> getType() {
|
||||
return wrapped.getType();
|
||||
}
|
||||
|
||||
public T parseDateTime(long systemTimeMillis) {
|
||||
return wrapped.parseDateTime(systemTimeMillis);
|
||||
}
|
||||
@Override
|
||||
public boolean isDateTimeCapable() {
|
||||
return wrapped.isDateTimeCapable();
|
||||
}
|
||||
|
||||
public T toBeanType(Object value) {
|
||||
return wrapped.toBeanType(value);
|
||||
}
|
||||
@Override
|
||||
public boolean isJdbcNative() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object toJdbcType(Object value) {
|
||||
return wrapped.toJdbcType(value);
|
||||
}
|
||||
@Override
|
||||
public void loadIgnore(DataReader dataReader) {
|
||||
wrapped.loadIgnore(dataReader);
|
||||
}
|
||||
|
||||
public void accumulateScalarTypes(String propName, CtCompoundTypeScalarList list) {
|
||||
wrapped.accumulateScalarTypes(propName, list);
|
||||
}
|
||||
|
||||
@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 parseDateTime(long systemTimeMillis) {
|
||||
return wrapped.parseDateTime(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 Object jsonRead(JsonParser ctx, JsonToken event) throws IOException {
|
||||
return wrapped.jsonRead(ctx, event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) throws IOException {
|
||||
wrapped.jsonWrite(ctx, name, value);
|
||||
}
|
||||
|
||||
@@ -12,99 +12,98 @@ import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonToken;
|
||||
|
||||
|
||||
/**
|
||||
* JPA standard based Enum scalar type.
|
||||
* <p>
|
||||
* Converts between bean Enum types to database string or integer columns.
|
||||
* </p>
|
||||
* <p>
|
||||
* 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.
|
||||
* 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.
|
||||
* </p>
|
||||
*/
|
||||
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 getContraintInValues(){
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
public static class StringEnum extends EnumBase implements ScalarTypeEnum {
|
||||
|
||||
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;
|
||||
}
|
||||
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 getContraintInValues() {
|
||||
|
||||
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 ((Enum<?>) beanValue).toString();
|
||||
}
|
||||
|
||||
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 ((Enum<?>)beanValue).toString();
|
||||
}
|
||||
|
||||
public Object toBeanType(Object dbValue) {
|
||||
if (dbValue == null || dbValue instanceof Enum<?>) {
|
||||
return dbValue;
|
||||
@@ -112,150 +111,155 @@ public class ScalarTypeEnumStandard {
|
||||
return Enum.valueOf(enumType, (String) dbValue);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings({"rawtypes","unchecked"})
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
||||
private final Object[] enumArray;
|
||||
|
||||
/**
|
||||
* Return the IN values for DB constraint construction.
|
||||
*/
|
||||
public String getContraintInValues(){
|
||||
|
||||
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).intValue();
|
||||
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;
|
||||
}
|
||||
|
||||
public String format(Object t) {
|
||||
return ((Enum<?>)t).name();
|
||||
}
|
||||
|
||||
public String formatValue(Object t) {
|
||||
return ((Enum<?>)t).name();
|
||||
}
|
||||
|
||||
public Object parse(String value) {
|
||||
return Enum.valueOf(enumType, value);
|
||||
}
|
||||
|
||||
public Object parseDateTime(long systemTimeMillis) {
|
||||
throw new TextException("Not Supported");
|
||||
}
|
||||
|
||||
public boolean isDateTimeCapable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, JsonToken event) throws IOException {
|
||||
|
||||
String val = ctx.getValueAsString();
|
||||
return parse(val);
|
||||
}
|
||||
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) throws IOException {
|
||||
ctx.writeStringField(name, formatValue(value));
|
||||
}
|
||||
|
||||
public Object readData(DataInput dataInput) throws IOException {
|
||||
if (!dataInput.readBoolean()) {
|
||||
return null;
|
||||
} else {
|
||||
String s = dataInput.readUTF();
|
||||
return parse(s);
|
||||
}
|
||||
}
|
||||
|
||||
public void writeData(DataOutput dataOutput, Object v) throws IOException {
|
||||
if (v == null){
|
||||
dataOutput.writeBoolean(false);
|
||||
} else {
|
||||
dataOutput.writeBoolean(true);
|
||||
dataOutput.writeUTF(format(v));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 getContraintInValues() {
|
||||
|
||||
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).intValue();
|
||||
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 parseDateTime(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));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,81 +16,83 @@ import com.fasterxml.jackson.core.JsonToken;
|
||||
*/
|
||||
public class ScalarTypeFloat extends ScalarTypeBase<Float> {
|
||||
|
||||
public ScalarTypeFloat() {
|
||||
super(Float.class, true, Types.REAL);
|
||||
}
|
||||
|
||||
public void bind(DataBind b, Float value) throws SQLException {
|
||||
if (value == null){
|
||||
b.setNull(Types.REAL);
|
||||
} else {
|
||||
b.setFloat(value.floatValue());
|
||||
}
|
||||
}
|
||||
public ScalarTypeFloat() {
|
||||
super(Float.class, true, Types.REAL);
|
||||
}
|
||||
|
||||
public Float read(DataReader dataReader) throws SQLException {
|
||||
|
||||
return dataReader.getFloat();
|
||||
}
|
||||
|
||||
public Object toJdbcType(Object value) {
|
||||
return BasicTypeConverter.toFloat(value);
|
||||
}
|
||||
@Override
|
||||
public void bind(DataBind b, Float value) throws SQLException {
|
||||
if (value == null) {
|
||||
b.setNull(Types.REAL);
|
||||
} else {
|
||||
b.setFloat(value.floatValue());
|
||||
}
|
||||
}
|
||||
|
||||
public Float toBeanType(Object value) {
|
||||
return BasicTypeConverter.toFloat(value);
|
||||
}
|
||||
@Override
|
||||
public Float read(DataReader dataReader) throws SQLException {
|
||||
return dataReader.getFloat();
|
||||
}
|
||||
|
||||
public String formatValue(Float t) {
|
||||
return t.toString();
|
||||
}
|
||||
|
||||
public Float parse(String value) {
|
||||
return Float.valueOf(value);
|
||||
}
|
||||
|
||||
public Float parseDateTime(long systemTimeMillis) {
|
||||
return Float.valueOf(systemTimeMillis);
|
||||
}
|
||||
@Override
|
||||
public Object toJdbcType(Object value) {
|
||||
return BasicTypeConverter.toFloat(value);
|
||||
}
|
||||
|
||||
public boolean isDateTimeCapable() {
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
public Float toBeanType(Object value) {
|
||||
return BasicTypeConverter.toFloat(value);
|
||||
}
|
||||
|
||||
public String toJsonString(Float value) {
|
||||
if(value.isInfinite() || value.isNaN()) {
|
||||
return "null";
|
||||
} else {
|
||||
return value.toString();
|
||||
}
|
||||
}
|
||||
|
||||
public Object readData(DataInput dataInput) throws IOException {
|
||||
if (!dataInput.readBoolean()) {
|
||||
return null;
|
||||
} else {
|
||||
float val = dataInput.readFloat();
|
||||
return Float.valueOf(val);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public String formatValue(Float t) {
|
||||
return t.toString();
|
||||
}
|
||||
|
||||
public void writeData(DataOutput dataOutput, Object v) throws IOException {
|
||||
|
||||
Float value = (Float)v;
|
||||
if (value == null){
|
||||
dataOutput.writeBoolean(false);
|
||||
} else {
|
||||
dataOutput.writeBoolean(true);
|
||||
dataOutput.writeFloat(value.floatValue());
|
||||
}
|
||||
@Override
|
||||
public Float parse(String value) {
|
||||
return Float.valueOf(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float parseDateTime(long systemTimeMillis) {
|
||||
return Float.valueOf(systemTimeMillis);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDateTimeCapable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object readData(DataInput dataInput) throws IOException {
|
||||
if (!dataInput.readBoolean()) {
|
||||
return null;
|
||||
} else {
|
||||
float val = dataInput.readFloat();
|
||||
return Float.valueOf(val);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, JsonToken event) throws IOException {
|
||||
return ctx.getFloatValue();
|
||||
}
|
||||
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) throws IOException {
|
||||
ctx.writeNumberField(name, (Float)value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeData(DataOutput dataOutput, Object v) throws IOException {
|
||||
|
||||
Float value = (Float) v;
|
||||
if (value == null) {
|
||||
dataOutput.writeBoolean(false);
|
||||
} else {
|
||||
dataOutput.writeBoolean(true);
|
||||
dataOutput.writeFloat(value.floatValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, JsonToken event) throws IOException {
|
||||
return ctx.getFloatValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) throws IOException {
|
||||
ctx.writeNumberField(name, (Float) value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,10 +32,12 @@ public class ScalarTypeInetAddress extends ScalarTypeBaseVarchar<InetAddress> {
|
||||
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);
|
||||
|
||||
@@ -17,60 +17,70 @@ import com.fasterxml.jackson.core.JsonToken;
|
||||
*/
|
||||
public class ScalarTypeInteger extends ScalarTypeBase<Integer> {
|
||||
|
||||
public ScalarTypeInteger() {
|
||||
super(Integer.class, true, Types.INTEGER);
|
||||
}
|
||||
|
||||
public void bind(DataBind b, Integer value) throws SQLException {
|
||||
if (value == null){
|
||||
b.setNull(Types.INTEGER);
|
||||
} else {
|
||||
b.setInt(value.intValue());
|
||||
}
|
||||
}
|
||||
public ScalarTypeInteger() {
|
||||
super(Integer.class, true, Types.INTEGER);
|
||||
}
|
||||
|
||||
public Integer read(DataReader dataReader) throws SQLException {
|
||||
|
||||
return dataReader.getInt();
|
||||
}
|
||||
|
||||
public Object readData(DataInput dataInput) throws IOException {
|
||||
return Integer.valueOf(dataInput.readInt());
|
||||
@Override
|
||||
public void bind(DataBind b, Integer value) throws SQLException {
|
||||
if (value == null) {
|
||||
b.setNull(Types.INTEGER);
|
||||
} else {
|
||||
b.setInt(value.intValue());
|
||||
}
|
||||
}
|
||||
|
||||
public void writeData(DataOutput dataOutput, Object v) throws IOException {
|
||||
dataOutput.writeInt((Integer) v);
|
||||
}
|
||||
|
||||
public Object toJdbcType(Object value) {
|
||||
return BasicTypeConverter.toInteger(value);
|
||||
}
|
||||
@Override
|
||||
public Integer read(DataReader dataReader) throws SQLException {
|
||||
return dataReader.getInt();
|
||||
}
|
||||
|
||||
public Integer toBeanType(Object value) {
|
||||
return BasicTypeConverter.toInteger(value);
|
||||
}
|
||||
@Override
|
||||
public Object readData(DataInput dataInput) throws IOException {
|
||||
return Integer.valueOf(dataInput.readInt());
|
||||
}
|
||||
|
||||
public String formatValue(Integer v) {
|
||||
return v.toString();
|
||||
}
|
||||
@Override
|
||||
public void writeData(DataOutput dataOutput, Object v) throws IOException {
|
||||
dataOutput.writeInt((Integer) v);
|
||||
}
|
||||
|
||||
public Integer parse(String value) {
|
||||
return Integer.valueOf(value);
|
||||
}
|
||||
@Override
|
||||
public Object toJdbcType(Object value) {
|
||||
return BasicTypeConverter.toInteger(value);
|
||||
}
|
||||
|
||||
public Integer parseDateTime(long systemTimeMillis) {
|
||||
throw new TextException("Not Supported");
|
||||
}
|
||||
@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 parseDateTime(long systemTimeMillis) {
|
||||
throw new TextException("Not Supported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDateTimeCapable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isDateTimeCapable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, JsonToken event) throws IOException {
|
||||
return ctx.getIntValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) throws IOException {
|
||||
ctx.writeNumberField(name, (Integer) value);
|
||||
}
|
||||
|
||||
@@ -12,39 +12,41 @@ import com.avaje.ebeaninternal.server.core.BasicTypeConverter;
|
||||
*/
|
||||
public class ScalarTypeJodaDateMidnight extends ScalarTypeBaseDate<DateMidnight> {
|
||||
|
||||
/**
|
||||
* Instantiates a new scalar type joda date midnight.
|
||||
*/
|
||||
public ScalarTypeJodaDateMidnight() {
|
||||
super(DateMidnight.class, false, Types.DATE);
|
||||
}
|
||||
/**
|
||||
* Instantiates a new scalar type joda date midnight.
|
||||
*/
|
||||
public ScalarTypeJodaDateMidnight() {
|
||||
super(DateMidnight.class, false, Types.DATE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long convertToMillis(Object value) {
|
||||
return ((DateMidnight) value).getMillis();
|
||||
}
|
||||
|
||||
@Override
|
||||
public DateMidnight convertFromDate(Date ts) {
|
||||
return new DateMidnight(ts.getTime());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Date convertToDate(DateMidnight t) {
|
||||
return new Date(t.getMillis());
|
||||
}
|
||||
|
||||
public Object toJdbcType(Object value) {
|
||||
if (value instanceof DateMidnight){
|
||||
return new Date(((DateMidnight)value).getMillis());
|
||||
}
|
||||
return BasicTypeConverter.toDate(value);
|
||||
}
|
||||
@Override
|
||||
public DateMidnight convertFromDate(Date ts) {
|
||||
return new DateMidnight(ts.getTime());
|
||||
}
|
||||
|
||||
public DateMidnight toBeanType(Object value) {
|
||||
if (value instanceof java.util.Date){
|
||||
return new DateMidnight(((java.util.Date)value).getTime());
|
||||
}
|
||||
return (DateMidnight)value;
|
||||
}
|
||||
@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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,37 +12,39 @@ import com.avaje.ebeaninternal.server.core.BasicTypeConverter;
|
||||
*/
|
||||
public class ScalarTypeJodaDateTime extends ScalarTypeBaseDateTime<DateTime> {
|
||||
|
||||
public ScalarTypeJodaDateTime() {
|
||||
super(DateTime.class, false, Types.TIMESTAMP);
|
||||
}
|
||||
|
||||
public ScalarTypeJodaDateTime() {
|
||||
super(DateTime.class, false, Types.TIMESTAMP);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long convertToMillis(Object value) {
|
||||
return ((DateTime) value).getMillis();
|
||||
}
|
||||
|
||||
@Override
|
||||
public DateTime convertFromTimestamp(Timestamp ts) {
|
||||
return new DateTime(ts.getTime());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Timestamp convertToTimestamp(DateTime t) {
|
||||
return new Timestamp(t.getMillis());
|
||||
@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());
|
||||
}
|
||||
|
||||
public Object toJdbcType(Object value) {
|
||||
if (value instanceof DateTime){
|
||||
return new Timestamp(((DateTime)value).getMillis());
|
||||
}
|
||||
return BasicTypeConverter.toTimestamp(value);
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
public DateTime toBeanType(Object value) {
|
||||
if (value instanceof java.util.Date){
|
||||
return new DateTime(((java.util.Date)value).getTime());
|
||||
}
|
||||
return (DateTime)value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -12,41 +12,44 @@ import com.avaje.ebeaninternal.server.core.BasicTypeConverter;
|
||||
*/
|
||||
public class ScalarTypeJodaLocalDate extends ScalarTypeBaseDate<LocalDate> {
|
||||
|
||||
public ScalarTypeJodaLocalDate() {
|
||||
super(LocalDate.class, false, Types.DATE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long convertToMillis(Object value) {
|
||||
return ((LocalDate)value).toDateMidnight().getMillis();
|
||||
public ScalarTypeJodaLocalDate() {
|
||||
super(LocalDate.class, false, Types.DATE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalDate convertFromDate(Date ts) {
|
||||
return new LocalDate(((java.util.Date)ts).getTime());
|
||||
}
|
||||
public long convertToMillis(Object value) {
|
||||
return ((LocalDate) value).toDateMidnight().getMillis();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Date convertToDate(LocalDate t) {
|
||||
return new java.sql.Date(t.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());
|
||||
}
|
||||
|
||||
public Object toJdbcType(Object value) {
|
||||
if (value instanceof LocalDate){
|
||||
return new java.sql.Date(((LocalDate)value).toDateMidnight().getMillis());
|
||||
}
|
||||
return BasicTypeConverter.toDate(value);
|
||||
}
|
||||
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 parseDateTime(long systemTimeMillis) {
|
||||
return new LocalDate(systemTimeMillis);
|
||||
}
|
||||
|
||||
public LocalDate toBeanType(Object value) {
|
||||
if (value instanceof java.util.Date){
|
||||
return new LocalDate(((java.util.Date)value).getTime());
|
||||
}
|
||||
return (LocalDate)value;
|
||||
}
|
||||
|
||||
public LocalDate parseDateTime(long systemTimeMillis) {
|
||||
return new LocalDate(systemTimeMillis);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+31
-30
@@ -12,43 +12,44 @@ import com.avaje.ebeaninternal.server.core.BasicTypeConverter;
|
||||
*/
|
||||
public class ScalarTypeJodaLocalDateTime extends ScalarTypeBaseDateTime<LocalDateTime> {
|
||||
|
||||
public ScalarTypeJodaLocalDateTime() {
|
||||
super(LocalDateTime.class, false, Types.TIMESTAMP);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public long convertToMillis(Object value) {
|
||||
return ((LocalDateTime)value).toDateTime().getMillis();
|
||||
public ScalarTypeJodaLocalDateTime() {
|
||||
super(LocalDateTime.class, false, Types.TIMESTAMP);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long convertToMillis(Object value) {
|
||||
return ((LocalDateTime) value).toDateTime().getMillis();
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalDateTime convertFromTimestamp(Timestamp ts) {
|
||||
return new LocalDateTime(ts.getTime());
|
||||
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 Timestamp convertToTimestamp(LocalDateTime t) {
|
||||
return new Timestamp(t.toDateTime().getMillis());
|
||||
@Override
|
||||
public LocalDateTime toBeanType(Object value) {
|
||||
if (value instanceof java.util.Date) {
|
||||
return new LocalDateTime(((java.util.Date) value).getTime());
|
||||
}
|
||||
|
||||
public Object toJdbcType(Object value) {
|
||||
if (value instanceof LocalDateTime){
|
||||
return new Timestamp(((LocalDateTime)value).toDateTime().getMillis());
|
||||
}
|
||||
return BasicTypeConverter.toTimestamp(value);
|
||||
}
|
||||
return (LocalDateTime) value;
|
||||
}
|
||||
|
||||
public LocalDateTime toBeanType(Object value) {
|
||||
if (value instanceof java.util.Date){
|
||||
return new LocalDateTime(((java.util.Date)value).getTime());
|
||||
}
|
||||
return (LocalDateTime)value;
|
||||
}
|
||||
|
||||
public LocalDateTime parseDateTime(long systemTimeMillis) {
|
||||
return new LocalDateTime(systemTimeMillis);
|
||||
}
|
||||
@Override
|
||||
public LocalDateTime parseDateTime(long systemTimeMillis) {
|
||||
return new LocalDateTime(systemTimeMillis);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,92 +20,102 @@ import com.fasterxml.jackson.core.JsonToken;
|
||||
*/
|
||||
public class ScalarTypeJodaLocalTime extends ScalarTypeBase<LocalTime> {
|
||||
|
||||
public ScalarTypeJodaLocalTime() {
|
||||
super(LocalTime.class, false, Types.TIME);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
public ScalarTypeJodaLocalTime() {
|
||||
super(LocalTime.class, false, Types.TIME);
|
||||
}
|
||||
|
||||
public LocalTime read(DataReader dataReader) throws SQLException {
|
||||
|
||||
Time sqlTime = dataReader.getTime();
|
||||
if (sqlTime == null){
|
||||
return null;
|
||||
} else {
|
||||
return new LocalTime(sqlTime, DateTimeZone.UTC);
|
||||
}
|
||||
}
|
||||
|
||||
public Object toJdbcType(Object value) {
|
||||
if (value instanceof LocalTime){
|
||||
return new Time(((LocalTime)value).getMillisOfDay());
|
||||
}
|
||||
return BasicTypeConverter.toTime(value);
|
||||
}
|
||||
|
||||
public LocalTime toBeanType(Object value) {
|
||||
if (value instanceof java.util.Date){
|
||||
return new LocalTime(value, DateTimeZone.UTC);
|
||||
}
|
||||
return (LocalTime)value;
|
||||
}
|
||||
|
||||
public String formatValue(LocalTime v) {
|
||||
return v.toString();
|
||||
@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);
|
||||
}
|
||||
|
||||
public LocalTime parse(String value) {
|
||||
return new LocalTime(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) throws IOException {
|
||||
ctx.writeStringField(name, value.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, JsonToken event) throws IOException {
|
||||
if (JsonToken.VALUE_NUMBER_INT == event) {
|
||||
long millis = ctx.getLongValue();
|
||||
return parseDateTime(millis);
|
||||
} else {
|
||||
String string = ctx.getValueAsString();
|
||||
throw new RuntimeException("convert "+string);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, JsonToken event) throws IOException {
|
||||
if (JsonToken.VALUE_NUMBER_INT == event) {
|
||||
long millis = ctx.getLongValue();
|
||||
return parseDateTime(millis);
|
||||
} else {
|
||||
String string = ctx.getValueAsString();
|
||||
throw new RuntimeException("convert " + string);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalTime parseDateTime(long systemTimeMillis) {
|
||||
return new LocalTime(systemTimeMillis);
|
||||
}
|
||||
return new LocalTime(systemTimeMillis);
|
||||
}
|
||||
|
||||
public boolean isDateTimeCapable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public Object readData(DataInput dataInput) throws IOException {
|
||||
if (!dataInput.readBoolean()) {
|
||||
return null;
|
||||
} else {
|
||||
String val = dataInput.readUTF();
|
||||
return parse(val);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public boolean isDateTimeCapable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void writeData(DataOutput dataOutput, Object v) throws IOException {
|
||||
|
||||
Time value = (Time)v;
|
||||
if (value == null){
|
||||
dataOutput.writeBoolean(false);
|
||||
} else {
|
||||
dataOutput.writeBoolean(true);
|
||||
dataOutput.writeUTF(format(value));
|
||||
}
|
||||
@Override
|
||||
public Object readData(DataInput dataInput) throws IOException {
|
||||
if (!dataInput.readBoolean()) {
|
||||
return null;
|
||||
} else {
|
||||
String val = dataInput.readUTF();
|
||||
return parse(val);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeData(DataOutput dataOutput, Object v) throws IOException {
|
||||
|
||||
Time value = (Time) v;
|
||||
if (value == null) {
|
||||
dataOutput.writeBoolean(false);
|
||||
} else {
|
||||
dataOutput.writeBoolean(true);
|
||||
dataOutput.writeUTF(format(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,62 +3,63 @@ package com.avaje.ebeaninternal.server.type;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* ScalarType for java.util.Currency which converts to and from a VARCHAR
|
||||
* database column.
|
||||
* ScalarType for java.util.Currency which converts to and from a VARCHAR database column.
|
||||
*/
|
||||
public class ScalarTypeLocale extends ScalarTypeBaseVarchar<Locale> {
|
||||
|
||||
public ScalarTypeLocale() {
|
||||
super(Locale.class);
|
||||
}
|
||||
public ScalarTypeLocale() {
|
||||
super(Locale.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLength() {
|
||||
return 20;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Locale convertFromDbString(String dbValue) {
|
||||
return parse(dbValue);
|
||||
}
|
||||
@Override
|
||||
public int getLength() {
|
||||
return 20;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String convertToDbString(Locale beanValue) {
|
||||
return ((Locale) beanValue).toString();
|
||||
}
|
||||
@Override
|
||||
public Locale convertFromDbString(String dbValue) {
|
||||
return parse(dbValue);
|
||||
}
|
||||
|
||||
public String formatValue(Locale t) {
|
||||
return t.toString();
|
||||
}
|
||||
@Override
|
||||
public String convertToDbString(Locale beanValue) {
|
||||
return ((Locale) beanValue).toString();
|
||||
}
|
||||
|
||||
public Locale parse(String value) {
|
||||
@Override
|
||||
public String formatValue(Locale t) {
|
||||
return t.toString();
|
||||
}
|
||||
|
||||
int pos1 = -1;
|
||||
int pos2 = -1;
|
||||
@Override
|
||||
public Locale parse(String value) {
|
||||
|
||||
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);
|
||||
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 {
|
||||
String country = value.substring(pos1 + 1, pos2);
|
||||
String variant = value.substring(pos2 + 1);
|
||||
return new Locale(language, country, variant);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,73 +16,83 @@ import com.fasterxml.jackson.core.JsonToken;
|
||||
*/
|
||||
public class ScalarTypeLong extends ScalarTypeBase<Long> {
|
||||
|
||||
public ScalarTypeLong() {
|
||||
super(Long.class, true, Types.BIGINT);
|
||||
}
|
||||
|
||||
public void bind(DataBind b, Long value) throws SQLException {
|
||||
if (value == null){
|
||||
b.setNull(Types.BIGINT);
|
||||
} else {
|
||||
b.setLong(value.longValue());
|
||||
}
|
||||
}
|
||||
|
||||
public Long read(DataReader dataReader) throws SQLException {
|
||||
|
||||
return dataReader.getLong();
|
||||
}
|
||||
|
||||
public Object toJdbcType(Object value) {
|
||||
return BasicTypeConverter.toLong(value);
|
||||
}
|
||||
public ScalarTypeLong() {
|
||||
super(Long.class, true, Types.BIGINT);
|
||||
}
|
||||
|
||||
public Long toBeanType(Object value) {
|
||||
return BasicTypeConverter.toLong(value);
|
||||
}
|
||||
|
||||
public String formatValue(Long t) {
|
||||
return t.toString();
|
||||
@Override
|
||||
public void bind(DataBind b, Long value) throws SQLException {
|
||||
if (value == null) {
|
||||
b.setNull(Types.BIGINT);
|
||||
} else {
|
||||
b.setLong(value.longValue());
|
||||
}
|
||||
}
|
||||
|
||||
public Long parse(String value) {
|
||||
return Long.valueOf(value);
|
||||
}
|
||||
|
||||
public Long parseDateTime(long systemTimeMillis) {
|
||||
return Long.valueOf(systemTimeMillis);
|
||||
}
|
||||
@Override
|
||||
public Long read(DataReader dataReader) throws SQLException {
|
||||
return dataReader.getLong();
|
||||
}
|
||||
|
||||
public boolean isDateTimeCapable() {
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
public Object toJdbcType(Object value) {
|
||||
return BasicTypeConverter.toLong(value);
|
||||
}
|
||||
|
||||
public Object readData(DataInput dataInput) throws IOException {
|
||||
if (!dataInput.readBoolean()) {
|
||||
return null;
|
||||
} else {
|
||||
long val = dataInput.readLong();
|
||||
return Long.valueOf(val);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public Long toBeanType(Object value) {
|
||||
return BasicTypeConverter.toLong(value);
|
||||
}
|
||||
|
||||
public void writeData(DataOutput dataOutput, Object v) throws IOException {
|
||||
|
||||
Long value = (Long)v;
|
||||
if (value == null){
|
||||
dataOutput.writeBoolean(false);
|
||||
} else {
|
||||
dataOutput.writeBoolean(true);
|
||||
dataOutput.writeLong(value.longValue());
|
||||
}
|
||||
@Override
|
||||
public String formatValue(Long t) {
|
||||
return t.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long parse(String value) {
|
||||
return Long.valueOf(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long parseDateTime(long systemTimeMillis) {
|
||||
return Long.valueOf(systemTimeMillis);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDateTimeCapable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object readData(DataInput dataInput) throws IOException {
|
||||
if (!dataInput.readBoolean()) {
|
||||
return null;
|
||||
} else {
|
||||
long val = dataInput.readLong();
|
||||
return Long.valueOf(val);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, JsonToken event) throws IOException {
|
||||
return ctx.getLongValue();
|
||||
}
|
||||
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) throws IOException {
|
||||
ctx.writeNumberField(name, (Long)value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeData(DataOutput dataOutput, Object v) throws IOException {
|
||||
|
||||
Long value = (Long) v;
|
||||
if (value == null) {
|
||||
dataOutput.writeBoolean(false);
|
||||
} else {
|
||||
dataOutput.writeBoolean(true);
|
||||
dataOutput.writeLong(value.longValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, JsonToken event) throws IOException {
|
||||
return ctx.getLongValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) throws IOException {
|
||||
ctx.writeNumberField(name, (Long) value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,13 +8,12 @@ import java.sql.Types;
|
||||
*/
|
||||
public class ScalarTypeLongVarchar extends ScalarTypeClob {
|
||||
|
||||
public ScalarTypeLongVarchar() {
|
||||
super(true, Types.LONGVARCHAR);
|
||||
}
|
||||
public ScalarTypeLongVarchar() {
|
||||
super(true, Types.LONGVARCHAR);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String read(DataReader dataReader) throws SQLException {
|
||||
|
||||
return dataReader.getStringFromStream();
|
||||
}
|
||||
@Override
|
||||
public String read(DataReader dataReader) throws SQLException {
|
||||
return dataReader.getStringFromStream();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,79 +17,89 @@ import com.fasterxml.jackson.core.JsonToken;
|
||||
*/
|
||||
public class ScalarTypeMathBigInteger extends ScalarTypeBase<BigInteger> {
|
||||
|
||||
public ScalarTypeMathBigInteger() {
|
||||
super(BigInteger.class, false, Types.BIGINT);
|
||||
}
|
||||
|
||||
public void bind(DataBind b, BigInteger value) throws SQLException {
|
||||
if (value == null){
|
||||
b.setNull(Types.BIGINT);
|
||||
} else {
|
||||
b.setLong(value.longValue());
|
||||
}
|
||||
}
|
||||
public ScalarTypeMathBigInteger() {
|
||||
super(BigInteger.class, false, Types.BIGINT);
|
||||
}
|
||||
|
||||
public BigInteger read(DataReader dataReader) throws SQLException {
|
||||
|
||||
Long l = dataReader.getLong();
|
||||
if (l == null){
|
||||
return null;
|
||||
}
|
||||
return new BigInteger(String.valueOf(l));
|
||||
}
|
||||
|
||||
public Object toJdbcType(Object value) {
|
||||
return BasicTypeConverter.toLong(value);
|
||||
}
|
||||
|
||||
public BigInteger toBeanType(Object value) {
|
||||
return BasicTypeConverter.toMathBigInteger(value);
|
||||
}
|
||||
|
||||
|
||||
public String formatValue(BigInteger v) {
|
||||
return v.toString();
|
||||
@Override
|
||||
public void bind(DataBind b, BigInteger value) throws SQLException {
|
||||
if (value == null) {
|
||||
b.setNull(Types.BIGINT);
|
||||
} else {
|
||||
b.setLong(value.longValue());
|
||||
}
|
||||
}
|
||||
|
||||
public BigInteger parse(String value) {
|
||||
return new BigInteger(value);
|
||||
}
|
||||
@Override
|
||||
public BigInteger read(DataReader dataReader) throws SQLException {
|
||||
|
||||
public BigInteger parseDateTime(long systemTimeMillis) {
|
||||
return BigInteger.valueOf(systemTimeMillis);
|
||||
}
|
||||
|
||||
public boolean isDateTimeCapable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public Object readData(DataInput dataInput) throws IOException {
|
||||
if (!dataInput.readBoolean()) {
|
||||
return null;
|
||||
} else {
|
||||
long val = dataInput.readLong();
|
||||
return Long.valueOf(val);
|
||||
}
|
||||
Long l = dataReader.getLong();
|
||||
if (l == null) {
|
||||
return null;
|
||||
}
|
||||
return new BigInteger(String.valueOf(l));
|
||||
}
|
||||
|
||||
public void writeData(DataOutput dataOutput, Object v) throws IOException {
|
||||
|
||||
Long value = (Long)v;
|
||||
if (value == null){
|
||||
dataOutput.writeBoolean(false);
|
||||
} else {
|
||||
dataOutput.writeBoolean(true);
|
||||
dataOutput.writeLong(value.longValue());
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public Object toJdbcType(Object value) {
|
||||
return BasicTypeConverter.toLong(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, JsonToken event) throws IOException {
|
||||
return ctx.getDecimalValue().toBigInteger();
|
||||
@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 parseDateTime(long systemTimeMillis) {
|
||||
return BigInteger.valueOf(systemTimeMillis);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDateTimeCapable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object readData(DataInput dataInput) throws IOException {
|
||||
if (!dataInput.readBoolean()) {
|
||||
return null;
|
||||
} else {
|
||||
long val = dataInput.readLong();
|
||||
return Long.valueOf(val);
|
||||
}
|
||||
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) throws IOException {
|
||||
ctx.writeNumberField(name, ((BigInteger)value).longValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeData(DataOutput dataOutput, Object v) throws IOException {
|
||||
|
||||
Long value = (Long) v;
|
||||
if (value == null) {
|
||||
dataOutput.writeBoolean(false);
|
||||
} else {
|
||||
dataOutput.writeBoolean(true);
|
||||
dataOutput.writeLong(value.longValue());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, JsonToken event) throws IOException {
|
||||
return ctx.getDecimalValue().toBigInteger();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) throws IOException {
|
||||
ctx.writeNumberField(name, ((BigInteger) value).longValue());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,6 +7,8 @@ import java.sql.SQLException;
|
||||
import java.util.Map;
|
||||
|
||||
import com.avaje.ebean.config.dbplatform.PostgresPlatform;
|
||||
import com.avaje.ebean.json.EJson;
|
||||
import com.avaje.ebean.text.TextException;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonToken;
|
||||
@@ -66,28 +68,23 @@ public class ScalarTypePostgresHstore extends ScalarTypeBase<Map> {
|
||||
public Map toBeanType(Object value) {
|
||||
return (Map)value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) {
|
||||
// TODO Auto-generated method stub
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, JsonToken event) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String formatValue(Map v) {
|
||||
// TODO format as json
|
||||
return null;
|
||||
try {
|
||||
return EJson.write(v);
|
||||
} catch (IOException e) {
|
||||
throw new TextException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map parse(String value) {
|
||||
// TODO parse json into map
|
||||
return null;
|
||||
try {
|
||||
return EJson.parseObject(value);
|
||||
} catch (IOException e) {
|
||||
throw new TextException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -102,13 +99,24 @@ public class ScalarTypePostgresHstore extends ScalarTypeBase<Map> {
|
||||
|
||||
@Override
|
||||
public Object readData(DataInput dataInput) throws IOException {
|
||||
return null;
|
||||
String json = dataInput.readUTF();
|
||||
return parse(json);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeData(DataOutput dataOutput, Object v) throws IOException {
|
||||
|
||||
String json = format(v);
|
||||
dataOutput.writeUTF(json);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) throws IOException {
|
||||
EJson.write(value, ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, JsonToken event) throws IOException {
|
||||
return EJson.parseObject(ctx);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -17,73 +17,83 @@ import com.fasterxml.jackson.core.JsonToken;
|
||||
*/
|
||||
public class ScalarTypeShort extends ScalarTypeBase<Short> {
|
||||
|
||||
public ScalarTypeShort() {
|
||||
super(Short.class, true, Types.SMALLINT);
|
||||
}
|
||||
|
||||
public void bind(DataBind b, Short value) throws SQLException {
|
||||
if (value == null){
|
||||
b.setNull(Types.SMALLINT);
|
||||
} else {
|
||||
b.setShort(value.shortValue());
|
||||
}
|
||||
}
|
||||
public ScalarTypeShort() {
|
||||
super(Short.class, true, Types.SMALLINT);
|
||||
}
|
||||
|
||||
public Short read(DataReader dataReader) throws SQLException {
|
||||
|
||||
return dataReader.getShort();
|
||||
}
|
||||
|
||||
public Object toJdbcType(Object value) {
|
||||
return BasicTypeConverter.toShort(value);
|
||||
}
|
||||
|
||||
public Short toBeanType(Object value) {
|
||||
return BasicTypeConverter.toShort(value);
|
||||
}
|
||||
|
||||
public String formatValue(Short v) {
|
||||
return v.toString();
|
||||
@Override
|
||||
public void bind(DataBind b, Short value) throws SQLException {
|
||||
if (value == null) {
|
||||
b.setNull(Types.SMALLINT);
|
||||
} else {
|
||||
b.setShort(value.shortValue());
|
||||
}
|
||||
}
|
||||
|
||||
public Short parse(String value) {
|
||||
return Short.valueOf(value);
|
||||
}
|
||||
@Override
|
||||
public Short read(DataReader dataReader) throws SQLException {
|
||||
return dataReader.getShort();
|
||||
}
|
||||
|
||||
public Short parseDateTime(long systemTimeMillis) {
|
||||
throw new TextException("Not Supported");
|
||||
}
|
||||
@Override
|
||||
public Object toJdbcType(Object value) {
|
||||
return BasicTypeConverter.toShort(value);
|
||||
}
|
||||
|
||||
public boolean isDateTimeCapable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object readData(DataInput dataInput) throws IOException {
|
||||
if (!dataInput.readBoolean()) {
|
||||
return null;
|
||||
} else {
|
||||
short val = dataInput.readShort();
|
||||
return Short.valueOf(val);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public Short toBeanType(Object value) {
|
||||
return BasicTypeConverter.toShort(value);
|
||||
}
|
||||
|
||||
public void writeData(DataOutput dataOutput, Object v) throws IOException {
|
||||
|
||||
Short value = (Short)v;
|
||||
if (value == null){
|
||||
dataOutput.writeBoolean(false);
|
||||
} else {
|
||||
dataOutput.writeBoolean(true);
|
||||
dataOutput.writeShort(value.shortValue());
|
||||
}
|
||||
@Override
|
||||
public String formatValue(Short v) {
|
||||
return v.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Short parse(String value) {
|
||||
return Short.valueOf(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Short parseDateTime(long systemTimeMillis) {
|
||||
throw new TextException("Not Supported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDateTimeCapable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object readData(DataInput dataInput) throws IOException {
|
||||
if (!dataInput.readBoolean()) {
|
||||
return null;
|
||||
} else {
|
||||
short val = dataInput.readShort();
|
||||
return Short.valueOf(val);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, JsonToken event) throws IOException {
|
||||
return ctx.getShortValue();
|
||||
}
|
||||
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) throws IOException {
|
||||
ctx.writeNumberField(name, (Short)value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeData(DataOutput dataOutput, Object v) throws IOException {
|
||||
|
||||
Short value = (Short) v;
|
||||
if (value == null) {
|
||||
dataOutput.writeBoolean(false);
|
||||
} else {
|
||||
dataOutput.writeBoolean(true);
|
||||
dataOutput.writeShort(value.shortValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, JsonToken event) throws IOException {
|
||||
return ctx.getShortValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) throws IOException {
|
||||
ctx.writeNumberField(name, (Short) value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,72 +16,82 @@ import com.fasterxml.jackson.core.JsonToken;
|
||||
*/
|
||||
public class ScalarTypeString extends ScalarTypeBase<String> {
|
||||
|
||||
public ScalarTypeString() {
|
||||
super(String.class, true, Types.VARCHAR);
|
||||
}
|
||||
|
||||
public void bind(DataBind b, String value) throws SQLException {
|
||||
if (value == null){
|
||||
b.setNull(Types.VARCHAR);
|
||||
} else {
|
||||
b.setString(value);
|
||||
}
|
||||
}
|
||||
public ScalarTypeString() {
|
||||
super(String.class, true, Types.VARCHAR);
|
||||
}
|
||||
|
||||
public String read(DataReader dataReader) throws SQLException {
|
||||
|
||||
return dataReader.getString();
|
||||
}
|
||||
|
||||
public Object toJdbcType(Object value) {
|
||||
return BasicTypeConverter.toString(value);
|
||||
}
|
||||
|
||||
public String toBeanType(Object value) {
|
||||
return BasicTypeConverter.toString(value);
|
||||
}
|
||||
|
||||
public String formatValue(String t) {
|
||||
return t;
|
||||
@Override
|
||||
public void bind(DataBind b, String value) throws SQLException {
|
||||
if (value == null) {
|
||||
b.setNull(Types.VARCHAR);
|
||||
} else {
|
||||
b.setString(value);
|
||||
}
|
||||
}
|
||||
|
||||
public String parse(String value) {
|
||||
return value;
|
||||
}
|
||||
|
||||
public String parseDateTime(long systemTimeMillis) {
|
||||
return String.valueOf(systemTimeMillis);
|
||||
}
|
||||
@Override
|
||||
public String read(DataReader dataReader) throws SQLException {
|
||||
return dataReader.getString();
|
||||
}
|
||||
|
||||
public boolean isDateTimeCapable() {
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
public Object toJdbcType(Object value) {
|
||||
return BasicTypeConverter.toString(value);
|
||||
}
|
||||
|
||||
public Object readData(DataInput dataInput) throws IOException {
|
||||
if (!dataInput.readBoolean()) {
|
||||
return null;
|
||||
} else {
|
||||
return dataInput.readUTF();
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public String toBeanType(Object value) {
|
||||
return BasicTypeConverter.toString(value);
|
||||
}
|
||||
|
||||
public void writeData(DataOutput dataOutput, Object v) throws IOException {
|
||||
|
||||
String value = (String)v;
|
||||
if (value == null){
|
||||
dataOutput.writeBoolean(false);
|
||||
} else {
|
||||
dataOutput.writeBoolean(true);
|
||||
dataOutput.writeUTF(value);
|
||||
}
|
||||
@Override
|
||||
public String formatValue(String t) {
|
||||
return t;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String parse(String value) {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String parseDateTime(long systemTimeMillis) {
|
||||
return String.valueOf(systemTimeMillis);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDateTimeCapable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object readData(DataInput dataInput) throws IOException {
|
||||
if (!dataInput.readBoolean()) {
|
||||
return null;
|
||||
} else {
|
||||
return dataInput.readUTF();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, JsonToken event) throws IOException {
|
||||
return ctx.getValueAsString();
|
||||
}
|
||||
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) throws IOException {
|
||||
ctx.writeStringField(name, (String)value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeData(DataOutput dataOutput, Object v) throws IOException {
|
||||
|
||||
String value = (String) v;
|
||||
if (value == null) {
|
||||
dataOutput.writeBoolean(false);
|
||||
} else {
|
||||
dataOutput.writeBoolean(true);
|
||||
dataOutput.writeUTF(value);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, JsonToken event) throws IOException {
|
||||
return ctx.getValueAsString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) throws IOException {
|
||||
ctx.writeStringField(name, (String) value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,67 +17,75 @@ import com.fasterxml.jackson.core.JsonToken;
|
||||
*/
|
||||
public class ScalarTypeTime extends ScalarTypeBase<Time> {
|
||||
|
||||
public ScalarTypeTime() {
|
||||
super(Time.class, true, Types.TIME);
|
||||
}
|
||||
|
||||
public void bind(DataBind b, Time value) throws SQLException {
|
||||
if (value == null){
|
||||
b.setNull(Types.TIME);
|
||||
} else {
|
||||
b.setTime(value);
|
||||
}
|
||||
}
|
||||
public ScalarTypeTime() {
|
||||
super(Time.class, true, Types.TIME);
|
||||
}
|
||||
|
||||
public Time read(DataReader dataReader) throws SQLException {
|
||||
|
||||
return dataReader.getTime();
|
||||
}
|
||||
|
||||
public Object toJdbcType(Object value) {
|
||||
return BasicTypeConverter.toTime(value);
|
||||
}
|
||||
|
||||
public Time toBeanType(Object value) {
|
||||
return BasicTypeConverter.toTime(value);
|
||||
}
|
||||
|
||||
|
||||
public String formatValue(Time v) {
|
||||
return v.toString();
|
||||
@Override
|
||||
public void bind(DataBind b, Time value) throws SQLException {
|
||||
if (value == null) {
|
||||
b.setNull(Types.TIME);
|
||||
} else {
|
||||
b.setTime(value);
|
||||
}
|
||||
}
|
||||
|
||||
public Time parse(String value) {
|
||||
return Time.valueOf(value);
|
||||
}
|
||||
|
||||
public Time parseDateTime(long systemTimeMillis) {
|
||||
return new Time(systemTimeMillis);
|
||||
}
|
||||
@Override
|
||||
public Time read(DataReader dataReader) throws SQLException {
|
||||
return dataReader.getTime();
|
||||
}
|
||||
|
||||
public boolean isDateTimeCapable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public Object readData(DataInput dataInput) throws IOException {
|
||||
if (!dataInput.readBoolean()) {
|
||||
return null;
|
||||
} else {
|
||||
String val = dataInput.readUTF();
|
||||
return parse(val);
|
||||
}
|
||||
@Override
|
||||
public Object toJdbcType(Object value) {
|
||||
return BasicTypeConverter.toTime(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Time toBeanType(Object value) {
|
||||
return BasicTypeConverter.toTime(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String formatValue(Time v) {
|
||||
return v.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Time parse(String value) {
|
||||
return Time.valueOf(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Time parseDateTime(long systemTimeMillis) {
|
||||
return new Time(systemTimeMillis);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDateTimeCapable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object readData(DataInput dataInput) throws IOException {
|
||||
if (!dataInput.readBoolean()) {
|
||||
return null;
|
||||
} else {
|
||||
String val = dataInput.readUTF();
|
||||
return parse(val);
|
||||
}
|
||||
}
|
||||
|
||||
public void writeData(DataOutput dataOutput, Object v) throws IOException {
|
||||
|
||||
Time value = (Time)v;
|
||||
if (value == null){
|
||||
dataOutput.writeBoolean(false);
|
||||
} else {
|
||||
dataOutput.writeBoolean(true);
|
||||
dataOutput.writeUTF(format(value));
|
||||
}
|
||||
@Override
|
||||
public void writeData(DataOutput dataOutput, Object v) throws IOException {
|
||||
|
||||
Time value = (Time) v;
|
||||
if (value == null) {
|
||||
dataOutput.writeBoolean(false);
|
||||
} else {
|
||||
dataOutput.writeBoolean(true);
|
||||
dataOutput.writeUTF(format(value));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, JsonToken event) throws IOException {
|
||||
@@ -86,7 +94,7 @@ public class ScalarTypeTime extends ScalarTypeBase<Time> {
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object value) throws IOException {
|
||||
ctx.writeStringField(name, value.toString());
|
||||
ctx.writeStringField(name, format(value));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -3,8 +3,7 @@ package com.avaje.ebeaninternal.server.type;
|
||||
import java.util.TimeZone;
|
||||
|
||||
/**
|
||||
* ScalarType for java.util.TimeZone which converts to and from a VARCHAR
|
||||
* database column.
|
||||
* ScalarType for java.util.TimeZone which converts to and from a VARCHAR database column.
|
||||
*/
|
||||
public class ScalarTypeTimeZone extends ScalarTypeBaseVarchar<TimeZone> {
|
||||
|
||||
@@ -27,10 +26,12 @@ public class ScalarTypeTimeZone extends ScalarTypeBaseVarchar<TimeZone> {
|
||||
return ((TimeZone) beanValue).getID();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String formatValue(TimeZone v) {
|
||||
return v.getID();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TimeZone parse(String value) {
|
||||
return TimeZone.getTimeZone(value);
|
||||
}
|
||||
|
||||
@@ -11,43 +11,46 @@ import com.avaje.ebeaninternal.server.core.BasicTypeConverter;
|
||||
*/
|
||||
public class ScalarTypeTimestamp extends ScalarTypeBaseDateTime<Timestamp> {
|
||||
|
||||
public ScalarTypeTimestamp() {
|
||||
super(Timestamp.class, true, Types.TIMESTAMP);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long convertToMillis(Object value) {
|
||||
return ((Timestamp)value).getTime();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Timestamp convertFromTimestamp(Timestamp ts) {
|
||||
return ts;
|
||||
public ScalarTypeTimestamp() {
|
||||
super(Timestamp.class, true, Types.TIMESTAMP);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long convertToMillis(Object value) {
|
||||
return ((Timestamp) value).getTime();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Timestamp convertFromTimestamp(Timestamp ts) {
|
||||
return ts;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Timestamp convertToTimestamp(Timestamp t) {
|
||||
return t;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bind(DataBind b, Timestamp value) throws SQLException {
|
||||
if (value == null) {
|
||||
b.setNull(Types.TIMESTAMP);
|
||||
} else {
|
||||
b.setTimestamp(value);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Timestamp convertToTimestamp(Timestamp t) {
|
||||
return t;
|
||||
}
|
||||
@Override
|
||||
public Timestamp read(DataReader dataReader) throws SQLException {
|
||||
return dataReader.getTimestamp();
|
||||
}
|
||||
|
||||
public void bind(DataBind b, Timestamp value) throws SQLException {
|
||||
if (value == null){
|
||||
b.setNull(Types.TIMESTAMP);
|
||||
} else {
|
||||
b.setTimestamp(value);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public Object toJdbcType(Object value) {
|
||||
return BasicTypeConverter.toTimestamp(value);
|
||||
}
|
||||
|
||||
public Timestamp read(DataReader dataReader) throws SQLException {
|
||||
|
||||
return dataReader.getTimestamp();
|
||||
}
|
||||
|
||||
public Object toJdbcType(Object value) {
|
||||
return BasicTypeConverter.toTimestamp(value);
|
||||
}
|
||||
|
||||
public Timestamp toBeanType(Object value) {
|
||||
return BasicTypeConverter.toTimestamp(value);
|
||||
}
|
||||
@Override
|
||||
public Timestamp toBeanType(Object value) {
|
||||
return BasicTypeConverter.toTimestamp(value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,33 +10,35 @@ import com.avaje.ebean.text.TextException;
|
||||
*/
|
||||
public class ScalarTypeURI extends ScalarTypeBaseVarchar<URI> {
|
||||
|
||||
public ScalarTypeURI() {
|
||||
super(URI.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI convertFromDbString(String dbValue) {
|
||||
try {
|
||||
return new URI(dbValue);
|
||||
} catch (URISyntaxException e) {
|
||||
throw new RuntimeException("Error with URI ["+dbValue+"] "+e);
|
||||
}
|
||||
}
|
||||
public ScalarTypeURI() {
|
||||
super(URI.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String convertToDbString(URI beanValue) {
|
||||
return beanValue.toString();
|
||||
@Override
|
||||
public URI convertFromDbString(String dbValue) {
|
||||
try {
|
||||
return new URI(dbValue);
|
||||
} catch (URISyntaxException e) {
|
||||
throw new RuntimeException("Error with URI [" + dbValue + "] " + e);
|
||||
}
|
||||
}
|
||||
|
||||
public String formatValue(URI v) {
|
||||
return v.toString();
|
||||
@Override
|
||||
public String convertToDbString(URI beanValue) {
|
||||
return beanValue.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String formatValue(URI v) {
|
||||
return v.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI parse(String value) {
|
||||
try {
|
||||
return new URI(value);
|
||||
} catch (URISyntaxException e) {
|
||||
throw new TextException("Error with URI [" + value + "] ", e);
|
||||
}
|
||||
|
||||
public URI parse(String value) {
|
||||
try {
|
||||
return new URI(value);
|
||||
} catch (URISyntaxException e) {
|
||||
throw new TextException("Error with URI ["+value+"] ", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,52 +1,45 @@
|
||||
package com.avaje.ebeaninternal.server.type;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
import com.avaje.ebean.text.TextException;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonToken;
|
||||
|
||||
/**
|
||||
* ScalarType for java.net.URL which converts to and from a VARCHAR database column.
|
||||
*/
|
||||
public class ScalarTypeURL extends ScalarTypeBaseVarchar<URL> {
|
||||
|
||||
public ScalarTypeURL() {
|
||||
super(URL.class);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public URL convertFromDbString(String dbValue) {
|
||||
try {
|
||||
return new URL(dbValue);
|
||||
} catch (MalformedURLException e) {
|
||||
throw new RuntimeException("Error with URL ["+dbValue+"] "+e);
|
||||
}
|
||||
}
|
||||
public ScalarTypeURL() {
|
||||
super(URL.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String convertToDbString(URL beanValue) {
|
||||
return formatValue(beanValue);
|
||||
@Override
|
||||
public URL convertFromDbString(String dbValue) {
|
||||
try {
|
||||
return new URL(dbValue);
|
||||
} catch (MalformedURLException e) {
|
||||
throw new RuntimeException("Error with URL [" + dbValue + "] " + e);
|
||||
}
|
||||
}
|
||||
|
||||
public String formatValue(URL v) {
|
||||
return v.toString();
|
||||
}
|
||||
@Override
|
||||
public String convertToDbString(URL beanValue) {
|
||||
return formatValue(beanValue);
|
||||
}
|
||||
|
||||
public URL parse(String value) {
|
||||
try {
|
||||
return new URL(value);
|
||||
} catch (MalformedURLException e) {
|
||||
throw new TextException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, JsonToken event) throws IOException {
|
||||
return parse(ctx.getValueAsString());
|
||||
@Override
|
||||
public String formatValue(URL v) {
|
||||
return v.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public URL parse(String value) {
|
||||
try {
|
||||
return new URL(value);
|
||||
} catch (MalformedURLException e) {
|
||||
throw new TextException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ public class ScalarTypeUUIDBinary extends ScalarTypeBase<UUID> {
|
||||
return 16;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object toJdbcType(Object value) {
|
||||
return convertToBytes(value);
|
||||
}
|
||||
@@ -50,6 +51,7 @@ public class ScalarTypeUUIDBinary extends ScalarTypeBase<UUID> {
|
||||
throw new IllegalStateException("Never called");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDateTimeCapable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
package com.avaje.ebeaninternal.server.type;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
import com.avaje.ebeaninternal.server.core.BasicTypeConverter;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonToken;
|
||||
|
||||
/**
|
||||
* ScalarType for java.util.UUID which converts to and from a VARCHAR database column.
|
||||
@@ -31,25 +28,24 @@ public class ScalarTypeUUIDVarchar extends ScalarTypeBaseVarchar<UUID> {
|
||||
return formatValue(beanValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID toBeanType(Object value) {
|
||||
return BasicTypeConverter.toUUID(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object toJdbcType(Object value) {
|
||||
return BasicTypeConverter.convert(value, jdbcType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String formatValue(UUID v) {
|
||||
return v.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID parse(String value) {
|
||||
return UUID.fromString(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, JsonToken event) throws IOException {
|
||||
return UUID.fromString(ctx.getValueAsString());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -11,8 +11,8 @@ import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonToken;
|
||||
|
||||
/**
|
||||
* A ScalarType that uses a ScalarTypeConverter to convert to and from another
|
||||
* underlying ScalarType.
|
||||
* A ScalarType that uses a ScalarTypeConverter to convert to and from another underlying
|
||||
* ScalarType.
|
||||
* <p>
|
||||
* Enables the use of a simple interface to add additional scalarTypes.
|
||||
* </p>
|
||||
@@ -20,165 +20,183 @@ import com.fasterxml.jackson.core.JsonToken;
|
||||
* @author rbygrave
|
||||
*
|
||||
* @param <B>
|
||||
* the logical type
|
||||
* the logical type
|
||||
* @param <S>
|
||||
* the underlying scalar type this is converted to
|
||||
* the underlying scalar type this is converted to
|
||||
*/
|
||||
public class ScalarTypeWrapper<B, S> implements ScalarType<B> {
|
||||
|
||||
private final ScalarType<S> scalarType;
|
||||
private final ScalarTypeConverter<B, S> converter;
|
||||
private final Class<B> wrapperType;
|
||||
private final ScalarType<S> scalarType;
|
||||
|
||||
private final B nullValue;
|
||||
|
||||
public ScalarTypeWrapper(Class<B> wrapperType, ScalarType<S> scalarType, ScalarTypeConverter<B, S> converter) {
|
||||
this.scalarType = scalarType;
|
||||
this.converter = converter;
|
||||
this.nullValue = converter.getNullValue();
|
||||
this.wrapperType = wrapperType;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "ScalarTypeWrapper " + wrapperType + " to " + scalarType.getType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMutable() {
|
||||
return scalarType.isMutable();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDirty(Object value) {
|
||||
return scalarType.isDirty(value);
|
||||
}
|
||||
private final ScalarTypeConverter<B, S> converter;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Object readData(DataInput dataInput) throws IOException {
|
||||
Object v = scalarType.readData(dataInput);
|
||||
return converter.wrapValue((S)v);
|
||||
private final Class<B> wrapperType;
|
||||
|
||||
private final B nullValue;
|
||||
|
||||
public ScalarTypeWrapper(Class<B> wrapperType, ScalarType<S> scalarType, ScalarTypeConverter<B, S> converter) {
|
||||
this.scalarType = scalarType;
|
||||
this.converter = converter;
|
||||
this.nullValue = converter.getNullValue();
|
||||
this.wrapperType = wrapperType;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "ScalarTypeWrapper " + wrapperType + " to " + scalarType.getType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMutable() {
|
||||
return scalarType.isMutable();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDirty(Object value) {
|
||||
return scalarType.isDirty(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Object readData(DataInput dataInput) throws IOException {
|
||||
Object v = scalarType.readData(dataInput);
|
||||
return converter.wrapValue((S) v);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public void writeData(DataOutput dataOutput, Object v) throws IOException {
|
||||
S sv = converter.unwrapValue((B) v);
|
||||
scalarType.writeData(dataOutput, sv);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bind(DataBind b, B value) throws SQLException {
|
||||
if (value == null) {
|
||||
scalarType.bind(b, null);
|
||||
} else {
|
||||
S sv = converter.unwrapValue(value);
|
||||
scalarType.bind(b, sv);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void writeData(DataOutput dataOutput, Object v) throws IOException {
|
||||
S sv = converter.unwrapValue((B)v);
|
||||
scalarType.writeData(dataOutput, sv);
|
||||
@Override
|
||||
public int getJdbcType() {
|
||||
return scalarType.getJdbcType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLength() {
|
||||
return scalarType.getLength();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<B> getType() {
|
||||
return wrapperType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDateTimeCapable() {
|
||||
return scalarType.isDateTimeCapable();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isJdbcNative() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public String format(Object v) {
|
||||
return formatValue((B) v);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String formatValue(B v) {
|
||||
S sv = converter.unwrapValue(v);
|
||||
return scalarType.formatValue(sv);
|
||||
}
|
||||
|
||||
@Override
|
||||
public B parse(String value) {
|
||||
S sv = scalarType.parse(value);
|
||||
if (sv == null) {
|
||||
return nullValue;
|
||||
}
|
||||
return converter.wrapValue(sv);
|
||||
}
|
||||
|
||||
public void bind(DataBind b, B value) throws SQLException {
|
||||
if (value == null) {
|
||||
scalarType.bind(b, null);
|
||||
} else {
|
||||
S sv = converter.unwrapValue(value);
|
||||
scalarType.bind(b, sv);
|
||||
}
|
||||
@Override
|
||||
public B parseDateTime(long systemTimeMillis) {
|
||||
S sv = scalarType.parseDateTime(systemTimeMillis);
|
||||
if (sv == null) {
|
||||
return nullValue;
|
||||
}
|
||||
return converter.wrapValue(sv);
|
||||
}
|
||||
|
||||
public int getJdbcType() {
|
||||
return scalarType.getJdbcType();
|
||||
@Override
|
||||
public void loadIgnore(DataReader dataReader) {
|
||||
dataReader.incrementPos(1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public B read(DataReader dataReader) throws SQLException {
|
||||
|
||||
S sv = scalarType.read(dataReader);
|
||||
if (sv == null) {
|
||||
return nullValue;
|
||||
}
|
||||
return converter.wrapValue(sv);
|
||||
}
|
||||
|
||||
public int getLength() {
|
||||
return scalarType.getLength();
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public B toBeanType(Object value) {
|
||||
if (value == null) {
|
||||
return nullValue;
|
||||
}
|
||||
|
||||
public Class<B> getType() {
|
||||
return wrapperType;
|
||||
if (getType().isAssignableFrom(value.getClass())) {
|
||||
return (B) value;
|
||||
}
|
||||
|
||||
public boolean isDateTimeCapable() {
|
||||
return scalarType.isDateTimeCapable();
|
||||
if (value instanceof String) {
|
||||
return parse((String) value);
|
||||
}
|
||||
S sv = scalarType.toBeanType(value);
|
||||
return converter.wrapValue(sv);
|
||||
}
|
||||
|
||||
public boolean isJdbcNative() {
|
||||
return false;
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Object toJdbcType(Object value) {
|
||||
|
||||
Object sv = converter.unwrapValue((B) value);
|
||||
if (sv == null) {
|
||||
return nullValue;
|
||||
}
|
||||
return scalarType.toJdbcType(sv);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public String format(Object v) {
|
||||
return formatValue((B)v);
|
||||
}
|
||||
@Override
|
||||
public void accumulateScalarTypes(String propName, CtCompoundTypeScalarList list) {
|
||||
list.addScalarType(propName, this);
|
||||
}
|
||||
|
||||
public String formatValue(B v) {
|
||||
S sv = converter.unwrapValue(v);
|
||||
return scalarType.formatValue(sv);
|
||||
}
|
||||
|
||||
public B parse(String value) {
|
||||
S sv = scalarType.parse(value);
|
||||
if (sv == null) {
|
||||
return nullValue;
|
||||
}
|
||||
return converter.wrapValue(sv);
|
||||
}
|
||||
|
||||
public B parseDateTime(long systemTimeMillis) {
|
||||
S sv = scalarType.parseDateTime(systemTimeMillis);
|
||||
if (sv == null) {
|
||||
return nullValue;
|
||||
}
|
||||
return converter.wrapValue(sv);
|
||||
}
|
||||
|
||||
public void loadIgnore(DataReader dataReader) {
|
||||
dataReader.incrementPos(1);
|
||||
}
|
||||
|
||||
public B read(DataReader dataReader) throws SQLException {
|
||||
|
||||
S sv = scalarType.read(dataReader);
|
||||
if (sv == null) {
|
||||
return nullValue;
|
||||
}
|
||||
return converter.wrapValue(sv);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public B toBeanType(Object value) {
|
||||
if (value == null) {
|
||||
return nullValue;
|
||||
}
|
||||
if (getType().isAssignableFrom(value.getClass())) {
|
||||
return (B) value;
|
||||
}
|
||||
if (value instanceof String) {
|
||||
return parse((String) value);
|
||||
}
|
||||
S sv = scalarType.toBeanType(value);
|
||||
return converter.wrapValue(sv);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Object toJdbcType(Object value) {
|
||||
|
||||
Object sv = converter.unwrapValue((B) value);
|
||||
if (sv == null) {
|
||||
return nullValue;
|
||||
}
|
||||
return scalarType.toJdbcType(sv);
|
||||
}
|
||||
|
||||
public void accumulateScalarTypes(String propName, CtCompoundTypeScalarList list) {
|
||||
list.addScalarType(propName, this);
|
||||
}
|
||||
|
||||
public ScalarType<?> getScalarType() {
|
||||
return this;
|
||||
}
|
||||
public ScalarType<?> getScalarType() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Object jsonRead(JsonParser ctx, JsonToken event) throws IOException {
|
||||
Object object = scalarType.jsonRead(ctx, event);
|
||||
return converter.wrapValue((S)object);
|
||||
return converter.wrapValue((S) object);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator ctx, String name, Object beanValue) throws IOException {
|
||||
@SuppressWarnings("unchecked")
|
||||
S unwrapValue = converter.unwrapValue((B)beanValue);
|
||||
S unwrapValue = converter.unwrapValue((B) beanValue);
|
||||
scalarType.jsonWrite(ctx, name, unwrapValue);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -59,8 +59,9 @@ Export-Package: com.avaje.ebean,
|
||||
com.avaje.ebeaninternal.server.util,
|
||||
com.avaje.ebeaninternal.util
|
||||
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
|
||||
Require-Bundle:
|
||||
Require-Bundle:
|
||||
org.slf4j.api;bundle-version="1.7.2",
|
||||
com.fasterxml.jackson.core;bundle-version="2.4.3",
|
||||
com.springsource.org.joda.time;bundle-version="1.6.0"
|
||||
Import-Package: javax.persistence;version="2.0.0"
|
||||
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
package com.avaje.ebeaninternal.server.type;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonFactory;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonToken;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class ScalarTypePostgresHstoreTest {
|
||||
|
||||
ScalarTypePostgresHstore hstore = new ScalarTypePostgresHstore();
|
||||
|
||||
JsonFactory jsonFactory = new JsonFactory();
|
||||
|
||||
@Test
|
||||
public void testIsMutable() throws Exception {
|
||||
assertTrue(hstore.isMutable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsDateTimeCapable() throws Exception {
|
||||
assertFalse(hstore.isDateTimeCapable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsDirty() throws Exception {
|
||||
Map<String,Object> emptyMap = new HashMap<String, Object>();
|
||||
assertTrue(hstore.isDirty(emptyMap));
|
||||
|
||||
ModifyAwareMap<String,Object> modAware = new ModifyAwareMap<String,Object>(emptyMap);
|
||||
assertFalse(hstore.isDirty(modAware));
|
||||
modAware.put("foo", "Rob");
|
||||
assertTrue(hstore.isDirty(emptyMap));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParse() throws Exception {
|
||||
Map<String,Object> map = (Map<String,Object>)hstore.parse("{\"name\":\"rob\"}");
|
||||
assertEquals(1, map.size());
|
||||
assertEquals("rob", map.get("name"));
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
public void testParseDateTime() throws Exception {
|
||||
Map<String,Object> map = (Map<String,Object>)hstore.parseDateTime(1234L);
|
||||
assertEquals(1, map.size());
|
||||
assertEquals("rob", map.get("name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonWrite() throws Exception {
|
||||
Map<String,Object> map = new LinkedHashMap<String, Object>();
|
||||
|
||||
assertEquals("{}", generateJson(map));
|
||||
|
||||
map.put("name", "rob");
|
||||
assertEquals("{\"name\":\"rob\"}", generateJson(map));
|
||||
|
||||
map.put("age", 12);
|
||||
assertEquals("{\"name\":\"rob\",\"age\":12}", generateJson(map));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonRead() throws Exception {
|
||||
|
||||
Map<String, Object> map = parse("{\"name\":\"rob\"}");
|
||||
assertEquals(1, map.size());
|
||||
assertEquals("rob", map.get("name"));
|
||||
|
||||
map = parse("{\"name\":\"rob\",\"age\":12}");
|
||||
assertEquals(2, map.size());
|
||||
assertEquals("rob", map.get("name"));
|
||||
assertEquals(12L, map.get("age"));
|
||||
|
||||
}
|
||||
|
||||
private Map<String,Object> parse(String json) throws IOException {
|
||||
JsonParser parser = jsonFactory.createParser(json);
|
||||
return (Map<String,Object>)hstore.jsonRead(parser, JsonToken.FIELD_NAME);
|
||||
}
|
||||
|
||||
private String generateJson(Map<String, Object> emptyMap) throws IOException {
|
||||
StringWriter writer = new StringWriter();
|
||||
JsonGenerator generator = jsonFactory.createGenerator(writer);
|
||||
hstore.jsonWrite(generator, "name", emptyMap);
|
||||
generator.flush();
|
||||
return writer.toString();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user