Skip to content

Commit

Permalink
Add SugarRecord.findById function that accepts an array of id strings…
Browse files Browse the repository at this point in the history
… as an argument

Closes #273

Thanks @dominicwong617 for the pull request!
  • Loading branch information
d0minicw0ng authored and whoshuu committed Apr 1, 2015
1 parent 07fd127 commit 7dd20cf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions library/src/main/java/com/orm/SugarRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import com.orm.dsl.Table;
import com.orm.util.NamingHelper;
import com.orm.util.ReflectionUtil;
import com.orm.util.QueryBuilder;

import java.lang.String;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -102,6 +104,11 @@ public static <T> T findById(Class<T> type, Integer id) {
return findById(type, Long.valueOf(id));
}

public static <T> List<T> findById(Class<T> type, String[] ids) {
String whereClause = "id IN (" + QueryBuilder.generatePlaceholders(ids.length) + ")";
return find(type, whereClause, ids);
}

public static <T> Iterator<T> findAll(Class<T> type) {
return findAsIterator(type, null, null, null, null, null);
}
Expand Down
15 changes: 15 additions & 0 deletions library/src/main/java/com/orm/util/QueryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import com.orm.SugarRecord;

import java.lang.RuntimeException;
import java.lang.StringBuilder;

public class QueryBuilder {

public static String getColumnType(Class<?> type) {
Expand Down Expand Up @@ -38,4 +41,16 @@ public static String getColumnType(Class<?> type) {
return "";
}

public static String generatePlaceholders(int numberOfArgs) {
if (numberOfArgs < 1) {
throw new RuntimeException("The number of arguments must be greater than or equal to 1.");
}

StringBuilder stringBuilder = new StringBuilder(numberOfArgs * 2 - 1);
stringBuilder.append("?");
for (int i = 1; i < numberOfArgs; i++) {
stringBuilder.append(",?");
}
return stringBuilder.toString();
}
}

0 comments on commit 7dd20cf

Please sign in to comment.