/** * Calculates the average value on a given column. * * <pre> * LitePal.average(Person.class, "age"); * </pre> * * You can also specify a where clause when calculating. * * <pre> * LitePal.where("age > ?", "15").average(Person.class, "age"); * </pre> * * @param modelClass * Which table to query from by class. * @param column * The based on column to calculate. * @return The average value on a given column. */ public double average(Class<?> modelClass, String column) { return average(BaseUtility.changeCase(modelClass.getSimpleName()), column); }
/** * Check if the specified conditions data already exists in the table. * @param modelClass * Which table to check by class. * @param conditions * A filter declaring which data to check. Exactly same use as * {@link Operator#where(String...)}, except null conditions will result in false. * @return Return true if the specified conditions data already exists in the table. * False otherwise. Null conditions will result in false. */ public static <T> boolean isExist(Class<T> modelClass, String... conditions) { return conditions != null && where(conditions).count(modelClass) > 0; }
/** * 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); }
/** * Count the records. * * <pre> * LitePal.count("person"); * </pre> * * This will count all rows in person table.<br> * You can also specify a where clause when counting. * * <pre> * LitePal.where("age > ?", "15").count("person"); * </pre> * * @param tableName * Which table to query from. * @return Count of the specified table. */ public static int count(String tableName) { synchronized (LitePalSupport.class) { FluentQuery cQuery = new FluentQuery(); return cQuery.count(tableName); } }
/** * Calculates the average value on a given column. * * <pre> * LitePal.average("person", "age"); * </pre> * * You can also specify a where clause when calculating. * * <pre> * LitePal.where("age > ?", "15").average("person", "age"); * </pre> * * @param tableName * Which table to query from. * @param column * The based on column to calculate. * @return The average value on a given column. */ public static double average(String tableName, String column) { synchronized (LitePalSupport.class) { FluentQuery cQuery = new FluentQuery(); return cQuery.average(tableName, column); } }
/** * Calculates the maximum value on a given column. The value is returned * with the same data type of the column. * * <pre> * LitePal.max("person", "age", int.class); * </pre> * * You can also specify a where clause when calculating. * * <pre> * LitePal.where("age > ?", "15").max("person", "age", Integer.TYPE); * </pre> * * @param tableName * Which table to query from. * @param columnName * The based on column to calculate. * @param columnType * The type of the based on column. * @return The maximum value on a given column. */ public static <T> T max(String tableName, String columnName, Class<T> columnType) { synchronized (LitePalSupport.class) { FluentQuery cQuery = new FluentQuery(); return cQuery.max(tableName, columnName, columnType); } }
.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") .where("id=? or id=? or id=?", String.valueOf(ids[0]), String.valueOf(ids[1]), String.valueOf(ids[2])).order("id").limit(2).offset(1).findFirst(Book.class); Book lastBook = LitePal .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).findLast(Book.class); assertEquals(2, books.size()); assertTrue(books.get(0).getId() < books.get(1).getId());
@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); for (int i = 0; i < books.size(); i++) { Book book = books.get(i); 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());
@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; }
/** * Calculates the maximum value on a given column. The value is returned * with the same data type of the column. * * <pre> * LitePal.max(Person.class, "age", int.class); * </pre> * * You can also specify a where clause when calculating. * * <pre> * LitePal.where("age > ?", "15").max(Person.class, "age", Integer.TYPE); * </pre> * * @param modelClass * Which table to query from by class. * @param columnName * The based on column to calculate. * @param columnType * The type of the based on column. * @return The maximum value on a given column. */ public <T> T max(Class<?> modelClass, String columnName, Class<T> columnType) { return max(BaseUtility.changeCase(modelClass.getSimpleName()), columnName, columnType); }
/** * Calculates the sum of values on a given column. The value is returned * with the same data type of the column. * * <pre> * LitePal.sum(Person.class, "age", int.class); * </pre> * * You can also specify a where clause when calculating. * * <pre> * LitePal.where("age > ?", "15").sum(Person.class, "age", Integer.TYPE); * </pre> * * @param modelClass * Which table to query from by class. * @param columnName * The based on column to calculate. * @param columnType * The type of the based on column. * @return The sum value on a given column. */ public <T> T sum(Class<?> modelClass, String columnName, Class<T> columnType) { return sum(BaseUtility.changeCase(modelClass.getSimpleName()), columnName, columnType); }
/** * Calculates the minimum value on a given column. The value is returned * with the same data type of the column. * * <pre> * LitePal.min(Person.class, "age", int.class); * </pre> * * You can also specify a where clause when calculating. * * <pre> * LitePal.where("age > ?", "15").min(Person.class, "age", Integer.TYPE); * </pre> * * @param modelClass * Which table to query from by class. * @param columnName * The based on column to calculate. * @param columnType * The type of the based on column. * @return The minimum value on a given column. */ public <T> T min(Class<?> modelClass, String columnName, Class<T> columnType) { return min(BaseUtility.changeCase(modelClass.getSimpleName()), columnName, columnType); }
/** * Calculates the sum of values on a given column. The value is returned * with the same data type of the column. * * <pre> * LitePal.sum("person", "age", int.class); * </pre> * * You can also specify a where clause when calculating. * * <pre> * LitePal.where("age > ?", "15").sum("person", "age", Integer.TYPE); * </pre> * * @param tableName * Which table to query from. * @param columnName * The based on column to calculate. * @param columnType * The type of the based on column. * @return The sum value on a given column. */ public static <T> T sum(String tableName, String columnName, Class<T> columnType) { synchronized (LitePalSupport.class) { FluentQuery cQuery = new FluentQuery(); return cQuery.sum(tableName, columnName, columnType); } }
/** * Calculates the minimum value on a given column. The value is returned * with the same data type of the column. * * <pre> * LitePal.min("person", "age", int.class); * </pre> * * You can also specify a where clause when calculating. * * <pre> * LitePal.where("age > ?", "15").min("person", "age", Integer.TYPE); * </pre> * * @param tableName * Which table to query from. * @param columnName * The based on column to calculate. * @param columnType * The type of the based on column. * @return The minimum value on a given column. */ public static <T> T min(String tableName, String columnName, Class<T> columnType) { synchronized (LitePalSupport.class) { FluentQuery cQuery = new FluentQuery(); return cQuery.min(tableName, columnName, columnType); } }
@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 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);
/** * Finds the last record 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(10).offset(2) * .findLast(Person.class); * </pre> * * Note that the associated models won't be loaded by default considering * the efficiency, but you can do that by using * {@link FluentQuery#findLast(Class, boolean)}. * * @param modelClass * Which table to query and the object type to return. * @return An object with founded data from database, or null. */ public <T> T findLast(Class<T> modelClass) { return findLast(modelClass, false); }
/** * Finds the first record 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(10).offset(2) * .findFirst(Person.class); * </pre> * * Note that the associated models won't be loaded by default considering * the efficiency, but you can do that by using * {@link FluentQuery#findFirst(Class, boolean)}. * * @param modelClass * Which table to query and the object type to return. * @return An object with founded data from database, or null. */ public <T> T findFirst(Class<T> modelClass) { return findFirst(modelClass, false); }
@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); Book lastBook = LitePal.select("bookname", "price").findLast(Book.class); assertNotNull(firstBook); for (int i = 0; i < books.size(); i++) {