#695 - ENH: Add @DbArray for storing scalar types (UUID, Long, String) to Postgres ARRAY type

This commit is contained in:
Robin Bygrave committed 2016-05-09 22:38:51 +12:00
1 parent 2bb39b02d0
commit b197d8a637
53 files changed
+1102 -229

No files matched your search

@@ -1367,7 +1367,7 @@ public class BeanProperty implements ElPropertyValue, Property {
DocPropertyType type = scalarType.getDocType();
DocPropertyOptions options = docOptions.copy();
if (DocPropertyType.ENUM == type || isStringId(type)) {
if (DocPropertyType.UUID == type || DocPropertyType.ENUM == type ||isStringId(type)) {
options.setCode(true);
}
@@ -187,6 +187,10 @@ public class AnnotationFields extends AnnotationParser {
util.setDbJsonBType(prop, dbJsonB);
}
}
DbArray dbArray = get(prop, DbArray.class);
if (dbArray != null) {
util.setDbArray(prop, dbArray);
}
DocCode docCode = get(prop, DocCode.class);
if (docCode != null) {
@@ -1,5 +1,6 @@
package com.avaje.ebeaninternal.server.deploy.parse;
import com.avaje.ebean.annotation.DbArray;
import com.avaje.ebean.annotation.DbHstore;
import com.avaje.ebean.annotation.DbJson;
import com.avaje.ebean.annotation.DbJsonB;
@@ -299,6 +300,7 @@ public class DeployCreateProperties {
private boolean isSpecialScalarType(Field field) {
return (field.getAnnotation(DbJson.class) != null)
|| (field.getAnnotation(DbJsonB.class) != null)
|| (field.getAnnotation(DbArray.class) != null)
|| (field.getAnnotation(DbHstore.class) != null);
}
@@ -5,6 +5,7 @@ import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.PersistenceException;
import com.avaje.ebean.annotation.DbArray;
import com.avaje.ebean.annotation.DbJson;
import com.avaje.ebean.annotation.DbJsonB;
import com.avaje.ebean.annotation.DbJsonType;
@@ -21,6 +22,7 @@ import com.avaje.ebeaninternal.server.deploy.meta.DeployBeanProperty;
import com.avaje.ebeaninternal.server.deploy.meta.DeployBeanPropertyCompound;
import com.avaje.ebeaninternal.server.type.DataEncryptSupport;
import com.avaje.ebeaninternal.server.type.ScalarType;
import com.avaje.ebeaninternal.server.type.ScalarTypeArrayList;
import com.avaje.ebeaninternal.server.type.ScalarTypeEnumStandard;
import com.avaje.ebeaninternal.server.type.SimpleAesEncryptor;
import com.avaje.ebeaninternal.server.type.TypeManager;
@@ -47,6 +49,8 @@ public class DeployUtil {
private static final int DEFAULT_JSON_VARCHAR_LENGTH = 3000;
private static final int DEFAULT_ARRAY_VARCHAR_LENGTH = 1000;
private final NamingConvention namingConvention;
private final TypeManager typeManager;
@@ -210,6 +214,30 @@ public class DeployUtil {
prop.setScalarType(scalarType);
}
/**
* Set the DbArray type (effectively Postgres only).
*/
public void setDbArray(DeployBeanProperty prop, DbArray dbArray) {
Class<?> type = prop.getPropertyType();
ScalarType<?> scalarType = typeManager.getArrayScalarType(type, dbArray, prop.getGenericType());
if (scalarType == null) {
throw new RuntimeException("No ScalarType for @DbArray type for [" + prop.getFullBeanName()+ "]");
}
int dbType = scalarType.getJdbcType();
prop.setDbType(dbType);
prop.setScalarType(scalarType);
if (scalarType instanceof ScalarTypeArrayList) {
prop.setDbColumnDefn(((ScalarTypeArrayList)scalarType).getDbColumnDefn());
}
if (dbType == Types.VARCHAR) {
// determine the db column size
int dbLength = dbArray.length();
int columnLength = (dbLength > 0) ? dbLength : DEFAULT_ARRAY_VARCHAR_LENGTH;
prop.setDbLength(columnLength);
}
}
/**
* This property is marked as a Lob object.
*/