Ejemplo n.º 1
0
  /**
   * 单表数据查询,按ResultSet的列定义转换返回Table,支持分页,当偏移行等于零时自动返回总行数。应从前端传入完整列定义(Baas. getDataColumns(data)),
   * 以解决oracle等数据库不区分date、time、datetime,导致的数据格式转换问题;兼容了以前只传入列名字符串 (data.getColumnIDs())的写法,但是已不再推荐。
   *
   * @param conn
   * @param tableName 表名
   * @param columns 列定义
   * @param filters 过滤条件列表
   * @param orderBy SQL的orderBy部分
   * @param params SQL中问号对应的参数值列表
   * @param offset 偏移行
   * @param limit 行数
   * @return
   * @throws SQLException
   */
  public static Table queryData(
      Connection conn,
      String tableName,
      Object columns,
      List<String> filters,
      String orderBy,
      List<Object> params,
      Integer offset,
      Integer limit)
      throws SQLException {
    String format = "SELECT %s FROM %s %s %s ";

    String where =
        (filters != null && filters.size() > 0)
            ? " WHERE " + Util.arrayJoin(filters.toArray(), "(%s)", " AND ")
            : "";
    orderBy = !Util.isEmptyString(orderBy) ? " ORDER BY " + orderBy : "";

    String sql = String.format(format, "*", tableName, where, orderBy);
    Table table = null;
    table = queryData(conn, sql.toString(), params, columns, offset, limit);
    if (offset != null && offset.equals(0)) {
      String sqlTotal = String.format(format, "COUNT(*)", tableName, where, "");
      Object total = Util.getValueBySQL(conn, sqlTotal.toString(), params);
      table.setTotal(Integer.parseInt(total.toString()));
    }
    return table;
  }
Ejemplo n.º 2
0
 public static DataType parse(String str) {
   return Util.isEmptyString(str) ? DataType.STRING : DataType.valueOf(str.toUpperCase());
 }