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);
}
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);
}
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());
}
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);
}
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++;
}
}
Aggregations