Skip to content

Commit

Permalink
Merge pull request #2826 from ebean-orm/feature/json-node-module
Browse files Browse the repository at this point in the history
Refactor extract ebean-jackson-jsonnode module - support for Jackson JsonNode (into separate module)
  • Loading branch information
rbygrave committed Oct 13, 2022
2 parents 9a25fb5 + c53ca6c commit 62adaa7
Show file tree
Hide file tree
Showing 39 changed files with 374 additions and 214 deletions.
6 changes: 6 additions & 0 deletions composites/ebean/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
<version>13.9.4-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>io.ebean</groupId>
<artifactId>ebean-jackson-jsonnode</artifactId>
<version>13.9.3-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>io.ebean</groupId>
<artifactId>ebean-datasource</artifactId>
Expand Down
6 changes: 6 additions & 0 deletions ebean-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@
<version>13.9.4-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>io.ebean</groupId>
<artifactId>ebean-jackson-jsonnode</artifactId>
<version>13.9.3-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>io.ebean</groupId>
<artifactId>ebean-ddl-generator</artifactId>
Expand Down
8 changes: 8 additions & 0 deletions ebean-core-type/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@
<optional>true</optional>
</dependency>

<!-- Provided scope for Postgres JSON/JSONB support -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.4.1</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.ebeaninternal.server.type;
package io.ebean.core.type;

import io.ebean.annotation.Platform;
import io.ebean.config.dbplatform.DatabasePlatform;
import org.postgresql.util.PGobject;

import java.sql.SQLException;
Expand All @@ -9,12 +11,12 @@ public final class PostgresHelper {
/**
* The Postgres JSON DB type.
*/
static final String JSON_TYPE = "json";
public static final String JSON_TYPE = "json";

/**
* The Postgres JSONB DB type.
*/
static final String JSONB_TYPE = "jsonb";
public static final String JSONB_TYPE = "jsonb";

static final String INET_TYPE = "inet";

Expand All @@ -31,4 +33,13 @@ public static Object asObject(String pgType, String rawJson) throws SQLException
pgo.setValue(rawJson);
return pgo;
}

