提交 850ca1a4 编写于 作者: G guolindev
上级 c3cbc0fe
......@@ -16,12 +16,15 @@
package org.litepal;
import android.text.TextUtils;
import org.litepal.crud.LitePalSupport;
import org.litepal.crud.QueryHandler;
import org.litepal.crud.async.AverageExecutor;
import org.litepal.crud.async.CountExecutor;
import org.litepal.crud.async.FindExecutor;
import org.litepal.crud.async.FindMultiExecutor;
import org.litepal.exceptions.LitePalSupportException;
import org.litepal.tablemanager.Connector;
import org.litepal.util.BaseUtility;
import org.litepal.util.DBUtility;
......@@ -313,8 +316,14 @@ public class FluentQuery {
*/
public <T> T findFirst(Class<T> modelClass, boolean isEager) {
synchronized (LitePalSupport.class) {
String limitTemp = mLimit;
if (!"0".equals(mLimit)) { // If mLimit not equals to 0, set mLimit to 1 to find the first record.
mLimit = "1";
}
List<T> list = find(modelClass, isEager);
mLimit = limitTemp; // Don't forget to change it back after finding operation.
if (list.size() > 0) {
if (list.size() != 1) throw new LitePalSupportException("Found multiple records while only one record should be found at most.");
return list.get(0);
}
return null;
......@@ -399,7 +408,29 @@ public class FluentQuery {
*/
public <T> T findLast(Class<T> modelClass, boolean isEager) {
synchronized (LitePalSupport.class) {
String orderByTemp = mOrderBy;
String limitTemp = mLimit;
if (TextUtils.isEmpty(mOffset) && TextUtils.isEmpty(mLimit)) { // If mOffset or mLimit is specified, we can't use the strategy in this block to speed up finding.
if (TextUtils.isEmpty(mOrderBy)) {
// If mOrderBy is null, we can use id desc order, then the first record will be the record value where want to find.
mOrderBy = "id desc";
} else {
// If mOrderBy is not null, check if it ends with desc.
if (mOrderBy.endsWith(" desc")) {
// If mOrderBy ends with desc, then the last record of desc order will be the first record of asc order, so we remove the desc.
mOrderBy = mOrderBy.replace(" desc", "");
} else {
// If mOrderBy not ends with desc, then the last record of asc order will be the first record of desc order, so we add the desc.
mOrderBy += " desc";
}
}
if (!"0".equals(mLimit)) {
mLimit = "1";
}
}
List<T> list = find(modelClass, isEager);
mOrderBy = orderByTemp;
mLimit = limitTemp;
int size = list.size();
if (size > 0) {
return list.get(size - 1);
......
......@@ -289,6 +289,23 @@ public class QueryClusterTest extends LitePalTestCase {
assertEquals(lastBook.getId(), b.getId());
}
}
Book first1 = LitePal.findFirst(Book.class);
Book first2 = LitePal.select("id").findFirst(Book.class);
assertEquals(first1.getId(), first2.getId());
Book last1 = LitePal.findLast(Book.class);
Book last2 = LitePal.select("id").findLast(Book.class);
assertEquals(last1.getId(), last2.getId());
List<Book> firstTwoBooks = LitePal.where("id=? or id=? or id=?", String.valueOf(ids[0]), String.valueOf(ids[1]),
String.valueOf(ids[2])).order("id").limit(2).find(Book.class);
firstBook = LitePal.where("id=? or id=? or id=?", String.valueOf(ids[0]), String.valueOf(ids[1]),
String.valueOf(ids[2])).order("id").limit(2).findFirst(Book.class);
lastBook = LitePal.where("id=? or id=? or id=?", String.valueOf(ids[0]), String.valueOf(ids[1]),
String.valueOf(ids[2])).order("id").limit(2).findLast(Book.class);
assertEquals(firstTwoBooks.get(0).getId(), firstBook.getId());
assertEquals(firstTwoBooks.get(1).getId(), lastBook.getId());
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册