示例#1
0
  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) {
        if (log.isDebugEnabled()) log.debug(st);
        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);
        }
        if (log.isDebugEnabled()) {
          log.debug(st);
        }

        // 准备运行语句
        ValueAdaptor[] adaptors = st.getAdaptors();
        // 创建语句并设置参数
        stat =
            conn.prepareStatement(
                sql, st.getContext().getResultSetType(), ResultSet.CONCUR_READ_ONLY);
        if (lastRow > 0) stat.setMaxRows(lastRow);
        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);
    } finally {
      Daos.safeClose(stat, rs);
    }
    // 打印更详细的调试信息
    if (log.isTraceEnabled()) log.trace("...DONE");
  }
示例#2
0
  public void exec(Connection conn, DaoStatement st) {
    // 这个变量声明,后面两 case 要用到
    Object[][] paramMatrix;

    // 在这个块里执行语句
    try {
      /*
       * 语句执行前的预操作
       */
      st.onBefore(conn);
      /*
       * 开始执行语句
       */
      switch (st.getSqlType()) {
          // 查询
        case SELECT:
          _runSelect(conn, st);
          break;
          // 创建 & 删除 & 修改 & 清空
        case ALTER:
        case TRUNCATE:
        case CREATE:
        case DROP:
          _runStatement(conn, st);
          st.onAfter(conn, null);
          break;
          // 仅仅是运行回调
        case RUN:
          st.onAfter(conn, null);
          break;
          // 插入 & 删除 & 更新
          // case DELETE:
          // case UPDATE:
          // case INSERT:
          // 见鬼了,未知类型,也当作普通 SQL 运行吧,见 Issue#13
        default:
          if (st.getSqlType() == SqlType.OTHER && log.isInfoEnabled())
            log.info("Can't indentify SQL type :   " + st);
          paramMatrix = st.getParamMatrix();
          // 木有参数,直接运行
          if (null == paramMatrix || paramMatrix.length == 0) {
            _runStatement(conn, st);
          }
          // 有参数,用缓冲语句
          else {
            _runPreparedStatement(conn, st, paramMatrix);
          }
          // 运行回调
          st.onAfter(conn, null);
      }
    }
    // If any SQLException happend, throw out the SQL string
    catch (SQLException e) {
      if (log.isInfoEnabled()) log.debug("SQLException", e);
      throw new DaoException(
          format(
              "!Nutz SQL Error: '%s'\nPreparedStatement: \n'%s'",
              st.toString(), st.toPreparedStatement()),
          e);
    }
  }