-
-
Notifications
You must be signed in to change notification settings - Fork 68
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8af6a54
Merge pull request #119 from nstdio/gh114
perplexhub ad97659
Fix jsonb support to work with 5.x
hchintamreddy 1aba52b
Revert the java update from 1.8 to 11
hchintamreddy 5a33e63
Revert the sprint boot version changes and junit4 to junit5 test changes
hchintamreddy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 114 additions & 9 deletions
123
...spring-boot-starter/src/main/java/io/github/perplexhub/rsql/RSQLJPAAutoConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,129 @@ | ||
package io.github.perplexhub.rsql; | ||
|
||
import java.util.Map; | ||
import static java.util.stream.Collectors.collectingAndThen; | ||
import static java.util.stream.Collectors.toMap; | ||
|
||
import io.github.perplexhub.rsql.RSQLJPAAutoConfiguration.HibernateEntityManagerDatabaseConfiguration; | ||
import javax.persistence.EntityManager; | ||
|
||
import java.util.IdentityHashMap; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.hibernate.Session; | ||
import org.hibernate.dialect.AbstractHANADialect; | ||
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; | ||
|
||
@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()); | ||
var entityManagerDatabase = entityManagerDatabaseProvider.getIfAvailable(() -> new EntityManagerDatabase(Map.of())); | ||
|
||
return new RSQLJPASupport(entityManagerMap, entityManagerDatabase.value()); | ||
} | ||
|
||
@Configuration | ||
@ConditionalOnClass(SessionImplementor.class) | ||
static | ||
class HibernateEntityManagerDatabaseConfiguration { | ||
|
||
@Bean | ||
public EntityManagerDatabase entityManagerDatabase(ObjectProvider<EntityManager> entityManagers) { | ||
return entityManagers.stream() | ||
.map(entityManager -> { | ||
var sessionFactory = entityManager.unwrap(Session.class).getSessionFactory(); | ||
var dialect = ((SessionFactoryImpl) sessionFactory).getJdbcServices().getDialect(); | ||
|
||
return Optional.ofNullable(toDatabase(dialect)) | ||
.map(db -> Map.entry(entityManager, db)) | ||
.orElse(null); | ||
}) | ||
.filter(Objects::nonNull) | ||
.collect(collectingAndThen( | ||
toMap(Entry::getKey, Entry::getValue, (db1, db2) -> db1, IdentityHashMap::new), | ||
m -> new EntityManagerDatabase(m) | ||
)); | ||
} | ||
|
||
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 AbstractHANADialect) { | ||
return Database.HANA; | ||
} 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 + "]"; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.