KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > access > jdbc > RowDescriptor


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. 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,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.access.jdbc;
21
22 import java.sql.ResultSet JavaDoc;
23 import java.sql.ResultSetMetaData JavaDoc;
24 import java.sql.SQLException JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import org.apache.cayenne.CayenneRuntimeException;
28 import org.apache.cayenne.access.types.ExtendedType;
29 import org.apache.cayenne.access.types.ExtendedTypeMap;
30
31 /**
32  * A descriptor of a result row obtained from a database.
33  *
34  * @since 1.2
35  * @author Andrus Adamchik
36  */

37 // replaces 1.1 ResultDescriptor
38
public class RowDescriptor {
39
40     protected ColumnDescriptor[] columns;
41     protected ExtendedType[] converters;
42
43     /**
44      * Creates an empty RowDescriptor. Intended mainly for testing and use by subclasses.
45      */

46     protected RowDescriptor() {
47
48     }
49
50     /**
51      * Creates a RowDescriptor for an array of columns.
52      */

53     public RowDescriptor(ColumnDescriptor[] columns, ExtendedTypeMap types) {
54         this.columns = columns;
55         indexTypes(types);
56     }
57
58     /**
59      * Creates new RowDescriptor using ResultSet metadata to determine the columns.
60      */

61     public RowDescriptor(ResultSet JavaDoc resultSet, ExtendedTypeMap types) {
62         this(resultSet, types, null);
63     }
64
65     /**
66      * Creates new RowDescriptor using ResultSet metadata to determine the columns. Note
67      * that if javaTypeOverrides array is null, default JDBC to Java types mapping is
68      * used.
69      */

70     public RowDescriptor(ResultSet JavaDoc resultSet, ExtendedTypeMap types, Map JavaDoc javaTypeOverrides) {
71
72         initFromResultSet(resultSet);
73
74         if (javaTypeOverrides != null) {
75             overrideJavaTypes(javaTypeOverrides);
76         }
77
78         indexTypes(types);
79     }
80     
81     /**
82      * Converts result column labels to uppercase using the default Locale.
83      *
84      * @since 3.0
85      */

86     public void forceUpperCaseColumnNames() {
87         for(int i = 0; i < columns.length; i++) {
88             columns[i].setLabel(columns[i].getLabel().toUpperCase());
89         }
90     }
91     
92     /**
93      * Converts result column labels to lowercase using the default Locale.
94      *
95      * @since 3.0
96      */

97     public void forceLowerCaseColumnNames() {
98         for(int i = 0; i < columns.length; i++) {
99             columns[i].setLabel(columns[i].getLabel().toLowerCase());
100         }
101     }
102
103     /**
104      * Initializes converters for columns.
105      */

106     protected void indexTypes(ExtendedTypeMap types) {
107         this.converters = new ExtendedType[columns.length];
108         for (int i = 0; i < columns.length; i++) {
109             converters[i] = types.getRegisteredType(columns[i].getJavaClass());
110         }
111     }
112
113     /**
114      * Builds columns list from ResultSet metadata.
115      */

116     protected void initFromResultSet(ResultSet JavaDoc resultSet) {
117         try {
118             ResultSetMetaData JavaDoc md = resultSet.getMetaData();
119             int len = md.getColumnCount();
120             if (len == 0) {
121                 throw new CayenneRuntimeException("No columns in ResultSet.");
122             }
123
124             this.columns = new ColumnDescriptor[len];
125
126             for (int i = 0; i < len; i++) {
127                 columns[i] = new ColumnDescriptor(md, i + 1);
128             }
129         }
130         catch (SQLException JavaDoc sqex) {
131             throw new CayenneRuntimeException("Error reading metadata.", sqex);
132         }
133     }
134
135     /**
136      * Overrides Java types of result columns. Keys in the map must correspond to the
137      * names of the columns.
138      */

139     protected void overrideJavaTypes(Map JavaDoc overrides) {
140
141         for (int i = 0; i < columns.length; i++) {
142             String JavaDoc type = (String JavaDoc) overrides.get(columns[i].getName());
143
144             if (type != null) {
145                 columns[i].setJavaClass(type);
146             }
147         }
148     }
149
150     /**
151      * Returns a number of columns in a row.
152      */

153     public int getWidth() {
154         return columns.length;
155     }
156
157     /**
158      * Returns column descriptors.
159      */

160     public ColumnDescriptor[] getColumns() {
161         return columns;
162     }
163
164     /**
165      * Returns extended types for columns.
166      */

167     public ExtendedType[] getConverters() {
168         return converters;
169     }
170 }
171
Popular Tags