@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());
  }
  // Test that primary key constraints are actually removed
  @Test
  public void removingPrimaryKeyRemovesConstraint() {
    SharedGroup db = new SharedGroup(testFile, SharedGroup.Durability.FULL, null);

    WriteTransaction trans = db.beginWrite();
    Table tbl = trans.getTable("EmployeeTable");
    tbl.addColumn(RealmFieldType.STRING, "name");
    tbl.addColumn(RealmFieldType.INTEGER, "number");
    tbl.setPrimaryKey("name");

    tbl.add("Foo", 42);
    try {
      tbl.add("Foo", 41);
    } catch (RealmPrimaryKeyConstraintException e1) {
      // Primary key check worked, now remove it and try again.
      tbl.setPrimaryKey("");
      try {
        tbl.add("Foo", 41);
        return;
      } catch (RealmException e2) {
        fail("Primary key not removed");
      }
    }

    fail("Primary key not enforced.");
  }
  @Test
  public void mustAllowDoubleCommitAndRollback() {
    SharedGroup db = new SharedGroup(testFile, SharedGroup.Durability.FULL, null);
    {
      WriteTransaction trans = db.beginWrite();
      Table tbl = trans.getTable("EmployeeTable");
      tbl.addColumn(RealmFieldType.STRING, "name");
      tbl.addColumn(RealmFieldType.INTEGER, "number");

      // allow commit before any changes
      assertEquals(0, tbl.size());
      tbl.add("Hello", 1);
      trans.commit();
    }
    {
      WriteTransaction trans = db.beginWrite();
      Table tbl = trans.getTable("EmployeeTable");
      // allow double rollback
      tbl.add("Hello", 2);
      assertEquals(2, tbl.size());
      trans.rollback();
      trans.rollback();
      trans.rollback();
      trans.rollback();
    }
    {
      ReadTransaction trans = db.beginRead();
      Table tbl = trans.getTable("EmployeeTable");
      assertEquals(1, tbl.size());
      trans.endRead();
    }
  }
  // Test if toString() returns a correct PrimaryKey field description from a Table
  public void testTableToStringWithPrimaryKey() {
    Table t = getTableWithStringPrimaryKey();
    t.addColumn(RealmFieldType.INTEGER, "intCol");
    t.addColumn(RealmFieldType.BOOLEAN, "boolCol");

    t.add("s1", 1, true);
    t.add("s2", 2, false);

    String expected =
        "The Table has 'colName' field as a PrimaryKey, and contains 3 columns: colName, intCol, boolCol. And 2 rows.";
    assertEquals(expected, t.toString());
  }
 /**
  * Add a table by itself. Add columns later.
  *
  * @param tableName
  */
 public Table addTable(String tableName) {
   Table table = new AbstractTable(tableName);
   tables.add(table);
   tableItems.put(tableName, new TableItem(table, tableName));
   table.addColumn(new Column("_id", Column.COLUMN_TYPE.INTEGER, true));
   return table;
 }
  // Test that primary key constraints are actually removed
  @Test
  public void removingPrimaryKeyRemovesConstraint_typeSetters() {
    SharedGroup db = new SharedGroup(testFile, SharedGroup.Durability.FULL, null);

    WriteTransaction trans = db.beginWrite();
    Table tbl = trans.getTable("EmployeeTable");
    tbl.addColumn(RealmFieldType.STRING, "name");
    tbl.setPrimaryKey("name");

    // Create first entry with name "foo"
    tbl.setString(0, tbl.addEmptyRow(), "Foo");

    long rowIndex = tbl.addEmptyRow();
    try {
      tbl.setString(0, rowIndex, "Foo"); // Try to create 2nd entry with name Foo
    } catch (RealmPrimaryKeyConstraintException e1) {
      tbl.setPrimaryKey(""); // Primary key check worked, now remove it and try again.
      try {
        tbl.setString(0, rowIndex, "Foo");
        return;
      } catch (RealmException e2) {
        fail("Primary key not removed");
      }
    }

    fail("Primary key not enforced.");
  }
 /**
  * Add a table with columns.
  *
  * @param tableName
  * @param columnNames
  * @return DBBuilder
  */
 public DBBuilder addTable(String tableName, List<String> columnNames) {
   Table table = addTable(tableName);
   for (String columnName : columnNames) {
     Column column = new Column(columnName, Column.COLUMN_TYPE.TEXT);
     table.addColumn(column);
   }
   return this;
 }
 private Table getTableWithIntegerPrimaryKey() {
   SharedGroup db = new SharedGroup(testFile, SharedGroup.Durability.FULL, null);
   WriteTransaction trans = db.beginWrite();
   Table t = trans.getTable("TestTable");
   t.addColumn(RealmFieldType.INTEGER, "colName");
   t.setPrimaryKey("colName");
   return t;
 }
  @SuppressWarnings("unchecked")
  @Override
  public Table createMappedForm(PersistentEntity entity) {
    Table table = super.createMappedForm(entity);
    CassandraPersistentEntity cassandraPersistentEntity = (CassandraPersistentEntity) entity;
    // read tableOptions
    ClassPropertyFetcher cpf = ClassPropertyFetcher.forClass(entity.getJavaClass());
    final Closure value = cpf.getStaticPropertyValue(TABLE_PROPERTIES, Closure.class);
    if (value != null) {
      MapConfigurationBuilder builder = new MapConfigurationBuilder();
      try {
        builder.evaluate(value);
      } catch (Exception e) {
        throw new IllegalMappingException(
            String.format("Error reading %s : %s", TABLE_PROPERTIES, e.toString()));
      }
      table.setTableProperties(builder.getProperties());
    }

    if (table.getKeyspace() == null) {
      table.setKeyspace(keyspace);
    }

    // additional static mapping block handling
    Map<String, Column> properties = entityToPropertyMap.get(entity);
    Object version = properties.get(MappingConfigurationBuilder.VERSION_KEY);
    if (version instanceof Boolean) {
      cassandraPersistentEntity.setVersion((Boolean) version);
    }

    Column idProperty = properties.get(IDENTITY_PROPERTY);
    Iterator<Entry<String, Column>> propertyIterator = properties.entrySet().iterator();

    while (propertyIterator.hasNext()) {
      Entry<String, Column> entry = propertyIterator.next();
      if (entry.getValue() instanceof Column) {
        String name = entry.getKey();
        Column column = entry.getValue();
        if (idProperty != null
            && idProperty.getName() != null
            && idProperty.getName().equals(name)) {
          // remove extra column created if id property in constraints block, as it conflicts with
          // the column created in mapping block.
          // constraints will be handled elsewhere in GORM
          propertyIterator.remove();
          continue;
        }

        if (column.getName() == null) {
          column.setName(name);
        }
        table.addColumn(column);
      }
    }

    return table;
  }
  protected void writeOneTransaction(SharedGroup db, long rows) {
    WriteTransaction trans = db.beginWrite();
    Table tbl = trans.getTable("EmployeeTable");
    tbl.addColumn(RealmFieldType.STRING, "name");
    tbl.addColumn(RealmFieldType.INTEGER, "number");

    for (long row = 0; row < rows; row++) tbl.add("Hi", 1);
    assertEquals(rows, tbl.size());
    trans.commit();

    // must throw exception as table is invalid now.
    try {
      assertEquals(1, tbl.size());
      fail();
    } catch (IllegalStateException e) {
      assertNotNull(e);
    }
  }
  @Test
  public void getTablesSubTest() throws Exception {
    Database db = new Database("test");
    Table t1 = new Table("tname1");

    t1.addColumn("A", CharField.class);
    t1.addColumn("B", IntegerField.class);

    Row r1 = t1.getRowSkeleton();
    r1.insert(0, Cell.makeCell("text1"));
    r1.insert(1, Cell.makeCell(11));
    t1.addRow(r1);

    r1.insert(0, Cell.makeCell("text2"));
    r1.insert(1, Cell.makeCell(12));
    t1.addRow(r1);

    r1.insert(0, Cell.makeCell("text3"));
    r1.insert(1, Cell.makeCell(13));
    t1.addRow(r1);

    Table t2 = new Table("tname2");
    t2.addColumn("A", CharField.class);
    t2.addColumn("B", IntegerField.class);
    Row r3 = t2.getRowSkeleton();
    r3.insert(0, Cell.makeCell("text2"));
    r3.insert(1, Cell.makeCell(12));
    t2.addRow(r3);

    Table resTable = db.getTablesSub(t1, t2);

    assertEquals(2, resTable.size());
    assertEquals(2, resTable.getHeader().size());
    assertEquals(CharField.class, resTable.getHeader().getTypeAt(0));
    assertEquals(IntegerField.class, resTable.getHeader().getTypeAt(1));
    assertEquals(2, resTable.size());

    assertEquals("text1", resTable.getStringRow(0).getCel().get(0));
    assertEquals("11", resTable.getStringRow(0).getCel().get(1));

    assertEquals("text3", resTable.getStringRow(1).getCel().get(0));
    assertEquals("13", resTable.getStringRow(1).getCel().get(1));
  }
 /**
  * Add a column to a table
  *
  * @param tableName
  * @param columnName
  */
 public void addTableColumn(String tableName, String columnName) {
   TableItem tableItem = tableItems.get(tableName);
   Table table = null;
   if (tableItem == null) {
     table = addTable(tableName);
   } else {
     table = tableItem.table;
   }
   Column column = new Column(columnName, Column.COLUMN_TYPE.TEXT);
   table.addColumn(column);
 }
