KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdbc > metadata > JdbcJavaTypeMapping


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdbc.metadata;
13
14 import com.versant.core.util.*;
15 import com.versant.core.jdbc.JdbcConverterFactory;
16 import com.versant.core.jdbc.JdbcConverterFactoryRegistry;
17
18 import com.versant.core.common.BindingSupportImpl;
19
20 /**
21  * This is a rule mapping a field name, java type and database name to
22  * column properties. These rules can be set at class level (on a per
23  * field basis) and at datastore level.
24  *
25  * @see JdbcMappingResolver
26  */

27 public class JdbcJavaTypeMapping {
28
29     public static final int NOT_SET = 0;
30     public static final int FALSE = 1;
31     public static final int TRUE = 2;
32
33     private Class JavaDoc javaType;
34     private String JavaDoc database;
35     private String JavaDoc columnName;
36     private int jdbcType;
37     private String JavaDoc sqlType;
38     private int length = -1;
39     private int scale = -1;
40     private int nulls = NOT_SET;
41     private int equalityTest = NOT_SET;
42     private JdbcConverterFactory converterFactory;
43     private int enabled = NOT_SET;
44     private int shared = NOT_SET;
45
46     public JdbcJavaTypeMapping() {
47     }
48
49     public JdbcJavaTypeMapping(Class JavaDoc javaType, int jdbcType, int length,
50             int scale, int nulls, JdbcConverterFactory converterFactory) {
51         this.javaType = javaType;
52         this.jdbcType = jdbcType;
53         this.length = length;
54         this.scale = scale;
55         this.nulls = nulls;
56         this.equalityTest = TRUE;
57         this.converterFactory = converterFactory;
58         enabled = TRUE;
59     }
60
61     public JdbcJavaTypeMapping(Class JavaDoc javaType, int jdbcType, int length,
62             int scale, int nulls, JdbcConverterFactory converterFactory,
63             boolean enabled) {
64         this(javaType, jdbcType, length, scale, nulls, converterFactory);
65         this.enabled = enabled ? TRUE : FALSE;
66     }
67
68     public JdbcJavaTypeMapping(Class JavaDoc javaType, int jdbcType, boolean nulls,
69             boolean equalityTest) {
70         this(javaType, jdbcType, nulls);
71         this.equalityTest = equalityTest ? TRUE : FALSE;
72         enabled = TRUE;
73     }
74
75     public JdbcJavaTypeMapping(Class JavaDoc javaType, int jdbcType, boolean nulls) {
76         this.javaType = javaType;
77         this.jdbcType = jdbcType;
78         this.nulls = nulls ? TRUE : FALSE;
79         enabled = TRUE;
80     }
81
82     public JdbcJavaTypeMapping(Class JavaDoc javaType, int jdbcType,
83             JdbcConverterFactory converterFactory) {
84         this.javaType = javaType;
85         this.jdbcType = jdbcType;
86         this.nulls = TRUE;
87         this.equalityTest = FALSE;
88         this.converterFactory = converterFactory;
89         enabled = TRUE;
90     }
91
92     public JdbcJavaTypeMapping(Class JavaDoc javaType, int jdbcType,
93             JdbcConverterFactory converterFactory, boolean enabled) {
94         this(javaType, jdbcType, converterFactory);
95         this.enabled = enabled ? TRUE : FALSE;
96     }
97
98     public Class JavaDoc getJavaType() {
99         return javaType;
100     }
101
102     public void setJavaType(Class JavaDoc javaType) {
103         this.javaType = javaType;
104     }
105
106     public String JavaDoc getDatabase() {
107         return database;
108     }
109
110     public void setDatabase(String JavaDoc database) {
111         this.database = database;
112     }
113
114     public String JavaDoc getColumnName() {
115         return columnName;
116     }
117
118     public void setColumnName(String JavaDoc columnName) {
119         this.columnName = columnName;
120     }
121
122     public int getJdbcType() {
123         return jdbcType;
124     }
125
126     public void setJdbcType(int jdbcType) {
127         this.jdbcType = jdbcType;
128     }
129
130     public String JavaDoc getSqlType() {
131         return sqlType;
132     }
133
134     public void setSqlType(String JavaDoc sqlType) {
135         this.sqlType = sqlType;
136     }
137
138     public int getLength() {
139         return length;
140     }
141
142     public void setLength(int length) {
143         this.length = length;
144     }
145
146     public int getScale() {
147         return scale;
148     }
149
150     public void setScale(int scale) {
151         this.scale = scale;
152     }
153
154     public int getNulls() {
155         return nulls;
156     }
157
158     public void setNulls(int nulls) {
159         this.nulls = nulls;
160     }
161
162     public void setNulls(boolean nulls) {
163         this.nulls = nulls ? TRUE : FALSE;
164     }
165
166     public int getEqualityTest() {
167         return equalityTest;
168     }
169
170     public void setEqualityTest(int equalityTest) {
171         this.equalityTest = equalityTest;
172     }
173
174     public void setEqualityTest(boolean equalityTest) {
175         this.equalityTest = equalityTest ? TRUE : FALSE;
176     }
177
178     public JdbcConverterFactory getConverterFactory() {
179         return converterFactory;
180     }
181
182     public void setConverterFactory(JdbcConverterFactory converterFactory) {
183         this.converterFactory = converterFactory;
184     }
185
186     public int getEnabled() {
187         return enabled;
188     }
189
190     public void setEnabled(boolean enabled) {
191         this.enabled = enabled ? TRUE : FALSE;
192     }
193
194     public int getShared() {
195         return shared;
196     }
197
198     public void setShared(int shared) {
199         this.shared = shared;
200     }
201
202     public void setShared(boolean on) {
203         this.shared = on ? TRUE : FALSE;
204     }
205
206     public String JavaDoc toString() {
207         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
208         if (javaType != null) s.append("javaType: " + javaType + " ");
209         if (database != null) s.append("database: " + database + " ");
210         if (jdbcType != 0) s.append("jdbcType: " + JdbcTypes.toString(jdbcType) + " ");
211         if (sqlType != null) s.append("sqlType: " + sqlType + " ");
212         if (length >= 0) s.append("length: " + length + " ");
213         if (scale >= 0) s.append("scale: " + scale + " ");
214         if (nulls != NOT_SET) s.append("nulls: " + (nulls == TRUE) + " ");
215         if (equalityTest != NOT_SET) s.append("equalityTest: " + (equalityTest == TRUE) + " ");
216         if (converterFactory != null) s.append("converterFactory: " + converterFactory);
217         if (enabled != NOT_SET) s.append("enabled: " + (enabled == TRUE) + " ");
218         if (shared != NOT_SET) s.append("shared: " + (shared == TRUE) + " ");
219         return s.toString();
220     }
221
222     /**
223      * Do the parameters match this rule?
224      * @param database Database name
225      * @param fieldName Field name (may be null)
226      * @param javaType Java type name
227      */

228     public boolean match(String JavaDoc database, String JavaDoc fieldName, Class JavaDoc javaType) {
229         return (this.database == null || this.database.equals(database))
230             && (this.javaType == null || this.javaType == javaType);
231     }
232
233     /**
234      * Copy in fields from another rule. Only fields not set in this rule
235      * are changed. If r is a composite rule then the extra mappings are
236      * duplicated and hooked up to this rule.
237      */

238     public void copyFrom(JdbcJavaTypeMapping r) {
239         if (javaType == null) javaType = r.javaType;
240         if (database == null) database = r.database;
241         if (columnName == null) columnName = r.columnName;
242         if (jdbcType == 0) jdbcType = r.jdbcType;
243         if (sqlType == null) sqlType = r.sqlType;
244         if (length == -1) length = r.length;
245         if (scale == -1) scale = r.scale;
246         if (nulls == NOT_SET) nulls = r.nulls;
247         if (equalityTest == NOT_SET) equalityTest = r.equalityTest;
248         if (converterFactory == null) converterFactory = r.converterFactory;
249         if (enabled == NOT_SET) enabled = r.enabled;
250         if (shared == NOT_SET) shared = r.shared;
251     }
252
253     /**
254      * Copy in fields from a type rule. Only fields not set in this rule
255      * are changed.
256      */

257     public void copyFrom(JdbcTypeMapping r) {
258         if (sqlType == null) sqlType = r.getSqlType();
259         if (length == -1) length = r.getLength();
260         if (scale == -1) scale = r.getScale();
261         if (nulls == NOT_SET) nulls = r.getNulls();
262         if (equalityTest == NOT_SET) equalityTest = r.getEqualityTest();
263         if (converterFactory == null) converterFactory = r.getConverterFactory();
264     }
265
266     private int nextTriState(StringListParser p, String JavaDoc name) {
267         String JavaDoc s = p.nextQuotedString();
268         if (s == null) return NOT_SET;
269         else if (s.equals("true")) return TRUE;
270         else if (s.equals("false")) return FALSE;
271         else throw BindingSupportImpl.getInstance().runtime("Invalid " + name + " setting: '" + s + "'");
272     }
273
274     /**
275      * Parse from p.
276      */

277     public void parse(StringListParser p, JdbcConverterFactoryRegistry jcfreg) {
278         try {
279             javaType = p.nextClass(jcfreg.getLoader());
280         } catch (ClassNotFoundException JavaDoc e) {
281             throw BindingSupportImpl.getInstance().runtime(
282                 "Unable to load class: " + e.getMessage() , e);
283         }
284         database = p.nextQuotedString();
285         String JavaDoc s = p.nextQuotedString();
286         jdbcType = s == null ? 0 : JdbcTypes.parse(s);
287         sqlType = p.nextQuotedString();
288         length = getInt(p);
289         scale = getInt(p);
290         nulls = nextTriState(p, "nulls");
291         equalityTest = nextTriState(p, "equalityTest");
292         s = p.nextQuotedString();
293         if (s != null) converterFactory = jcfreg.getFactory(s);
294         if (p.hasNext()) enabled = nextTriState(p, "enabled");
295     }
296
297     private int getInt(StringListParser p) {
298         String JavaDoc s = p.nextQuotedString();
299         if (s == null) return -1;
300         return Integer.parseInt(s);
301     }
302
303 }
304
305
Popular Tags