KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > dialect > DialectFactory


1 // $Id: DialectFactory.java,v 1.4 2005/06/17 16:15:48 oneovthafew Exp $
2
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 JavaDoc;
9 import java.util.Map JavaDoc;
10 import java.util.HashMap JavaDoc;
11
12 /**
13  * A factory for generating Dialect instances.
14  *
15  * @author Steve Ebersole
16  */

17 public class DialectFactory {
18
19     /**
20      * Builds an appropriate Dialect instance.
21      * <p/>
22      * If a dialect is explicitly named in the incoming properties, it is used. Otherwise, the database name and version
23      * (obtained from connection metadata) are used to make the dertemination.
24      * <p/>
25      * An exception is thrown if a dialect was not explicitly set and the database name is not known.
26      *
27      * @param props The configuration properties.
28      * @param databaseName The name of the database product (obtained from metadata).
29      * @param databaseMajorVersion The major version of the database product (obtained from metadata).
30      *
31      * @return The appropriate dialect.
32      *
33      * @throws HibernateException No dialect specified and database name not known.
34      */

35     public static Dialect buildDialect(Properties JavaDoc props, String JavaDoc databaseName, int databaseMajorVersion)
36             throws HibernateException {
37         String JavaDoc 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     /**
47      * Determine the appropriate Dialect to use given the database product name
48      * and major version.
49      *
50      * @param databaseName The name of the database product (obtained from metadata).
51      * @param databaseMajorVersion The major version of the database product (obtained from metadata).
52      *
53      * @return An appropriate dialect instance.
54      */

55     public static Dialect determineDialect(String JavaDoc 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 JavaDoc dialectName = mapper.getDialectClass( databaseMajorVersion );
66         return buildDialect( dialectName );
67     }
68
69     /**
70      * Returns a dialect instance given the name of the class to use.
71      *
72      * @param dialectName The name of the dialect class.
73      *
74      * @return The dialect instance.
75      */

76     public static Dialect buildDialect(String JavaDoc dialectName) {
77         try {
78             return ( Dialect ) ReflectHelper.classForName( dialectName ).newInstance();
79         }
80         catch ( ClassNotFoundException JavaDoc cnfe ) {
81             throw new HibernateException( "Dialect class not found: " + dialectName );
82         }
83         catch ( Exception JavaDoc e ) {
84             throw new HibernateException( "Could not instantiate dialect class", e );
85         }
86     }
87
88     /**
89      * For a given database product name, instances of
90      * DatabaseDialectMapper know which Dialect to use for different versions.
91      */

92     public static interface DatabaseDialectMapper {
93         public String JavaDoc getDialectClass(int majorVersion);
94     }
95
96     /**
97      * A simple DatabaseDialectMapper for dialects which are independent
98      * of the underlying database product version.
99      */

100     public static class VersionInsensitiveMapper implements DatabaseDialectMapper {
101         private String JavaDoc dialectClassName;
102
103         public VersionInsensitiveMapper(String JavaDoc dialectClassName) {
104             this.dialectClassName = dialectClassName;
105         }
106
107         public String JavaDoc getDialectClass(int majorVersion) {
108             return dialectClassName;
109         }
110     }
111
112     private static final Map JavaDoc MAPPERS = new HashMap JavaDoc();
113     static {
114         // TODO : this is the stuff it'd be nice to move to a properties file or some other easily user-editable place
115
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 JavaDoc 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