Search in sources :

Example 6 with ValueAdaptor

use of org.nutz.dao.jdbc.ValueAdaptor in project nutz by nutzam.

the class NutPojo method getAdaptors.

public ValueAdaptor[] getAdaptors() {
    ValueAdaptor[] adaptors = new ValueAdaptor[_params_count()];
    int i = 0;
    for (PItem item : items) i = item.joinAdaptor(getEntity(), adaptors, i);
    return adaptors;
}
Also used : ValueAdaptor(org.nutz.dao.jdbc.ValueAdaptor) PItem(org.nutz.dao.sql.PItem)

Example 7 with ValueAdaptor

use of org.nutz.dao.jdbc.ValueAdaptor in project nutz by nutzam.

the class NutSql method getAdaptors.

public ValueAdaptor[] getAdaptors() {
    ValueAdaptor[] adaptors = new ValueAdaptor[_params_count()];
    int i = 0;
    for (PItem item : items) i = item.joinAdaptor(getEntity(), adaptors, i);
    return adaptors;
}
Also used : ValueAdaptor(org.nutz.dao.jdbc.ValueAdaptor) AbstractPItem(org.nutz.dao.impl.sql.pojo.AbstractPItem) StaticPItem(org.nutz.dao.impl.sql.pojo.StaticPItem) PItem(org.nutz.dao.sql.PItem)

Example 8 with ValueAdaptor

use of org.nutz.dao.jdbc.ValueAdaptor 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);
            afterCreateStatement(stat, st);
            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);
            afterCreateStatement(stat, st);
            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 ValueAdaptor

use of org.nutz.dao.jdbc.ValueAdaptor in project nutz by nutzam.

the class NutDaoExecutor method _runPreparedStatement.

private void _runPreparedStatement(Connection conn, DaoStatement st, Object[][] paramMatrix) throws SQLException {
    ValueAdaptor[] adaptors = st.getAdaptors();
    if (adaptors.length != paramMatrix[0].length)
        throw Lang.makeThrow("DaoStatement adaptor MUST same width with param matrix.");
    boolean statIsClosed = false;
    String sql = st.toPreparedStatement();
    PreparedStatement pstat = null;
    try {
        // 创建 SQL 语句
        if (st.getContext().attr("RETURN_GENERATED_KEYS") == null)
            pstat = conn.prepareStatement(sql);
        else
            pstat = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
        // 就一条记录,不要批了吧
        if (paramMatrix.length == 1) {
            for (int i = 0; i < paramMatrix[0].length; i++) {
                adaptors[i].set(pstat, paramMatrix[0][i], i + 1);
            }
            pstat.execute();
            st.getContext().setUpdateCount(pstat.getUpdateCount());
            st.onAfter(conn, null, pstat);
            pstat.close();
            statIsClosed = true;
        } else // 恩,批
        {
            for (Object[] params : paramMatrix) {
                for (int i = 0; i < params.length; i++) {
                    adaptors[i].set(pstat, params[i], i + 1);
                }
                // 需要配置一下batchSize,嘻嘻,不然分分钟爆内存!!
                pstat.addBatch();
            }
            int[] counts = pstat.executeBatch();
            // 计算总共影响的行数
            int sum = 0;
            for (int i : counts) if (i > 0)
                sum += i;
            if (sum == 0)
                sum = pstat.getUpdateCount();
            st.onAfter(conn, null, pstat);
            pstat.close();
            statIsClosed = true;
            st.getContext().setUpdateCount(sum);
        }
    } finally {
        if (!statIsClosed)
            Daos.safeClose(pstat);
    }
    // 打印更详细的调试信息
    if (log.isTraceEnabled())
        log.trace("...DONE");
}
Also used : ValueAdaptor(org.nutz.dao.jdbc.ValueAdaptor) PreparedStatement(java.sql.PreparedStatement)

Aggregations

ValueAdaptor (org.nutz.dao.jdbc.ValueAdaptor)9 PreparedStatement (java.sql.PreparedStatement)6 ResultSet (java.sql.ResultSet)3 MappingField (org.nutz.dao.entity.MappingField)3 CallableStatement (java.sql.CallableStatement)2 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 Chain (org.nutz.dao.Chain)2 ConnCallback (org.nutz.dao.ConnCallback)2 DaoException (org.nutz.dao.DaoException)2 PItem (org.nutz.dao.sql.PItem)2 Sql (org.nutz.dao.sql.Sql)2 Statement (java.sql.Statement)1 HashMap (java.util.HashMap)1 Record (org.nutz.dao.entity.Record)1 AbstractPItem (org.nutz.dao.impl.sql.pojo.AbstractPItem)1 StaticPItem (org.nutz.dao.impl.sql.pojo.StaticPItem)1 Pager (org.nutz.dao.pager.Pager)1 DaoStatement (org.nutz.dao.sql.DaoStatement)1