Fixed: Search queries with ' caused crash

This commit is contained in:
daniel oeh 2013-04-07 12:14:49 +02:00
parent 40ab4e415e
commit 02f3cdfb42

View File

@ -6,6 +6,7 @@ import java.util.List;
import android.content.ContentValues; import android.content.ContentValues;
import android.content.Context; import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.MergeCursor; import android.database.MergeCursor;
import android.database.SQLException; import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
@ -212,7 +213,6 @@ public class PodDBAdapter {
public static final int IDX_FI_EXTRA_CONTENT_ENCODED = 2; public static final int IDX_FI_EXTRA_CONTENT_ENCODED = 2;
public static final int IDX_FI_EXTRA_FEED = 3; public static final int IDX_FI_EXTRA_FEED = 3;
public PodDBAdapter(Context c) { public PodDBAdapter(Context c) {
this.context = c; this.context = c;
helper = new PodDBHelper(context, DATABASE_NAME, null, DATABASE_VERSION); helper = new PodDBHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
@ -656,6 +656,18 @@ public class PodDBAdapter {
return image; return image;
} }
/**
* Uses DatabaseUtils to escape a search query and removes ' at the
* beginning and the end of the string returned by the escape method.
*/
private String prepareSearchQuery(String query) {
StringBuilder builder = new StringBuilder();
DatabaseUtils.appendEscapedSQLString(builder, query);
builder.deleteCharAt(0);
builder.deleteCharAt(builder.length() - 1);
return builder.toString();
}
/** /**
* Searches for the given query in the description of all items or the items * Searches for the given query in the description of all items or the items
* of a specified feed. * of a specified feed.
@ -666,13 +678,15 @@ public class PodDBAdapter {
if (feed != null) { if (feed != null) {
// search items in specific feed // search items in specific feed
return db.query(TABLE_NAME_FEED_ITEMS, SEL_FI_EXTRA, KEY_FEED return db.query(TABLE_NAME_FEED_ITEMS, SEL_FI_EXTRA, KEY_FEED
+ "=? AND " + KEY_DESCRIPTION + " LIKE '%" + query + "%'", new String[] { + "=? AND " + KEY_DESCRIPTION + " LIKE '%"
String.valueOf(feed.getId()) }, null, null, null); + prepareSearchQuery(query) + "%'",
new String[] { String.valueOf(feed.getId()) }, null, null,
null);
} else { } else {
// search through all items // search through all items
return db.query(TABLE_NAME_FEED_ITEMS, SEL_FI_EXTRA, return db.query(TABLE_NAME_FEED_ITEMS, SEL_FI_EXTRA,
KEY_DESCRIPTION + " LIKE '%" + query + "%'", null, null, KEY_DESCRIPTION + " LIKE '%" + prepareSearchQuery(query)
null, null); + "%'", null, null, null, null);
} }
} }
@ -686,14 +700,16 @@ public class PodDBAdapter {
if (feed != null) { if (feed != null) {
// search items in specific feed // search items in specific feed
return db.query(TABLE_NAME_FEED_ITEMS, SEL_FI_EXTRA, KEY_FEED return db.query(TABLE_NAME_FEED_ITEMS, SEL_FI_EXTRA, KEY_FEED
+ "=? AND " + KEY_CONTENT_ENCODED + " LIKE '%" + query + "%'", + "=? AND " + KEY_CONTENT_ENCODED + " LIKE '%"
new String[] { String.valueOf(feed.getId())}, null, + prepareSearchQuery(query) + "%'",
null, null); new String[] { String.valueOf(feed.getId()) }, null, null,
null);
} else { } else {
// search through all items // search through all items
return db.query(TABLE_NAME_FEED_ITEMS, SEL_FI_EXTRA, return db.query(TABLE_NAME_FEED_ITEMS, SEL_FI_EXTRA,
KEY_CONTENT_ENCODED + " LIKE '%" + query + "%'", null, KEY_CONTENT_ENCODED + " LIKE '%"
null, null, null); + prepareSearchQuery(query) + "%'", null, null,
null, null);
} }
} }