Is it a good practice to use rawQuery in ContentProvider?

I am using my custom ContentProvider to communicate with sqlite database. I would like to display on a list (using ListFragment), data that comes from two tables (with many to many relation). The only solution I can think of for such case is to use rawQuery. And the questions is, if it is a good practice, or should I solve this in some other way?

Example of tables:

Table A: ID, COLUMN_FROM_A

Table B: ID, COLUMN_FROM_B

Joining table AB: ID, FK_ID_A, FK_ID_B

Example of overridden query method in ContentProvider:

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
    Cursor cursor = null;
    int uriType = URIMatcher.match(uri);
    switch (uriType) {
        case TABLE_A_URI:
            queryBuilder.setTables("TABLE_A");
            cursor = queryBuilder.query(databaseHelper.getReadableDatabase(), projection, selection, selectionArgs, null, null, sortOrder);
            break;
        case TABLE_B_URI:
            queryBuilder.setTables("TABLE_B");
            cursor = queryBuilder.query(databaseHelper.getReadableDatabase(), projection, selection, selectionArgs, null, null, sortOrder);
            break;
        case TABLE_JOIN_A_B_URI:
             cursor = databaseHelper.getReadableDatabase().rawQuery("select a.COLUMN_FORM_A, b.COLUMN_FROM_B from TABLE_A a, TABLE_B b, TABLE_AB ab where ab.FK_ID_A=a.ID and ab.FK_ID_B=b.ID", null);
            break;
        default:
            throw new IllegalArgumentException("Unknown URI");
    }

    cursor.setNotificationUri(getContext().getContentResolver(), uri);
    return cursor;
}

Answers


It's a good and common practice, very appropriate in this case.

I don't foresee any problems, we have used it in many apps.


Need Your Help

When was Javac StringBuilder/StringBuffer optimization introduced?

java javac stringbuilder stringbuffer jdk1.4

I know that Javac compiler is able to transform String concatenation + using StringBuilder/StringBuffer, and I'm curious to know starting from which version this change was introduced?

Stemming algorithm that produces real words

php nlp stemming snowball porter-stemmer

I need to take a paragraph of text and extract from it a list of "tags". Most of this is quite straight forward. However I need some help now stemming the resulting word list to avoid duplicates.