KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > test > jdbcstub > JDBCStubUtil


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.db.test.jdbcstub;
21
22 import java.sql.ResultSet JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.List JavaDoc;
27 import org.netbeans.test.stub.api.Stub;
28
29 /**
30  *
31  * @author Andrei Badea
32  */

33 public final class JDBCStubUtil {
34     
35     // TODO need methods for returning the resultset columns, not just the resultsets
36

37     private JDBCStubUtil() {
38     }
39     
40     public static ResultSet JavaDoc createResultSet(List JavaDoc/*<List<Object>>*/ columns) {
41         return (ResultSet JavaDoc)Stub.create(ResultSet JavaDoc.class, new ResultSetImpl(columns));
42     }
43     
44     public static ResultSet JavaDoc singleColumnResultSet(String JavaDoc columnName, String JavaDoc[] values) {
45         List JavaDoc column = new ArrayList JavaDoc();
46         column.add(columnName);
47         column.addAll(Arrays.asList(values));
48         
49         List JavaDoc columns = Collections.singletonList(column);
50         return createResultSet(columns);
51     }
52     
53     public static ResultSet JavaDoc catalogsResultSet(String JavaDoc[] catalogNames) {
54         return singleColumnResultSet("TABLE_CAT", catalogNames);
55     }
56     
57     public static ResultSet JavaDoc schemasResultSet(String JavaDoc[] schemaNames) {
58         return singleColumnResultSet("TABLE_SCHEM", schemaNames);
59     }
60     
61     /**
62      * @param tablesBySchema something like { { "schema1", "table1", "table2" }, { "schema2", "table1" }, ... }
63      */

64     public static ResultSet JavaDoc tablesResultSet(String JavaDoc[][] tableNamesBySchema) {
65         List JavaDoc tableSchemCol = new ArrayList JavaDoc();
66         tableSchemCol.add("TABLE_SCHEM");
67         List JavaDoc tableNameCol = new ArrayList JavaDoc();
68         tableNameCol.add("TABLE_NAME");
69         
70         for (int i = 0; i < tableNamesBySchema.length; i++) {
71             String JavaDoc[] tables = tableNamesBySchema[i];
72             assert tables.length > 1;
73             
74             for (int j = 1; j < tables.length; j++) {
75                 tableSchemCol.add(tables[0]);
76                 tableNameCol.add(tables[j]);
77             }
78         }
79         
80         List JavaDoc columns = new ArrayList JavaDoc();
81         columns.add(tableSchemCol);
82         columns.add(tableNameCol);
83         return createResultSet(columns);
84     }
85     
86     public static ResultSet JavaDoc columnsResultSet(String JavaDoc[] columnNames, String JavaDoc[] typeNames, int[] sqlTypes, int[] columnSizes, int[] decimalDigits, int[] nullables) {
87         List JavaDoc columnNameColumn = writeableSingletonList("COLUMN_NAME");
88         columnNameColumn.addAll(Arrays.asList(columnNames));
89         
90         List JavaDoc typeNameColumn = writeableSingletonList("TYPE_NAME");
91         typeNameColumn.addAll(Arrays.asList(typeNames));
92         
93         List JavaDoc sqlTypeColumn = writeableSingletonList("DATA_TYPE");
94         addAllAsReferenceType(sqlTypeColumn, sqlTypes);
95         
96         List JavaDoc columnSizeColumn = writeableSingletonList("COLUMN_SIZE");
97         addAllAsReferenceType(columnSizeColumn, columnSizes);
98         
99         List JavaDoc decimalDigitsColumn = writeableSingletonList("DECIMAL_DIGITS");
100         addAllAsReferenceType(decimalDigitsColumn, decimalDigits);
101         
102         List JavaDoc nullablesColumn = writeableSingletonList("NULLABLE");
103         addAllAsReferenceType(nullablesColumn, nullables);
104         
105         List JavaDoc columns = new ArrayList JavaDoc();
106         columns.add(columnNameColumn);
107         columns.add(typeNameColumn);
108         columns.add(sqlTypeColumn);
109         columns.add(columnSizeColumn);
110         columns.add(decimalDigitsColumn);
111         columns.add(nullablesColumn);
112         return createResultSet(columns);
113     }
114     
115     public static ResultSet JavaDoc indexesResultSet(String JavaDoc[] indexNames, String JavaDoc[] columnNames, boolean[] nonUniques) {
116         List JavaDoc indexNameColumn = writeableSingletonList("INDEX_NAME");
117         indexNameColumn.addAll(Arrays.asList(indexNames));
118         
119         List JavaDoc columnNameColumn = writeableSingletonList("COLUMN_NAME");
120         columnNameColumn.addAll(Arrays.asList(columnNames));
121         
122         List JavaDoc nonUniqueColumn = writeableSingletonList("NON_UNIQUE");
123         addAllAsReferenceType(nonUniqueColumn, nonUniques);
124         
125         List JavaDoc columns = new ArrayList JavaDoc();
126         columns.add(indexNameColumn);
127         columns.add(columnNameColumn);
128         columns.add(nonUniqueColumn);
129         return createResultSet(columns);
130     }
131     
132     public static ResultSet JavaDoc primaryKeysResultSet(String JavaDoc[] pkNames, String JavaDoc[] columnNames, short[] keySeqs) {
133         List JavaDoc pkNameColumn = writeableSingletonList("PK_NAME");
134         pkNameColumn.addAll(Arrays.asList(pkNames));
135         
136         List JavaDoc columnNameColumn = writeableSingletonList("COLUMN_NAME");
137         columnNameColumn.addAll(Arrays.asList(columnNames));
138         
139         List JavaDoc keySeqColumn = writeableSingletonList("KEY_SEQ");
140         addAllAsReferenceType(keySeqColumn, keySeqs);
141         
142         List JavaDoc columns = new ArrayList JavaDoc();
143         columns.add(pkNameColumn);
144         columns.add(columnNameColumn);
145         columns.add(keySeqColumn);
146         return createResultSet(columns);
147     }
148     
149     public static ResultSet JavaDoc importedKeysResultSet(
150             String JavaDoc[] fkNames,
151             String JavaDoc[] pkTableCats, String JavaDoc[] pkTableSchemas, String JavaDoc[] pkTableNames, String JavaDoc[] pkColumnNames,
152             String JavaDoc[] fkTableCats, String JavaDoc[] fkTableSchemas, String JavaDoc[] fkTableNames, String JavaDoc[] fkColumnNames) {
153         
154         List JavaDoc pkTableCatColumn = writeableSingletonList("PKTABLE_CAT");
155         pkTableCatColumn.addAll(Arrays.asList(pkTableCats));
156         
157         List JavaDoc pkTableSchemaColumn = writeableSingletonList("PKTABLE_SCHEM");
158         pkTableSchemaColumn.addAll(Arrays.asList(pkTableSchemas));
159         
160         List JavaDoc pkTableNameColumn = writeableSingletonList("PKTABLE_NAME");
161         pkTableNameColumn.addAll(Arrays.asList(pkTableNames));
162         
163         List JavaDoc pkColumnNameColumn = writeableSingletonList("PKCOLUMN_NAME");
164         pkColumnNameColumn.addAll(Arrays.asList(pkColumnNames));
165         
166         List JavaDoc fkTableCatColumn = writeableSingletonList("FKTABLE_CAT");
167         fkTableCatColumn.addAll(Arrays.asList(fkTableCats));
168         
169         List JavaDoc fkTableSchemaColumn = writeableSingletonList("FKTABLE_SCHEM");
170         fkTableSchemaColumn.addAll(Arrays.asList(fkTableSchemas));
171         
172         List JavaDoc fkTableNameColumn = writeableSingletonList("FKTABLE_NAME");
173         fkTableNameColumn.addAll(Arrays.asList(fkTableNames));
174         
175         List JavaDoc fkColumnNameColumn = writeableSingletonList("FKCOLUMN_NAME");
176         fkColumnNameColumn.addAll(Arrays.asList(fkColumnNames));
177         
178         List JavaDoc fkNameColumn = writeableSingletonList("FK_NAME");
179         fkNameColumn.addAll(Arrays.asList(fkNames));
180         
181         List JavaDoc columns = new ArrayList JavaDoc();
182         columns.add(pkTableCatColumn);
183         columns.add(pkTableSchemaColumn);
184         columns.add(pkTableNameColumn);
185         columns.add(pkColumnNameColumn);
186         columns.add(fkTableCatColumn);
187         columns.add(fkTableSchemaColumn);
188         columns.add(fkTableNameColumn);
189         columns.add(fkColumnNameColumn);
190         columns.add(fkNameColumn);
191         return createResultSet(columns);
192     }
193     
194     public static ResultSet JavaDoc emptyResultSet() {
195         return createResultSet(Collections.EMPTY_LIST);
196     }
197     
198     private static List JavaDoc writeableSingletonList(Object JavaDoc value) {
199         List JavaDoc result = new ArrayList JavaDoc();
200         result.add(value);
201         return result;
202     }
203     
204     private static void addAllAsReferenceType(List JavaDoc list, int[] values) {
205         for (int i = 0; i < values.length; i++) {
206             list.add(new Integer JavaDoc(values[i]));
207         }
208     }
209     
210     private static void addAllAsReferenceType(List JavaDoc list, short[] values) {
211         for (int i = 0; i < values.length; i++) {
212             list.add(new Short JavaDoc(values[i]));
213         }
214     }
215     
216     private static void addAllAsReferenceType(List JavaDoc list, boolean[] values) {
217         for (int i = 0; i < values.length; i++) {
218             list.add(Boolean.valueOf(values[i]));
219         }
220     }
221 }
222
Popular Tags