/** * Finds multiple records by the cluster parameters. You can use the below * way to finish a complicated query: * * <pre> * LitePal.select("name").where("age > ?", "14").order("age").limit(1).offset(2) * .find(Person.class); * </pre> * * You can also do the same job with SQLiteDatabase like this: * * <pre> * getSQLiteDatabase().query("Person", "name", "age > ?", new String[] { "14" }, null, null, "age", * "2,1"); * </pre> * * Obviously, the first way is much more semantic.<br> * Note that the associated models won't be loaded by default considering * the efficiency, but you can do that by using * {@link FluentQuery#find(Class, boolean)}. * * @param modelClass * Which table to query and the object type to return as a list. * @return An object list with founded data from database, or an empty list. */ public <T> List<T> find(Class<T> modelClass) { return find(modelClass, false); }
@Test public void testWhere() { List<Book> books = LitePal.where("bookname = ?", "Android First Line").find(Book.class); Book firstBook = LitePal.where("bookname = ?", "Android First Line").findFirst(Book.class); Book lastBook = LitePal.where("bookname = ?", "Android First Line").findLast(Book.class); new String[] { "Android%Line" }, null, null, null, null); List<Book> realBooks = LitePal.where("bookname like ?", "Android%Line") .find(Book.class); assertEquals(expectedBooks.size(), realBooks.size());
@Test public void testSelect() { List<Book> expectedBooks = getBooks(null, null, null, null, null, null, null); List<Book> books = LitePal.select("bookname", "price").find(Book.class); assertEquals(expectedBooks.size(), books.size()); Book firstBook = LitePal.select("bookname", "price").findFirst(Book.class);
/** * It is mostly same as {@link FluentQuery#findFirst(Class)} but an isEager * parameter. If set true the associated models will be loaded as well. * <br> * Note that isEager will only work for one deep level relation, considering the query efficiency. * You have to implement on your own if you need to load multiple deepness of relation at once. * * @param modelClass * Which table to query and the object type to return. * @param isEager * True to load the associated models, false not. * @return An object with founded data from database, or null. */ public <T> T findFirst(Class<T> modelClass, boolean isEager) { synchronized (LitePalSupport.class) { List<T> list = find(modelClass, isEager); if (list.size() > 0) { return list.get(0); } return null; } }
.select("pages", "isPublished") .where("id=? or id=? or id=?", String.valueOf(ids[0]), String.valueOf(ids[1]), String.valueOf(ids[2])).order("id").limit(2).offset(1).find(Book.class); Book firstBook = LitePal .select("pages", "isPublished")
/** * It is mostly same as {@link FluentQuery#findLast(Class)} but an isEager * parameter. If set true the associated models will be loaded as well. * <br> * Note that isEager will only work for one deep level relation, considering the query efficiency. * You have to implement on your own if you need to load multiple deepness of relation at once. * * @param modelClass * Which table to query and the object type to return. * @param isEager * True to load the associated models, false not. * @return An object with founded data from database, or null. */ public <T> T findLast(Class<T> modelClass, boolean isEager) { synchronized (LitePalSupport.class) { List<T> list = find(modelClass, isEager); int size = list.size(); if (size > 0) { return list.get(size - 1); } return null; } }
@Test public void testOrder() { List<Book> books = LitePal.order("ID").find(Book.class); Book firstBook = LitePal.order("ID").findFirst(Book.class); Book lastBook = LitePal.order("ID").findLast(Book.class); List<Book> inverseBooks = LitePal.order("ID desc").find(Book.class); Book inverseFirstBook = LitePal.order("ID desc").findFirst(Book.class); Book inverseLastBook = LitePal.order("ID desc").findLast(Book.class);
@Override public void run() { synchronized (LitePalSupport.class) { final List<T> t = find(modelClass, isEager); if (executor.getListener() != null) { Operator.getHandler().post(new Runnable() { @Override public void run() { executor.getListener().onFinish(t); } }); } } } };
@SuppressWarnings("unchecked") public int onDeleteAll(Class<?> modelClass, String... conditions) { BaseUtility.checkConditionsCorrect(conditions); if (conditions != null && conditions.length > 0) { conditions[0] = DBUtility.convertWhereClauseToColumnName(conditions[0]); } List<Field> supportedGenericFields = getSupportedGenericFields(modelClass.getName()); if (!supportedGenericFields.isEmpty()) { List<LitePalSupport> list = (List<LitePalSupport>) Operator.select("id").where(conditions).find(modelClass); if (list.size() > 0) { long[] ids = new long[list.size()]; for (int i = 0; i < ids.length; i++) { LitePalSupport dataSupport = list.get(i); ids[i] = dataSupport.getBaseObjId(); } deleteGenericData(modelClass, supportedGenericFields, ids); } } analyzeAssociations(modelClass); int rowsAffected = deleteAllCascade(modelClass, conditions); rowsAffected += mDatabase.delete(getTableName(modelClass), getWhereClause(conditions), getWhereArgs(conditions)); getForeignKeyTableToDelete().clear(); return rowsAffected; }
return save(); List<LitePalSupport> list = (List<LitePalSupport>) Operator.where(conditions).find(getClass()); if (list.isEmpty()) { return save();
@Test public void testMarkAsDeleted() { List<Student> students = new ArrayList<>(); for (int i = 0; i < 5; i++) { Student s = new Student(); s.setName("Dusting"); s.setAge(i + 10); students.add(s); } LitePal.saveAll(students); List<Student> list = LitePal.where("name=?", "Dusting").find(Student.class); assertTrue(list.size() >= 5); LitePal.deleteAll(Student.class, "name=?", "Dusting"); list = LitePal.where("name=?", "Dusting").find(Student.class); assertEquals(0, list.size()); LitePal.saveAll(students); list = LitePal.where("name=?", "Dusting").find(Student.class); assertEquals(0, list.size()); LitePal.markAsDeleted(students); LitePal.saveAll(students); list = LitePal.where("name=?", "Dusting").find(Student.class); assertEquals(5, list.size()); }
@Test public void testEagerClusterQuery() { resetData(); List<Student> sList = LitePal.where("id = ?", String.valueOf(student1.getId())).find( Student.class); assertEquals(1, sList.size()); Student s = sList.get(0); assertNull(s.getClassroom()); sList = LitePal.where("id = ?", String.valueOf(student1.getId())).find(Student.class, true); assertEquals(1, sList.size()); s = sList.get(0); assertNotNull(s.getClassroom()); Classroom c = s.getClassroom(); assertEquals("Classroom 11", c.getName()); }
long[] ids = null; if (!supportedGenericFields.isEmpty()) { List<LitePalSupport> list = (List<LitePalSupport>) Operator.select("id").where(conditions).find(baseObj.getClass()); if (list.size() > 0) { ids = new long[list.size()];
@Test public void testSaveAllWithO2O() { List<IdCard> idcardList = new ArrayList<IdCard>(); List<Student> studentList = new ArrayList<Student>(); for (int i = 0; i < 50; i++) { IdCard idcard = new IdCard(); idcard.setNumber(String.valueOf(new Random().nextInt(2000000))); Student student = new Student(); student.setName("Jim"); student.setAge(new Random().nextInt(20)); student.setIdcard(idcard); idcardList.add(idcard); studentList.add(student); } LitePal.saveAll(idcardList); LitePal.saveAll(studentList); for (Student student : studentList) { List<IdCard> result = LitePal .where(studentTable + "_id=?", String.valueOf(student.getId())).find(IdCard.class); assertEquals(1, result.size()); } }
@Test public void testOffset() { List<Book> list = LitePal.offset(1).find(Book.class); assertEquals(0, list.size()); List<Book> bookList = LitePal.limit(1).offset(1).find(Book.class); assertEquals(1, bookList.size()); Book book = bookList.get(0); assertTrue(book.isSaved()); List<Book> expectedBooks = getBooks(null, null, null, null, null, null, null); Book expectedBook = expectedBooks.get(1); assertEquals(expectedBook.getBookName(), book.getBookName()); assertEquals(expectedBook.getPages(), book.getPages()); assertEquals(expectedBook.isPublished(), book.isPublished()); assertEquals(expectedBook.getArea(), book.getArea()); assertEquals(expectedBook.getPrice(), book.getPrice()); assertEquals(expectedBook.getIsbn(), book.getIsbn()); assertEquals(expectedBook.getLevel(), book.getLevel()); assertEquals(expectedBook.getId(), book.getId()); }
@Test public void testSaveAllWithM2OOnManySide() { Classroom classroom = new Classroom(); classroom.setName("English room"); List<Student> studentList = new ArrayList<Student>(); for (int i = 0; i < 50; i++) { Student student = new Student(); student.setName("Tom"); student.setAge(new Random().nextInt(20)); student.setClassroom(classroom); studentList.add(student); } LitePal.saveAll(studentList); classroom.save(); List<Student> list = LitePal.where(classroomTable + "_id = ?", String.valueOf(classroom.get_id())).find(Student.class); assertEquals(50, list.size()); }
@Test public void testSaveAllWithM2OOnOneSide() { Classroom classroom = new Classroom(); classroom.setName("Music room"); for (int i = 0; i < 50; i++) { Student student = new Student(); student.setName("Tom"); student.setAge(new Random().nextInt(20)); classroom.getStudentCollection().add(student); } LitePal.saveAll(classroom.getStudentCollection()); classroom.save(); List<Student> list = LitePal.where(classroomTable + "_id = ?", String.valueOf(classroom.get_id())).find(Student.class); assertEquals(50, list.size()); }
@Test public void testLimit() { List<Book> bookList = LitePal.limit(1).find(Book.class); assertEquals(1, bookList.size()); Book book = bookList.get(0); assertEquals(firstBook.getLevel(), book.getLevel()); assertEquals(firstBook.getId(), book.getId()); bookList = LitePal.order("id desc").limit(1).find(Book.class); assertEquals(1, bookList.size()); book = bookList.get(0);
@Test public void testSaveIfExists() { String serial = UUID.randomUUID().toString(); Cellphone cell = new Cellphone(); cell.setBrand("iPhone"); cell.setPrice(4998.01); cell.setInStock('Y'); cell.setSerial(serial); assertTrue(cell.saveIfNotExist("serial = ?", serial)); Cellphone cell2 = new Cellphone(); cell2.setBrand("Android"); cell2.setPrice(1998.01); cell2.setInStock('Y'); cell2.setSerial(serial); assertFalse(cell.saveIfNotExist("serial = ?", serial)); List<Cellphone> cellphoneList = LitePal.where("serial = ?", serial).find(Cellphone.class); assertEquals(1, cellphoneList.size()); }
/** * 检查数据库里是否已经存在该视频信息,如果没有则添加,如果有则更新 * * @param video * @return */ private void check(Video video) { List<Video> videos = LitePal.where("path = ?", video.getPath()).find(Video.class); if (videos.size() > 0) { video.update(video.getId()); } else { video.save(); } }