This commit is contained in:
Redkale
2016-07-12 23:44:41 +08:00
parent 00b6910986
commit 839c742423
4 changed files with 50 additions and 33 deletions

View File

@@ -532,7 +532,7 @@ public class HttpRequest extends Request<HttpContext> {
} }
/** /**
* 获取请求URL分段中含prefix段的int值 例如请求URL /pipes/record/query/start:0/size:50 获取page参数: int start = request.getRequstURIPath("start:", 0); 获取size参数: int size = request.getRequstURIPath("size:", 20); * 获取请求URL分段中含prefix段的int值 例如请求URL /pipes/record/query/offset:0/limit:50 获取limit参数: int offset = request.getRequstURIPath("offset:", 0); 获取size参数: int limit = request.getRequstURIPath("limit:", 20);
* *
* @param prefix prefix段前缀 * @param prefix prefix段前缀
* @param defvalue 默认int值 * @param defvalue 默认int值

View File

@@ -19,6 +19,23 @@ import javax.sql.ConnectionPoolDataSource;
import javax.xml.stream.*; import javax.xml.stream.*;
import org.redkale.util.*; import org.redkale.util.*;
import static org.redkale.source.FilterNode.formatToString; import static org.redkale.source.FilterNode.formatToString;
import static org.redkale.source.FilterNode.formatToString;
import static org.redkale.source.FilterNode.formatToString;
import static org.redkale.source.FilterNode.formatToString;
import static org.redkale.source.FilterNode.formatToString;
import static org.redkale.source.FilterNode.formatToString;
import static org.redkale.source.FilterNode.formatToString;
import static org.redkale.source.FilterNode.formatToString;
import static org.redkale.source.FilterNode.formatToString;
import static org.redkale.source.FilterNode.formatToString;
import static org.redkale.source.FilterNode.formatToString;
import static org.redkale.source.FilterNode.formatToString;
import static org.redkale.source.FilterNode.formatToString;
import static org.redkale.source.FilterNode.formatToString;
import static org.redkale.source.FilterNode.formatToString;
import static org.redkale.source.FilterNode.formatToString;
import static org.redkale.source.FilterNode.formatToString;
import static org.redkale.source.FilterNode.formatToString;
/** /**
* *
@@ -1830,11 +1847,11 @@ public final class DataDefaultSource implements DataSource, Function<Class, Enti
final String sql = "SELECT a.* FROM " + info.getTable() + " a" + (join == null ? "" : join) final String sql = "SELECT a.* FROM " + info.getTable() + " a" + (join == null ? "" : join)
+ ((where == null || where.length() == 0) ? "" : (" WHERE " + where)) + info.createSQLOrderby(flipper); + ((where == null || where.length() == 0) ? "" : (" WHERE " + where)) + info.createSQLOrderby(flipper);
if (debug.get() && info.isLoggable(Level.FINEST)) if (debug.get() && info.isLoggable(Level.FINEST))
logger.finest(clazz.getSimpleName() + " query sql=" + sql + (flipper == null ? "" : (" LIMIT " + flipper.getStart() + "," + flipper.getSize()))); logger.finest(clazz.getSimpleName() + " query sql=" + sql + (flipper == null ? "" : (" LIMIT " + flipper.getOffset() + "," + flipper.getLimit())));
final PreparedStatement ps = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); final PreparedStatement ps = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
final ResultSet set = ps.executeQuery(); final ResultSet set = ps.executeQuery();
if (flipper != null && flipper.getStart() > 0) set.absolute(flipper.getStart()); if (flipper != null && flipper.getOffset() > 0) set.absolute(flipper.getOffset());
final int limit = flipper == null ? Integer.MAX_VALUE : flipper.getSize(); final int limit = flipper == null ? Integer.MAX_VALUE : flipper.getLimit();
int i = 0; int i = 0;
while (set.next()) { while (set.next()) {
i++; i++;

View File

@@ -303,7 +303,7 @@ public final class EntityCache<T> {
Stream<T> stream = this.list.stream(); Stream<T> stream = this.list.stream();
if (filter != null) stream = stream.filter(filter); if (filter != null) stream = stream.filter(filter);
if (comparator != null) stream = stream.sorted(comparator); if (comparator != null) stream = stream.sorted(comparator);
if (flipper != null) stream = stream.skip(flipper.getStart()).limit(flipper.getSize()); if (flipper != null) stream = stream.skip(flipper.getOffset()).limit(flipper.getLimit());
final List<T> rs = new ArrayList<>(); final List<T> rs = new ArrayList<>();
if (selects == null) { if (selects == null) {
Consumer<? super T> action = x -> rs.add(needcopy ? newReproduce.copy(creator.create(), x) : x); Consumer<? super T> action = x -> rs.add(needcopy ? newReproduce.copy(creator.create(), x) : x);

View File

@@ -16,89 +16,89 @@ import java.io.Serializable;
*/ */
public final class Flipper implements Serializable, Cloneable { public final class Flipper implements Serializable, Cloneable {
public static int DEFAULT_PAGESIZE = 20; public static int DEFAULT_LIMIT = 20;
private int size = DEFAULT_PAGESIZE; private int limit = DEFAULT_LIMIT;
private int start = 0; private int offset = 0;
private String sort = ""; private String sort = "";
public Flipper() { public Flipper() {
} }
public Flipper(int pageSize) { public Flipper(int limit) {
this.size = pageSize; this.limit = limit;
} }
public Flipper(String sortColumn) { public Flipper(String sortColumn) {
this.sort = sortColumn; this.sort = sortColumn;
} }
public Flipper(int pageSize, int startIndex) { public Flipper(int limit, int offset) {
this.size = pageSize > 0 ? pageSize : DEFAULT_PAGESIZE; this.limit = limit > 0 ? limit : DEFAULT_LIMIT;
this.start = startIndex < 0 ? 0 : startIndex; this.offset = offset < 0 ? 0 : offset;
} }
public Flipper(int pageSize, int startIndex, String sortColumn) { public Flipper(int limit, int offset, String sortColumn) {
this.size = pageSize > 0 ? pageSize : DEFAULT_PAGESIZE; this.limit = limit > 0 ? limit : DEFAULT_LIMIT;
this.start = startIndex < 0 ? 0 : startIndex; this.offset = offset < 0 ? 0 : offset;
this.sort = sortColumn; this.sort = sortColumn;
} }
public void copyTo(Flipper copy) { public void copyTo(Flipper copy) {
if (copy == null) return; if (copy == null) return;
copy.start = this.start; copy.offset = this.offset;
copy.size = this.size; copy.limit = this.limit;
copy.sort = this.sort; copy.sort = this.sort;
} }
public void copyFrom(Flipper copy) { public void copyFrom(Flipper copy) {
if (copy == null) return; if (copy == null) return;
this.start = copy.start; this.offset = copy.offset;
this.size = copy.size; this.limit = copy.limit;
this.sort = copy.sort; this.sort = copy.sort;
} }
public Flipper next() { public Flipper next() {
this.start = getStart() + this.size; this.offset = getOffset() + this.limit;
return this; return this;
} }
@Override @Override
@SuppressWarnings("CloneDoesntCallSuperClone") @SuppressWarnings("CloneDoesntCallSuperClone")
public Flipper clone() { public Flipper clone() {
return new Flipper(this.size, this.start, this.sort); return new Flipper(this.limit, this.offset, this.sort);
} }
public int getStart() { public int getOffset() {
return start; return offset;
} }
@Override @Override
public String toString() { public String toString() {
return this.getClass().getSimpleName() + "{start:" + this.start + ", size=" + this.size + ", sort=" + this.sort + "}"; return this.getClass().getSimpleName() + "{offset:" + this.offset + ", limit:" + this.limit + ", sort:" + this.sort + "}";
} }
public int getSize() { public int getLimit() {
return size; return limit;
} }
public void setSize(int size) { public void setLimit(int limit) {
if (size > 0) { if (limit > 0) {
this.size = size; this.limit = limit;
} }
} }
public void setStart(int start) { public void setOffset(int offset) {
this.start = start < 0 ? 0 : start; this.offset = offset < 0 ? 0 : offset;
} }
public String getSort() { public String getSort() {
return sort; return sort;
} }
public Flipper sortIfEmpty(String sort) { public Flipper sortIfAbsent(String sort) {
if (this.sort == null || this.sort.isEmpty()) { if (this.sort == null || this.sort.isEmpty()) {
this.sort = sort; this.sort = sort;
} }