Skip to content

Fix jsonb support to work with 5.x #182

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

Open
wants to merge 4 commits into
base: 5.x
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions rsql-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
<artifactId>hamcrest</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.hypersistence</groupId>
<artifactId>hypersistence-utils-hibernate-62</artifactId>
<version>3.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import javax.persistence.metamodel.ManagedType;
import javax.persistence.metamodel.PluralAttribute;

import lombok.Getter;
import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.util.StringUtils;

import cz.jirutka.rsql.parser.ast.RSQLVisitor;
Expand All @@ -26,6 +28,7 @@ public abstract class RSQLVisitorBase<R, A> implements RSQLVisitor<R, A> {

protected static volatile @Setter Map<Class, ManagedType> managedTypeMap;
protected static volatile @Setter Map<String, EntityManager> entityManagerMap;
protected static volatile @Setter @Getter Map<EntityManager, Database> entityManagerDatabase = new HashMap();
protected static final Map<Class, Class> primitiveToWrapper;
protected static volatile @Setter Map<Class<?>, Map<String, String>> propertyRemapping;
protected static volatile @Setter Map<Class<?>, List<String>> globalPropertyWhitelist;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,122 @@
package io.github.perplexhub.rsql;

import java.util.Map;

import io.github.perplexhub.rsql.RSQLJPAAutoConfiguration.HibernateEntityManagerDatabaseConfiguration;
import javax.persistence.EntityManager;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.dialect.DB2Dialect;
import org.hibernate.dialect.DerbyDialect;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.dialect.HSQLDialect;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.dialect.OracleDialect;
import org.hibernate.dialect.PostgreSQLDialect;
import org.hibernate.dialect.SQLServerDialect;
import org.hibernate.dialect.SybaseDialect;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.internal.SessionFactoryImpl;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Import;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Configuration
@ConditionalOnClass(EntityManager.class)
@Import(HibernateEntityManagerDatabaseConfiguration.class)
public class RSQLJPAAutoConfiguration {

@Bean
public RSQLCommonSupport rsqlCommonSupport(Map<String, EntityManager> entityManagerMap) {
log.info("RSQLJPAAutoConfiguration.rsqlCommonSupport(entityManagerMap:{})", entityManagerMap.size());
return new RSQLCommonSupport(entityManagerMap);
}
@Bean
public RSQLCommonSupport rsqlCommonSupport(Map<String, EntityManager> entityManagerMap,
ObjectProvider<EntityManagerDatabase> entityManagerDatabaseProvider) {
log.info("RSQLJPAAutoConfiguration.rsqlCommonSupport(entityManagerMap:{})", entityManagerMap.size());
EntityManagerDatabase entityManagerDatabase = entityManagerDatabaseProvider.getIfAvailable(() -> new EntityManagerDatabase(new HashMap()));

return new RSQLJPASupport(entityManagerMap, entityManagerDatabase.value());
}

@Configuration
@ConditionalOnClass(SessionImplementor.class)
static
class HibernateEntityManagerDatabaseConfiguration {

@Transactional
@Bean
public EntityManagerDatabase entityManagerDatabase(ObjectProvider<EntityManager> entityManagers) {
Map<EntityManager, Database> value = new HashMap<>();
EntityManager entityManager = entityManagers.getIfAvailable();
SessionFactory sessionFactory = entityManager.unwrap(Session.class).getSessionFactory();
Dialect dialect = ((SessionFactoryImpl) sessionFactory).getJdbcServices().getDialect();

Database db = toDatabase(dialect);
if (db != null) {
value.put(entityManager, db);
}

return new EntityManagerDatabase(value);
}

private Database toDatabase(Dialect dialect) {
if (dialect instanceof PostgreSQLDialect) {
return Database.POSTGRESQL;
} else if (dialect instanceof MySQLDialect) {
return Database.MYSQL;
} else if (dialect instanceof SQLServerDialect) {
return Database.SQL_SERVER;
} else if (dialect instanceof OracleDialect) {
return Database.ORACLE;
} else if (dialect instanceof DerbyDialect) {
return Database.DERBY;
} else if (dialect instanceof DB2Dialect) {
return Database.DB2;
} else if (dialect instanceof H2Dialect) {
return Database.H2;
} else if (dialect instanceof HSQLDialect) {
return Database.HSQL;
} else if (dialect instanceof SybaseDialect) {
return Database.SQL_SERVER;
}

return null;
}
}

public static final class EntityManagerDatabase {
private final Map<EntityManager, Database> value;

public EntityManagerDatabase(Map<EntityManager, Database> value) {
this.value = value;
}

public Map<EntityManager, Database> value() {
return value;
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
EntityManagerDatabase that = (EntityManagerDatabase) obj;
return Objects.equals(value, that.value);
}

@Override
public int hashCode() {
return Objects.hash(value);
}

@Override
public String toString() {
return "EntityManagerDatabase[value=" + value + "]";
}
}
}
28 changes: 28 additions & 0 deletions rsql-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,34 @@
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>1.19.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>5.1.2.Final</version>
</dependency>
<dependency>
<groupId>io.hypersistence</groupId>
<artifactId>hypersistence-utils-hibernate-52</artifactId>
<version>3.7.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>5.14.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down
Loading