/**
* Return true if the platform is Postgres or compatible like Yugabyte, Cockroach.
*/
public static boolean isPostgresCompatible(DatabasePlatform databasePlatform) {
return databasePlatform.isPlatform(Platform.POSTGRES)
|| databasePlatform.isPlatform(Platform.YUGABYTE)
|| databasePlatform.isPlatform(Platform.COCKROACH);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.ebean.core.type;

import io.avaje.lang.Nullable;

/**
* A ScalarType that has variations based on the mapped JDBC type (like VARCHAR, CLOB, JSON etc).
*/
public interface ScalarTypeSet<T> {

/**
* The property type these scalar types map to.
*/
Class<?> type();

/**
* Return a default ScalarType to use when no other annotations like {@code @DbJson} are present.
*/
@Nullable
ScalarType<?> defaultType();

/**
* Return the scalarType to use for the given jdbc type.
* <p>
* For example VARCHAR, CLOB, JSON etc.
*/
ScalarType<T> forType(int jdbcType);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.ebean.core.type;

import io.avaje.lang.Nullable;
import io.ebean.config.DatabaseConfig;

/**
* Factory to create ScalarTypeSet.
*/
public interface ScalarTypeSetFactory {

/**
* Create the ScalarTypeSet given the config and optional objectMapper.
*/
@Nullable
ScalarTypeSet<?> createTypeSet(DatabaseConfig config, @Nullable Object objectMapper);

}
1 change: 1 addition & 0 deletions ebean-core-type/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

requires transitive java.sql;
requires transitive io.ebean.api;
requires static org.postgresql.jdbc;

requires static com.fasterxml.jackson.core;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public BeanDescriptorManager(InternalConfiguration config) {
this.dbIdentity = config.getDatabasePlatform().dbIdentity();
this.deplyInherit = config.getDeployInherit();
this.deployUtil = config.getDeployUtil();
this.typeManager = deployUtil.getTypeManager();
this.typeManager = deployUtil.typeManager();
this.beanManagerFactory = new BeanManagerFactory(config.getDatabasePlatform());
this.beanLifecycleAdapterFactory = new BeanLifecycleAdapterFactory(this.config);
this.persistControllerManager = new PersistControllerManager(bootupClasses);
Expand Down Expand Up @@ -184,12 +184,12 @@ private void trimQueryPlans() {

@Override
public ScalarType<?> scalarType(String cast) {
return typeManager.getScalarType(cast);
return typeManager.type(cast);
}

@Override
public ScalarType<?> scalarType(int jdbcType) {
return typeManager.getScalarType(jdbcType);
return typeManager.type(jdbcType);
}

/**
Expand Down Expand Up @@ -884,7 +884,7 @@ private boolean findMappedBy(DeployBeanPropertyAssocMany<?> prop) {
private void makeOrderColumn(DeployBeanPropertyAssocMany<?> oneToMany) {
DeployBeanDescriptor<?> targetDesc = targetDescriptor(oneToMany);
DeployOrderColumn orderColumn = oneToMany.getOrderColumn();
final ScalarType<?> scalarType = typeManager.getScalarType(Integer.class);
final ScalarType<?> scalarType = typeManager.type(Integer.class);
DeployBeanProperty orderProperty = new DeployBeanProperty(targetDesc, Integer.class, scalarType, null);
orderProperty.setName(DeployOrderColumn.LOGICAL_NAME);
orderProperty.setDbColumn(orderColumn.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ private void readElementCollection(DeployBeanPropertyAssocMany<?> prop, ElementC
dbKeyColumn = mapKeyColumn.name();
}

ScalarType<?> keyScalarType = util.getTypeManager().getScalarType(prop.getMapKeyType());
ScalarType<?> keyScalarType = util.typeManager().type(prop.getMapKeyType());

DeployBeanProperty keyProp = new DeployBeanProperty(elementDescriptor, elementType, keyScalarType, null);
setElementProperty(keyProp, "key", dbKeyColumn, sortOrder++);
Expand All @@ -234,10 +234,10 @@ private void readElementCollection(DeployBeanPropertyAssocMany<?> prop, ElementC
}
}

ScalarType<?> valueScalarType = util.getTypeManager().getScalarType(elementType);
ScalarType<?> valueScalarType = util.typeManager().type(elementType);
if (valueScalarType == null && elementType.isEnum()) {
Class<? extends Enum<?>> enumClass = (Class<? extends Enum<?>>)elementType;
valueScalarType = util.getTypeManager().createEnumScalarType(enumClass, EnumType.STRING);
valueScalarType = util.typeManager().enumType(enumClass, EnumType.STRING);
}

boolean scalar = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ abstract class AnnotationBase {

AnnotationBase(DeployUtil util) {
this.util = util;
this.databasePlatform = util.getDbPlatform();
this.databasePlatform = util.dbPlatform();
this.platform = databasePlatform.platform();
this.namingConvention = util.getNamingConvention();
this.namingConvention = util.namingConvention();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ private void initWhen(DeployBeanProperty prop) {

private void initEncrypt(DeployBeanProperty prop) {
if (!prop.isTransient()) {
EncryptDeploy encryptDeploy = util.getEncryptDeploy(info.getDescriptor().getBaseTableFull(), prop.getDbColumn());
EncryptDeploy encryptDeploy = util.encryptDeploy(info.getDescriptor().getBaseTableFull(), prop.getDbColumn());
if (encryptDeploy == null || encryptDeploy.getMode() == Mode.MODE_ANNOTATION) {
Encrypted encrypted = get(prop, Encrypted.class);
if (encrypted != null) {
Expand Down Expand Up @@ -398,7 +398,7 @@ private void setEncryption(DeployBeanProperty prop, boolean dbEncString, int dbL
return;
}
if (dbEncString) {
DbEncrypt dbEncrypt = util.getDbPlatform().dbEncrypt();
DbEncrypt dbEncrypt = util.dbPlatform().dbEncrypt();
if (dbEncrypt != null) {
// check if we have a DB encryption function for this type
int jdbcType = prop.getScalarType().jdbcType();
Expand Down Expand Up @@ -427,7 +427,7 @@ private ScalarTypeEncryptedWrapper<?> createScalarType(DeployBeanProperty prop,

private ScalarTypeBytesBase getDbEncryptType(DeployBeanProperty prop) {
int dbType = prop.isLob() ? Types.BLOB : Types.VARBINARY;
return (ScalarTypeBytesBase) util.getTypeManager().getScalarType(dbType);
return (ScalarTypeBytesBase) util.typeManager().type(dbType);
}

private DataEncryptSupport createDataEncryptSupport(DeployBeanProperty prop) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private void createProperties(DeployBeanDescriptor<?> desc, Class<?> beanType, i
@SuppressWarnings({"unchecked"})
private DeployBeanProperty createManyType(DeployBeanDescriptor<?> desc, Class<?> targetType, ManyType manyType) {
try {
ScalarType<?> scalarType = typeManager.getScalarType(targetType);
ScalarType<?> scalarType = typeManager.type(targetType);
if (scalarType != null) {
return new DeployBeanPropertySimpleCollection(desc, targetType, manyType);
}
Expand Down Expand Up @@ -141,7 +141,7 @@ private DeployBeanProperty createProp(DeployBeanDescriptor<?> desc, Field field)
if (propertyType.isEnum() || propertyType.isPrimitive()) {
return new DeployBeanProperty(desc, propertyType, null, null);
}
ScalarType<?> scalarType = typeManager.getScalarType(propertyType);
ScalarType<?> scalarType = typeManager.type(propertyType);
if (scalarType != null) {
return new DeployBeanProperty(desc, propertyType, scalarType, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ public DeployUtil(TypeManager typeMgr, DatabaseConfig config) {
this.useValidationNotNull = config.isUseValidationNotNull();
}

public TypeManager getTypeManager() {
public TypeManager typeManager() {
return typeManager;
}

public DatabasePlatform getDbPlatform() {
public DatabasePlatform dbPlatform() {
return dbPlatform;
}

public NamingConvention getNamingConvention() {
public NamingConvention namingConvention() {
return namingConvention;
}

Expand All @@ -82,7 +82,7 @@ void checkEncryptKeyManagerDefined(String fullPropName) {
}
}

EncryptDeploy getEncryptDeploy(TableName table, String column) {
EncryptDeploy encryptDeploy(TableName table, String column) {
if (encryptDeployManager == null) {
return EncryptDeploy.ANNOTATION;
}
Expand All @@ -102,7 +102,7 @@ void setEnumScalarType(Enumerated enumerated, DeployBeanProperty prop) {
try {
Class<? extends Enum<?>> enumClass = (Class<? extends Enum<?>>) enumType;
EnumType type = enumerated != null ? enumerated.value() : null;
ScalarType<?> scalarType = typeManager.createEnumScalarType(enumClass, type);
ScalarType<?> scalarType = typeManager.enumType(enumClass, type);
prop.setScalarType(scalarType);
prop.setDbType(scalarType.jdbcType());
} catch (IllegalStateException e) {
Expand All @@ -123,7 +123,7 @@ public void setScalarType(DeployBeanProperty property) {
// this will be an Enum type...
return;
}
ScalarType<?> scalarType = getScalarType(property);
ScalarType<?> scalarType = scalarType(property);
if (scalarType != null) {
// set the jdbc type this maps to
property.setDbType(scalarType.jdbcType());
Expand All @@ -132,12 +132,12 @@ public void setScalarType(DeployBeanProperty property) {
}
}

private ScalarType<?> getScalarType(DeployBeanProperty property) {
private ScalarType<?> scalarType(DeployBeanProperty property) {
// Note that Temporal types already have dbType
// set via annotations
Class<?> propType = property.getPropertyType();
try {
ScalarType<?> scalarType = typeManager.getScalarType(propType, property.getDbType());
ScalarType<?> scalarType = typeManager.type(propType, property.getDbType());
if (scalarType != null || property.isTransient()) {
return scalarType;
}
Expand All @@ -155,7 +155,7 @@ private ScalarType<?> getScalarType(DeployBeanProperty property) {
* Map to Postgres HSTORE type (with fallback to JSON storage in VARCHAR).
*/
void setDbMap(DeployBeanProperty prop, DbMap dbMap) {
ScalarType<?> scalarType = typeManager.getDbMapScalarType();
ScalarType<?> scalarType = typeManager.dbMapType();
int dbType = scalarType.jdbcType();
prop.setDbType(dbType);
prop.setScalarType(scalarType);
Expand All @@ -177,7 +177,7 @@ void setDbArray(DeployBeanProperty prop, DbArray dbArray) {
prop.setNullable(false);
}
Class<?> type = prop.getPropertyType();
ScalarType<?> scalarType = typeManager.getArrayScalarType(type, prop.getGenericType(), prop.isNullable());
ScalarType<?> scalarType = typeManager.dbArrayType(type, prop.getGenericType(), prop.isNullable());
if (scalarType == null) {
throw new RuntimeException("No ScalarType for @DbArray type for " + prop.getFullBeanName());
}
Expand All @@ -198,7 +198,7 @@ void setDbArray(DeployBeanProperty prop, DbArray dbArray) {
}

void setDbJsonType(DeployBeanProperty prop, DbJson dbJsonType) {
int dbType = getDbJsonStorage(dbJsonType.storage());
int dbType = dbJsonStorage(dbJsonType.storage());
setDbJsonType(prop, dbType, dbJsonType.length(), dbJsonType.mutationDetection());
}

Expand All @@ -209,7 +209,7 @@ void setDbJsonBType(DeployBeanProperty prop, DbJsonB dbJsonB) {
private void setDbJsonType(DeployBeanProperty prop, int dbType, int dbLength, MutationDetection mutationDetection) {
prop.setDbType(dbType);
prop.setMutationDetection(mutationDetection);
ScalarType<?> scalarType = typeManager.getJsonScalarType(prop, dbType, dbLength);
ScalarType<?> scalarType = typeManager.dbJsonType(prop, dbType, dbLength);
if (scalarType == null) {
throw new RuntimeException("No ScalarType for JSON property " + prop + " dbType:" + dbType);
}
Expand All @@ -224,7 +224,7 @@ private void setDbJsonType(DeployBeanProperty prop, int dbType, int dbLength, Mu
/**
* Return the JDBC type for the JSON storage type.
*/
private int getDbJsonStorage(DbJsonType dbJsonType) {
private int dbJsonStorage(DbJsonType dbJsonType) {
switch (dbJsonType) {
case JSONB:
return DbPlatformType.JSONB;
Expand Down Expand Up @@ -253,7 +253,7 @@ void setLobType(DeployBeanProperty prop) {
// this also sets the lob flag on DeployBeanProperty
int lobType = isClobType(type) ? dbCLOBType : dbBLOBType;

scalarType = typeManager.getScalarType(type, lobType);
scalarType = typeManager.type(type, lobType);
if (scalarType == null) {
// this should never occur actually
throw new RuntimeException("No ScalarType for LOB type " + type + " dbType:" + lobType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class DtoMetaConstructor {
this.types = constructor.getParameterTypes();
this.scalarTypes = new ScalarType[types.length];
for (int i = 0; i < types.length; i++) {
scalarTypes[i] = typeManager.getScalarType(types[i]);
scalarTypes[i] = typeManager.type(types[i]);
}
this.handle = LOOKUP.findConstructor(someClass, typeFor(types));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final class DtoMetaProperty implements DtoReadSet {
this.name = name;
if (writeMethod != null) {
this.setter = lookupMethodHandle(dtoType, writeMethod);
this.scalarType = typeManager.getScalarType(propertyType(writeMethod), propertyClass(writeMethod));
this.scalarType = typeManager.type(propertyType(writeMethod), propertyClass(writeMethod));
} else {
this.scalarType = null;
this.setter = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void write(JsonGenerator gen, Object value) throws IOException {
gen.writeRaw(']');

} else {
ScalarType scalarType = typeManager.getScalarType(value.getClass());
ScalarType scalarType = typeManager.type(value.getClass());
if (scalarType == null) {
throw new IllegalArgumentException("unhandled type " + value.getClass());
}
Expand Down
Loading

0 comments on commit 62adaa7

Please sign in to comment.