Search in sources :

Example 1 with DaoInterceptor

use of org.nutz.dao.DaoInterceptor in project nutz by nutzam.

the class DaoSupport method setInterceptors.

public void setInterceptors(List<Object> interceptors) {
    List<DaoInterceptor> list = new LinkedList<DaoInterceptor>();
    for (Object it : interceptors) {
        DaoInterceptor d = makeInterceptor(it);
        if (d != null)
            list.add(d);
    }
    this._interceptors = list;
}
Also used : DaoInterceptor(org.nutz.dao.DaoInterceptor) LinkedList(java.util.LinkedList)

Example 2 with DaoInterceptor

use of org.nutz.dao.DaoInterceptor in project nutz by nutzam.

the class DaoSupport method addInterceptor.

public void addInterceptor(Object it) {
    DaoInterceptor d = makeInterceptor(it);
    if (d != null) {
        List<DaoInterceptor> list = new LinkedList<DaoInterceptor>(this._interceptors);
        list.add(d);
        this._interceptors = list;
    }
}
Also used : DaoInterceptor(org.nutz.dao.DaoInterceptor) LinkedList(java.util.LinkedList)

Example 3 with DaoInterceptor

use of org.nutz.dao.DaoInterceptor in project nutz by nutzam.

the class SimpleDaoInterceptorTest method issue_1325.

/**
 * 测试的内容: 在DaoInterceptor内设置字段过滤,看看能不能起作用
 */
@Test
public void issue_1325() {
    // 现有默认dao创建记录
    this.dao.create(Pet.class, true);
    Pet pet = Pet.create("wendal");
    pet.setAge(30);
    this.dao.insert(pet);
    // 然后构建一个临时用的Dao实例, 拦截器顺序: 设置FieldFilter, log
    DataSource ds = ioc.get(DataSource.class);
    NutDao dao = new NutDao(ds);
    dao.setInterceptors(Arrays.asList(new DaoInterceptor() {

        public void filter(DaoInterceptorChain chain) throws DaoException {
            chain.getDaoStatement().getContext().setFieldMatcher(FieldMatcher.make(null, "age", false));
            chain.doChain();
        }
    }, "log"));
    // 用临时dao,应该是没有age数据
    pet = dao.fetch(Pet.class, Cnd.where("name", "=", "wendal"));
    Assert.assertEquals(0, pet.getAge());
    // 用默认dao, 应该有age数据
    pet = this.dao.fetch(Pet.class, Cnd.where("name", "=", "wendal"));
    Assert.assertEquals(30, pet.getAge());
}
Also used : DaoInterceptorChain(org.nutz.dao.DaoInterceptorChain) NutDao(org.nutz.dao.impl.NutDao) DaoInterceptor(org.nutz.dao.DaoInterceptor) Pet(org.nutz.dao.test.meta.Pet) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Aggregations

DaoInterceptor (org.nutz.dao.DaoInterceptor)3 LinkedList (java.util.LinkedList)2 DataSource (javax.sql.DataSource)1 Test (org.junit.Test)1 DaoInterceptorChain (org.nutz.dao.DaoInterceptorChain)1 NutDao (org.nutz.dao.impl.NutDao)1 Pet (org.nutz.dao.test.meta.Pet)1