01: /* ***** BEGIN LICENSE BLOCK *****
02: * Version: MPL 1.1
03: * The contents of this file are subject to the Mozilla Public License Version
04: * 1.1 (the "License"); you may not use this file except in compliance with
05: * the License. You may obtain a copy of the License at
06: * http://www.mozilla.org/MPL/
07: *
08: * Software distributed under the License is distributed on an "AS IS" basis,
09: * 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;
27: import java.util.HashSet;
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 nativeTypes = new HashMap();
44:
45: private HashSet typesWithLength = new HashSet();
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 getName() {
55: String 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 jdbcName, String nativeName) {
63: registerType(jdbcName, nativeName, false);
64: }
65:
66: protected final void registerType(String jdbcName,
67: String nativeName, boolean hasLength) {
68:
69: nativeTypes.put(jdbcName, nativeName);
70: if (hasLength) {
71: typesWithLength.add(jdbcName);
72: }
73: }
74:
75: protected boolean typeHasLength(String type) {
76: return typesWithLength.contains(type);
77: }
78:
79: protected final String getColumnType(Column column) {
80: String type = (String) 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: }
|