/** * 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); } }
/** * 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); }
@Override public void onClick(View view) { int result = 0; switch (view.getId()) { case R.id.max_btn1: result = LitePal.max(Singer.class, "age", Integer.TYPE); mResultText.setText(String.valueOf(result)); break; case R.id.max_btn2: try { result = LitePal.where("age < ?", mAgeEdit.getText().toString()).max( Singer.class, "age", Integer.TYPE); mResultText.setText(String.valueOf(result)); } catch (Exception e) { e.printStackTrace(); } break; default: } }
@Override public void run() { synchronized (LitePalSupport.class) { final T t = max(tableName, columnName, columnType); if (executor.getListener() != null) { Operator.getHandler().post(new Runnable() { @Override public void run() { executor.getListener().onFinish(t); } }); } } } };
@Test public void testMax() { int result = LitePal.max(Student.class, "age", Integer.TYPE); int realResult = -100; Cursor cursor = LitePal.findBySQL("select max(age) from " + studentTable); if (cursor.moveToFirst()) { realResult = cursor.getInt(0); } cursor.close(); assertEquals(realResult, result); result = LitePal.where("age < ?", "20").max(studentTable, "age", Integer.TYPE); cursor = LitePal.findBySQL("select max(age) from " + studentTable + " where age < ?", "20"); if (cursor.moveToFirst()) { realResult = cursor.getInt(0); } cursor.close(); assertEquals(realResult, result); }