KickJava   Java API By Example, From Geeks To Geeks.

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


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.StringListParser;
15 import com.versant.core.jdbc.JdbcConverterFactory;
16 import com.versant.core.jdbc.JdbcConverterFactoryRegistry;
17 import com.versant.core.jdbc.JdbcConverter;
18 import com.versant.core.jdbc.JdbcTypeRegistry;
19
20 import com.versant.core.common.BindingSupportImpl;
21
22 /**
23  * This is a rule mapping a JDBC type code from java.sql.Types and optional
24  * database type name to column properties. The SqlDriver class for the
25  * datastore provides a list of these to do basic mapping. The datastore
26  * may provide additional rules to override the driver supplied rules.
27  *
28  * @see JdbcMappingResolver
29  */

30 public class JdbcTypeMapping implements Cloneable JavaDoc {
31
32     public static final int NOT_SET = 0;
33     public static final int FALSE = 1;
34     public static final int TRUE = 2;
35
36     private String JavaDoc database;
37     private int jdbcType;
38     private String JavaDoc sqlType;
39     private int length = -1;
40     private int scale = -1;
41     private int nulls = NOT_SET;
42     private int equalityTest = NOT_SET;
43     private JdbcConverterFactory converterFactory;
44
45     public JdbcTypeMapping() {
46     }
47
48     public JdbcTypeMapping(String JavaDoc sqlType, int length, int scale,
49             int nulls, int equalityTest, JdbcConverterFactory converter) {
50         this.sqlType = sqlType;
51         this.length = length;
52         this.scale = scale;
53         this.nulls = nulls;
54         this.equalityTest = equalityTest;
55         this.converterFactory = converter;
56     }
57
58     public String JavaDoc getDatabase() {
59         return database;
60     }
61
62     public void setDatabase(String JavaDoc database) {
63         this.database = database;
64     }
65
66     public int getJdbcType() {
67         return jdbcType;
68     }
69
70     public void setJdbcType(int jdbcType) {
71         this.jdbcType = jdbcType;
72     }
73
74     public String JavaDoc getSqlType() {
75         return sqlType;
76     }
77
78     public void setSqlType(String JavaDoc sqlType) {
79         this.sqlType = sqlType;
80     }
81
82     public int getLength() {
83         return length;
84     }
85
86     public void setLength(int length) {
87         this.length = length;
88     }
89
90     public int getScale() {
91         return scale;
92     }
93
94     public void setScale(int scale) {
95         this.scale = scale;
96     }
97
98     public int getNulls() {
99         return nulls;
100     }
101
102     public void setNulls(int nulls) {
103         this.nulls = nulls;
104     }
105
106     public int getEqualityTest() {
107         return equalityTest;
108     }
109
110     public void setEqualityTest(int equalityTest) {
111         this.equalityTest = equalityTest;
112     }
113
114     public JdbcConverterFactory getConverterFactory() {
115         return converterFactory;
116     }
117
118     public void setConverterFactory(JdbcConverterFactory converterFactory) {
119         this.converterFactory = converterFactory;
120     }
121
122     public String JavaDoc toString() {
123         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
124         if (database != null) s.append("database: " + database + " ");
125         if (jdbcType != 0) {
126             s.append("jdbcType: " + JdbcTypes.toString(jdbcType) + " ");
127         }
128         if (sqlType != null) s.append("sqlType: " + sqlType + " ");
129         if (length >= 0) s.append("length: " + length + " ");
130         if (scale >= 0) s.append("scale: " + scale + " ");
131         if (nulls != NOT_SET) s.append("nulls: " + (nulls == TRUE) + " ");
132         if (equalityTest != NOT_SET) {
133             s.append("equalityTest: " + (equalityTest == TRUE) + " ");
134         }
135         if (converterFactory != null) {
136             s.append("converterFactory: " + converterFactory);
137         }
138         return s.toString();
139     }
140
141     private int nextTriState(StringListParser p, String JavaDoc name) {
142         String JavaDoc s = p.nextQuotedString();
143         if (s == null) {
144             return NOT_SET;
145         } else if (s.equals("true")) {
146             return TRUE;
147         } else if (s.equals("false")) {
148             return FALSE;
149         } else {
150             throw BindingSupportImpl.getInstance().runtime(
151                     "Invalid " + name + " setting: '" + s + "'");
152         }
153     }
154
155     /**
156      * Parse from p.
157      */

158     public void parse(StringListParser p, JdbcConverterFactoryRegistry jcfreg) {
159         jdbcType = JdbcTypes.parse(p.nextString());
160         database = p.nextQuotedString();
161         sqlType = p.nextQuotedString();
162         length = getInt(p);
163         scale = getInt(p);
164         nulls = nextTriState(p, "nulls");
165         equalityTest = nextTriState(p, "equalityTest");
166         String JavaDoc s = p.nextQuotedString();
167         if (s != null) converterFactory = jcfreg.getFactory(s);
168     }
169
170     private int getInt(StringListParser p) {
171         String JavaDoc s = p.nextQuotedString();
172         if (s == null) return -1;
173         return Integer.parseInt(s);
174     }
175
176     /**
177      * Do the parameters match this rule?
178      *
179      * @param jdbcType JDBC type code from java.sql.Types
180      * @param database Database name
181      */

182     public boolean match(int jdbcType, String JavaDoc database) {
183         return this.jdbcType == jdbcType
184                 && (database == null
185                 || this.database == null
186                 || this.database.equals(database));
187     }
188
189     /**
190      * Copy in fields from another rule. Only fields not set in this rule
191      * are changed.
192      */

193     public void copyFrom(JdbcTypeMapping r) {
194         if (database == null) database = r.database;
195         if (jdbcType == 0) jdbcType = r.jdbcType;
196         if (sqlType == null) sqlType = r.sqlType;
197         if (length == -1) length = r.length;
198         if (scale == -1) scale = r.scale;
199         if (nulls == NOT_SET) nulls = r.nulls;
200         if (equalityTest == NOT_SET) equalityTest = r.equalityTest;
201         if (converterFactory == null) converterFactory = r.converterFactory;
202     }
203
204     public Object JavaDoc clone() {
205         try {
206             return super.clone();
207         } catch (CloneNotSupportedException JavaDoc e) {
208             throw new IllegalStateException JavaDoc("clone() failed?");
209         }
210     }
211
212 }
213
Popular Tags