| 
1 | 1 | package io.github.perplexhub.rsql;  | 
2 | 2 | 
 
  | 
3 |  | -import java.util.Map;  | 
4 |  | - | 
5 |  | -import javax.persistence.EntityManager;  | 
 | 3 | +import static java.util.stream.Collectors.collectingAndThen;  | 
 | 4 | +import static java.util.stream.Collectors.toMap;  | 
6 | 5 | 
 
  | 
 | 6 | +import io.github.perplexhub.rsql.RSQLJPAAutoConfiguration.HibernateEntityManagerDatabaseConfiguration;  | 
 | 7 | +import jakarta.persistence.EntityManager;  | 
 | 8 | +import java.util.IdentityHashMap;  | 
 | 9 | +import java.util.Map;  | 
 | 10 | +import java.util.Map.Entry;  | 
 | 11 | +import java.util.Objects;  | 
 | 12 | +import java.util.Optional;  | 
 | 13 | +import lombok.extern.slf4j.Slf4j;  | 
 | 14 | +import org.hibernate.Session;  | 
 | 15 | +import org.hibernate.dialect.AbstractHANADialect;  | 
 | 16 | +import org.hibernate.dialect.CockroachDialect;  | 
 | 17 | +import org.hibernate.dialect.DB2Dialect;  | 
 | 18 | +import org.hibernate.dialect.DerbyDialect;  | 
 | 19 | +import org.hibernate.dialect.Dialect;  | 
 | 20 | +import org.hibernate.dialect.H2Dialect;  | 
 | 21 | +import org.hibernate.dialect.HSQLDialect;  | 
 | 22 | +import org.hibernate.dialect.MySQLDialect;  | 
 | 23 | +import org.hibernate.dialect.OracleDialect;  | 
 | 24 | +import org.hibernate.dialect.PostgreSQLDialect;  | 
 | 25 | +import org.hibernate.dialect.SQLServerDialect;  | 
 | 26 | +import org.hibernate.dialect.SybaseDialect;  | 
 | 27 | +import org.hibernate.engine.spi.SessionImplementor;  | 
 | 28 | +import org.hibernate.internal.SessionFactoryImpl;  | 
 | 29 | +import org.springframework.beans.factory.ObjectProvider;  | 
7 | 30 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;  | 
8 | 31 | import org.springframework.context.annotation.Bean;  | 
9 | 32 | import org.springframework.context.annotation.Configuration;  | 
10 |  | - | 
11 |  | -import lombok.extern.slf4j.Slf4j;  | 
 | 33 | +import org.springframework.context.annotation.Import;  | 
 | 34 | +import org.springframework.orm.jpa.vendor.Database;  | 
12 | 35 | 
 
  | 
13 | 36 | @Slf4j  | 
14 | 37 | @Configuration  | 
15 | 38 | @ConditionalOnClass(EntityManager.class)  | 
 | 39 | +@Import(HibernateEntityManagerDatabaseConfiguration.class)  | 
16 | 40 | public class RSQLJPAAutoConfiguration {  | 
17 | 41 | 
 
  | 
18 |  | -	@Bean  | 
19 |  | -	public RSQLCommonSupport rsqlCommonSupport(Map<String, EntityManager> entityManagerMap) {  | 
20 |  | -		log.info("RSQLJPAAutoConfiguration.rsqlCommonSupport(entityManagerMap:{})", entityManagerMap.size());  | 
21 |  | -		return new RSQLCommonSupport(entityManagerMap);  | 
22 |  | -	}  | 
 | 42 | +  @Bean  | 
 | 43 | +  public RSQLCommonSupport rsqlCommonSupport(Map<String, EntityManager> entityManagerMap,  | 
 | 44 | +      ObjectProvider<EntityManagerDatabase> entityManagerDatabaseProvider) {  | 
 | 45 | +    log.info("RSQLJPAAutoConfiguration.rsqlCommonSupport(entityManagerMap:{})", entityManagerMap.size());  | 
 | 46 | +    var entityManagerDatabase = entityManagerDatabaseProvider.getIfAvailable(() -> new EntityManagerDatabase(Map.of()));  | 
 | 47 | + | 
 | 48 | +    return new RSQLJPASupport(entityManagerMap, entityManagerDatabase.value());  | 
 | 49 | +  }  | 
 | 50 | + | 
 | 51 | +  @Configuration  | 
 | 52 | +  @ConditionalOnClass(SessionImplementor.class)  | 
 | 53 | +  static  | 
 | 54 | +  class HibernateEntityManagerDatabaseConfiguration {  | 
 | 55 | + | 
 | 56 | +    @Bean  | 
 | 57 | +    public EntityManagerDatabase entityManagerDatabase(ObjectProvider<EntityManager> entityManagers) {  | 
 | 58 | +      return entityManagers.stream()  | 
 | 59 | +          .map(entityManager -> {  | 
 | 60 | +            var sessionFactory = entityManager.unwrap(Session.class).getSessionFactory();  | 
 | 61 | +            var dialect = ((SessionFactoryImpl) sessionFactory).getJdbcServices().getDialect();  | 
 | 62 | + | 
 | 63 | +            return Optional.ofNullable(toDatabase(dialect))  | 
 | 64 | +                .map(db -> Map.entry(entityManager, db))  | 
 | 65 | +                .orElse(null);  | 
 | 66 | +          })  | 
 | 67 | +          .filter(Objects::nonNull)  | 
 | 68 | +          .collect(collectingAndThen(  | 
 | 69 | +              toMap(Entry::getKey, Entry::getValue, (db1, db2) -> db1, IdentityHashMap::new),  | 
 | 70 | +              EntityManagerDatabase::new  | 
 | 71 | +          ));  | 
 | 72 | +    }  | 
 | 73 | + | 
 | 74 | +    private Database toDatabase(Dialect dialect) {  | 
 | 75 | +      if (dialect instanceof PostgreSQLDialect || dialect instanceof CockroachDialect) {  | 
 | 76 | +        return Database.POSTGRESQL;  | 
 | 77 | +      } else if (dialect instanceof MySQLDialect) {  | 
 | 78 | +        return Database.MYSQL;  | 
 | 79 | +      } else if (dialect instanceof SQLServerDialect) {  | 
 | 80 | +        return Database.SQL_SERVER;  | 
 | 81 | +      } else if (dialect instanceof OracleDialect) {  | 
 | 82 | +        return Database.ORACLE;  | 
 | 83 | +      } else if (dialect instanceof DerbyDialect) {  | 
 | 84 | +        return Database.DERBY;  | 
 | 85 | +      } else if (dialect instanceof DB2Dialect) {  | 
 | 86 | +        return Database.DB2;  | 
 | 87 | +      } else if (dialect instanceof H2Dialect) {  | 
 | 88 | +        return Database.H2;  | 
 | 89 | +      } else if (dialect instanceof AbstractHANADialect) {  | 
 | 90 | +        return Database.HANA;  | 
 | 91 | +      } else if (dialect instanceof HSQLDialect) {  | 
 | 92 | +        return Database.HSQL;  | 
 | 93 | +      } else if (dialect instanceof SybaseDialect) {  | 
 | 94 | +        return Database.SQL_SERVER;  | 
 | 95 | +      }  | 
 | 96 | + | 
 | 97 | +      return null;  | 
 | 98 | +    }  | 
 | 99 | +  }  | 
 | 100 | + | 
 | 101 | +  record EntityManagerDatabase(Map<EntityManager, Database> value) {  | 
23 | 102 | 
 
  | 
 | 103 | +  }  | 
24 | 104 | }  | 
0 commit comments