#693 - ENH: Add support for mapping @DbJson to list of scalar types like List<String>

This commit is contained in:
Robin Bygrave committed 2016-05-05 16:36:16 +12:00
1 parent 8c86976e68
commit 2c95d9dc9a
30 files changed
+524 -98

No files matched your search

@@ -984,11 +984,11 @@ public class BeanProperty implements ElPropertyValue, Property {
/**
* Return the DB column type definition.
*/
public String renderDbType(DbType dbType) {
public String renderDbType(DbType dbType, boolean strict) {
if (dbColumnDefn != null) {
return dbColumnDefn;
}
return dbType.renderType(dbLength, dbScale);
return dbType.renderType(dbLength, dbScale, strict);
}
/**
@@ -175,12 +175,16 @@ public class AnnotationFields extends AnnotationParser {
if (comment != null) {
prop.setDbComment(comment.value());
}
if (get(prop, DbHstore.class) != null) {
util.setDbHstore(prop);
}
DbJson dbJson = get(prop, DbJson.class);
if (dbJson != null) {
util.setDbJsonType(prop, dbJson);
} else {
if (get(prop, DbJsonB.class) != null) {
util.setDbJsonBType(prop);
DbJsonB dbJsonB = get(prop, DbJsonB.class);
if (dbJsonB != null) {
util.setDbJsonBType(prop, dbJsonB);
}
}
@@ -197,10 +201,6 @@ public class AnnotationFields extends AnnotationParser {
prop.setDocProperty(docProperty);
}
if (get(prop, DbHstore.class) != null) {
util.setDbHstore(prop);
}
Formula formula = get(prop, Formula.class);
if (formula != null) {
prop.setSqlFormula(formula.select(), formula.join());
@@ -223,21 +223,18 @@ public class DeployCreateProperties {
Class<?> propertyType = field.getType();
ManyToOne manyToOne = field.getAnnotation(ManyToOne.class);
if (manyToOne != null){
Class<?> tt = manyToOne.targetEntity();
if (tt != null && !tt.equals(void.class)){
if (!tt.equals(void.class)){
propertyType = tt;
}
}
if (isMappedType(field)) {
if (isSpecialScalarType(field)) {
return new DeployBeanProperty(desc, propertyType, null, null);
}
// check for Collection type (list, set or map)
ManyType manyType = determineManyType.getManyType(propertyType);
if (manyType != null) {
// List, Set or Map based object
Class<?> targetType = determineTargetType(field);
@@ -299,7 +296,7 @@ public class DeployCreateProperties {
/**
* Return true if the field has one of the special mappings.
*/
private boolean isMappedType(Field field) {
private boolean isSpecialScalarType(Field field) {
return (field.getAnnotation(DbJson.class) != null)
|| (field.getAnnotation(DbJsonB.class) != null)
|| (field.getAnnotation(DbHstore.class) != null);
@@ -6,6 +6,7 @@ import javax.persistence.Enumerated;
import javax.persistence.PersistenceException;
import com.avaje.ebean.annotation.DbJson;
import com.avaje.ebean.annotation.DbJsonB;
import com.avaje.ebean.annotation.DbJsonType;
import com.avaje.ebean.config.EncryptDeploy;
import com.avaje.ebean.config.EncryptDeployManager;
@@ -103,6 +104,7 @@ public class DeployUtil {
return new DataEncryptSupport(encryptKeyManager, bytesEncryptor, table, column);
}
@SuppressWarnings("unchecked")
public void setEnumScalarType(Enumerated enumerated, DeployBeanProperty prop) {
Class<?> enumType = prop.getPropertyType();
@@ -200,11 +202,9 @@ public class DeployUtil {
* Map to Postgres HSTORE type.
*/
public void setDbHstore(DeployBeanProperty prop) {
ScalarType<?> scalarType = typeManager.getScalarType(DbType.HSTORE);
if (scalarType == null) {
// this should never occur actually
throw new RuntimeException("No ScalarType found for HSTORE on [" + prop.getFullBeanName() + "]");
throw new RuntimeException("No ScalarType found for HSTORE on [" + prop.getFullBeanName() + "] ?");
}
prop.setDbType(DbType.HSTORE);
prop.setScalarType(scalarType);
@@ -219,22 +219,20 @@ public class DeployUtil {
setDbJsonType(prop, dbType, dbJsonType.length());
}
public void setDbJsonBType(DeployBeanProperty prop) {
setDbJsonType(prop, DbType.JSONB, 0);
public void setDbJsonBType(DeployBeanProperty prop, DbJsonB dbJsonB) {
setDbJsonType(prop, DbType.JSONB, dbJsonB.length());
}
private void setDbJsonType(DeployBeanProperty prop, int dbType, int dbLength) {
Class<?> type = prop.getPropertyType();
ScalarType<?> scalarType = typeManager.getJsonScalarType(type, dbType);
ScalarType<?> scalarType = typeManager.getJsonScalarType(type, dbType, dbLength);
if (scalarType == null) {
// this should never occur actually
throw new RuntimeException("No ScalarType for JSON type [" + type + "] [" + dbType + "]");
}
prop.setDbType(dbType);
prop.setScalarType(scalarType);
if (dbType == Types.VARCHAR) {
if (dbType == Types.VARCHAR || dbLength > 0) {
// determine the db column size
int columnLength = (dbLength > 0) ? dbLength : DEFAULT_JSON_VARCHAR_LENGTH;
prop.setDbLength(columnLength);
@@ -155,6 +155,8 @@ public final class DefaultTypeManager implements TypeManager, KnownImmutable {
private final boolean java7Present;
private final boolean postgres;
// OPTIONAL ScalarTypes registered if Jackson/JsonNode is in the classpath
/**
@@ -195,14 +197,14 @@ public final class DefaultTypeManager implements TypeManager, KnownImmutable {
this.objectMapperPresent = config.getClassLoadConfig().isJacksonObjectMapperPresent();
this.extraTypeFactory = new DefaultTypeFactory(config);
this.postgres = isPostgres(config.getDatabasePlatform());
initialiseStandard(jsonDateTime, config);
initialiseJavaTimeTypes(jsonDateTime, config);
initialiseJodaTypes(jsonDateTime, config);
initialiseJacksonTypes(config);
if (isPostgres(config.getDatabasePlatform())) {
// Postgres has special DB types for JSON/JSONB
if (postgres) {
this.jsonMapJson = new ScalarTypeJsonMapPostgres.JSON();
this.jsonMapJsonb = new ScalarTypeJsonMapPostgres.JSONB();
} else {
@@ -368,7 +370,17 @@ public final class DefaultTypeManager implements TypeManager, KnownImmutable {
}
@Override
public ScalarType<?> getJsonScalarType(Class<?> type, int dbType) {
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;
}
if (type.equals(Map.class)) {
// @DbJson Map<String,Object> property
@@ -24,6 +24,10 @@ public class ModifyAwareList<E> implements List<E>, ModifyAwareOwner {
this.owner = owner;
}
public String toString() {
return list.toString();
}
@Override
public boolean isMarkedDirty() {
return owner.isMarkedDirty();
@@ -0,0 +1,29 @@
package com.avaje.ebeaninternal.server.type;
import org.postgresql.util.PGobject;
import java.sql.SQLException;
public class PostgresHelper {
/**
* The Postgres JSON DB type.
*/
public static final String JSON_TYPE = "json";
/**
* The Postgres JSONB DB type.
*/
public static final String JSONB_TYPE = "jsonb";
/**
* Construct and return Postgres specific PG object.
*/
public static Object asObject(String pgType, String rawJson) throws SQLException {
PGobject pgo = new PGobject();
pgo.setType(pgType);
pgo.setValue(rawJson);
return pgo;
}
}
@@ -0,0 +1,75 @@
package com.avaje.ebeaninternal.server.type;
import com.avaje.ebeanservice.docstore.api.mapping.DocPropertyType;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
abstract class ScalarTypeJsonCollection<T> extends ScalarTypeBase<T> {
public ScalarTypeJsonCollection(Class<T> type, int dbType) {
super(type, false, dbType);
}
/**
* Consider as a mutable type. Use the isDirty() method to check for dirty state.
*/
@Override
public boolean isMutable() {
return true;
}
/**
* Return true if the value should be considered dirty (and included in an update).
*/
@Override
public boolean isDirty(Object value) {
return !(value instanceof ModifyAwareOwner) || ((ModifyAwareOwner) value).isMarkedDirty();
}
@Override
public Object toJdbcType(Object value) {
return value;
}
@Override
@SuppressWarnings("unchecked")
public T toBeanType(Object value) {
return (T)value;
}
@Override
public DocPropertyType getDocType() {
return DocPropertyType.LIST;
}
@Override
public boolean isDateTimeCapable() {
return false;
}
@Override
public T convertFromMillis(long dateTime) {
return null;
}
@Override
public T readData(DataInput dataInput) throws IOException {
if (!dataInput.readBoolean()) {
return null;
} else {
return parse(dataInput.readUTF());
}
}
@Override
public void writeData(DataOutput dataOutput, T value) throws IOException {
if (value == null) {
dataOutput.writeBoolean(false);
} else {
ScalarHelp.writeUTF(dataOutput, format(value));
}
}
}
@@ -0,0 +1,137 @@
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.List;
/**
* Types for mapping List in JSON format to DB types VARCHAR, JSON and JSONB.
*/
public class ScalarTypeJsonList {
public static final ScalarTypeJsonCollection<List> VARCHAR = new ScalarTypeJsonList.Varchar();
public static final ScalarTypeJsonCollection<List> JSON = new ScalarTypeJsonList.Json();
public static final ScalarTypeJsonCollection<List> JSONB = new ScalarTypeJsonList.JsonB();
/**
* List mapped to DB VARCHAR.
*/
private static class Varchar extends ScalarTypeJsonList.Base {
public Varchar() {
super(Types.VARCHAR);
}
}
/**
* List mapped to Postgres JSON.
*/
private static class Json extends ScalarTypeJsonList.PgBase {
public Json() {
super(DbType.JSON, PostgresHelper.JSON_TYPE);
}
}
/**
* List mapped to Postgres JSONB.
*/
private static class JsonB extends ScalarTypeJsonList.PgBase {
public JsonB() {
super(DbType.JSONB, PostgresHelper.JSONB_TYPE);
}
}
/**
* Base class for List handling.
*/
private abstract static class Base extends ScalarTypeJsonCollection<List> {
public Base(int dbType) {
super(List.class, dbType);
}
@Override
public List read(DataReader dataReader) throws SQLException {
try {
// parse JSON into modifyAware list
return EJson.parseList(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, List 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 List into JSON content", e);
}
}
}
@Override
public String formatValue(List value) {
try {
return EJson.write(value);
} catch (IOException e) {
throw new PersistenceException("Failed to format List into JSON content", e);
}
}
@Override
public List parse(String value) {
try {
return EJson.parseList(value, false);
} catch (IOException e) {
throw new PersistenceException("Failed to parse JSON content as List: ["+value+"]", e);
}
}
@Override
public List jsonRead(JsonParser parser) throws IOException {
return EJson.parseList(parser, parser.getCurrentToken());
}
@Override
public void jsonWrite(JsonGenerator writer, List value) throws IOException {
EJson.write(value, writer);
}
}
/**
* Postgres extension to base List handling.
*/
private static class PgBase extends ScalarTypeJsonList.Base {
final String pgType;
PgBase(int jdbcType, String pgType) {
super(jdbcType);
this.pgType = pgType;
}
@Override
public void bind(DataBind bind, List value) throws SQLException {
String rawJson = (value == null) ? null : formatValue(value);
bind.setObject(PostgresHelper.asObject(pgType, rawJson));
}
}
}
@@ -2,6 +2,7 @@ package com.avaje.ebeaninternal.server.type;
import com.avaje.ebean.text.TextException;
import com.avaje.ebean.text.json.EJson;
import com.avaje.ebeaninternal.util.EncodeUtil;
import com.avaje.ebeanservice.docstore.api.mapping.DocPropertyType;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
@@ -12,7 +13,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Map;
@@ -77,7 +77,7 @@ public abstract class ScalarTypeJsonMap extends ScalarTypeBase<Map> {
b.setNull(Types.BLOB);
} else {
String rawJson = formatValue(value);
b.setBytes(rawJson.getBytes(StandardCharsets.UTF_8));
b.setBytes(EncodeUtil.utf8ToBytes(rawJson));
}
}
}
@@ -1,7 +1,6 @@
package com.avaje.ebeaninternal.server.type;
import com.avaje.ebean.config.dbplatform.DbType;
import org.postgresql.util.PGobject;
import java.sql.SQLException;
import java.util.Map;
@@ -11,10 +10,6 @@ import java.util.Map;
*/
public abstract class ScalarTypeJsonMapPostgres extends ScalarTypeJsonMap {
private static final String POSTGRES_TYPE_JSON = "json";
private static final String POSTGRES_TYPE_JSONB = "jsonb";
final String postgresType;
ScalarTypeJsonMapPostgres(int jdbcType, String postgresType) {
@@ -23,14 +18,10 @@ public abstract class ScalarTypeJsonMapPostgres extends ScalarTypeJsonMap {
}
@Override
public void bind(DataBind b, Map value) throws SQLException {
public void bind(DataBind bind, Map value) throws SQLException {
String rawJson = (value == null) ? null : formatValue(value);
PGobject pgo = new PGobject();
pgo.setType(postgresType);
pgo.setValue(rawJson);
b.setObject(pgo);
bind.setObject(PostgresHelper.asObject(postgresType, rawJson));
}
/**
@@ -39,7 +30,7 @@ public abstract class ScalarTypeJsonMapPostgres extends ScalarTypeJsonMap {
public static class JSON extends ScalarTypeJsonMapPostgres {
public JSON() {
super(DbType.JSON, POSTGRES_TYPE_JSON);
super(DbType.JSON, PostgresHelper.JSON_TYPE);
}
}
@@ -49,7 +40,7 @@ public abstract class ScalarTypeJsonMapPostgres extends ScalarTypeJsonMap {
public static class JSONB extends ScalarTypeJsonMapPostgres {
public JSONB() {
super(DbType.JSONB, POSTGRES_TYPE_JSONB);
super(DbType.JSONB, PostgresHelper.JSONB_TYPE);
}
}
}
@@ -1,6 +1,7 @@
package com.avaje.ebeaninternal.server.type;
import com.avaje.ebean.text.TextException;
import com.avaje.ebeaninternal.util.EncodeUtil;
import com.avaje.ebeanservice.docstore.api.mapping.DocPropertyType;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
@@ -13,7 +14,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;
import java.sql.Types;
@@ -85,7 +85,7 @@ public abstract class ScalarTypeJsonNode extends ScalarTypeBase<JsonNode> {
dataBind.setNull(Types.BLOB);
} else {
String rawJson = formatValue(value);
dataBind.setBlob(rawJson.getBytes(StandardCharsets.UTF_8));
dataBind.setBlob(EncodeUtil.utf8ToBytes(rawJson));
}
}
}
@@ -3,7 +3,6 @@ package com.avaje.ebeaninternal.server.type;
import com.avaje.ebean.config.dbplatform.DbType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.postgresql.util.PGobject;
import java.sql.SQLException;
@@ -12,10 +11,6 @@ import java.sql.SQLException;
*/
public abstract class ScalarTypeJsonNodePostgres extends ScalarTypeJsonNode {
private static final String POSTGRES_TYPE_JSON = "json";
private static final String POSTGRES_TYPE_JSONB = "jsonb";
final ObjectMapper objectMapper;
final String postgresType;
@@ -27,14 +22,9 @@ public abstract class ScalarTypeJsonNodePostgres extends ScalarTypeJsonNode {
}
@Override
public void bind(DataBind dataBind, JsonNode value) throws SQLException {
public void bind(DataBind bind, JsonNode value) throws SQLException {
String rawJson = (value == null) ? null : formatValue(value);
PGobject pgo = new PGobject();
pgo.setType(postgresType);
pgo.setValue(rawJson);
dataBind.setObject(pgo);
bind.setObject(PostgresHelper.asObject(postgresType, rawJson));
}
/**
@@ -43,7 +33,7 @@ public abstract class ScalarTypeJsonNodePostgres extends ScalarTypeJsonNode {
public static class JSON extends ScalarTypeJsonNodePostgres {
public JSON(ObjectMapper objectMapper) {
super(objectMapper, DbType.JSON, POSTGRES_TYPE_JSON);
super(objectMapper, DbType.JSON, PostgresHelper.JSON_TYPE);
}
}
@@ -53,7 +43,7 @@ public abstract class ScalarTypeJsonNodePostgres extends ScalarTypeJsonNode {
public static class JSONB extends ScalarTypeJsonNodePostgres {
public JSONB(ObjectMapper objectMapper) {
super(objectMapper, DbType.JSONB, POSTGRES_TYPE_JSONB);
super(objectMapper, DbType.JSONB, PostgresHelper.JSONB_TYPE);
}
}
}
@@ -63,5 +63,5 @@ public interface TypeManager {
* Note that type expected to be JsonNode or Map.
* </p>
*/
ScalarType<?> getJsonScalarType(Class<?> type, int dbType);
ScalarType<?> getJsonScalarType(Class<?> type, int dbType, int dbLength);
}