From e7be6bda2cd3f1e93f306e298a6a8c523a607373 Mon Sep 17 00:00:00 2001 From: Vasu Nori Date: Sun, 10 Oct 2010 14:58:08 -0700 Subject: [PATCH] bug:3082865 don't use IN to construct sql to delete downloads DownloadManager should not use IN clause when constructing SQL to delete downloaded files Dowload app. Lexer code in Download app doesn't know how to parse it. Real fix is to fix Lexer. But seriously real fix is to get rid of this complexity - that I am planning for next version. also, the following 2 are identical, in terms of SQL performance WHERE _id IN (?, ?, ?) WHERE (_id = ? OR _id = ? OR _id = ?) Change-Id: Icca659a17c412247a193879e8d2f34e1b43ec9e5 --- core/java/android/app/DownloadManager.java | 27 +++++++++------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/core/java/android/app/DownloadManager.java b/core/java/android/app/DownloadManager.java index 6256303cfacc0..00d8ae3ec7b03 100644 --- a/core/java/android/app/DownloadManager.java +++ b/core/java/android/app/DownloadManager.java @@ -28,6 +28,7 @@ import android.os.Environment; import android.os.ParcelFileDescriptor; import android.provider.BaseColumns; import android.provider.Downloads; +import android.util.Log; import android.util.Pair; import java.io.File; @@ -53,6 +54,8 @@ import java.util.Set; * download in a notification or from the downloads UI. */ public class DownloadManager { + private static final String TAG = "DownloadManager"; + /** * An identifier for a particular download, unique across the system. Clients use this ID to * make subsequent calls related to the download. @@ -748,20 +751,11 @@ public class DownloadManager { * @return the number of downloads actually removed */ public int remove(long... ids) { - StringBuilder whereClause = new StringBuilder(); - String[] whereArgs = new String[ids.length]; - - whereClause.append(Downloads.Impl._ID + " IN ("); - for (int i = 0; i < ids.length; i++) { - if (i > 0) { - whereClause.append(","); - } - whereClause.append("?"); - whereArgs[i] = Long.toString(ids[i]); + if (ids == null || ids.length == 0) { + // called with nothing to remove! + throw new IllegalArgumentException("input param 'ids' can't be null"); } - whereClause.append(")"); - - return mResolver.delete(mBaseUri, whereClause.toString(), whereArgs); + return mResolver.delete(mBaseUri, getWhereClauseForIds(ids), getWhereArgsForIds(ids)); } /** @@ -828,12 +822,13 @@ public class DownloadManager { */ static String getWhereClauseForIds(long[] ids) { StringBuilder whereClause = new StringBuilder(); - whereClause.append(Downloads.Impl._ID + " IN ("); + whereClause.append("("); for (int i = 0; i < ids.length; i++) { if (i > 0) { - whereClause.append(","); + whereClause.append("OR "); } - whereClause.append("?"); + whereClause.append(Downloads.Impl._ID); + whereClause.append(" = ? "); } whereClause.append(")"); return whereClause.toString();