コード例 #1
0
ファイル: SqlSource.java プロジェクト: txteam/tx-parent
  /**
   * 获取分页查询sql <功能详细描述> [参数说明]
   *
   * @return void [返回类型说明]
   * @exception throws [异常类型] [异常说明]
   * @see [类、类#方法、类#成员]
   */
  public String queryPagedSql(Object obj, int pageIndex, int pageSize) {
    String querySql = querySql(obj);
    int offset = pageSize * (pageIndex - 1);
    int limit = pageSize * pageIndex;
    final boolean isSupportsVariableLimit = dialect.supportsVariableLimit(); // 是否支持物理分页
    final boolean isSupportsLimit = dialect.supportsLimit(); // 是否支持limit
    final boolean isSupportsLimitOffset = dialect.supportsLimitOffset(); // 是否支持offset

    // 如果不支持物理分页,直接返回sql
    if (!isSupportsVariableLimit || (!isSupportsLimit && !isSupportsLimitOffset)) {
      return querySql;
    } else {
      // 如果支持
      String limitSql = dialect.getLimitString(querySql, offset, limit);
      return limitSql;
    }
  }
コード例 #2
0
ファイル: SqlSource.java プロジェクト: txteam/tx-parent
  /**
   * 获取查询条件的Setter对象 <功能详细描述>
   *
   * @param obj
   * @return [参数说明]
   * @return PreparedStatementSetter [返回类型说明]
   * @exception throws [异常类型] [异常说明]
   * @see [类、类#方法、类#成员]
   */
  public PreparedStatementSetter getPagedQueryCondtionSetter(Object obj, int offset, int limit) {
    final MetaObject metaObject = MetaObjectUtils.forObject(obj);

    // 如果支持isSupportsLimitOffset并且当前需要偏移值
    // final boolean isSupportsVariableLimit = dialect.supportsVariableLimit();//是否支持物理分页
    final boolean isBindOnFirst = dialect.bindLimitParametersFirst(); // 是否绑定在前
    final boolean isSupportsLimit = dialect.supportsLimit(); // 是否支持limit
    final boolean isSupportsLimitOffset = dialect.supportsLimitOffset(); // 是否支持offset
    final boolean bindLimitParametersInReverseOrder = dialect.bindLimitParametersInReverseOrder();
    final boolean isNeedSetOffset =
        isSupportsLimitOffset && (offset > 0 || dialect.forceLimitUsage());
    final boolean isUseMaxForLimit = dialect.useMaxForLimit();
    final int trueOffset = offset;
    final int trueLimit = isUseMaxForLimit ? limit : limit - offset;

    PreparedStatementSetter res =
        new PreparedStatementSetter() {
          @Override
          public void setValues(PreparedStatement ps) throws SQLException {
            int i = 1;
            if (isBindOnFirst) {
              if (bindLimitParametersInReverseOrder) {
                if (isSupportsLimit) {
                  ps.setInt(i++, trueLimit);
                }
                if (isNeedSetOffset) {
                  ps.setInt(i++, trueOffset);
                }
              } else {
                if (isNeedSetOffset) {
                  ps.setInt(i++, trueOffset);
                }
                if (isSupportsLimit) {
                  ps.setInt(i++, trueLimit);
                }
              }
            }
            for (Entry<String, String> entryTemp : queryConditionKey2SqlMapping.entrySet()) {
              String queryKeyName = entryTemp.getKey();
              Object valueObj = metaObject.getValue(queryKeyName);
              if (ObjectUtils.isEmpty(valueObj)) {
                continue;
              }
              JdbcType jdbcType = queryConditionKey2JdbcTypeMapping.get(queryKeyName);
              JdbcUtils.setPreparedStatementValueForSimpleType(ps, i++, valueObj, jdbcType);
            }
            if (!isBindOnFirst) {
              if (bindLimitParametersInReverseOrder) {
                if (isSupportsLimit) {
                  ps.setInt(i++, trueLimit);
                }
                if (isNeedSetOffset) {
                  ps.setInt(i++, trueOffset);
                }
              } else {
                if (isNeedSetOffset) {
                  ps.setInt(i++, trueOffset);
                }
                if (isSupportsLimit) {
                  ps.setInt(i++, trueLimit);
                }
              }
            }
          }
        };
    return res;
  }
コード例 #3
0
 public boolean isMatch(Dialect dialect) {
   return dialect.supportsLimit() && dialect.supportsLimitOffset();
 }