#693 - ENH: ... support @DbJson with Set<String> and Set<Long> support

This commit is contained in:
Robin Bygrave committed 2016-05-05 21:34:29 +12:00
1 parent 2c95d9dc9a
commit deb6ce23d7
11 files changed
+388 -110

No files matched your search

@@ -63,6 +63,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
@@ -85,14 +86,6 @@ public final class DefaultTypeManager implements TypeManager, KnownImmutable {
private final DefaultTypeFactory extraTypeFactory;
private final ScalarTypeJsonMap JSON_MAP_CLOB = new ScalarTypeJsonMap.Clob();
private final ScalarTypeJsonMap jsonMapClob = JSON_MAP_CLOB;
private final ScalarTypeJsonMap jsonMapBlob = new ScalarTypeJsonMap.Blob();
private final ScalarTypeJsonMap jsonMapVarchar = new ScalarTypeJsonMap.Varchar();
private final ScalarTypeJsonMap jsonMapJson;
private final ScalarTypeJsonMap jsonMapJsonb;
private final ScalarTypeFile fileType = new ScalarTypeFile();
private final ScalarType<?> charType = new ScalarTypeChar();
@@ -204,14 +197,6 @@ public final class DefaultTypeManager implements TypeManager, KnownImmutable {
initialiseJodaTypes(jsonDateTime, config);
initialiseJacksonTypes(config);
if (postgres) {
this.jsonMapJson = new ScalarTypeJsonMapPostgres.JSON();
this.jsonMapJsonb = new ScalarTypeJsonMapPostgres.JSONB();
} else {
this.jsonMapJson = JSON_MAP_CLOB;
this.jsonMapJsonb = JSON_MAP_CLOB;
}
if (bootupClasses != null) {
initialiseCustomScalarTypes(jsonDateTime, bootupClasses, config);
initialiseScalarConverters(bootupClasses);
@@ -373,30 +358,18 @@ public final class DefaultTypeManager implements TypeManager, KnownImmutable {
public ScalarType<?> getJsonScalarType(Class<?> type, int dbType, int dbLength) {
if (type.equals(List.class)) {
if (postgres) {
switch (dbType) {
case DbType.JSONB: return ScalarTypeJsonList.JSONB;
case DbType.JSON: return ScalarTypeJsonList.JSON;
}
}
return ScalarTypeJsonList.VARCHAR;
return ScalarTypeJsonList.typeFor(postgres, dbType);
}
if (type.equals(Set.class)) {
return ScalarTypeJsonSet.typeFor(postgres, dbType);
}
if (type.equals(Map.class)) {
// @DbJson Map<String,Object> property
switch (dbType) {
case Types.VARCHAR : return jsonMapVarchar;
case Types.BLOB: return jsonMapBlob;
case Types.CLOB : return jsonMapClob;
case DbType.JSONB: return jsonMapJsonb;
case DbType.JSON: return jsonMapJson;
default:
return jsonMapJson;
}
return ScalarTypeJsonMap.typeFor(postgres, dbType);
}
if (type.equals(JsonNode.class)) {
// @DbJson JsonNode property (Jackson)
switch (dbType) {
case Types.VARCHAR : return jsonNodeVarchar;
case Types.BLOB: return jsonNodeBlob;
@@ -2,6 +2,7 @@ package com.avaje.ebeaninternal.server.type;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.ListIterator;
@@ -168,4 +169,14 @@ public class ModifyAwareList<E> implements List<E>, ModifyAwareOwner {
public List<E> subList(int fromIndex, int toIndex) {
return new ModifyAwareList<E>(owner,list.subList(fromIndex, toIndex));
}
/**
* Create an return a modify aware Set.
*/
public ModifyAwareSet<E> asSet() {
LinkedHashSet<E> set = new LinkedHashSet<E>();
set.addAll(list);
return new ModifyAwareSet<E>(owner, set);
}
}
@@ -5,7 +5,7 @@ import java.util.Set;
/**
* Wraps a Set for the purposes of detecting modifications.
*/
public class ModifyAwareSet<E> extends ModifyAwareCollection<E> implements Set<E> {
public class ModifyAwareSet<E> extends ModifyAwareCollection<E> implements Set<E>, ModifyAwareOwner {
/**
* Create with an Owner that is notified of modifications.
@@ -13,5 +13,19 @@ public class ModifyAwareSet<E> extends ModifyAwareCollection<E> implements Set<E
public ModifyAwareSet(ModifyAwareOwner owner, Set<E> s) {
super(owner, s);
}
@Override
public boolean isMarkedDirty() {
return owner.isMarkedDirty();
}
@Override
public void markAsModified() {
owner.markAsModified();
}
@Override
public void resetMarkedDirty() {
owner.resetMarkedDirty();
}
}
@@ -22,6 +22,19 @@ public class ScalarTypeJsonList {
public static final ScalarTypeJsonCollection<List> JSONB = new ScalarTypeJsonList.JsonB();
/**
* Return the appropriate ScalarType based requested dbType and if Postgres.
*/
public static ScalarType<?> typeFor(boolean postgres, int dbType) {
if (postgres) {
switch (dbType) {
case DbType.JSONB: return ScalarTypeJsonList.JSONB;
case DbType.JSON: return ScalarTypeJsonList.JSON;
}
}
return ScalarTypeJsonList.VARCHAR;
}
/**
* List mapped to DB VARCHAR.
*/
@@ -1,5 +1,6 @@
package com.avaje.ebeaninternal.server.type;
import com.avaje.ebean.config.dbplatform.DbType;
import com.avaje.ebean.text.TextException;
import com.avaje.ebean.text.json.EJson;
import com.avaje.ebeaninternal.util.EncodeUtil;
@@ -22,6 +23,28 @@ import java.util.Map;
*/
public abstract class ScalarTypeJsonMap extends ScalarTypeBase<Map> {
private static final ScalarTypeJsonMap CLOB = new ScalarTypeJsonMap.Clob();
private static final ScalarTypeJsonMap BLOB = new ScalarTypeJsonMap.Blob();
private static final ScalarTypeJsonMap VARCHAR = new ScalarTypeJsonMap.Varchar();
private static final ScalarTypeJsonMap JSON = new ScalarTypeJsonMapPostgres.JSON();
private static final ScalarTypeJsonMap JSONB = new ScalarTypeJsonMapPostgres.JSONB();
/**
* Return the ScalarType for the requested dbType and postgres.
*/
public static ScalarTypeJsonMap typeFor(boolean postgres, int dbType) {
switch (dbType) {
case Types.VARCHAR : return VARCHAR;
case Types.BLOB: return BLOB;
case Types.CLOB : return CLOB;
case DbType.JSONB: return postgres ? JSONB : CLOB;
case DbType.JSON: return postgres ? JSON : CLOB;
default:
throw new IllegalStateException("Unknown dbType "+dbType);
}
}
public static class Clob extends ScalarTypeJsonMap {
public Clob() {
@@ -0,0 +1,158 @@
package com.avaje.ebeaninternal.server.type;
import com.avaje.ebean.config.dbplatform.DbType;
import com.avaje.ebean.text.json.EJson;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import javax.persistence.PersistenceException;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Types;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
/**
* Types for mapping List in JSON format to DB types VARCHAR, JSON and JSONB.
*/
public class ScalarTypeJsonSet {
private static final ScalarTypeJsonCollection<Set> VARCHAR = new ScalarTypeJsonSet.Varchar();
private static final ScalarTypeJsonCollection<Set> JSON = new ScalarTypeJsonSet.Json();
private static final ScalarTypeJsonCollection<Set> JSONB = new ScalarTypeJsonSet.JsonB();
/**
* Return the appropriate ScalarType for the requested dbType and Postgres.
*/
public static ScalarType<?> typeFor(boolean postgres, int dbType) {
if (postgres) {
switch (dbType) {
case DbType.JSONB: return ScalarTypeJsonSet.JSONB;
case DbType.JSON: return ScalarTypeJsonSet.JSON;
}
}
return ScalarTypeJsonSet.VARCHAR;
}
/**
* List mapped to DB VARCHAR.
*/
private static class Varchar extends ScalarTypeJsonSet.Base {
public Varchar() {
super(Types.VARCHAR);
}
}
/**
* List mapped to Postgres JSON.
*/
private static class Json extends ScalarTypeJsonSet.PgBase {
public Json() {
super(DbType.JSON, PostgresHelper.JSON_TYPE);
}
}
/**
* List mapped to Postgres JSONB.
*/
private static class JsonB extends ScalarTypeJsonSet.PgBase {
public JsonB() {
super(DbType.JSONB, PostgresHelper.JSONB_TYPE);
}
}
/**
* Base class for List handling.
*/
private abstract static class Base extends ScalarTypeJsonCollection<Set> {
public Base(int dbType) {
super(Set.class, dbType);
}
@Override
public Set read(DataReader dataReader) throws SQLException {
try {
// parse JSON into modifyAware list
return EJson.parseSet(dataReader.getString(), true);
} catch (IOException e) {
throw new SQLException("Failed to parse JSON content as List: ["+ dataReader.getString() +"]", e);
}
}
@Override
public void bind(DataBind b, Set value) throws SQLException {
if (value == null) {
b.setNull(Types.VARCHAR);
} else if (value.isEmpty()) {
b.setString("[]");
} else {
try {
b.setString(EJson.write(value));
} catch (IOException e) {
throw new SQLException("Failed to format Set into JSON content", e);
}
}
}
@Override
public String formatValue(Set value) {
try {
return EJson.write(value);
} catch (IOException e) {
throw new PersistenceException("Failed to format List into JSON content", e);
}
}
@Override
public Set parse(String value) {
try {
return convertList(EJson.parseList(value));
} catch (IOException e) {
throw new PersistenceException("Failed to parse JSON content as Set: ["+value+"]", e);
}
}
@Override
public Set jsonRead(JsonParser parser) throws IOException {
return convertList(EJson.parseList(parser, parser.getCurrentToken()));
}
@Override
public void jsonWrite(JsonGenerator writer, Set value) throws IOException {
EJson.write(value, writer);
}
@SuppressWarnings("unchecked")
private Set convertList(List list) {
LinkedHashSet set = new LinkedHashSet();
set.addAll(list);
return set;
}
}
/**
* Postgres extension to base List handling.
*/
private static class PgBase extends ScalarTypeJsonSet.Base {
final String pgType;
PgBase(int jdbcType, String pgType) {
super(jdbcType);
this.pgType = pgType;
}
@Override
public void bind(DataBind bind, Set value) throws SQLException {
String rawJson = (value == null) ? null : formatValue(value);
bind.setObject(PostgresHelper.asObject(pgType, rawJson));
}
}
}