001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.jems.hibernate;
023:
024: import org.hibernate.HibernateException;
025:
026: import java.util.HashMap;
027: import java.util.Map;
028:
029: /**
030: * Ripped off hibernate 3.1 that is not in 3.0. A factory for generating Dialect instances.
031: *
032: * @author Steve Ebersole
033: * @version $Revision: 8784 $
034: */
035: public class DialectFactory {
036:
037: private DialectFactory() {
038: }
039:
040: /**
041: * Determine the appropriate Dialect to use given the database product name and major version.
042: *
043: * @param databaseName The name of the database product (obtained from metadata).
044: * @param databaseMajorVersion The major version of the database product (obtained from metadata).
045: * @return An appropriate dialect instance.
046: */
047: public static String determineDialect(String databaseName,
048: int databaseMajorVersion) {
049: if (databaseName == null) {
050: throw new HibernateException(
051: "Hibernate Dialect must be explicitly set");
052: }
053:
054: DatabaseDialectMapper mapper = (DatabaseDialectMapper) MAPPERS
055: .get(databaseName);
056: if (mapper == null) {
057: throw new HibernateException(
058: "Hibernate Dialect must be explicitly set for database: "
059: + databaseName);
060: }
061:
062: return mapper.getDialectClass(databaseMajorVersion);
063: }
064:
065: /**
066: * For a given database product name, instances of DatabaseDialectMapper know which Dialect to use for different
067: * versions.
068: */
069: public static interface DatabaseDialectMapper {
070: public String getDialectClass(int majorVersion);
071: }
072:
073: /** A simple DatabaseDialectMapper for dialects which are independent of the underlying database product version. */
074: public static class VersionInsensitiveMapper implements
075: DatabaseDialectMapper {
076: private String dialectClassName;
077:
078: public VersionInsensitiveMapper(String dialectClassName) {
079: this .dialectClassName = dialectClassName;
080: }
081:
082: public String getDialectClass(int majorVersion) {
083: return dialectClassName;
084: }
085: }
086:
087: private static final Map MAPPERS = new HashMap();
088:
089: static {
090: // TODO : this is the stuff it'd be nice to move to a properties file or some other easily user-editable place
091: MAPPERS.put("HSQL Database Engine",
092: new VersionInsensitiveMapper(
093: "org.hibernate.dialect.HSQLDialect"));
094: MAPPERS.put("DB2/NT", new VersionInsensitiveMapper(
095: "org.hibernate.dialect.DB2Dialect"));
096: MAPPERS.put("MySQL", new VersionInsensitiveMapper(
097: "org.hibernate.dialect.MySQLDialect"));
098: MAPPERS.put("PostgreSQL", new VersionInsensitiveMapper(
099: "org.hibernate.dialect.PostgreSQLDialect"));
100: MAPPERS.put("Microsoft SQL Server Database",
101: new VersionInsensitiveMapper(
102: "org.hibernate.dialect.SQLServerDialect"));
103: MAPPERS.put("Microsoft SQL Server",
104: new VersionInsensitiveMapper(
105: "org.hibernate.dialect.SQLServerDialect"));
106: MAPPERS.put("Sybase SQL Server", new VersionInsensitiveMapper(
107: "org.hibernate.dialect.SybaseDialect"));
108: MAPPERS.put("Informix Dynamic Server",
109: new VersionInsensitiveMapper(
110: "org.hibernate.dialect.InformixDialect"));
111:
112: MAPPERS.put("Oracle", new DatabaseDialectMapper() {
113: public String getDialectClass(int majorVersion) {
114: return majorVersion > 8 ? "org.hibernate.dialect.Oracle9Dialect"
115: : "org.hibernate.dialect.OracleDialect";
116: }
117: });
118: }
119: }
|