KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > in > co > daffodil > db > jdbc > DaffodilDBArray


1 package in.co.daffodil.db.jdbc;
2
3 import java.sql.*;
4 import java.util.*;
5
6 /**
7  * The mapping in the Java programming language for the SQL type
8  * <code>ARRAY</code>.
9  * By default, an <code>Array</code> value is a transaction-duration
10  * reference to an SQL <code>ARRAY</code> value. By default, an <code>Array</code>
11  * object is implemented using an SQL LOCATOR(array) internally, which
12  * means that an <code>Array</code> object contains a logical pointer
13  * to the data in the SQL <code>ARRAY</code> value rather
14  * than containing the <code>ARRAY</code> value's data.
15  * <p>
16  * The <code>Array</code> interface provides methods for bringing an SQL
17  * <code>ARRAY</code> value's data to the client as either an array or a
18  * <code>ResultSet</code> object.
19  * If the elements of the SQL <code>ARRAY</code>
20  * are a UDT, they may be custom mapped. To create a custom mapping,
21  * a programmer must do two things:
22  * <ul>
23  * <li>create a class that implements the {@link SQLData}
24  * interface for the UDT to be custom mapped.
25  * <li>make an entry in a type map that contains
26  * <ul>
27  * <li>the fully-qualified SQL type name of the UDT
28  * <li>the <code>Class</code> object for the class implementing
29  * <code>SQLData</code>
30  * </ul>
31  * </ul>
32  * <p>
33  * When a type map with an entry for
34  * the base type is supplied to the methods <code>getArray</code>
35  * and <code>getResultSet</code>, the mapping
36  * it contains will be used to map the elements of the <code>ARRAY</code> value.
37  * If no type map is supplied, which would typically be the case,
38  * the connection's type map is used by default.
39  * If the connection's type map or a type map supplied to a method has no entry
40  * for the base type, the elements are mapped according to the standard mapping.
41  * <p>
42  * @since 1.2
43  */

