Search in sources :

Example 1 with DaoInterceptorChain

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

the class DaoSupport method _exec.

protected int _exec(final DaoStatement... sts) {
    if (sts != null)
        for (DaoStatement ds : sts) {
            ds.setExpert(expert);
        }
    final DaoInterceptorChain callback = new DaoInterceptorChain(sts);
    callback.setExecutor(executor);
    callback.setAutoTransLevel(autoTransLevel);
    callback.setInterceptors(Collections.unmodifiableList(this._interceptors));
    run(callback);
    // 搞定,返回结果 ^_^
    return callback.getUpdateCount();
}
Also used : DaoInterceptorChain(org.nutz.dao.DaoInterceptorChain) DaoStatement(org.nutz.dao.sql.DaoStatement)

Example 2 with DaoInterceptorChain

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

the class NutDaoRunner method run.

public void run(final DataSource dataSource, final ConnCallback callback) {
    if (callback instanceof DaoInterceptorChain) {
        DaoInterceptorChain chain = (DaoInterceptorChain) callback;
        // 看看是不是应该强制使用事务
        DaoStatement[] sts = chain.getDaoStatements();
        boolean useTrans = false;
        boolean isAllSelect = true;
        for (DaoStatement st : sts) {
            if (!st.isSelect() && !st.isForceExecQuery()) {
                isAllSelect = false;
                break;
            }
        }
        switch(meta.getType()) {
            case PSQL:
                // PSQL必须带事务,不然Clob和Blob操作必死
                useTrans = true;
                break;
            case SQLITE:
                // SQLITE仅支持2种事务级别
                Transaction t = Trans.get();
                if (t == null) {
                    if (isAllSelect)
                        useTrans = false;
                    else {
                        chain.setAutoTransLevel(Connection.TRANSACTION_READ_UNCOMMITTED);
                        useTrans = true;
                    }
                } else if (t.getLevel() != Connection.TRANSACTION_SERIALIZABLE && t.getLevel() != Connection.TRANSACTION_READ_UNCOMMITTED) {
                    t.setLevel(Connection.TRANSACTION_READ_UNCOMMITTED);
                    useTrans = true;
                }
                break;
            default:
                useTrans = !(Trans.isTransactionNone() && (sts.length == 1 || isAllSelect));
                break;
        }
        // 看来需要开启事务了
        if (useTrans && chain.getAutoTransLevel() > 0) {
            Trans.exec(chain.getAutoTransLevel(), new Atom() {

                public void run() {
                    _run(dataSource, callback);
                }
            });
            return;
        }
    }
    // 不需要额外加事务,直接通过
    _run(dataSource, callback);
}
Also used : DaoInterceptorChain(org.nutz.dao.DaoInterceptorChain) Transaction(org.nutz.trans.Transaction) DaoStatement(org.nutz.dao.sql.DaoStatement) Atom(org.nutz.trans.Atom)

Example 3 with DaoInterceptorChain

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

the class NutDaoRunner method selectDataSource.

protected DataSource selectDataSource(Transaction t, DataSource master, ConnCallback callback) {
    if (this.slaveDataSource == null)
        return master;
    if (t == null && callback instanceof DaoInterceptorChain) {
        DaoInterceptorChain chain = (DaoInterceptorChain) callback;
        DaoStatement[] sts = chain.getDaoStatements();
        if (sts.length == 1 && (sts[0].isSelect() || sts[0].isForceExecQuery())) {
            return slaveDataSource;
        }
    }
    return master;
}
Also used : DaoInterceptorChain(org.nutz.dao.DaoInterceptorChain) DaoStatement(org.nutz.dao.sql.DaoStatement)

Example 4 with DaoInterceptorChain

use of org.nutz.dao.DaoInterceptorChain 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

DaoInterceptorChain (org.nutz.dao.DaoInterceptorChain)4 DaoStatement (org.nutz.dao.sql.DaoStatement)3 DataSource (javax.sql.DataSource)1 Test (org.junit.Test)1 DaoInterceptor (org.nutz.dao.DaoInterceptor)1 NutDao (org.nutz.dao.impl.NutDao)1 Pet (org.nutz.dao.test.meta.Pet)1 Atom (org.nutz.trans.Atom)1 Transaction (org.nutz.trans.Transaction)1