|                                                                                                              1
 24
 25  package com.mckoi.database;
 26
 27  import java.util.ArrayList
  ; 28  import com.mckoi.util.IntegerVector;
 29  import com.mckoi.debug.*;
 30  import com.mckoi.database.global.TypeUtil;
 31
 32
 41
 42  public final class TemporaryTable extends DefaultDataTable {
 43
 44
 47    private DataTableDef table_def;
 48
 49
 53    private ArrayList
  table_storage; 54
 55
 58    public TemporaryTable(Database database,
 59                          String
  name, DataTableColumnDef[] fields) { 60      super(database);
 61
 62      table_storage = new ArrayList
  (); 63
 64      table_def = new DataTableDef();
 65      table_def.setTableName(new TableName(null, name));
 66      for (int i = 0; i < fields.length; ++i) {
 67        table_def.addVirtualColumn(new DataTableColumnDef(fields[i]));
 68      }
 69      table_def.setImmutable();
 70    }
 71
 72
 76    public TemporaryTable(String
  name, Table based_on) { 77      super(based_on.getDatabase());
 78
 79      table_def = new DataTableDef(based_on.getDataTableDef());
 80      table_def.setTableName(new TableName(null, name));
 81      table_def.setImmutable();
 82    }
 83
 84
 87    public TemporaryTable(DefaultDataTable based_on) {
 88      super(based_on.getDatabase());
 89
 90      table_def = new DataTableDef(based_on.getDataTableDef());
 91      table_def.setImmutable();
 92    }
 93
 94
 95
 96
 97
 98
 102   private Variable resolveToVariable(String
  col_name) { 103     Variable partial = Variable.resolve(col_name);
 104     return partial;
 105   }
 107
 108
 111   public void newRow() {
 112     table_storage.add(new TObject[getColumnCount()]);
 113     ++row_count;
 114   }
 115
 116
 119   public void setRowCell(TObject cell, int column, int row) {
 120     TObject[] cells = (TObject[]) table_storage.get(row);
 121     cells[column] = cell;
 122   }
 123
 124
 128   public void setRowCell(TObject cell, String
  col_name) { 129     Variable v = resolveToVariable(col_name);
 130     setRowCell(cell, findFieldName(v), row_count - 1);
 131   }
 132
 133
 137   public void setRowObject(TObject ob, int col_index, int row) {
 138     setRowCell(ob, col_index, row);
 139   }
 140
 141
 145   public void setRowObject(TObject ob, String
  col_name) { 146     Variable v = resolveToVariable(col_name);
 147     setRowObject(ob, findFieldName(v));
 148   }
 149
 150
 154   public void setRowObject(TObject ob, int col_index) {
 155     setRowObject(ob, col_index, row_count - 1);
 156   }
 157
 158
 162   public void setCellFrom(Table table, int src_col, int src_row,
 163                           String
  to_col) { 164     Variable v = resolveToVariable(to_col);
 165     TObject cell = table.getCellContents(src_col, src_row);
 166     setRowCell(cell, findFieldName(v), row_count - 1);
 167   }
 168
 169
 173   public void copyFrom(Table table, int row) {
 174     newRow();
 175
 176     Variable[] vars = new Variable[table.getColumnCount()];
 177     for (int i = 0; i < vars.length; ++i) {
 178       vars[i] = table.getResolvedVariable(i);
 179     }
 180
 181     for (int i = 0; i < getColumnCount(); ++i) {
 182       Variable v = getResolvedVariable(i);
 183       String
  col_name = v.getName(); 184       try {
 185         int tcol_index = -1;
 186         for (int n = 0; n < vars.length || tcol_index == -1; ++n) {
 187           if (vars[n].getName().equals(col_name)) {
 188             tcol_index = n;
 189           }
 190         }
 191         setRowCell(table.getCellContents(tcol_index, row), i, row_count - 1);
 192       }
 193       catch (Exception
  e) { 194         Debug().writeException(e);
 195         throw new Error
  (e.getMessage()); 196       }
 197     }
 198
 199   }
 200
 201
 202
 203
 204
 205
 212   public void setupAllSelectableSchemes() {
 213     blankSelectableSchemes(1);       for (int row_number = 0; row_number < row_count; ++row_number) {
 215       addRowToColumnSchemes(row_number);
 216     }
 217   }
 218
 219
 220
 221   public DataTableDef getDataTableDef() {
 222     return table_def;
 223   }
 224
 225
 230   public TObject getCellContents(int column, int row) {
 231     TObject[] cells = (TObject[]) table_storage.get(row);
 232     TObject cell = cells[column];
 233     if (cell == null) {
 234       throw new Error
  ("NULL cell!  (" + column + ", " + row + ")"); 235     }
 236     return cell;
 237   }
 238
 239
 243   public RowEnumeration rowEnumeration() {
 244     return new SimpleRowEnumeration(row_count);
 245   }
 246
 247
 257   void addDataTableListener(DataTableListener listener) {
 258       }
 260
 261
 267   void removeDataTableListener(DataTableListener listener) {
 268       }
 270
 271
 279   public void lockRoot(int lock_key) {
 280           }
 283
 284
 289   public void unlockRoot(int lock_key) {
 290           }
 293
 294
 298   public boolean hasRootsLocked() {
 299         return true;
 301   }
 302
 303
 304
 306
 309   static final TemporaryTable singleColumnTable(Database database,
 310                                                 String
  col_name, Class  c) { 311     TType ttype = TType.fromClass(c);
 312     DataTableColumnDef col_def = new DataTableColumnDef();
 313     col_def.setName(col_name);
 314     col_def.setFromTType(ttype);
 315     TemporaryTable table = new TemporaryTable(database, "single",
 316                                         new DataTableColumnDef[] { col_def });
 317
 318     return table;
 323   }
 324
 325 }
 326
                                                                                                                                                                                                             |                                                                       
 
 
 
 
 
                                                                                   Popular Tags                                                                                                                                                                                              |