44
45 public class DaffodilDBArray implements Array{
46     String JavaDoc dataBaseTypeName;
47     HashMap sqlJavaMap;
48     Object JavaDoc values;
49     SQLInput sqlInput;
50
51     public DaffodilDBArray(Object JavaDoc values,String JavaDoc dataBaseTypeName,HashMap sqlJavaMap){
52         this.values = values;
53         this.dataBaseTypeName = dataBaseTypeName;
54         this.sqlJavaMap = sqlJavaMap;
55     }
56
57
58   /**
59    * Retrieves the SQL type name of the elements in
60    * the array designated by this <code>Array</code> object.
61    * If the elements are a built-in type, it returns
62    * the database-specific type name of the elements.
63    * If the elements are a user-defined type (UDT),
64    * this method returns the fully-qualified SQL type name.
65    *
66    * @return a <code>String</code> that is the database-specific
67    * name for a built-in base type; or the fully-qualified SQL type
68    * name for a base type that is a UDT
69    * @exception SQLException if an error occurs while attempting
70    * to access the type name
71    * @since 1.2
72    */

73     public String JavaDoc getBaseTypeName() throws SQLException{
74         return dataBaseTypeName;
75     }
76
77   /**
78    * Retrieves the JDBC type of the elements in the array designated
79    * by this <code>Array</code> object.
80    *
81    * @return a constant from the class {@link java.sql.Types} that is
82    * the type code for the elements in the array designated by this
83    * <code>Array</code> object
84    * @exception SQLException if an error occurs while attempting
85    * to access the base type
86    * @since 1.2
87    */

88     public int getBaseType() throws SQLException{
89         return 0;
90     }
91
92   /**
93    * Retrieves the contents of the SQL <code>ARRAY</code> value designated
94    * by this
95    * <code>Array</code> object in the form of an array in the Java
96    * programming language. This version of the method <code>getArray</code>
97    * uses the type map associated with the connection for customizations of
98    * the type mappings.
99    *
100    * @return an array in the Java programming language that contains
101    * the ordered elements of the SQL <code>ARRAY</code> value
102    * designated by this <code>Array</code> object
103    * @exception SQLException if an error occurs while attempting to
104    * access the array
105    * @since 1.2
106    */

107     public Object JavaDoc getArray() throws SQLException{
108         if(values == null)
109             return null;
110         return getJavaTypeArray(1,((byte[])values).length,sqlJavaMap);
111     }
112
113   /**
114    * Retrieves the contents of the SQL <code>ARRAY</code> value designated by this
115    * <code>Array</code> object.
116    * This method uses
117    * the specified <code>map</code> for type map customizations
118    * unless the base type of the array does not match a user-defined
119    * type in <code>map</code>, in which case it
120    * uses the standard mapping. This version of the method
121    * <code>getArray</code> uses either the given type map or the standard mapping;
122    * it never uses the type map associated with the connection.
123    *
124    * @param map a <code>java.util.Map</code> object that contains mappings
125    * of SQL type names to classes in the Java programming language
126    * @return an array in the Java programming language that contains the ordered
127    * elements of the SQL array designated by this object
128    * @exception SQLException if an error occurs while attempting to
129    * access the array
130    * @since 1.2
131    */

132     public Object JavaDoc getArray(java.util.Map JavaDoc map) throws SQLException{
133         if(values == null)
134             return null;
135         return getJavaTypeArray(1,((byte[])values).length,map);
136     }
137
138   /**
139    * Retrieves a slice of the SQL <code>ARRAY</code>
140    * value designated by this <code>Array</code> object, beginning with the
141    * specified <code>index</code> and containing up to <code>count</code>
142    * successive elements of the SQL array. This method uses the type map
143    * associated with the connection for customizations of the type mappings.
144    *
145    * @param index the array index of the first element to retrieve;
146    * the first element is at index 1
147    * @param count the number of successive SQL array elements to retrieve
148    * @return an array containing up to <code>count</code> consecutive elements
149    * of the SQL array, beginning with element <code>index</code>
150    * @exception SQLException if an error occurs while attempting to
151    * access the array
152    * @since 1.2
153    */

154     public Object JavaDoc getArray(long index, int count) throws SQLException{
155         return getJavaTypeArray(index,count,sqlJavaMap);
156     }
157
158   /**
159    * Retreives a slice of the SQL <code>ARRAY</code> value
160    * designated by this <code>Array</code> object, beginning with the specified
161    * <code>index</code> and containing up to <code>count</code>
162    * successive elements of the SQL array.
163    * <P>
164    * This method uses
165    * the specified <code>map</code> for type map customizations
166    * unless the base type of the array does not match a user-defined
167    * type in <code>map</code>, in which case it
168    * uses the standard mapping. This version of the method
169    * <code>getArray</code> uses either the given type map or the standard mapping;
170    * it never uses the type map associated with the connection.
171    *
172    * @param index the array index of the first element to retrieve;
173    * the first element is at index 1
174    * @param count the number of successive SQL array elements to
175    * retrieve
176    * @param map a <code>java.util.Map</code> object
177    * that contains SQL type names and the classes in
178    * the Java programming language to which they are mapped
179    * @return an array containing up to <code>count</code>
180    * consecutive elements of the SQL <code>ARRAY</code> value designated by this
181    * <code>Array</code> object, beginning with element
182    * <code>index</code>
183    * @exception SQLException if an error occurs while attempting to
184    * access the array
185    * @since 1.2
186    */

187     public Object JavaDoc getArray(long index, int count, java.util.Map JavaDoc map)throws SQLException{
188         return getJavaTypeArray(index,count,map);
189     }
190
191   /**
192    * Retrieves a result set that contains the elements of the SQL
193    * <code>ARRAY</code> value
194    * designated by this <code>Array</code> object. If appropriate,
195    * the elements of the array are mapped using the connection's type
196    * map; otherwise, the standard mapping is used.
197    * <p>
198    * The result set contains one row for each array element, with
199    * two columns in each row. The second column stores the element
200    * value; the first column stores the index into the array for
201    * that element (with the first array element being at index 1).
202    * The rows are in ascending order corresponding to
203    * the order of the indices.
204    *
205    * @return a {@link ResultSet} object containing one row for each
206    * of the elements in the array designated by this <code>Array</code>
207    * object, with the rows in ascending order based on the indices.
208    * @exception SQLException if an error occurs while attempting to
209    * access the array
210    * @since 1.2
211    */

212     public ResultSet getResultSet () throws SQLException{
213         if(values == null)
214             return null;
215         return getResultSet(1,((byte[])values).length);
216     }
217
218   /**
219    * Retrieves a result set that contains the elements of the SQL
220    * <code>ARRAY</code> value designated by this <code>Array</code> object.
221    * This method uses
222    * the specified <code>map</code> for type map customizations
223    * unless the base type of the array does not match a user-defined
224    * type in <code>map</code>, in which case it
225    * uses the standard mapping. This version of the method
226    * <code>getResultSet</code> uses either the given type map or the standard mapping;
227    * it never uses the type map associated with the connection.
228    * <p>
229    * The result set contains one row for each array element, with
230    * two columns in each row. The second column stores the element
231    * value; the first column stores the index into the array for
232    * that element (with the first array element being at index 1).
233    * The rows are in ascending order corresponding to
234    * the order of the indices.
235    *
236    * @param map contains the mapping of SQL user-defined types to
237    * classes in the Java programming language
238    * @return a <code>ResultSet</code> object containing one row for each
239    * of the elements in the array designated by this <code>Array</code>
240    * object, with the rows in ascending order based on the indices.
241    * @exception SQLException if an error occurs while attempting to
242    * access the array
243    * @since 1.2
244    */

245     public ResultSet getResultSet (java.util.Map JavaDoc map) throws SQLException{
246         if(values == null)
247             return null;
248         return getResultSet(1,((byte[])values).length,map);
249     }
250
251   /**
252    * Retrieves a result set holding the elements of the subarray that
253    * starts at index <code>index</code> and contains up to
254    * <code>count</code> successive elements. This method uses
255    * the connection's type map to map the elements of the array if
256    * the map contains an entry for the base type. Otherwise, the
257    * standard mapping is used.
258    * <P>
259    * The result set has one row for each element of the SQL array
260    * designated by this object, with the first row containing the
261    * element at index <code>index</code>. The result set has
262    * up to <code>count</code> rows in ascending order based on the
263    * indices. Each row has two columns: The second column stores
264    * the element value; the first column stores the index into the
265    * array for that element.
266    *
267    * @param index the array index of the first element to retrieve;
268    * the first element is at index 1
269    * @param count the number of successive SQL array elements to retrieve
270    * @return a <code>ResultSet</code> object containing up to
271    * <code>count</code> consecutive elements of the SQL array
272    * designated by this <code>Array</code> object, starting at
273    * index <code>index</code>.
274    * @exception SQLException if an error occurs while attempting to
275    * access the array
276    * @since 1.2
277    */

278     public ResultSet getResultSet(long index, int count) throws SQLException{
279         if(values == null)
280             return null;
281         return getResultSet(index,count,sqlJavaMap);
282     }
283
284   /**
285    * Retrieves a result set holding the elements of the subarray that
286    * starts at index <code>index</code> and contains up to
287    * <code>count</code> successive elements.
288    * This method uses
289    * the specified <code>map</code> for type map customizations
290    * unless the base type of the array does not match a user-defined
291    * type in <code>map</code>, in which case it
292    * uses the standard mapping. This version of the method
293    * <code>getResultSet</code> uses either the given type map or the standard mapping;
294    * it never uses the type map associated with the connection.
295    * <P>
296    * The result set has one row for each element of the SQL array
297    * designated by this object, with the first row containing the
298    * element at index <code>index</code>. The result set has
299    * up to <code>count</code> rows in ascending order based on the
300    * indices. Each row has two columns: The second column stores
301    * the element value; the first column stroes the index into the
302    * array for that element.
303    *
304    * @param index the array index of the first element to retrieve;
305    * the first element is at index 1
306    * @param count the number of successive SQL array elements to retrieve
307    * @param map the <code>Map</code> object that contains the mapping
308    * of SQL type names to classes in the Java(tm) programming language
309    * @return a <code>ResultSet</code> object containing up to
310    * <code>count</code> consecutive elements of the SQL array
311    * designated by this <code>Array</code> object, starting at
312    * index <code>index</code>.
313    * @exception SQLException if an error occurs while attempting to
314    * access the array
315    * @since 1.2
316    */

317     public ResultSet getResultSet (long index, int count, java.util.Map JavaDoc map)throws SQLException{
318         try{
319             Object JavaDoc[] array = getJavaTypeArray(index,count,map);
320             if(array == null)
321                 return null;
322             ResultSet resultSet = null;
323             for(int i =0 ; i< array.length;i++){
324             }
325             return resultSet;
326         } catch (Exception JavaDoc e){
327             e.printStackTrace();
328             return null;
329         }
330     }
331
332 /**
333  * @todo
334  * This method should throw Exception instead of catch Exception
335 */

336     private Object JavaDoc[] getJavaTypeArray(long start,int count,Map map){
337         try{
338             if(values == null)
339                 return null;
340             Object JavaDoc [] array = new Object JavaDoc[count];
341             long i = start -1;
342             int j = 0;
343             for(i = start-1,j =0 ;j<count;i++,j++){
344                 array[j] = convertToJavaObject(sqlInput,dataBaseTypeName,map);
345             }
346             return array;
347         }catch (Exception JavaDoc e){
348             e.printStackTrace();
349         }
350         return null;
351     }
352
353 /**
354  * @todo
355  * check the body of the function, Map not used why ???
356  */

357     public Object JavaDoc convertToJavaObject(SQLInput stream,String JavaDoc typeName,Map map){
358         try{
359             Object JavaDoc element = ((Class JavaDoc)(sqlJavaMap.get(typeName))).newInstance();
360             SQLData sqlData = (SQLData)(element);
361             sqlData.readSQL(sqlInput,dataBaseTypeName);
362             return element;
363         } catch (Exception JavaDoc e){
364             e.printStackTrace();
365             return null;
366         }
367     }
368 }
369
Popular Tags