Search in sources :

Example 6 with DaoStatement

use of org.nutz.dao.sql.DaoStatement in project nutz by nutzam.

the class NutDao method func.

public int func(Class<?> classOfT, String funcName, String colName, Condition cnd) {
    Entity<?> en = holder.getEntity(classOfT);
    if (null != en.getField(colName))
        colName = en.getField(colName).getColumnNameInSql();
    DaoStatement pojo = pojoMaker.makeFunc(en.getViewName(), funcName, colName).append(Pojos.Items.cnd(cnd)).setAfter(_pojo_fetchInt).setEntity(en);
    _exec(pojo);
    return pojo.getInt();
}
Also used : DaoStatement(org.nutz.dao.sql.DaoStatement)

Example 7 with DaoStatement

use of org.nutz.dao.sql.DaoStatement in project nutz by nutzam.

the class NutDao method func2.

public Object func2(String tableName, String func2Name, String colName, Condition cnd) {
    DaoStatement pojo = pojoMaker.makeFunc(tableName, func2Name, colName).append(Pojos.Items.cnd(cnd)).setAfter(_pojo_fetchObject);
    _exec(pojo);
    return pojo.getResult();
}
Also used : DaoStatement(org.nutz.dao.sql.DaoStatement)

Example 8 with DaoStatement

use of org.nutz.dao.sql.DaoStatement in project nutz by nutzam.

the class NutDaoExecutor method _runSelect.

private void _runSelect(Connection conn, DaoStatement st) throws SQLException {
    Object[][] paramMatrix = st.getParamMatrix();
    // -------------------------------------------------
    // 以下代码,就为了该死的游标分页!!
    // -------------------------------------------------
    int startRow = -1;
    int lastRow = -1;
    if (st.getContext().getResultSetType() == ResultSet.TYPE_SCROLL_INSENSITIVE) {
        Pager pager = st.getContext().getPager();
        if (pager != null) {
            startRow = pager.getOffset();
            lastRow = pager.getOffset() + pager.getPageSize();
        }
    }
    // -------------------------------------------------
    // 生成 Sql 语句
    String sql = st.toPreparedStatement();
    // 打印调试信息
    ResultSet rs = null;
    Statement stat = null;
    try {
        // 木有参数,直接运行
        if (null == paramMatrix || paramMatrix.length == 0 || paramMatrix[0].length == 0) {
            stat = conn.createStatement(st.getContext().getResultSetType(), ResultSet.CONCUR_READ_ONLY);
            if (lastRow > 0)
                // 游标分页,现在总行数
                stat.setMaxRows(lastRow);
            if (st.getContext().getFetchSize() != 0)
                stat.setFetchSize(st.getContext().getFetchSize());
            rs = stat.executeQuery(sql);
        } else // 有参数,用缓冲语句
        {
            // 打印调试信息
            if (paramMatrix.length > 1) {
                if (log.isWarnEnabled())
                    log.warnf("Drop last %d rows parameters for:\n%s", paramMatrix.length - 1, st);
            }
            // 准备运行语句
            ValueAdaptor[] adaptors = st.getAdaptors();
            // 创建语句并设置参数
            stat = conn.prepareStatement(sql, st.getContext().getResultSetType(), ResultSet.CONCUR_READ_ONLY);
            if (lastRow > 0)
                stat.setMaxRows(lastRow);
            if (st.getContext().getFetchSize() != 0)
                stat.setFetchSize(st.getContext().getFetchSize());
            for (int i = 0; i < paramMatrix[0].length; i++) {
                adaptors[i].set((PreparedStatement) stat, paramMatrix[0][i], i + 1);
            }
            rs = ((PreparedStatement) stat).executeQuery();
        }
        if (startRow > 0)
            rs.absolute(startRow);
        // 执行回调
        st.onAfter(conn, rs, stat);
    } finally {
        Daos.safeClose(stat, rs);
    }
    // 打印更详细的调试信息
    if (log.isTraceEnabled())
        log.trace("...DONE");
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) DaoStatement(org.nutz.dao.sql.DaoStatement) Pager(org.nutz.dao.pager.Pager) ValueAdaptor(org.nutz.dao.jdbc.ValueAdaptor) ResultSet(java.sql.ResultSet)

Example 9 with DaoStatement

use of org.nutz.dao.sql.DaoStatement in project nutz by nutzam.

the class NutDaoRunner method run.

public void run(final DataSource dataSource, final ConnCallback callback) {
    if (callback instanceof DaoInterceptorChain) {
        // 看看是不是应该强制使用事务
        DaoStatement[] sts = ((DaoInterceptorChain) callback).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 {
                        ((DaoInterceptorChain) callback).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) {
            Trans.exec(((DaoInterceptorChain) callback).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 10 with DaoStatement

use of org.nutz.dao.sql.DaoStatement 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)

Aggregations

DaoStatement (org.nutz.dao.sql.DaoStatement)11 CallableStatement (java.sql.CallableStatement)2 PreparedStatement (java.sql.PreparedStatement)2 Statement (java.sql.Statement)2 DaoInterceptorChain (org.nutz.dao.DaoInterceptorChain)2 Method (java.lang.reflect.Method)1 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 DataSource (javax.sql.DataSource)1 Test (org.junit.Test)1 DaoException (org.nutz.dao.DaoException)1 DaoExecutor (org.nutz.dao.impl.DaoExecutor)1 NutDao (org.nutz.dao.impl.NutDao)1 SimpleDataSource (org.nutz.dao.impl.SimpleDataSource)1 ValueAdaptor (org.nutz.dao.jdbc.ValueAdaptor)1 Pager (org.nutz.dao.pager.Pager)1 Atom (org.nutz.trans.Atom)1 Transaction (org.nutz.trans.Transaction)1