mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#586 - Refactor the UUID ScalarTypes (Native, Varchar and Binary) to have a common base
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
package com.avaje.ebeaninternal.server.type;
|
||||
|
||||
import com.avaje.ebeanservice.docstore.api.mapping.DocPropertyType;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Base UUID type for string formatting, json handling etc.
|
||||
*/
|
||||
public abstract class ScalarTypeUUIDBase extends ScalarTypeBase<UUID> {
|
||||
|
||||
public ScalarTypeUUIDBase(boolean jdbcNative, int jdbcType) {
|
||||
super(UUID.class, jdbcNative, jdbcType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMutable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDirty(Object value) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String formatValue(UUID value) {
|
||||
return value.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID parse(String value) {
|
||||
return UUID.fromString(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID convertFromMillis(long dateTime) {
|
||||
throw new RuntimeException("Should never be called");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDateTimeCapable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID readData(DataInput dataInput) throws IOException {
|
||||
if (!dataInput.readBoolean()) {
|
||||
return null;
|
||||
} else {
|
||||
return parse(dataInput.readUTF());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeData(DataOutput dataOutput, UUID value) throws IOException {
|
||||
if (value == null) {
|
||||
dataOutput.writeBoolean(false);
|
||||
} else {
|
||||
ScalarHelp.writeUTF(dataOutput, format(value));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator writer, UUID value) throws IOException {
|
||||
writer.writeString(formatValue(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID jsonRead(JsonParser parser) throws IOException {
|
||||
return parse(parser.getValueAsString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public DocPropertyType getDocType() {
|
||||
return DocPropertyType.STRING;
|
||||
}
|
||||
}
|
||||
@@ -1,24 +1,18 @@
|
||||
package com.avaje.ebeaninternal.server.type;
|
||||
|
||||
import com.avaje.ebeanservice.docstore.api.mapping.DocPropertyType;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataInput;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutput;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
import java.util.UUID;
|
||||
|
||||
public class ScalarTypeUUIDBinary extends ScalarTypeBase<UUID> {
|
||||
public class ScalarTypeUUIDBinary extends ScalarTypeUUIDBase {
|
||||
|
||||
protected ScalarTypeUUIDBinary() {
|
||||
super(UUID.class, false, Types.BINARY);
|
||||
super(false, Types.BINARY);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -37,23 +31,22 @@ public class ScalarTypeUUIDBinary extends ScalarTypeBase<UUID> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String formatValue(UUID v) {
|
||||
return v.toString();
|
||||
public void bind(DataBind b, UUID value) throws SQLException {
|
||||
if (value == null) {
|
||||
b.setNull(Types.BINARY);
|
||||
} else {
|
||||
b.setBytes(convertToBytes(value));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID parse(String value) {
|
||||
return UUID.fromString(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID convertFromMillis(long dateTime) {
|
||||
throw new IllegalStateException("Never called");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDateTimeCapable() {
|
||||
return false;
|
||||
public UUID read(DataReader dataReader) throws SQLException {
|
||||
byte[] bytes = dataReader.getBytes();
|
||||
if (bytes == null) {
|
||||
return null;
|
||||
} else {
|
||||
return convertFromBytes(bytes);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -103,56 +96,4 @@ public class ScalarTypeUUIDBinary extends ScalarTypeBase<UUID> {
|
||||
return baos.toByteArray();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bind(DataBind b, UUID value) throws SQLException {
|
||||
if (value == null) {
|
||||
b.setNull(Types.BINARY);
|
||||
|
||||
} else {
|
||||
b.setBytes(convertToBytes(value));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID read(DataReader dataReader) throws SQLException {
|
||||
byte[] bytes = dataReader.getBytes();
|
||||
if (bytes == null) {
|
||||
return null;
|
||||
} else {
|
||||
return convertFromBytes(bytes);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID readData(DataInput dataInput) throws IOException {
|
||||
if (!dataInput.readBoolean()) {
|
||||
return null;
|
||||
} else {
|
||||
return parse(dataInput.readUTF());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeData(DataOutput dataOutput, UUID value) throws IOException {
|
||||
if (value == null) {
|
||||
dataOutput.writeBoolean(false);
|
||||
} else {
|
||||
ScalarHelp.writeUTF(dataOutput, format(value));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID jsonRead(JsonParser parser) throws IOException {
|
||||
return UUID.fromString(parser.getValueAsString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator writer, UUID value) throws IOException {
|
||||
writer.writeString(value.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public DocPropertyType getDocType() {
|
||||
return DocPropertyType.STRING;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,46 +1,26 @@
|
||||
package com.avaje.ebeaninternal.server.type;
|
||||
|
||||
import com.avaje.ebean.config.dbplatform.DbType;
|
||||
import com.avaje.ebeanservice.docstore.api.mapping.DocPropertyType;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonToken;
|
||||
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Postgres Hstore type which maps Map<String,String> to a single 'HStore column' in the DB.
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
public class ScalarTypeUUIDNative extends ScalarTypeBase<UUID> {
|
||||
public class ScalarTypeUUIDNative extends ScalarTypeUUIDBase {
|
||||
|
||||
public ScalarTypeUUIDNative() {
|
||||
super(UUID.class, false, DbType.UUID);
|
||||
super(false, DbType.UUID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMutable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDirty(Object value) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public UUID read(DataReader dataReader) throws SQLException {
|
||||
|
||||
Object value = dataReader.getObject();
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
return (UUID)value;
|
||||
return (UUID) value;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -58,57 +38,4 @@ public class ScalarTypeUUIDNative extends ScalarTypeBase<UUID> {
|
||||
return (UUID) value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String formatValue(UUID v) {
|
||||
return v.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID parse(String value) {
|
||||
return UUID.fromString(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID convertFromMillis(long dateTime) {
|
||||
throw new RuntimeException("Should never be called");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDateTimeCapable() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID readData(DataInput dataInput) throws IOException {
|
||||
if (!dataInput.readBoolean()) {
|
||||
return null;
|
||||
} else {
|
||||
String json = dataInput.readUTF();
|
||||
return parse(json);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeData(DataOutput dataOutput, UUID value) throws IOException {
|
||||
if (value == null) {
|
||||
dataOutput.writeBoolean(false);
|
||||
} else {
|
||||
ScalarHelp.writeUTF(dataOutput, format(value));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator writer, UUID value) throws IOException {
|
||||
writer.writeString(formatValue(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID jsonRead(JsonParser parser) throws IOException {
|
||||
return parse(parser.getValueAsString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public DocPropertyType getDocType() {
|
||||
return DocPropertyType.STRING;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.avaje.ebeaninternal.server.type;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
import java.util.UUID;
|
||||
|
||||
import com.avaje.ebeaninternal.server.core.BasicTypeConverter;
|
||||
@@ -7,10 +9,10 @@ import com.avaje.ebeaninternal.server.core.BasicTypeConverter;
|
||||
/**
|
||||
* ScalarType for java.util.UUID which converts to and from a VARCHAR database column.
|
||||
*/
|
||||
public class ScalarTypeUUIDVarchar extends ScalarTypeBaseVarchar<UUID> {
|
||||
public class ScalarTypeUUIDVarchar extends ScalarTypeUUIDBase {
|
||||
|
||||
public ScalarTypeUUIDVarchar() {
|
||||
super(UUID.class);
|
||||
protected ScalarTypeUUIDVarchar() {
|
||||
super(false, Types.VARCHAR);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -18,16 +20,6 @@ public class ScalarTypeUUIDVarchar extends ScalarTypeBaseVarchar<UUID> {
|
||||
return 40;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID convertFromDbString(String dbValue) {
|
||||
return UUID.fromString(dbValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String convertToDbString(UUID beanValue) {
|
||||
return formatValue(beanValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID toBeanType(Object value) {
|
||||
return BasicTypeConverter.toUUID(value);
|
||||
@@ -39,13 +31,22 @@ public class ScalarTypeUUIDVarchar extends ScalarTypeBaseVarchar<UUID> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String formatValue(UUID v) {
|
||||
return v.toString();
|
||||
public void bind(DataBind b, UUID value) throws SQLException {
|
||||
if (value == null) {
|
||||
b.setNull(Types.VARCHAR);
|
||||
} else {
|
||||
b.setString(formatValue(value));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID parse(String value) {
|
||||
return UUID.fromString(value);
|
||||
public UUID read(DataReader dataReader) throws SQLException {
|
||||
String value = dataReader.getString();
|
||||
if (value == null) {
|
||||
return null;
|
||||
} else {
|
||||
return parse(value);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user