KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > revolt > dialect > AbstractDialect


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.dialect;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.HashSet JavaDoc;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.riotfamily.revolt.Dialect;
32 import org.riotfamily.revolt.definition.Column;
33 import org.riotfamily.revolt.support.TypeMap;
34
35 /**
36  * @author Felix Gnass [fgnass at neteye dot de]
37  *
38  */

39 public abstract class AbstractDialect implements Dialect {
40
41     private static Log log = LogFactory.getLog(AbstractDialect.class);
42     
43     private HashMap JavaDoc nativeTypes = new HashMap JavaDoc();
44     
45     private HashSet JavaDoc typesWithLength = new HashSet JavaDoc();
46
47     public AbstractDialect() {
48         registerTypes();
49         if (!TypeMap.isComplete(nativeTypes)) {
50             log.warn("Dialect does not support all JDBC types!");
51         }
52     }
53     
54     public String JavaDoc getName() {
55         String JavaDoc s = getClass().getName();
56         s = s.substring(s.lastIndexOf('.') + 1);
57         return s.substring(0, s.indexOf("Dialect"));
58     }
59
60     protected abstract void registerTypes();
61     
62     protected final void registerType(String JavaDoc jdbcName, String JavaDoc nativeName) {
63         registerType(jdbcName, nativeName, false);
64     }
65     
66     protected final void registerType(String JavaDoc jdbcName, String JavaDoc nativeName,
67             boolean hasLength) {
68         
69         nativeTypes.put(jdbcName, nativeName);
70         if (hasLength) {
71             typesWithLength.add(jdbcName);
72         }
73     }
74     
75     protected boolean typeHasLength(String JavaDoc type) {
76         return typesWithLength.contains(type);
77     }
78
79     protected final String JavaDoc getColumnType(Column column) {
80         String JavaDoc type = (String JavaDoc) nativeTypes.get(column.getType());
81         if (type == null) {
82             throw new TypeNotSupportedException(column.getType());
83         }
84         if (column.isLengthSet() && typeHasLength(type)) {
85             type += "(" + column.getLength() + ")";
86         }
87         return type;
88     }
89     
90 }
91
Popular Tags