Jezor Jezor - 3 months ago 15
Java Question

Is there any reason for count() method in Sugar ORM to take String[] parameter instead of String...?

I'm developing a simple quiz solving app and in the database there's many-to-one relation between the questions and the quizzes, so that a single quiz can have multiple questions assigned to it.

To get every question of a quiz I'm simply calling:

Question.find(Question.class, "quiz = ?", String.valueOf(quiz.getId()));


Sometimes though, I would like to know how many questions there are, I thought I can just do:

Question.count(Question.class, "quiz = ?", String.valueOf(quiz.getId()));


Unfortunately, unlike
find()
,
SugarRecord.count()
method accepts array of
String
s, so I have to call it like:

Question.count(Question.class, "quiz = ?", new String[]{String.valueOf(quiz.getId())});


Is there any reason for that kind of inconsistency in the API?

Answer

The reason being the usage of overloading. There is a method with groupBy, orderBy and limit. Dumb, dumb, dumb.

public static <T> long count(Class<T> type); 
public static <T> long count(Class<T> type, String whereClause, String[] whereArgs);
public static <T> long count(Class<T> type, String whereClause,
        String[] whereArgs, String groupBy, String orderBy, String limit);

However make your own:

public static <T> long countWhere(Class<T> type, String whereClause, String... whereArgs) {
     return Question.count(type, whereClause, whereArgs);
}
Comments