001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.db.test.jdbcstub;
043:
044: import java.sql.ResultSet;
045: import java.util.ArrayList;
046: import java.util.Arrays;
047: import java.util.Collections;
048: import java.util.List;
049: import org.netbeans.test.stub.api.Stub;
050:
051: /**
052: *
053: * @author Andrei Badea
054: */
055: public final class JDBCStubUtil {
056:
057: // TODO need methods for returning the resultset columns, not just the resultsets
058:
059: private JDBCStubUtil() {
060: }
061:
062: public static ResultSet createResultSet(
063: List/*<List<Object>>*/columns) {
064: return (ResultSet) Stub.create(ResultSet.class,
065: new ResultSetImpl(columns));
066: }
067:
068: public static ResultSet singleColumnResultSet(String columnName,
069: String[] values) {
070: List column = new ArrayList();
071: column.add(columnName);
072: column.addAll(Arrays.asList(values));
073:
074: List columns = Collections.singletonList(column);
075: return createResultSet(columns);
076: }
077:
078: public static ResultSet catalogsResultSet(String[] catalogNames) {
079: return singleColumnResultSet("TABLE_CAT", catalogNames);
080: }
081:
082: public static ResultSet schemasResultSet(String[] schemaNames) {
083: return singleColumnResultSet("TABLE_SCHEM", schemaNames);
084: }
085:
086: /**
087: * @param tablesBySchema something like { { "schema1", "table1", "table2" }, { "schema2", "table1" }, ... }
088: */
089: public static ResultSet tablesResultSet(
090: String[][] tableNamesBySchema) {
091: List tableSchemCol = new ArrayList();
092: tableSchemCol.add("TABLE_SCHEM");
093: List tableNameCol = new ArrayList();
094: tableNameCol.add("TABLE_NAME");
095:
096: for (int i = 0; i < tableNamesBySchema.length; i++) {
097: String[] tables = tableNamesBySchema[i];
098: assert tables.length > 1;
099:
100: for (int j = 1; j < tables.length; j++) {
101: tableSchemCol.add(tables[0]);
102: tableNameCol.add(tables[j]);
103: }
104: }
105:
106: List columns = new ArrayList();
107: columns.add(tableSchemCol);
108: columns.add(tableNameCol);
109: return createResultSet(columns);
110: }
111:
112: public static ResultSet columnsResultSet(String[] columnNames,
113: String[] typeNames, int[] sqlTypes, int[] columnSizes,
114: int[] decimalDigits, int[] nullables) {
115: List columnNameColumn = writeableSingletonList("COLUMN_NAME");
116: columnNameColumn.addAll(Arrays.asList(columnNames));
117:
118: List typeNameColumn = writeableSingletonList("TYPE_NAME");
119: typeNameColumn.addAll(Arrays.asList(typeNames));
120:
121: List sqlTypeColumn = writeableSingletonList("DATA_TYPE");
122: addAllAsReferenceType(sqlTypeColumn, sqlTypes);
123:
124: List columnSizeColumn = writeableSingletonList("COLUMN_SIZE");
125: addAllAsReferenceType(columnSizeColumn, columnSizes);
126:
127: List decimalDigitsColumn = writeableSingletonList("DECIMAL_DIGITS");
128: addAllAsReferenceType(decimalDigitsColumn, decimalDigits);
129:
130: List nullablesColumn = writeableSingletonList("NULLABLE");
131: addAllAsReferenceType(nullablesColumn, nullables);
132:
133: List columns = new ArrayList();
134: columns.add(columnNameColumn);
135: columns.add(typeNameColumn);
136: columns.add(sqlTypeColumn);
137: columns.add(columnSizeColumn);
138: columns.add(decimalDigitsColumn);
139: columns.add(nullablesColumn);
140: return createResultSet(columns);
141: }
142:
143: public static ResultSet indexesResultSet(String[] indexNames,
144: String[] columnNames, boolean[] nonUniques) {
145: List indexNameColumn = writeableSingletonList("INDEX_NAME");
146: indexNameColumn.addAll(Arrays.asList(indexNames));
147:
148: List columnNameColumn = writeableSingletonList("COLUMN_NAME");
149: columnNameColumn.addAll(Arrays.asList(columnNames));
150:
151: List nonUniqueColumn = writeableSingletonList("NON_UNIQUE");
152: addAllAsReferenceType(nonUniqueColumn, nonUniques);
153:
154: List columns = new ArrayList();
155: columns.add(indexNameColumn);
156: columns.add(columnNameColumn);
157: columns.add(nonUniqueColumn);
158: return createResultSet(columns);
159: }
160:
161: public static ResultSet primaryKeysResultSet(String[] pkNames,
162: String[] columnNames, short[] keySeqs) {
163: List pkNameColumn = writeableSingletonList("PK_NAME");
164: pkNameColumn.addAll(Arrays.asList(pkNames));
165:
166: List columnNameColumn = writeableSingletonList("COLUMN_NAME");
167: columnNameColumn.addAll(Arrays.asList(columnNames));
168:
169: List keySeqColumn = writeableSingletonList("KEY_SEQ");
170: addAllAsReferenceType(keySeqColumn, keySeqs);
171:
172: List columns = new ArrayList();
173: columns.add(pkNameColumn);
174: columns.add(columnNameColumn);
175: columns.add(keySeqColumn);
176: return createResultSet(columns);
177: }
178:
179: public static ResultSet importedKeysResultSet(String[] fkNames,
180: String[] pkTableCats, String[] pkTableSchemas,
181: String[] pkTableNames, String[] pkColumnNames,
182: String[] fkTableCats, String[] fkTableSchemas,
183: String[] fkTableNames, String[] fkColumnNames) {
184:
185: List pkTableCatColumn = writeableSingletonList("PKTABLE_CAT");
186: pkTableCatColumn.addAll(Arrays.asList(pkTableCats));
187:
188: List pkTableSchemaColumn = writeableSingletonList("PKTABLE_SCHEM");
189: pkTableSchemaColumn.addAll(Arrays.asList(pkTableSchemas));
190:
191: List pkTableNameColumn = writeableSingletonList("PKTABLE_NAME");
192: pkTableNameColumn.addAll(Arrays.asList(pkTableNames));
193:
194: List pkColumnNameColumn = writeableSingletonList("PKCOLUMN_NAME");
195: pkColumnNameColumn.addAll(Arrays.asList(pkColumnNames));
196:
197: List fkTableCatColumn = writeableSingletonList("FKTABLE_CAT");
198: fkTableCatColumn.addAll(Arrays.asList(fkTableCats));
199:
200: List fkTableSchemaColumn = writeableSingletonList("FKTABLE_SCHEM");
201: fkTableSchemaColumn.addAll(Arrays.asList(fkTableSchemas));
202:
203: List fkTableNameColumn = writeableSingletonList("FKTABLE_NAME");
204: fkTableNameColumn.addAll(Arrays.asList(fkTableNames));
205:
206: List fkColumnNameColumn = writeableSingletonList("FKCOLUMN_NAME");
207: fkColumnNameColumn.addAll(Arrays.asList(fkColumnNames));
208:
209: List fkNameColumn = writeableSingletonList("FK_NAME");
210: fkNameColumn.addAll(Arrays.asList(fkNames));
211:
212: List columns = new ArrayList();
213: columns.add(pkTableCatColumn);
214: columns.add(pkTableSchemaColumn);
215: columns.add(pkTableNameColumn);
216: columns.add(pkColumnNameColumn);
217: columns.add(fkTableCatColumn);
218: columns.add(fkTableSchemaColumn);
219: columns.add(fkTableNameColumn);
220: columns.add(fkColumnNameColumn);
221: columns.add(fkNameColumn);
222: return createResultSet(columns);
223: }
224:
225: public static ResultSet emptyResultSet() {
226: return createResultSet(Collections.EMPTY_LIST);
227: }
228:
229: private static List writeableSingletonList(Object value) {
230: List result = new ArrayList();
231: result.add(value);
232: return result;
233: }
234:
235: private static void addAllAsReferenceType(List list, int[] values) {
236: for (int i = 0; i < values.length; i++) {
237: list.add(new Integer(values[i]));
238: }
239: }
240:
241: private static void addAllAsReferenceType(List list, short[] values) {
242: for (int i = 0; i < values.length; i++) {
243: list.add(new Short(values[i]));
244: }
245: }
246:
247: private static void addAllAsReferenceType(List list,
248: boolean[] values) {
249: for (int i = 0; i < values.length; i++) {
250: list.add(Boolean.valueOf(values[i]));
251: }
252: }
253: }
|