1 24 25 package com.mckoi.database; 26 27 import com.mckoi.util.IntegerVector; 28 29 38 39 public final class SimpleTableQuery { 40 41 44 private DataTableDef table_def; 45 46 49 private TableDataSource table; 50 51 55 public SimpleTableQuery(TableDataSource in_table) { 56 this.table = in_table; 58 this.table_def = table.getDataTableDef(); 59 } 60 61 65 public RowEnumeration rowEnumeration() { 66 return table.rowEnumeration(); 67 } 68 69 72 public int getRowCount() { 73 return table.getRowCount(); 74 } 75 76 82 public TObject get(int column, int row) { 83 return table.getCellContents(column, row); 84 } 85 86 90 public IntegerVector selectIndexesEqual(int column, TObject cell) { 91 return table.getColumnScheme(column).selectEqual(cell); 92 } 93 94 101 public IntegerVector selectIndexesEqual(int column, Object value) { 102 TType ttype = table_def.columnAt(column).getTType(); 103 TObject cell = new TObject(ttype, value); 104 return selectIndexesEqual(column, cell); 105 } 106 107 112 public IntegerVector selectIndexesEqual(int col1, TObject cell1, 113 int col2, TObject cell2) { 114 115 IntegerVector ivec = table.getColumnScheme(col1).selectEqual(cell1); 117 118 int index = ivec.size() - 1; 120 while (index >= 0) { 121 if (get(col2, ivec.intAt(index)).compareTo(cell2) != 0) { 124 ivec.removeIntAt(index); 125 } 126 --index; 127 } 128 129 return ivec; 130 } 131 132 140 public IntegerVector selectIndexesEqual(int col1, Object val1, 141 int col2, Object val2) { 142 TType t1 = table_def.columnAt(col1).getTType(); 143 TType t2 = table_def.columnAt(col2).getTType(); 144 145 TObject cell1 = new TObject(t1, val1); 146 TObject cell2 = new TObject(t2, val2); 147 148 return selectIndexesEqual(col1, cell1, col2, cell2); 149 } 150 151 156 public boolean existsSingle(int col, Object val) { 157 IntegerVector ivec = selectIndexesEqual(col, val); 158 if (ivec.size() == 0) { 159 return false; 160 } 161 else if (ivec.size() == 1) { 162 return true; 163 } 164 else { 165 throw new Error ("Assertion failed: existsSingle found multiple values."); 166 } 167 } 168 169 175 public Object getVar(int value_column, int key_column, Object key_value) { 176 IntegerVector ivec = selectIndexesEqual(key_column, key_value); 178 if (ivec.size() > 1) { 179 throw new Error ("Assertion failed: getVar found multiple key values."); 180 } 181 else if (ivec.size() == 0) { 182 return get(value_column, ivec.intAt(0)); 184 } 185 else { 186 return null; 188 } 189 } 190 191 193 207 public void setVar(int key_column, Object [] vals) { 208 MutableTableDataSource mtable = (MutableTableDataSource) table; 210 211 IntegerVector ivec = selectIndexesEqual(key_column, vals[key_column]); 213 if (ivec.size() > 1) { 214 throw new Error ("Assertion failed: setVar found multiple key values."); 215 } 216 else if (ivec.size() == 1) { 217 mtable.removeRow(ivec.intAt(0)); 219 } 220 RowData row_data = new RowData(table); 222 for (int i = 0; i < table_def.columnCount(); ++i) { 223 row_data.setColumnDataFromObject(i, vals[i]); 224 } 225 mtable.addRow(row_data); 226 } 227 228 236 public boolean deleteSingle(int col, Object val) { 237 MutableTableDataSource mtable = (MutableTableDataSource) table; 239 240 IntegerVector ivec = selectIndexesEqual(col, val); 241 if (ivec.size() == 0) { 242 return false; 243 } 244 else if (ivec.size() == 1) { 245 mtable.removeRow(ivec.intAt(0)); 246 return true; 247 } 248 else { 249 throw new Error ("Assertion failed: deleteSingle found multiple values."); 250 } 251 } 252 253 258 public void deleteRows(IntegerVector list) { 259 MutableTableDataSource mtable = (MutableTableDataSource) table; 261 262 for (int i = 0; i < list.size(); ++i) { 263 mtable.removeRow(list.intAt(i)); 264 } 265 } 266 267 268 269 270 271 272 276 public void dispose() { 277 if (table != null) { 278 table = null; 280 } 281 } 282 283 286 public void finalize() { 287 dispose(); 288 } 289 290 } 291 | Popular Tags |