Search in sources :

Example 1 with Molecule

use of org.nutz.trans.Molecule in project nutz by nutzam.

the class NutDao method query.

public <T> List<T> query(final Class<T> classOfT, final Condition cnd, final Pager pager, String regex) {
    if (regex == null)
        return query(classOfT, cnd, pager);
    FieldFilter ff = FieldFilter.create(classOfT, FieldMatcher.make(regex, null, false));
    Molecule<List<T>> m = new Molecule<List<T>>() {

        public void run() {
            setObj(query(classOfT, cnd, pager));
        }
    };
    return ff.run(m);
}
Also used : FieldFilter(org.nutz.dao.FieldFilter) Molecule(org.nutz.trans.Molecule) List(java.util.List) ArrayList(java.util.ArrayList)

Example 2 with Molecule

use of org.nutz.trans.Molecule in project nutz by nutzam.

the class NutDao method insert.

public <T> T insert(final T t, boolean ignoreNull, boolean ignoreZero, boolean ignoreBlankStr) {
    Object obj = Lang.first(t);
    Entity<?> en = getEntity(obj.getClass());
    List<String> names = new ArrayList<String>();
    for (MappingField mf : en.getMappingFields()) {
        Object tmp = mf.getValue(obj);
        if (ignoreNull && tmp == null) {
            continue;
        }
        if (ignoreZero && (tmp == null || (tmp instanceof Number && ((Number) tmp).intValue() == 0))) {
            continue;
        }
        if (ignoreBlankStr && (tmp instanceof CharSequence && Strings.isBlank((CharSequence) tmp)))
            continue;
        names.add(mf.getName());
    }
    FieldFilter ff = FieldFilter.create(obj.getClass(), "^(" + Strings.join("|", names.toArray()) + ")$");
    Molecule<T> m = new Molecule<T>() {

        public void run() {
            insert(t);
            setObj(t);
        }
    };
    return ff.run(m);
}
Also used : FieldFilter(org.nutz.dao.FieldFilter) Molecule(org.nutz.trans.Molecule) ArrayList(java.util.ArrayList) MappingField(org.nutz.dao.entity.MappingField)

Example 3 with Molecule

use of org.nutz.trans.Molecule in project nutz by nutzam.

the class QueryTest method test_fetcy_by_fieldfilter.

/**
     * add for Issue #605
     */
@Test
public void test_fetcy_by_fieldfilter() {
    Molecule<Pet> mo = new Molecule<Pet>() {

        public void run() {
            setObj(dao.fetch(Pet.class, 5));
        }
    };
    FieldFilter.create(Pet.class, "^id|name$").run(mo);
    Pet pet = mo.getObj();
    assertEquals(5, pet.getId());
    assertEquals("pet4", pet.getName());
    assertEquals(0, pet.getAge());
    assertNull(pet.getBirthday());
    assertNull(pet.getNickName());
}
Also used : Molecule(org.nutz.trans.Molecule) Pet(org.nutz.dao.test.meta.Pet) Test(org.junit.Test)

Example 4 with Molecule

use of org.nutz.trans.Molecule in project nutz by nutzam.

the class NutDao method query.

public <T> List<T> query(final Class<T> classOfT, final Condition cnd, final Pager pager, FieldMatcher matcher) {
    if (matcher == null)
        return query(classOfT, cnd, pager);
    FieldFilter ff = FieldFilter.create(classOfT, matcher);
    Molecule<List<T>> m = new Molecule<List<T>>() {

        public void run() {
            setObj(query(classOfT, cnd, pager));
        }
    };
    return ff.run(m);
}
Also used : FieldFilter(org.nutz.dao.FieldFilter) Molecule(org.nutz.trans.Molecule) List(java.util.List) ArrayList(java.util.ArrayList)

Example 5 with Molecule

use of org.nutz.trans.Molecule in project nutz by nutzam.

the class QueryTest method test_query_by_fieldfilter.

/**
     * add for Issue #605
     */
@Test
public void test_query_by_fieldfilter() {
    Molecule<List<Pet>> mo = new Molecule<List<Pet>>() {

        public void run() {
            setObj(dao.query(Pet.class, Cnd.orderBy().asc("id")));
        }
    };
    FieldFilter.create(Pet.class, "^id|name$").run(mo);
    int i = 0;
    for (Pet pet : mo.getObj()) {
        assertEquals(i + 1, pet.getId());
        assertEquals("pet" + i, pet.getName());
        assertEquals(0, pet.getAge());
        assertNull(pet.getBirthday());
        assertNull(pet.getNickName());
        i++;
    }
}
Also used : Molecule(org.nutz.trans.Molecule) List(java.util.List) Pet(org.nutz.dao.test.meta.Pet) Test(org.junit.Test)

Aggregations

Molecule (org.nutz.trans.Molecule)5 ArrayList (java.util.ArrayList)3 List (java.util.List)3 FieldFilter (org.nutz.dao.FieldFilter)3 Test (org.junit.Test)2 Pet (org.nutz.dao.test.meta.Pet)2 MappingField (org.nutz.dao.entity.MappingField)1