Code example for SQLiteQueryBuilder

Methods: appendWhere, query, setDistinct, setTables

0
		return true; 
	} 
	 
	@Override 
	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
		SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
		int uriType = sURIMatcher.match(uri);
		 
	    switch (uriType) {
	    case COLLECTION_FROM_ALL_FOLDERS: // Collection from All Folders
	    	queryBuilder.setTables("collection INNER JOIN games ON collection.game_id = games._id " +
	    			"LEFT JOIN consoles ON games.console_id = consoles._id"); 
	    	queryBuilder.setDistinct(true);
	    	break; 
	    case COLLECTION_FROM_OWNED_FOLDERS: // Collection from Owned Folders
	    	queryBuilder.setTables("collection INNER JOIN games ON collection.game_id = games._id " +
    			"INNER JOIN folders ON collection.folder_id = folders._id " + 
				"LEFT JOIN consoles ON games.console_id = consoles._id"); 
    		queryBuilder.appendWhere("is_owned = 1");
    		queryBuilder.setDistinct(true);
	    	break; 
	    case COLLECTION_FROM_SPECIFIC_FOLDER: // Collection from Specific Folder
	    	queryBuilder.setTables("collection INNER JOIN games ON collection.game_id = games._id " +
				"LEFT JOIN consoles ON games.console_id = consoles._id"); 
    		queryBuilder.appendWhere("folder_id = " + uri.getLastPathSegment());
	    	break; 
	    case COLLECTION_FOR_SPECIFIC_GAME: // Collection for Specific Game
	    	queryBuilder.setTables("collection INNER JOIN folders ON collection.folder_id = folders._id");
	    	queryBuilder.appendWhere("game_id = " + uri.getLastPathSegment());
	    	break; 
	    case FOLDERS_ALL: // All Folders
	    	queryBuilder.setTables("folders");
	    	break; 
	    case FOLDERS_OWNED: // Owned Folders
	    	queryBuilder.setTables("folders");
	    	queryBuilder.appendWhere("is_owned = 1");
	    	break; 
	    case FOLDERS_SPECIFIC: // Individual Folder
	    	queryBuilder.setTables("folders");
	    	queryBuilder.appendWhere(_ID + " = " + uri.getLastPathSegment());
	    	break; 
	    case GAME_SPECIFIC: // Individual Game
	    	queryBuilder.setTables("games");
	    	 
	    	try { 
	    		long gameId = Long.parseLong(uri.getLastPathSegment());
	    		queryBuilder.appendWhere(_ID + " = " + gameId);
	    	} catch (NumberFormatException n) {
	    		queryBuilder.appendWhere("rfgid = '" + uri.getLastPathSegment() + "'");
	    	} 
	    	break; 
	    default: 
	        throw new IllegalArgumentException("Unknown URI");
	    } 
		 
	    Cursor cursor = queryBuilder.query(rfgData.getReadableDatabase(),
	            projection, selection, selectionArgs, null, null, sortOrder);
	    cursor.setNotificationUri(getContext().getContentResolver(), uri);
	    return cursor;
	}