1 19 20 package org.netbeans.modules.db.test.jdbcstub; 21 22 import java.sql.ResultSet ; 23 import java.util.ArrayList ; 24 import java.util.Arrays ; 25 import java.util.Collections ; 26 import java.util.List ; 27 import org.netbeans.test.stub.api.Stub; 28 29 33 public final class JDBCStubUtil { 34 35 37 private JDBCStubUtil() { 38 } 39 40 public static ResultSet createResultSet(List columns) { 41 return (ResultSet )Stub.create(ResultSet .class, new ResultSetImpl(columns)); 42 } 43 44 public static ResultSet singleColumnResultSet(String columnName, String [] values) { 45 List column = new ArrayList (); 46 column.add(columnName); 47 column.addAll(Arrays.asList(values)); 48 49 List columns = Collections.singletonList(column); 50 return createResultSet(columns); 51 } 52 53 public static ResultSet catalogsResultSet(String [] catalogNames) { 54 return singleColumnResultSet("TABLE_CAT", catalogNames); 55 } 56 57 public static ResultSet schemasResultSet(String [] schemaNames) { 58 return singleColumnResultSet("TABLE_SCHEM", schemaNames); 59 } 60 61 64 public static ResultSet tablesResultSet(String [][] tableNamesBySchema) { 65 List tableSchemCol = new ArrayList (); 66 tableSchemCol.add("TABLE_SCHEM"); 67 List tableNameCol = new ArrayList (); 68 tableNameCol.add("TABLE_NAME"); 69 70 for (int i = 0; i < tableNamesBySchema.length; i++) { 71 String [] 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 columns = new ArrayList (); 81 columns.add(tableSchemCol); 82 columns.add(tableNameCol); 83 return createResultSet(columns); 84 } 85 86 public static ResultSet columnsResultSet(String [] columnNames, String [] typeNames, int[] sqlTypes, int[] columnSizes, int[] decimalDigits, int[] nullables) { 87 List columnNameColumn = writeableSingletonList("COLUMN_NAME"); 88 columnNameColumn.addAll(Arrays.asList(columnNames)); 89 90 List typeNameColumn = writeableSingletonList("TYPE_NAME"); 91 typeNameColumn.addAll(Arrays.asList(typeNames)); 92 93 List sqlTypeColumn = writeableSingletonList("DATA_TYPE"); 94 addAllAsReferenceType(sqlTypeColumn, sqlTypes); 95 96 List columnSizeColumn = writeableSingletonList("COLUMN_SIZE"); 97 addAllAsReferenceType(columnSizeColumn, columnSizes); 98 99 List decimalDigitsColumn = writeableSingletonList("DECIMAL_DIGITS"); 100 addAllAsReferenceType(decimalDigitsColumn, decimalDigits); 101 102 List nullablesColumn = writeableSingletonList("NULLABLE"); 103 addAllAsReferenceType(nullablesColumn, nullables); 104 105 List columns = new ArrayList (); 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 indexesResultSet(String [] indexNames, String [] columnNames, boolean[] nonUniques) { 116 List indexNameColumn = writeableSingletonList("INDEX_NAME"); 117 indexNameColumn.addAll(Arrays.asList(indexNames)); 118 119 List columnNameColumn = writeableSingletonList("COLUMN_NAME"); 120 columnNameColumn.addAll(Arrays.asList(columnNames)); 121 122 List nonUniqueColumn = writeableSingletonList("NON_UNIQUE"); 123 addAllAsReferenceType(nonUniqueColumn, nonUniques); 124 125 List columns = new ArrayList (); 126 columns.add(indexNameColumn); 127 columns.add(columnNameColumn); 128 columns.add(nonUniqueColumn); 129 return createResultSet(columns); 130 } 131 132 public static ResultSet primaryKeysResultSet(String [] pkNames, String [] columnNames, short[] keySeqs) { 133 List pkNameColumn = writeableSingletonList("PK_NAME"); 134 pkNameColumn.addAll(Arrays.asList(pkNames)); 135 136 List columnNameColumn = writeableSingletonList("COLUMN_NAME"); 137 columnNameColumn.addAll(Arrays.asList(columnNames)); 138 139 List keySeqColumn = writeableSingletonList("KEY_SEQ"); 140 addAllAsReferenceType(keySeqColumn, keySeqs); 141 142 List columns = new ArrayList (); 143 columns.add(pkNameColumn); 144 columns.add(columnNameColumn); 145 columns.add(keySeqColumn); 146 return createResultSet(columns); 147 } 148 149 public static ResultSet importedKeysResultSet( 150 String [] fkNames, 151 String [] pkTableCats, String [] pkTableSchemas, String [] pkTableNames, String [] pkColumnNames, 152 String [] fkTableCats, String [] fkTableSchemas, String [] fkTableNames, String [] fkColumnNames) { 153 154 List pkTableCatColumn = writeableSingletonList("PKTABLE_CAT"); 155 pkTableCatColumn.addAll(Arrays.asList(pkTableCats)); 156 157 List pkTableSchemaColumn = writeableSingletonList("PKTABLE_SCHEM"); 158 pkTableSchemaColumn.addAll(Arrays.asList(pkTableSchemas)); 159 160 List pkTableNameColumn = writeableSingletonList("PKTABLE_NAME"); 161 pkTableNameColumn.addAll(Arrays.asList(pkTableNames)); 162 163 List pkColumnNameColumn = writeableSingletonList("PKCOLUMN_NAME"); 164 pkColumnNameColumn.addAll(Arrays.asList(pkColumnNames)); 165 166 List fkTableCatColumn = writeableSingletonList("FKTABLE_CAT"); 167 fkTableCatColumn.addAll(Arrays.asList(fkTableCats)); 168 169 List fkTableSchemaColumn = writeableSingletonList("FKTABLE_SCHEM"); 170 fkTableSchemaColumn.addAll(Arrays.asList(fkTableSchemas)); 171 172 List fkTableNameColumn = writeableSingletonList("FKTABLE_NAME"); 173 fkTableNameColumn.addAll(Arrays.asList(fkTableNames)); 174 175 List fkColumnNameColumn = writeableSingletonList("FKCOLUMN_NAME"); 176 fkColumnNameColumn.addAll(Arrays.asList(fkColumnNames)); 177 178 List fkNameColumn = writeableSingletonList("FK_NAME"); 179 fkNameColumn.addAll(Arrays.asList(fkNames)); 180 181 List columns = new ArrayList (); 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 emptyResultSet() { 195 return createResultSet(Collections.EMPTY_LIST); 196 } 197 198 private static List writeableSingletonList(Object value) { 199 List result = new ArrayList (); 200 result.add(value); 201 return result; 202 } 203 204 private static void addAllAsReferenceType(List list, int[] values) { 205 for (int i = 0; i < values.length; i++) { 206 list.add(new Integer (values[i])); 207 } 208 } 209 210 private static void addAllAsReferenceType(List list, short[] values) { 211 for (int i = 0; i < values.length; i++) { 212 list.add(new Short (values[i])); 213 } 214 } 215 216 private static void addAllAsReferenceType(List 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 |