Code example for SQLiteQueryBuilder

Methods: appendWhere, query, setProjectionMap, setTables

0
    @Override 
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        if (!ensureDb()) return null; 
 
        SQLiteQueryBuilder qBuilder = new SQLiteQueryBuilder();
        String[] defaultProjection = null;
        String groupBy = null;
 
        int uriMatch = sUriMatcher.match(uri);
        // Set up table and default projection 
        switch (uriMatch) {
        case APPS:
        case APPS_UID_GRP:
        case APP_ID:
        case APP_UID:
            qBuilder.setTables(Apps.APPS_LOGS_JOIN);
            qBuilder.setProjectionMap(sAppsProjectionMap);
            defaultProjection = Apps.DEFAULT_PROJECTION;
//            qBuilder.appendWhere("apps.allow!=-1"); // Leave out apps only there for Log purposes 
            groupBy = Apps.TABLE_NAME + "." + Apps._ID;
            sortOrder = sortOrder==null?Apps.DEFAULT_SORT_ORDER:sortOrder;
            sortOrder = sortOrder + (!TextUtils.isEmpty(sortOrder)?", ":"");
            sortOrder = sortOrder + Logs.DATE + " DESC";
            break; 
        case APP_ID_LOGS:
        case APP_UID_LOGS:
        case LOGS:
        case LOGS_APP_ID:
        case LOGS_TYPE:
            qBuilder.setTables(Logs.LOGS_APPS_JOIN);
            qBuilder.setProjectionMap(sLogsProjectionMap);
            defaultProjection = Logs.DEFAULT_PROJECTION;
            sortOrder = sortOrder==null?Logs.DEFAULT_SORT_ORDER:sortOrder;
            break; 
        case APP_COUNT:
        case APP_COUNT_TYPE:
            qBuilder.setTables(Apps.TABLE_NAME);
            defaultProjection = new String[] { "COUNT() as rows" };
            break; 
        default: 
            throw new IllegalArgumentException("Unsupported URI: " + uri);
        } 
 
        // Append a particular item if necessary 
        switch (uriMatch) {
        case APP_ID:
            qBuilder.appendWhere(" apps._id=" + uri.getPathSegments().get(1));
            break; 
        case APP_ID_LOGS:
        case LOGS_APP_ID:
            qBuilder.appendWhere("apps._id=" + uri.getPathSegments().get(1));
            break; 
        case APP_UID:
            qBuilder.appendWhere(" apps.uid=" + uri.getPathSegments().get(2));
            break; 
        case APP_UID_LOGS:
            qBuilder.appendWhere("apps.uid=" + uri.getPathSegments().get(3));
            break; 
        case LOGS_TYPE:
            qBuilder.appendWhere("logs.type=" + uri.getPathSegments().get(2));
            break; 
        case APP_COUNT_TYPE:
            qBuilder.appendWhere("apps.allow=" + uri.getPathSegments().get(2));
        } 
         
        if (uriMatch == APPS_UID_GRP)
        	groupBy = Apps.UID + ", " + Apps.ALLOW;
 
        // TODO: Check columns in incoming projection to make sure they're valid 
        projection = projection==null?defaultProjection:projection;
        Cursor c = null;
        try { 
            c = qBuilder.query(mDb,
                    projection,
                    selection,
                    selectionArgs,
                    groupBy,
                    null,