|  | 
| 1 | 1 | package io.github.perplexhub.rsql; | 
| 2 | 2 | 
 | 
| 3 |  | -import java.util.Map; | 
| 4 | 3 | 
 | 
|  | 4 | +import io.github.perplexhub.rsql.RSQLJPAAutoConfiguration.HibernateEntityManagerDatabaseConfiguration; | 
| 5 | 5 | 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; | 
| 7 | 25 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | 
| 8 | 26 | import org.springframework.context.annotation.Bean; | 
| 9 | 27 | 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; | 
| 12 | 31 | 
 | 
| 13 | 32 | @Slf4j | 
| 14 | 33 | @Configuration | 
| 15 | 34 | @ConditionalOnClass(EntityManager.class) | 
|  | 35 | +@Import(HibernateEntityManagerDatabaseConfiguration.class) | 
| 16 | 36 | public class RSQLJPAAutoConfiguration { | 
| 17 | 37 | 
 | 
| 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 | +      } | 
| 23 | 116 | 
 | 
|  | 117 | +      @Override | 
|  | 118 | +      public String toString() { | 
|  | 119 | +          return "EntityManagerDatabase[value=" + value + "]"; | 
|  | 120 | +      } | 
|  | 121 | +  } | 
| 24 | 122 | } | 
0 commit comments