1 package org.hibernate.dialect; 3 4 import org.hibernate.cfg.Environment; 5 import org.hibernate.HibernateException; 6 import org.hibernate.util.ReflectHelper; 7 8 import java.util.Properties ; 9 import java.util.Map ; 10 import java.util.HashMap ; 11 12 17 public class DialectFactory { 18 19 35 public static Dialect buildDialect(Properties props, String databaseName, int databaseMajorVersion) 36 throws HibernateException { 37 String dialectName = props.getProperty( Environment.DIALECT ); 38 if ( dialectName == null ) { 39 return determineDialect( databaseName, databaseMajorVersion ); 40 } 41 else { 42 return buildDialect( dialectName ); 43 } 44 } 45 46 55 public static Dialect determineDialect(String databaseName, int databaseMajorVersion) { 56 if ( databaseName == null ) { 57 throw new HibernateException( "Hibernate Dialect must be explicitly set" ); 58 } 59 60 DatabaseDialectMapper mapper = ( DatabaseDialectMapper ) MAPPERS.get( databaseName ); 61 if ( mapper == null ) { 62 throw new HibernateException( "Hibernate Dialect must be explicitly set for database: " + databaseName ); 63 } 64 65 String dialectName = mapper.getDialectClass( databaseMajorVersion ); 66 return buildDialect( dialectName ); 67 } 68 69 76 public static Dialect buildDialect(String dialectName) { 77 try { 78 return ( Dialect ) ReflectHelper.classForName( dialectName ).newInstance(); 79 } 80 catch ( ClassNotFoundException cnfe ) { 81 throw new HibernateException( "Dialect class not found: " + dialectName ); 82 } 83 catch ( Exception e ) { 84 throw new HibernateException( "Could not instantiate dialect class", e ); 85 } 86 } 87 88 92 public static interface DatabaseDialectMapper { 93 public String getDialectClass(int majorVersion); 94 } 95 96 100 public static class VersionInsensitiveMapper implements DatabaseDialectMapper { 101 private String dialectClassName; 102 103 public VersionInsensitiveMapper(String dialectClassName) { 104 this.dialectClassName = dialectClassName; 105 } 106 107 public String getDialectClass(int majorVersion) { 108 return dialectClassName; 109 } 110 } 111 112 private static final Map MAPPERS = new HashMap (); 113 static { 114 MAPPERS.put( "HSQL Database Engine", new VersionInsensitiveMapper( "org.hibernate.dialect.HSQLDialect" ) ); 116 MAPPERS.put( "DB2/NT", new VersionInsensitiveMapper( "org.hibernate.dialect.DB2Dialect" ) ); 117 MAPPERS.put( "MySQL", new VersionInsensitiveMapper( "org.hibernate.dialect.MySQLDialect" ) ); 118 MAPPERS.put( "PostgreSQL", new VersionInsensitiveMapper( "org.hibernate.dialect.PostgreSQLDialect" ) ); 119 MAPPERS.put( "Microsoft SQL Server Database", new VersionInsensitiveMapper( "org.hibernate.dialect.SQLServerDialect" ) ); 120 MAPPERS.put( "Microsoft SQL Server", new VersionInsensitiveMapper( "org.hibernate.dialect.SQLServerDialect" ) ); 121 MAPPERS.put( "Sybase SQL Server", new VersionInsensitiveMapper( "org.hibernate.dialect.SybaseDialect" ) ); 122 MAPPERS.put( "Informix Dynamic Server", new VersionInsensitiveMapper( "org.hibernate.dialect.InformixDialect" ) ); 123 124 MAPPERS.put( 125 "Oracle", 126 new DatabaseDialectMapper() { 127 public String getDialectClass(int majorVersion) { 128 return majorVersion > 8 129 ? "org.hibernate.dialect.Oracle9Dialect" 130 : "org.hibernate.dialect.OracleDialect"; 131 } 132 } 133 ); 134 } 135 } 136 | Popular Tags |