001: /*
002: * Copyright (c) 1998 - 2005 Versant Corporation
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Versant Corporation - initial API and implementation
010: */
011: package com.versant.core.jdbc.metadata;
012:
013: import com.versant.core.util.StringListParser;
014: import com.versant.core.jdbc.JdbcConverterFactory;
015: import com.versant.core.jdbc.JdbcConverterFactoryRegistry;
016: import com.versant.core.jdbc.JdbcConverter;
017: import com.versant.core.jdbc.JdbcTypeRegistry;
018:
019: import com.versant.core.common.BindingSupportImpl;
020:
021: /**
022: * This is a rule mapping a JDBC type code from java.sql.Types and optional
023: * database type name to column properties. The SqlDriver class for the
024: * datastore provides a list of these to do basic mapping. The datastore
025: * may provide additional rules to override the driver supplied rules.
026: *
027: * @see JdbcMappingResolver
028: */
029: public class JdbcTypeMapping implements Cloneable {
030:
031: public static final int NOT_SET = 0;
032: public static final int FALSE = 1;
033: public static final int TRUE = 2;
034:
035: private String database;
036: private int jdbcType;
037: private String sqlType;
038: private int length = -1;
039: private int scale = -1;
040: private int nulls = NOT_SET;
041: private int equalityTest = NOT_SET;
042: private JdbcConverterFactory converterFactory;
043:
044: public JdbcTypeMapping() {
045: }
046:
047: public JdbcTypeMapping(String sqlType, int length, int scale,
048: int nulls, int equalityTest, JdbcConverterFactory converter) {
049: this .sqlType = sqlType;
050: this .length = length;
051: this .scale = scale;
052: this .nulls = nulls;
053: this .equalityTest = equalityTest;
054: this .converterFactory = converter;
055: }
056:
057: public String getDatabase() {
058: return database;
059: }
060:
061: public void setDatabase(String database) {
062: this .database = database;
063: }
064:
065: public int getJdbcType() {
066: return jdbcType;
067: }
068:
069: public void setJdbcType(int jdbcType) {
070: this .jdbcType = jdbcType;
071: }
072:
073: public String getSqlType() {
074: return sqlType;
075: }
076:
077: public void setSqlType(String sqlType) {
078: this .sqlType = sqlType;
079: }
080:
081: public int getLength() {
082: return length;
083: }
084:
085: public void setLength(int length) {
086: this .length = length;
087: }
088:
089: public int getScale() {
090: return scale;
091: }
092:
093: public void setScale(int scale) {
094: this .scale = scale;
095: }
096:
097: public int getNulls() {
098: return nulls;
099: }
100:
101: public void setNulls(int nulls) {
102: this .nulls = nulls;
103: }
104:
105: public int getEqualityTest() {
106: return equalityTest;
107: }
108:
109: public void setEqualityTest(int equalityTest) {
110: this .equalityTest = equalityTest;
111: }
112:
113: public JdbcConverterFactory getConverterFactory() {
114: return converterFactory;
115: }
116:
117: public void setConverterFactory(
118: JdbcConverterFactory converterFactory) {
119: this .converterFactory = converterFactory;
120: }
121:
122: public String toString() {
123: StringBuffer s = new StringBuffer();
124: if (database != null)
125: s.append("database: " + database + " ");
126: if (jdbcType != 0) {
127: s.append("jdbcType: " + JdbcTypes.toString(jdbcType) + " ");
128: }
129: if (sqlType != null)
130: s.append("sqlType: " + sqlType + " ");
131: if (length >= 0)
132: s.append("length: " + length + " ");
133: if (scale >= 0)
134: s.append("scale: " + scale + " ");
135: if (nulls != NOT_SET)
136: s.append("nulls: " + (nulls == TRUE) + " ");
137: if (equalityTest != NOT_SET) {
138: s.append("equalityTest: " + (equalityTest == TRUE) + " ");
139: }
140: if (converterFactory != null) {
141: s.append("converterFactory: " + converterFactory);
142: }
143: return s.toString();
144: }
145:
146: private int nextTriState(StringListParser p, String name) {
147: String s = p.nextQuotedString();
148: if (s == null) {
149: return NOT_SET;
150: } else if (s.equals("true")) {
151: return TRUE;
152: } else if (s.equals("false")) {
153: return FALSE;
154: } else {
155: throw BindingSupportImpl.getInstance().runtime(
156: "Invalid " + name + " setting: '" + s + "'");
157: }
158: }
159:
160: /**
161: * Parse from p.
162: */
163: public void parse(StringListParser p,
164: JdbcConverterFactoryRegistry jcfreg) {
165: jdbcType = JdbcTypes.parse(p.nextString());
166: database = p.nextQuotedString();
167: sqlType = p.nextQuotedString();
168: length = getInt(p);
169: scale = getInt(p);
170: nulls = nextTriState(p, "nulls");
171: equalityTest = nextTriState(p, "equalityTest");
172: String s = p.nextQuotedString();
173: if (s != null)
174: converterFactory = jcfreg.getFactory(s);
175: }
176:
177: private int getInt(StringListParser p) {
178: String s = p.nextQuotedString();
179: if (s == null)
180: return -1;
181: return Integer.parseInt(s);
182: }
183:
184: /**
185: * Do the parameters match this rule?
186: *
187: * @param jdbcType JDBC type code from java.sql.Types
188: * @param database Database name
189: */
190: public boolean match(int jdbcType, String database) {
191: return this .jdbcType == jdbcType
192: && (database == null || this .database == null || this .database
193: .equals(database));
194: }
195:
196: /**
197: * Copy in fields from another rule. Only fields not set in this rule
198: * are changed.
199: */
200: public void copyFrom(JdbcTypeMapping r) {
201: if (database == null)
202: database = r.database;
203: if (jdbcType == 0)
204: jdbcType = r.jdbcType;
205: if (sqlType == null)
206: sqlType = r.sqlType;
207: if (length == -1)
208: length = r.length;
209: if (scale == -1)
210: scale = r.scale;
211: if (nulls == NOT_SET)
212: nulls = r.nulls;
213: if (equalityTest == NOT_SET)
214: equalityTest = r.equalityTest;
215: if (converterFactory == null)
216: converterFactory = r.converterFactory;
217: }
218:
219: public Object clone() {
220: try {
221: return super .clone();
222: } catch (CloneNotSupportedException e) {
223: throw new IllegalStateException("clone() failed?");
224: }
225: }
226:
227: }
|