Build a paging statement

@param pageRow page param
@return paging sql
162745
private String buildPageSQL(String sql, PageRow pageRow) {
        SQLParams sqlParams = SQLParams.builder()
                .modelClass(this.modelClass)
                .selectColumns(this.selectColumns)
                .tableName(this.tableName)
                .pkName(this.primaryKeyColumn)
                .conditionSQL(this.conditionSQL)
                .excludedColumns(this.excludedColumns)
                .customSQL(sql)
                .orderBy(this.orderBySQL.toString())
                .pageRow(pageRow)
                .build();
        return Anima.of().dialect().paginate(sqlParams);
    }
728593
public Query<T> build() {
    QueryBean<T> queryBean = new QueryBean<T>();
    queryBean.setStatement(genStatement());
    queryBean.setParams(getParams());
    if (null != limit) {
      queryBean.setLimit(new PageLimit(limit.getPageIndex(), limit.getPageSize()));
    }
    queryBean.setCountStatement(genCountStatement());
    queryBean.setCacheable(cacheable);
    queryBean.setLang(getLang());
    return queryBean;
  }
362005
public SqlBuilder query(Query query) {
		return this.select(query.getFields()).from(query.getTableNames()).where(LogicalOperator.AND, query.getWhere());
	}
791340
public PageResult<Entity> page(Connection conn, Collection<String> fields, Entity where, Page page) throws SQLException {
		checkConn(conn);
		
		//查询全部
		if(null == page){
			List<Entity> entityList = this.find(conn, fields, where, new EntityListHandler());
			final PageResult<Entity> pageResult = new PageResult<Entity>(0, entityList.size(), entityList.size());
			pageResult.addAll(entityList);
			return pageResult;
		}
		
		final int count = count(conn, where);
		PageResultHandler pageResultHandler = PageResultHandler.create(new PageResult<Entity>(page.getPageNumber(), page.getPageSize(), count));
		return this.page(conn, fields, where, page, pageResultHandler);
	}
235034
public Query execute(PageContext pc, SQL sql, int maxrows, int fetchsize, TimeSpan timeout) throws PageException {
	Stopwatch stopwatch = new Stopwatch(Stopwatch.UNIT_NANO);
	stopwatch.start();
	String prettySQL = null;
	Selects selects = null;

	// First Chance
	try {
	    SelectParser parser = new SelectParser();
	    selects = parser.parse(sql.getSQLString());

	    Query q = qoq.execute(pc, sql, selects, maxrows);
	    q.setExecutionTime(stopwatch.time());

	    return q;
	}
	catch (SQLParserException spe) {
	    // lucee.print.printST(spe);
	    // sp
	    // lucee.print.out("sql parser crash at:");
	    // lucee.print.out("--------------------------------");
	    // lucee.print.out(sql.getSQLString().trim());
	    // lucee.print.out("--------------------------------");
	    // print.e("1:"+sql.getSQLString());
	    prettySQL = SQLPrettyfier.prettyfie(sql.getSQLString());
	    // print.e("2:"+prettySQL);
	    try {
		Query query = executer.execute(pc, sql, prettySQL, maxrows);
		query.setExecutionTime(stopwatch.time());
		return query;
	    }
	    catch (PageException ex) {
		// lucee.print.printST(ex);
		// lucee.print.out("old executor/zql crash at:");
		// lucee.print.out("--------------------------------");
		// lucee.print.out(sql.getSQLString().trim());
		// lucee.print.out("--------------------------------");

	    }

	}
	catch (PageException e) {
	    // throw e;
	    // print.out("new executor crash at:");
	    // print.out("--------------------------------");
	    // print.out(sql.getSQLString().trim());
	    // print.out("--------------------------------");
	}
	// if(true) throw new RuntimeException();

	// SECOND Chance with hsqldb
	try {
	    boolean isUnion = false;
	    Set<String> tables = null;
	    if (selects != null) {
		HSQLUtil2 hsql2 = new HSQLUtil2(selects);
		isUnion = hsql2.isUnion();
		tables = hsql2.getInvokedTables();
	    }
	    else {
		if (prettySQL == null) prettySQL = SQLPrettyfier.prettyfie(sql.getSQLString());
		HSQLUtil hsql = new HSQLUtil(prettySQL);
		tables = hsql.getInvokedTables();
		isUnion = hsql.isUnion();
	    }

	    String strSQL = StringUtil.replace(sql.getSQLString(), "[", "", false);
	    strSQL = StringUtil.replace(strSQL, "]", "", false);
	    sql.setSQLString(strSQL);
	    return _execute(pc, sql, maxrows, fetchsize, timeout, stopwatch, tables, isUnion);

	}
	catch (ParseException e) {
	    throw new DatabaseException(e.getMessage(), null, sql, null);
	}

    }
1203107
public List<Entity> pageForEntityList(Entity where, int page, int numPerPage) throws SQLException {
		return pageForEntityList(where, new Page(page, numPerPage));
	}
767506
private SelectQuery incrementQueryPagination(SelectQuery query, SelectResultValue prevResult)
  {
    Map<String, Integer> pagingIdentifiers = prevResult.getPagingIdentifiers();
    Map<String, Integer> newPagingIdentifers = new HashMap<>();

    for (String segmentId : pagingIdentifiers.keySet()) {
      int newOffset = pagingIdentifiers.get(segmentId) + 1;
      newPagingIdentifers.put(segmentId, newOffset);
    }

    return query.withPagingSpec(new PagingSpec(newPagingIdentifers, pagingThreshold));
  }
