KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > backend > SequoiaResultSetMetaDataFactory


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2006 Continuent, Inc.
4  * Contact: sequoia@continuent.org
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * Initial developer(s): Emmanuel Cecchet.
19  * Contributor(s): ______________________.
20  */

21
22 package org.continuent.sequoia.controller.backend;
23
24 import java.sql.ResultSetMetaData JavaDoc;
25 import java.sql.SQLException JavaDoc;
26
27 import org.continuent.sequoia.common.protocol.Field;
28 import org.continuent.sequoia.controller.cache.metadata.MetadataCache;
29
30 /**
31  * This class defines a SequoiaResultSetMetaDataFactory
32  *
33  * @author <a HREF="mailto:emmanuel.cecchet@continuent.com">Emmanuel Cecchet</a>
34  * @version 1.0
35  */

36 public class SequoiaResultSetMetaDataFactory
37     implements
38       ResultSetMetaDataFactory
39 {
40
41   /**
42    * @see org.continuent.sequoia.controller.backend.ResultSetMetaDataFactory#copyResultSetMetaData(java.sql.ResultSetMetaData,
43    * org.continuent.sequoia.controller.cache.metadata.MetadataCache)
44    */

45   public Field[] copyResultSetMetaData(ResultSetMetaData JavaDoc metaData,
46       MetadataCache metadataCache) throws SQLException JavaDoc
47   {
48     if (metaData == null)
49     {
50       // Nothing to fetch
51
return new Field[0];
52       // At least two drivers (pgjdbc & hsqldb) return a null reference
53
// in this case, so we are not transparent here. See SEQUOIA-262.
54
}
55
56     int nbColumn = metaData.getColumnCount();
57     Field[] fields = new Field[nbColumn];
58     for (int i = 1; i <= nbColumn; i++)
59     { // 1st column is 1
60
String JavaDoc columnName = metaData.getColumnName(i);
61       String JavaDoc fTableName = null;
62       try
63       {
64         fTableName = metaData.getTableName(i);
65       }
66       catch (Exception JavaDoc ignore)
67       {
68       }
69       String JavaDoc columnLabel = null;
70       try
71       {
72         columnLabel = metaData.getColumnLabel(i);
73       }
74       catch (Exception JavaDoc ignore)
75       {
76       }
77       if (metadataCache != null)
78       { // Check Field cache
79
fields[i - 1] = metadataCache.getField(fTableName + "." + columnName
80             + "." + columnLabel);
81         if (fields[i - 1] != null)
82           continue; // Cache hit
83
}
84       // Field cache miss
85
int fColumnDisplaySize = 0;
86       try
87       {
88         fColumnDisplaySize = metaData.getColumnDisplaySize(i);
89       }
90       catch (Exception JavaDoc ignore)
91       {
92       }
93       int columnType = -1;
94       try
95       {
96         columnType = metaData.getColumnType(i);
97       }
98       catch (Exception JavaDoc ignore)
99       {
100       }
101       String JavaDoc columnTypeName = null;
102       try
103       {
104         columnTypeName = metaData.getColumnTypeName(i);
105       }
106       catch (Exception JavaDoc ignore)
107       {
108       }
109       String JavaDoc fColumnClassName = null;
110       try
111       {
112         fColumnClassName = metaData.getColumnClassName(i);
113       }
114       catch (Exception JavaDoc ignore)
115       {
116       }
117       boolean fIsAutoIncrement = false;
118       try
119       {
120         fIsAutoIncrement = metaData.isAutoIncrement(i);
121       }
122       catch (Exception JavaDoc ignore)
123       {
124       }
125       boolean fIsCaseSensitive = false;
126       try
127       {
128         fIsCaseSensitive = metaData.isCaseSensitive(i);
129       }
130       catch (Exception JavaDoc ignore)
131       {
132       }
133       boolean fIsCurrency = false;
134       try
135       {
136         fIsCurrency = metaData.isCurrency(i);
137       }
138       catch (Exception JavaDoc ignore)
139       {
140       }
141       int fIsNullable = ResultSetMetaData.columnNullableUnknown;
142       try
143       {
144         fIsNullable = metaData.isNullable(i);
145       }
146       catch (Exception JavaDoc ignore)
147       {
148       }
149       boolean fIsReadOnly = false;
150       try
151       {
152         fIsReadOnly = metaData.isReadOnly(i);
153       }
154       catch (Exception JavaDoc ignore)
155       {
156       }
157       boolean fIsWritable = false;
158       try
159       {
160         fIsWritable = metaData.isWritable(i);
161       }
162       catch (Exception JavaDoc ignore)
163       {
164       }
165       boolean fIsDefinitelyWritable = false;
166       try
167       {
168         fIsDefinitelyWritable = metaData.isDefinitelyWritable(i);
169       }
170       catch (Exception JavaDoc ignore)
171       {
172       }
173       boolean fIsSearchable = false;
174       try
175       {
176         fIsSearchable = metaData.isSearchable(i);
177       }
178       catch (Exception JavaDoc ignore)
179       {
180       }
181       boolean fIsSigned = false;
182       try
183       {
184         fIsSigned = metaData.isSigned(i);
185       }
186       catch (Exception JavaDoc ignore)
187       {
188       }
189       int fPrecision = 0;
190       try
191       {
192         fPrecision = metaData.getPrecision(i);
193       }
194       catch (Exception JavaDoc ignore)
195       {
196       }
197       int fScale = 0;
198       try
199       {
200         fScale = metaData.getScale(i);
201       }
202       catch (Exception JavaDoc ignore)
203       {
204       }
205       String JavaDoc encoding = null;
206       // Here we trust the backend's driver not to change behind our back the
207
// metadata Strings it gave to us (this would be a rather weird
208
// interpretation of the JDBC spec)
209
fields[i - 1] = new Field(fTableName, columnName, columnLabel,
210           fColumnDisplaySize, columnType, columnTypeName, fColumnClassName,
211           fIsAutoIncrement, fIsCaseSensitive, fIsCurrency, fIsNullable,
212           fIsReadOnly, fIsWritable, fIsDefinitelyWritable, fIsSearchable,
213           fIsSigned, fPrecision, fScale, encoding);
214
215       if (metadataCache != null)
216         // Add field to cache
217
metadataCache.addField(fTableName + "." + columnName + "."
218             + columnLabel, fields[i - 1]);
219     } // for
220
return fields;
221   }
222
223 }
224
Popular Tags