Search in sources :

Example 6 with ForeignKeyField

use of com.orm.androrm.field.ForeignKeyField in project androrm by androrm.

the class TableDefinitionTest method testForeignKeyField.

public void testForeignKeyField() {
    TableDefinition def = new TableDefinition("foo");
    ForeignKeyField<Product> fk = new ForeignKeyField<Product>(Product.class);
    def.addField("product", fk);
    assertEquals("CREATE TABLE IF NOT EXISTS `foo` (" + "`product` integer," + "FOREIGN KEY (`product`) " + "REFERENCES `product` (`mId`) " + "ON DELETE CASCADE);", def.toString());
}
Also used : ForeignKeyField(com.orm.androrm.field.ForeignKeyField) TableDefinition(com.orm.androrm.TableDefinition) Product(com.orm.androrm.impl.Product)

Example 7 with ForeignKeyField

use of com.orm.androrm.field.ForeignKeyField in project androrm by androrm.

the class ForeignKeyFieldTest method testSetAndGet.

public void testSetAndGet() {
    Product p = new Product();
    p.setName("test product");
    ForeignKeyField<Product> fk = new ForeignKeyField<Product>(Product.class);
    fk.set(p);
    assertEquals(p.getName(), fk.get(getContext()).getName());
    p.save(getContext());
    fk = new ForeignKeyField<Product>(Product.class);
    fk.set(p.getId());
    assertEquals(p.getName(), fk.get(getContext()).getName());
}
Also used : ForeignKeyField(com.orm.androrm.field.ForeignKeyField) Product(com.orm.androrm.impl.Product)

Example 8 with ForeignKeyField

use of com.orm.androrm.field.ForeignKeyField in project androrm by androrm.

the class DatabaseBuilder method getRelationDefinitions.

@SuppressWarnings("unchecked")
private static final <T extends Model> void getRelationDefinitions(T instance, Class<T> clazz, List<TableDefinition> definitions) {
    if (clazz != null && clazz.isInstance(instance)) {
        for (Field field : getFields(clazz, instance)) {
            try {
                Object o = field.get(instance);
                if (o instanceof ManyToManyField) {
                    ManyToManyField<T, ?> m = (ManyToManyField<T, ?>) o;
                    String leftHand = getTableName(clazz);
                    String rightHand = getTableName(m.getTarget());
                    TableDefinition definition = new TableDefinition(m.getRelationTableName());
                    ForeignKeyField<T> leftLink = m.getLeftLinkDescriptor();
                    ForeignKeyField<?> rightLink = m.getRightHandDescriptor();
                    definition.addField(leftHand, leftLink);
                    definition.addField(rightHand, rightLink);
                    definitions.add(definition);
                }
            } catch (IllegalAccessException e) {
                Log.e(TAG, "could not gather relation definitions for class " + clazz.getSimpleName(), e);
            }
        }
        getRelationDefinitions(instance, Model.getSuperclass(clazz), definitions);
    }
}
Also used : ManyToManyField(com.orm.androrm.field.ManyToManyField) OneToManyField(com.orm.androrm.field.OneToManyField) ForeignKeyField(com.orm.androrm.field.ForeignKeyField) Field(java.lang.reflect.Field) DataField(com.orm.androrm.field.DataField) ManyToManyField(com.orm.androrm.field.ManyToManyField)

Aggregations

ForeignKeyField (com.orm.androrm.field.ForeignKeyField)8 Product (com.orm.androrm.impl.Product)5 TableDefinition (com.orm.androrm.TableDefinition)2 DataField (com.orm.androrm.field.DataField)2 ManyToManyField (com.orm.androrm.field.ManyToManyField)2 OneToManyField (com.orm.androrm.field.OneToManyField)2 Field (java.lang.reflect.Field)2 AndrormField (com.orm.androrm.field.AndrormField)1 IntegerField (com.orm.androrm.field.IntegerField)1 Relation (com.orm.androrm.field.Relation)1 SelectStatement (com.orm.androrm.statement.SelectStatement)1