371853
public String forPaginate(int pageNumber, int pageSize, StringBuilder findSql) {
		int end = pageNumber * pageSize;
		if (end <= 0) {
			end = pageSize;
		}
		int begin = (pageNumber - 1) * pageSize;
		if (begin < 0) {
			begin = 0;
		}
		StringBuilder ret = new StringBuilder();
		ret.append("SELECT * FROM ( SELECT row_number() over (order by tempcolumn) temprownumber, * FROM ");
		ret.append(" ( SELECT TOP ").append(end).append(" tempcolumn=0,");
		ret.append(findSql.toString().replaceFirst("(?i)select", ""));
		ret.append(")vip)mvp where temprownumber>").append(begin);
		return ret.toString();
	}
750214
@Override
    public PaginatedList queryForPaginatedList(String statementName, int pageSize)
            throws DataAccessException {
        return super.queryForPaginatedList(statementName, pageSize);
    }
562211
public PageResult<Entity> page(Entity where, Page page) throws SQLException{
		return db.page(fixEntity(where), page);
	}
835443
protected void appendPagination(SolrQuery query, @Nullable Long offset, @Nullable Integer rows) {

		if (offset != null && offset.intValue() >= 0) {
			query.setStart(offset.intValue());
		}
		if (rows != null && rows >= 0) {
			query.setRows(rows);
		}
	}
660768
public Query execute(PageContext pc, SQL sql, Selects selects, int maxrows) throws PageException {
	Column[] orders = selects.getOrderbys();
	Select[] arrSelects = selects.getSelects();

	QueryImpl target = new QueryImpl(new Collection.Key[0], 0, "query", sql);

	for (int i = 0; i < arrSelects.length; i++) {
	    arrSelects[i].getFroms();
	    Column[] froms = arrSelects[i].getFroms();
	    if (froms.length > 1) throw new DatabaseException("can only work with single tables yet", null, sql, null);
	    executeSingle(pc, arrSelects[i], getSingleTable(pc, froms[0]), target, arrSelects.length > 1 ? -1 : maxrows, sql, orders.length > 0);
	}

	// Order By
	if (orders.length > 0) {
	    order(target, orders);
	    if (maxrows > -1) target.cutRowsTo(maxrows);
	}
	// Distinct
	if (selects.isDistinct()) {
	    order(target, selects.getDistincts()); // order to filter
	    // print.e(selects.getDistincts());
	    Key[] _keys = target.getColumnNames();
	    QueryColumn[] columns = new QueryColumn[_keys.length];
	    for (int i = 0; i < columns.length; i++) {
		columns[i] = target.getColumn(_keys[i]);
	    }

	    int i;
	    Object l, r;
	    outer: for (int row = target.getRecordcount(); row > 1; row--) {
		for (i = 0; i < columns.length; i++) {
		    l = columns[i].get(row, null);
		    r = columns[i].get(row - 1, null);
		    if (l == null || r == null) {
			if (l != r) continue outer;
		    }
		    else if (!Operator.equals(l, r, true)) continue outer;
		}
		target.removeRow(row);
	    }
	}
	order(target, orders);

	return target;
    }
411287
public static Object currentrow(PageContext pc, Object obj) throws PageException {
	if (obj instanceof Query) return Caster.toDouble(((Query) obj).getCurrentrow(pc.getId()));
	return pc.getCollection(obj, KeyConstants._CURRENTROW);
    }
1010245
public static <P extends ParaObject> List<P> readPageFromTable(String appid, Pager p) {
		Pager pager = (p != null) ? p : new Pager();
		ScanRequest scanRequest = new ScanRequest().
				withTableName(getTableNameForAppid(appid)).
				withLimit(pager.getLimit()).
				withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL);

		if (!StringUtils.isBlank(pager.getLastKey())) {
			scanRequest = scanRequest.withExclusiveStartKey(Collections.
					singletonMap(Config._KEY, new AttributeValue(pager.getLastKey())));
		}

		ScanResult result = getClient().scan(scanRequest);
		LinkedList<P> results = new LinkedList<>();
		for (Map<String, AttributeValue> item : result.getItems()) {
			P obj = fromRow(item);
			if (obj != null) {
				results.add(obj);
			}
		}

		if (result.getLastEvaluatedKey() != null) {
			pager.setLastKey(result.getLastEvaluatedKey().get(Config._KEY).getS());
		} else if (!results.isEmpty()) {
			// set last key to be equal to the last result - end reached.
			pager.setLastKey(results.peekLast().getId());
		}
		return results;
	}
1197915
public function DatabaseBuilderInit($obj, $pageSize = null, $columns = ['*'], $pageNow = null)
  {
    if ($pageSize===false) {
      $this->pageArray = null;
      $this->lists = $obj->get($columns);
    }else{
      $pageNow = empty($pageNow) ? $this->pageArray['inputPage'] : intval($pageNow);
      $pageSize = empty($pageSize) || $pageSize === true ? $this->pageArray['size'] : intval($pageSize);
      $this->LengthAwarePaginatorInit($obj->paginate($pageSize, $columns, 'pageNow', $pageNow));
    }
    return $this;
  }
637537
public Page<T> page(String sql, List<Object> paramValues, PageRow pageRow) {
        return this.page(sql, paramValues.toArray(), pageRow);
    }
490932
public static String call(PageContext pc, Object var, boolean serializeQueryByColumns, String strCharset) throws PageException {
	Charset cs = StringUtil.isEmpty(strCharset) ? pc.getWebCharset() : CharsetUtil.toCharset(strCharset);
	return _call(pc, var, serializeQueryByColumns, cs);
    }