KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > revolt > support > DialectResolver


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.revolt.support;
25
26 import java.sql.Connection JavaDoc;
27 import java.sql.DatabaseMetaData JavaDoc;
28 import java.sql.SQLException JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32
33 import javax.sql.DataSource JavaDoc;
34
35 import org.riotfamily.revolt.Dialect;
36 import org.riotfamily.revolt.dialect.HsqlDialect;
37 import org.riotfamily.revolt.dialect.MySqlDialect;
38 import org.riotfamily.revolt.dialect.Postgresql8Dialect;
39 import org.riotfamily.revolt.dialect.PostgresqlDialect;
40 import org.springframework.dao.DataAccessException;
41 import org.springframework.jdbc.core.ConnectionCallback;
42 import org.springframework.jdbc.core.JdbcTemplate;
43
44 /**
45  * Picks a suitable dialect form a list of implementations based on the
46  * product name and version returned by the JDBC driver.
47  *
48  * @author Felix Gnass [fgnass at neteye dot de]
49  */

50 public class DialectResolver {
51
52     private List JavaDoc dialects;
53
54     public DialectResolver() {
55         dialects = new ArrayList JavaDoc();
56         dialects.add(new PostgresqlDialect());
57         dialects.add(new Postgresql8Dialect());
58         dialects.add(new MySqlDialect());
59         dialects.add(new HsqlDialect());
60     }
61
62     public Dialect getDialect(DataSource JavaDoc dataSource)
63             throws DatabaseNotSupportedException {
64         
65         JdbcTemplate template = new JdbcTemplate(dataSource);
66         return (Dialect) template.execute(new ConnectionCallback() {
67             public Object JavaDoc doInConnection(Connection JavaDoc connection)
68                     throws SQLException JavaDoc, DataAccessException {
69                 
70                 DatabaseMetaData JavaDoc metaData = connection.getMetaData();
71                 String JavaDoc productName = metaData.getDatabaseProductName();
72                 int major = metaData.getDatabaseMajorVersion();
73                 int minor = metaData.getDatabaseMinorVersion();
74                 return getDialect(productName, major, minor);
75             }
76         });
77     }
78
79     protected Dialect getDialect(String JavaDoc productName, int major, int minor) {
80         Iterator JavaDoc it = dialects.iterator();
81         while (it.hasNext()) {
82             Dialect dialect = (Dialect) it.next();
83             if (dialect.supports(productName, major, minor)) {
84                 return dialect;
85             }
86         }
87         throw new DatabaseNotSupportedException(productName, major, minor);
88     }
89
90 }
91
Popular Tags