Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract ebean-jackson-mapper module #2828

Merged
merged 13 commits into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions composites/ebean/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@
<version>13.9.3-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-jackson-mapper</artifactId>
<version>13.9.3-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>io.ebean</groupId>
<artifactId>ebean-datasource</artifactId>
Expand Down
12 changes: 12 additions & 0 deletions ebean-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,18 @@
<version>13.9.3-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-jackson-mapper</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,24 @@
package io.ebean.core.type;

import io.ebean.annotation.MutationDetection;

/**
* Holds the configured mapper and default mutation detection mode to be used.
*/
public interface ScalarJsonManager {

/**
* Return the default mutation detection mode.
*/
MutationDetection mutationDetection();

/**
* Return the object mapper supplied in DatabaseConfig.
*/
Object mapper();

/**
* Return the Postgres type given the jdbc type.
*/
String postgresType(int dbType);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.ebean.core.type;

import io.avaje.lang.Nullable;

import java.lang.annotation.Annotation;

/**
* Supports JSON bean properties that use something like Jackson ObjectMapper to convert between
* JSON content and the logical bean property value.
*/
public interface ScalarJsonMapper {

/**
* Return a ScalarType for the given JSON property.
*/
ScalarType<?> createType(ScalarJsonRequest request);

/**
* Return a marker annotation to detect when the JSON mapper should be used explicitly.
*/
@Nullable
<A extends Annotation> Class<A> markerAnnotation();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package io.ebean.core.type;

import io.ebean.annotation.MutationDetection;

/**
* A request to create a ScalarType for a bean property (typically annotated with {@code @DbJson}).
*/
public final class ScalarJsonRequest {

private final ScalarJsonManager manager;
private final int dbType;
private final DocPropertyType docType;
private final Class<?> beanType;
private final MutationDetection mode;
private final String name;

public ScalarJsonRequest(ScalarJsonManager manager, int dbType, DocPropertyType docType, Class<?> beanType, MutationDetection mode, String name) {
this.manager = manager;
this.dbType = dbType;
this.docType = docType;
this.beanType = beanType;
this.mode = mode;
this.name = name;
}

/**
* Return the manager.
*/
public ScalarJsonManager manager() {
return manager;
}

/**
* Return the jdbc type this property maps to (e.g. JSON, JSONB etc).
*/
public int dbType() {
return dbType;
}

/**
* Return the document store docType.
*/
public DocPropertyType docType() {
return docType;
}

/**
* Return the type of the bean that this property belongs to.
*/
public Class<?> beanType() {
return beanType;
}

/**
* Return the mutation detection mode the property should use.
*/
public MutationDetection mode() {
return mode;
}

/**
* Return the property name.
*/
public String name() {
return name;
}
}
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 @@ -152,7 +152,6 @@ public int compare(DeployBeanProperty o1, DeployBeanProperty o2) {
private DocStoreMode docStoreDelete;
private DeployBeanProperty idProperty;
private TableJoin primaryKeyJoin;
private Object jacksonAnnotatedClass;

/**
* Construct the BeanDescriptor.
Expand Down Expand Up @@ -1130,14 +1129,4 @@ private String getDeployWord(String expression) {
}
}

/**
* Returns the jackson annotated class, if jackson is present.
*/
@SuppressWarnings("unchecked")
Object /*AnnotatedClass*/ getJacksonAnnotatedClass() {
if (jacksonAnnotatedClass == null) {
jacksonAnnotatedClass = new DeployBeanObtainJackson(config, beanType).obtain();
}
return jacksonAnnotatedClass;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1042,20 +1042,6 @@ public void setElementProperty() {
this.elementProperty = true;
}

/**
* Returns the jackson annotated field, if jackson is present.
*/
public Object /*AnnotatedField*/ getJacksonField() {
com.fasterxml.jackson.databind.introspect.AnnotatedClass jac =
(com.fasterxml.jackson.databind.introspect.AnnotatedClass) getDesc().getJacksonAnnotatedClass();
for (com.fasterxml.jackson.databind.introspect.AnnotatedField candidate : jac.fields()) {
if (candidate.getName().equals(getName())) {
return candidate;
}
}
return null;
}

public void initMetaAnnotations(Set<Class<?>> metaAnnotationsFilter) {
metaAnnotations = AnnotationUtil.metaFindAllFor(field, metaAnnotationsFilter);
}
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
Loading