Skip to content
This repository was archived by the owner on Feb 11, 2022. It is now read-only.

ContentResolver Query utils #62

Merged
merged 6 commits into from
Jul 23, 2015
Merged

ContentResolver Query utils #62

merged 6 commits into from
Jul 23, 2015

Conversation

danybony
Copy link
Contributor

This PR adds a couple of methods useful for ContentResolver operations, when multiple selection arguments are needed.

ContentResolver operations can use ? for placeholders to be replaced from the content of the operation selectionArgs parameter. Every selectionArg, however, requires a dedicated placeholders.

For example the following code won't work:

String[] selectionArgs = {"1, 2, 3"};
contentResolver.delete(uri, "_id IN (?)", selectionArgs);

while this will:

String[] selectionArgs = {"1, 2, 3"};
contentResolver.delete(uri, "_id IN (?, ?, ?)", selectionArgs);

The two added methods allows to easily create both the selection part (?, ?, ... ?) and the selectionArgs String array

* @param size The number of selection arguments
* @return Example: for size == 3 : "?, ?, ?"
*/
public static String createQuerySelectionPlaceholdersOfSize(int size) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be a good thing to test (if notils has tests)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does, good point 👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the created string is for us with IN operators. Should be mentioned in the comment. It could also contain the operator and the brackets.

For other queries this is not a very common pattern for place holders.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should not contain Query in the name, as the class contains it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 for removing Query from the name and mentioning IN in the comment.
I would avoid adding brackets and operator

@danybony
Copy link
Contributor Author

@friedger @amlcurran method name updated and tests added

* @return Example: for size == 3 : "?, ?, ?"
*/
public static String createSelectionPlaceholdersOfSize(int size) {
String[] questionMarks = new String[size];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about something like:

int sizeOfResult = size * 3 - 2;
char[] result = new char[sizeOfResult];
for (int i = 0; i < size -1; i++) {
  result[i] = '?';
  result[i + 1] = ',';
  result[i + 2] = ' ';
}
if (i > 0) {
  result[sizeOfResult - 1] = '?';
}
return new String(result);

it creates only two objects, instead of size + 5, and is still understandable and simple.

friedger added a commit that referenced this pull request Jul 23, 2015
ContentResolver Query utils
@friedger friedger merged commit 576e03d into master Jul 23, 2015
@friedger friedger deleted the query_utils branch July 23, 2015 10:06
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants