@Test
  public void testLoadSave() throws IOException {
    Database db = new Database("test", "test.dbl");
    Table t1 = new Table("tname");

    t1.addColumn("A", CharField.class);
    t1.addColumn("B", DateField.class);
    Row r1 = t1.getRowSkeleton();

    Calendar calendar = Calendar.getInstance();
    calendar.set(70, 0, 0);
    Date date = calendar.getTime();

    r1.insert(1, Cell.makeCell(date));
    r1.insert(0, Cell.makeCell("text0"));

    t1.addRow(r1);
    db.addTable(t1);

    db.saveToStorage();
    db = new Database("test", "test.dbl");
    db.loadFromStorage();

    db.loadFromStorage();
    System.out.println(db);
    System.out.println(t1);

    assertTrue(db.getTable("tname").getHeader().equalsModel(t1.getHeader()));
    assertTrue(db.getTable("tname").row(0).at(0).getStringData().equals("text0"));
    assertEquals(
        db.getTable("tname").row(0).at(1).getStringData(), Cell.makeCell(date).getStringData());
    assertEquals(1, t1.size());
  }
 /* (non-Javadoc)
  * @see org.rimudb.generic.binders.IIterativeResultSetBinder#initialize(org.rimudb.Database)
  */
 public void initialize(Database database) throws RimuDBException {
   tableList = new ArrayList<Table>();
   for (int i = 0; i < dataObjectClasses.length; i++) {
     tableList.add(database.getTable(dataObjectClasses[i]));
   }
 }
示例#3
0
  public DatabaseDiff(final Database fromDatabase, final Database toDatabase) {
    this.fromDatabase = fromDatabase;
    this.toDatabase = toDatabase;

    final List<Table> addedTables = Lists.newArrayList();
    final List<Table> identicalTables = Lists.newArrayList();
    final List<Table> modifiedTables = Lists.newArrayList();
    final List<Table> removedTables = Lists.newArrayList();

    for (final String tableName : Sets.newTreeSet(fromDatabase.getTableNames())) {
      final Table fromTable = fromDatabase.getTable(tableName);
      final Table toTable = toDatabase.getTable(tableName);

      if (fromTable.isEmpty() && toTable.isEmpty()) continue;

      final Rows identicalRows = new Rows();
      final Rows modifiedRows = new Rows();
      final Rows removedRows = new Rows();
      final Rows addedRows = toDatabase.find(tableName);

      final Rows fromRows = fromDatabase.find(tableName);

      for (final Row fromRow : fromRows) {
        // Search by tables pkey values
        final Rows toRows = toTable.find(fromRow);
        if (toRows.isEmpty()) {
          // Deleted
          removedRows.add(fromRow);
          continue;
        }

        // [EB]: this only happens when a table fails to specify a primary key column
        if (toRows.size() > 1) {
          throw new IllegalStateException("This cannot happen unless the schema is broken :(");
        }

        // Now look for the *exact* match in the 'to' Rows
        final Rows record = toRows.find(fromRow);
        if (!record.isEmpty()) {
          // Identical
          addedRows.remove(record.get(0));
          identicalRows.add(record.get(0));
        } else {
          // Changed
          final Row updatedRow = toTable.get(toRows.get(0));
          final Row row = toTable.rowDiff(fromRow, updatedRow);

          addedRows.remove(updatedRow);
          modifiedRows.add(row);
        }
      }

      if (!addedRows.isEmpty()) addedTables.add(new Table(fromTable, addedRows));
      if (!modifiedRows.isEmpty()) modifiedTables.add(new Table(fromTable, modifiedRows));
      if (!removedRows.isEmpty()) removedTables.add(new Table(fromTable, removedRows));
      if (!identicalRows.isEmpty()) identicalTables.add(new Table(fromTable, identicalRows));
    }

    added = new Database(addedTables);
    modified = new Database(modifiedTables);
    removed = new Database(removedTables);
    identical = new Database(identicalTables);
  }