1 24 25 package com.mckoi.database; 26 27 import com.mckoi.util.BlockIntegerList; 28 import com.mckoi.util.IntegerVector; 29 30 39 40 final class INHelper { 41 42 49 final static IntegerVector origIn(Table table1, Table table2, 50 int column1, int column2) { 51 52 56 Table small_table; 57 Table large_table; 58 int small_column; 59 int large_column; 60 61 if (table1.getRowCount() < table2.getRowCount()) { 62 small_table = table1; 63 large_table = table2; 64 65 small_column = column1; 66 large_column = column2; 67 68 } 69 else { 70 small_table = table2; 71 large_table = table1; 72 73 small_column = column2; 74 large_column = column1; 75 } 76 77 81 IntegerVector result_rows = new IntegerVector(); 82 83 RowEnumeration e = small_table.rowEnumeration(); 84 85 Operator EQUALSOP = Operator.get("="); 86 87 while (e.hasMoreRows()) { 88 89 int small_row_index = e.nextRowIndex(); 90 TObject cell = 91 small_table.getCellContents(small_column, small_row_index); 92 93 IntegerVector selected_set = 94 large_table.selectRows(large_column, EQUALSOP, cell); 95 96 98 if (selected_set.size() > 0) { 99 100 105 if (large_table == table1) { 106 result_rows.append(selected_set); 107 } 108 else { 109 result_rows.addInt(small_row_index); 110 } 111 } 112 113 } 114 115 return result_rows; 116 117 } 118 119 124 final static IntegerVector in(Table table1, Table table2, 125 int column1, int column2) { 126 127 131 Table small_table; 132 Table large_table; 133 int small_column; 134 int large_column; 135 136 if (table1.getRowCount() < table2.getRowCount()) { 137 small_table = table1; 138 large_table = table2; 139 140 small_column = column1; 141 large_column = column2; 142 143 } 144 else { 145 small_table = table2; 146 large_table = table1; 147 148 small_column = column2; 149 large_column = column1; 150 } 151 152 156 BlockIntegerList result_rows = new BlockIntegerList(); 157 158 RowEnumeration e = small_table.rowEnumeration(); 159 160 Operator EQUALSOP = Operator.get("="); 161 162 while (e.hasMoreRows()) { 163 164 int small_row_index = e.nextRowIndex(); 165 TObject cell = 166 small_table.getCellContents(small_column, small_row_index); 167 168 IntegerVector selected_set = 169 large_table.selectRows(large_column, EQUALSOP, cell); 170 171 173 if (selected_set.size() > 0) { 174 175 180 if (large_table == table1) { 181 int sz = selected_set.size(); 183 boolean rs = true; 184 for (int i = 0; rs == true && i < sz; ++i) { 185 rs = result_rows.uniqueInsertSort(selected_set.intAt(i)); 186 } 187 } 188 else { 189 result_rows.add(small_row_index); 191 } 192 } 193 194 } 195 196 return new IntegerVector(result_rows); 197 198 } 199 200 203 final static IntegerVector in(Table table1, Table table2, 204 int[] t1_cols, int[] t2_cols) { 205 if (t1_cols.length > 1) { 206 throw new Error ("Multi-column 'in' not supported."); 207 } 208 return in(table1, table2, t1_cols[0], t2_cols[0]); 209 } 210 211 216 final static IntegerVector notIn(Table table1, Table table2, 217 int col1, int col2) { 218 219 int t2_row_count = table2.getRowCount(); 221 if (t2_row_count == 0) { 222 return table1.selectAll(col1); 224 } 225 else if (t2_row_count == 1) { 226 RowEnumeration e = table2.rowEnumeration(); 228 TObject cell = table2.getCellContents(col2, e.nextRowIndex()); 229 return table1.selectRows(col1, Operator.get("<>"), cell); 230 } 231 232 IntegerVector result_rows = new IntegerVector(); 236 237 RowEnumeration e = table1.rowEnumeration(); 238 239 Operator EQUALSOP = Operator.get("="); 240 241 while (e.hasMoreRows()) { 242 243 int row_index = e.nextRowIndex(); 244 TObject cell = table1.getCellContents(col1, row_index); 245 246 IntegerVector selected_set = 247 table2.selectRows(col2, Operator.get("="), cell); 248 249 252 if (selected_set.size() <= 0) { 253 result_rows.addInt(row_index); 254 } 255 256 } 257 258 return result_rows; 259 } 260 261 264 final static IntegerVector notIn(Table table1, Table table2, 265 int[] t1_cols, int[] t2_cols) { 266 if (t1_cols.length > 1) { 267 throw new Error ("Multi-column 'not in' not supported."); 268 } 269 return notIn(table1, table2, t1_cols[0], t2_cols[0]); 270 } 271 272 273 } 274 | Popular Tags |