Search in sources :

Example 1 with ForeignKeyField

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

the class Model method getForeignKeyField.

private static final <T extends Model, O extends Model> Field getForeignKeyField(Class<T> target, Class<O> originClass, O origin) throws IllegalArgumentException, IllegalAccessException {
    Field fk = null;
    if (originClass != null && originClass.isInstance(origin)) {
        for (Field field : DatabaseBuilder.getFields(originClass, origin)) {
            Object f = field.get(origin);
            if (f instanceof ForeignKeyField) {
                ForeignKeyField<?> tmp = (ForeignKeyField<?>) f;
                Class<? extends Model> t = tmp.getTarget();
                if (t.equals(target)) {
                    fk = field;
                    break;
                }
            }
        }
        if (fk == null) {
            fk = getForeignKeyField(target, getSuperclass(originClass), origin);
        }
    }
    return fk;
}
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) IntegerField(com.orm.androrm.field.IntegerField) DataField(com.orm.androrm.field.DataField) AndrormField(com.orm.androrm.field.AndrormField) ForeignKeyField(com.orm.androrm.field.ForeignKeyField)

Example 2 with ForeignKeyField

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

the class QueryBuilder method resolveLastField.

private static final <T extends Model> SelectStatement resolveLastField(Object field, Class<T> clazz, Rule rule) {
    SelectStatement select = new SelectStatement();
    if (DatabaseBuilder.isRelationalField(field) && !(field instanceof ForeignKeyField)) {
        Relation<?> r = (Relation<?>) field;
        return getRelationSelection(r, clazz, rule);
    }
    String tableName = DatabaseBuilder.getTableName(clazz);
    Where where = new Where();
    where.setStatement(rule.getStatement());
    select.from(tableName).distinct().select(Model.PK + " AS " + tableName).where(where);
    return select;
}
Also used : SelectStatement(com.orm.androrm.statement.SelectStatement) ForeignKeyField(com.orm.androrm.field.ForeignKeyField) Relation(com.orm.androrm.field.Relation)

Example 3 with ForeignKeyField

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

the class ForeignKeyFieldTest method testReset.

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

Example 4 with ForeignKeyField

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

the class ForeignKeyFieldTest method testIsPersisted.

public void testIsPersisted() {
    ForeignKeyField<Product> fk = new ForeignKeyField<Product>(Product.class);
    Product p = new Product();
    assertFalse(fk.isPersisted());
    fk.set(p);
    assertFalse(fk.isPersisted());
    p.save(getContext());
    assertTrue(fk.isPersisted());
}
Also used : ForeignKeyField(com.orm.androrm.field.ForeignKeyField) Product(com.orm.androrm.impl.Product)

Example 5 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