Skip to content

Commit de77079

Browse files
Fix jsonb support to work with 5.x (#182)
* Merge pull request #119 from nstdio/gh114 Basic support for queries against Postgres jsonb type. * Fix jsonb support to work with 5.x * Revert the java update from 1.8 to 11 * Revert the sprint boot version changes and junit4 to junit5 test changes --------- Co-authored-by: A F <[email protected]>
1 parent 2546ce3 commit de77079

File tree

13 files changed

+578
-42
lines changed

13 files changed

+578
-42
lines changed

rsql-common/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@
3737
<artifactId>hamcrest</artifactId>
3838
<scope>test</scope>
3939
</dependency>
40+
<dependency>
41+
<groupId>io.hypersistence</groupId>
42+
<artifactId>hypersistence-utils-hibernate-62</artifactId>
43+
<version>3.5.1</version>
44+
<scope>test</scope>
45+
</dependency>
4046
<dependency>
4147
<groupId>com.h2database</groupId>
4248
<artifactId>h2</artifactId>

rsql-common/src/main/java/io/github/perplexhub/rsql/RSQLVisitorBase.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
import javax.persistence.metamodel.ManagedType;
1313
import javax.persistence.metamodel.PluralAttribute;
1414

15+
import lombok.Getter;
1516
import org.springframework.core.convert.support.ConfigurableConversionService;
17+
import org.springframework.orm.jpa.vendor.Database;
1618
import org.springframework.util.StringUtils;
1719

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

2729
protected static volatile @Setter Map<Class, ManagedType> managedTypeMap;
2830
protected static volatile @Setter Map<String, EntityManager> entityManagerMap;
31+
protected static volatile @Setter @Getter Map<EntityManager, Database> entityManagerDatabase = new HashMap();
2932
protected static final Map<Class, Class> primitiveToWrapper;
3033
protected static volatile @Setter Map<Class<?>, Map<String, String>> propertyRemapping;
3134
protected static volatile @Setter Map<Class<?>, List<String>> globalPropertyWhitelist;
Lines changed: 107 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,122 @@
11
package io.github.perplexhub.rsql;
22

3-
import java.util.Map;
43

4+
import io.github.perplexhub.rsql.RSQLJPAAutoConfiguration.HibernateEntityManagerDatabaseConfiguration;
55
import javax.persistence.EntityManager;
6-
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
import java.util.Objects;
9+
import lombok.extern.slf4j.Slf4j;
10+
import org.hibernate.Session;
11+
import org.hibernate.SessionFactory;
12+
import org.hibernate.dialect.DB2Dialect;
13+
import org.hibernate.dialect.DerbyDialect;
14+
import org.hibernate.dialect.Dialect;
15+
import org.hibernate.dialect.H2Dialect;
16+
import org.hibernate.dialect.HSQLDialect;
17+
import org.hibernate.dialect.MySQLDialect;
18+
import org.hibernate.dialect.OracleDialect;
19+
import org.hibernate.dialect.PostgreSQLDialect;
20+
import org.hibernate.dialect.SQLServerDialect;
21+
import org.hibernate.dialect.SybaseDialect;
22+
import org.hibernate.engine.spi.SessionImplementor;
23+
import org.hibernate.internal.SessionFactoryImpl;
24+
import org.springframework.beans.factory.ObjectProvider;
725
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
826
import org.springframework.context.annotation.Bean;
927
import org.springframework.context.annotation.Configuration;
10-
11-
import lombok.extern.slf4j.Slf4j;
28+
import org.springframework.context.annotation.Import;
29+
import org.springframework.orm.jpa.vendor.Database;
30+
import org.springframework.transaction.annotation.Transactional;
1231

1332
@Slf4j
1433
@Configuration
1534
@ConditionalOnClass(EntityManager.class)
35+
@Import(HibernateEntityManagerDatabaseConfiguration.class)
1636
public class RSQLJPAAutoConfiguration {
1737

18-
@Bean
19-
public RSQLCommonSupport rsqlCommonSupport(Map<String, EntityManager> entityManagerMap) {
20-
log.info("RSQLJPAAutoConfiguration.rsqlCommonSupport(entityManagerMap:{})", entityManagerMap.size());
21-
return new RSQLCommonSupport(entityManagerMap);
22-
}
38+
@Bean
39+
public RSQLCommonSupport rsqlCommonSupport(Map<String, EntityManager> entityManagerMap,
40+
ObjectProvider<EntityManagerDatabase> entityManagerDatabaseProvider) {
41+
log.info("RSQLJPAAutoConfiguration.rsqlCommonSupport(entityManagerMap:{})", entityManagerMap.size());
42+
EntityManagerDatabase entityManagerDatabase = entityManagerDatabaseProvider.getIfAvailable(() -> new EntityManagerDatabase(new HashMap()));
43+
44+
return new RSQLJPASupport(entityManagerMap, entityManagerDatabase.value());
45+
}
46+
47+
@Configuration
48+
@ConditionalOnClass(SessionImplementor.class)
49+
static
50+
class HibernateEntityManagerDatabaseConfiguration {
51+
52+
@Transactional
53+
@Bean
54+
public EntityManagerDatabase entityManagerDatabase(ObjectProvider<EntityManager> entityManagers) {
55+
Map<EntityManager, Database> value = new HashMap<>();
56+
EntityManager entityManager = entityManagers.getIfAvailable();
57+
SessionFactory sessionFactory = entityManager.unwrap(Session.class).getSessionFactory();
58+
Dialect dialect = ((SessionFactoryImpl) sessionFactory).getJdbcServices().getDialect();
59+
60+
Database db = toDatabase(dialect);
61+
if (db != null) {
62+
value.put(entityManager, db);
63+
}
64+
65+
return new EntityManagerDatabase(value);
66+
}
67+
68+
private Database toDatabase(Dialect dialect) {
69+
if (dialect instanceof PostgreSQLDialect) {
70+
return Database.POSTGRESQL;
71+
} else if (dialect instanceof MySQLDialect) {
72+
return Database.MYSQL;
73+
} else if (dialect instanceof SQLServerDialect) {
74+
return Database.SQL_SERVER;
75+
} else if (dialect instanceof OracleDialect) {
76+
return Database.ORACLE;
77+
} else if (dialect instanceof DerbyDialect) {
78+
return Database.DERBY;
79+
} else if (dialect instanceof DB2Dialect) {
80+
return Database.DB2;
81+
} else if (dialect instanceof H2Dialect) {
82+
return Database.H2;
83+
} else if (dialect instanceof HSQLDialect) {
84+
return Database.HSQL;
85+
} else if (dialect instanceof SybaseDialect) {
86+
return Database.SQL_SERVER;
87+
}
88+
89+
return null;
90+
}
91+
}
92+
93+
public static final class EntityManagerDatabase {
94+
private final Map<EntityManager, Database> value;
95+
96+
public EntityManagerDatabase(Map<EntityManager, Database> value) {
97+
this.value = value;
98+
}
99+
100+
public Map<EntityManager, Database> value() {
101+
return value;
102+
}
103+
104+
@Override
105+
public boolean equals(Object obj) {
106+
if (this == obj) return true;
107+
if (obj == null || getClass() != obj.getClass()) return false;
108+
EntityManagerDatabase that = (EntityManagerDatabase) obj;
109+
return Objects.equals(value, that.value);
110+
}
111+
112+
@Override
113+
public int hashCode() {
114+
return Objects.hash(value);
115+
}
23116

117+
@Override
118+
public String toString() {
119+
return "EntityManagerDatabase[value=" + value + "]";
120+
}
121+
}
24122
}

rsql-jpa/pom.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,34 @@
4141
<artifactId>h2</artifactId>
4242
<scope>test</scope>
4343
</dependency>
44+
<dependency>
45+
<groupId>org.postgresql</groupId>
46+
<artifactId>postgresql</artifactId>
47+
<scope>test</scope>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.testcontainers</groupId>
51+
<artifactId>postgresql</artifactId>
52+
<version>1.19.0</version>
53+
<scope>test</scope>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.hibernate.common</groupId>
57+
<artifactId>hibernate-commons-annotations</artifactId>
58+
<version>5.1.2.Final</version>
59+
</dependency>
60+
<dependency>
61+
<groupId>io.hypersistence</groupId>
62+
<artifactId>hypersistence-utils-hibernate-52</artifactId>
63+
<version>3.7.6</version>
64+
<scope>test</scope>
65+
</dependency>
66+
<dependency>
67+
<groupId>net.java.dev.jna</groupId>
68+
<artifactId>jna</artifactId>
69+
<version>5.14.0</version>
70+
<scope>test</scope>
71+
</dependency>
4472
<dependency>
4573
<groupId>org.projectlombok</groupId>
4674
<artifactId>lombok</artifactId>

0 commit comments

Comments
 (0)