Exemple #13
0
  private Table initTable(String name) {
    Table table = new Table(this.charsetName);
    table.setName(name);

    java.util.Iterator iterator = this.fields.iterator();
    while (iterator.hasNext()) {
      DBFField field = (DBFField) iterator.next();
      table.addColumn(field.name);
    }

    return table;
  }
Exemple #14
0
 public static void setColumnsInSchemaTable(
     Table paramTable, HsqlNameManager.HsqlName[] paramArrayOfHsqlName, Type[] paramArrayOfType) {
   for (int i = 0; i < paramArrayOfHsqlName.length; i++) {
     HsqlNameManager.HsqlName localHsqlName = paramArrayOfHsqlName[i];
     localHsqlName =
         paramTable.database.nameManager.newColumnSchemaHsqlName(
             paramTable.getName(), localHsqlName);
     ColumnSchema localColumnSchema =
         new ColumnSchema(localHsqlName, paramArrayOfType[i], true, false, null);
     paramTable.addColumn(localColumnSchema);
   }
   paramTable.setColumnStructures();
 }
  @Test
  public void onlyOneCommit() {
    SharedGroup db = new SharedGroup(testFile, SharedGroup.Durability.FULL, null);
    WriteTransaction trans = db.beginWrite();

    try {
      Table tbl = trans.getTable("EmployeeTable");
      tbl.addColumn(RealmFieldType.STRING, "name");
      trans.commit();
      try {
        trans.commit(); // should throw
        fail();
      } catch (IllegalStateException e) {
        assertNotNull(e);
      }

    } catch (Throwable t) {
      trans.rollback();
    }
  }
  @Test
  public void shouldThrowExceptionAfterClosedReadTransactionWhenWriting() {
    SharedGroup db = new SharedGroup(testFile, SharedGroup.Durability.FULL, null);
    writeOneTransaction(db, 10);
    ReadTransaction rt = db.beginRead();

    try {
      Table tbl = rt.getTable("EmployeeTable");
      rt.endRead();
      try {
        tbl.addColumn(
            RealmFieldType.STRING,
            "newString"); // Should throw exception, as adding a column is not allowed in read
                          // transaction
        fail();
      } catch (IllegalStateException e) {
        // assertNotNull(e);
      }
    } finally {
      rt.endRead();
    }
  }