1
0
Fork 0
mirror of https://github.com/MatomoCamp/matomocamp-companion-android.git synced 2024-09-19 16:13:46 +02:00

Re-indented SQL queries in DatabaseManager

for better readability
This commit is contained in:
Christophe Beyls 2016-01-05 22:17:23 +01:00
parent 1d410fb2b8
commit b70cd4d0bd

View file

@ -23,6 +23,7 @@ import android.net.Uri;
import android.provider.BaseColumns; import android.provider.BaseColumns;
import android.support.v4.content.LocalBroadcastManager; import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils; import android.text.TextUtils;
import be.digitalia.fosdem.model.Day; import be.digitalia.fosdem.model.Day;
import be.digitalia.fosdem.model.Event; import be.digitalia.fosdem.model.Event;
import be.digitalia.fosdem.model.Link; import be.digitalia.fosdem.model.Link;
@ -34,7 +35,6 @@ import be.digitalia.fosdem.utils.DateUtils;
* Here comes the badass SQL. * Here comes the badass SQL.
* *
* @author Christophe Beyls * @author Christophe Beyls
*
*/ */
public class DatabaseManager { public class DatabaseManager {
@ -111,7 +111,6 @@ public class DatabaseManager {
} }
/** /**
*
* @return The last update time in milliseconds since EPOCH, or -1 if not available. * @return The last update time in milliseconds since EPOCH, or -1 if not available.
*/ */
public long getLastUpdateTime() { public long getLastUpdateTime() {
@ -119,7 +118,6 @@ public class DatabaseManager {
} }
/** /**
*
* @return The time identifier of the current version of the database. * @return The time identifier of the current version of the database.
*/ */
public String getLastModifiedTag() { public String getLastModifiedTag() {
@ -322,7 +320,6 @@ public class DatabaseManager {
} }
/** /**
*
* @return The Days the events span to. * @return The Days the events span to.
*/ */
public List<Day> getDays() { public List<Day> getDays() {
@ -377,8 +374,11 @@ public class DatabaseManager {
public Cursor getTracks(Day day) { public Cursor getTracks(Day day) {
String[] selectionArgs = new String[]{String.valueOf(day.getIndex())}; String[] selectionArgs = new String[]{String.valueOf(day.getIndex())};
Cursor cursor = helper.getReadableDatabase().rawQuery( Cursor cursor = helper.getReadableDatabase().rawQuery(
"SELECT t.id AS _id, t.name, t.type" + " FROM " + DatabaseHelper.TRACKS_TABLE_NAME + " t" + " JOIN " + DatabaseHelper.EVENTS_TABLE_NAME "SELECT t.id AS _id, t.name, t.type" + " FROM " + DatabaseHelper.TRACKS_TABLE_NAME + " t"
+ " e ON t.id = e.track_id" + " WHERE e.day_index = ?" + " GROUP BY t.id" + " ORDER BY t.name ASC", selectionArgs); + " JOIN " + DatabaseHelper.EVENTS_TABLE_NAME + " e ON t.id = e.track_id"
+ " WHERE e.day_index = ?"
+ " GROUP BY t.id"
+ " ORDER BY t.name ASC", selectionArgs);
cursor.setNotificationUri(context.getContentResolver(), URI_EVENTS); cursor.setNotificationUri(context.getContentResolver(), URI_EVENTS);
return cursor; return cursor;
} }
@ -406,29 +406,16 @@ public class DatabaseManager {
*/ */
public Event getEvent(long id) { public Event getEvent(long id) {
String[] selectionArgs = new String[]{String.valueOf(id)}; String[] selectionArgs = new String[]{String.valueOf(id)};
Cursor cursor = helper Cursor cursor = helper.getReadableDatabase().rawQuery(
.getReadableDatabase()
.rawQuery(
"SELECT e.id AS _id, e.start_time, e.end_time, e.room_name, e.slug, et.title, et.subtitle, e.abstract, e.description, GROUP_CONCAT(p.name, ', '), e.day_index, d.date, t.name, t.type" "SELECT e.id AS _id, e.start_time, e.end_time, e.room_name, e.slug, et.title, et.subtitle, e.abstract, e.description, GROUP_CONCAT(p.name, ', '), e.day_index, d.date, t.name, t.type"
+ " FROM " + " FROM " + DatabaseHelper.EVENTS_TABLE_NAME + " e"
+ DatabaseHelper.EVENTS_TABLE_NAME + " JOIN " + DatabaseHelper.EVENTS_TITLES_TABLE_NAME + " et ON e.id = et.rowid"
+ " e" + " JOIN " + DatabaseHelper.DAYS_TABLE_NAME + " d ON e.day_index = d._index"
+ " JOIN " + " JOIN " + DatabaseHelper.TRACKS_TABLE_NAME + " t ON e.track_id = t.id"
+ DatabaseHelper.EVENTS_TITLES_TABLE_NAME + " LEFT JOIN " + DatabaseHelper.EVENTS_PERSONS_TABLE_NAME + " ep ON e.id = ep.event_id"
+ " et ON e.id = et.rowid" + " LEFT JOIN " + DatabaseHelper.PERSONS_TABLE_NAME + " p ON ep.person_id = p.rowid"
+ " JOIN " + " WHERE e.id = ?"
+ DatabaseHelper.DAYS_TABLE_NAME + " GROUP BY e.id", selectionArgs);
+ " d ON e.day_index = d._index"
+ " JOIN "
+ DatabaseHelper.TRACKS_TABLE_NAME
+ " t ON e.track_id = t.id"
+ " LEFT JOIN "
+ DatabaseHelper.EVENTS_PERSONS_TABLE_NAME
+ " ep ON e.id = ep.event_id"
+ " LEFT JOIN "
+ DatabaseHelper.PERSONS_TABLE_NAME
+ " p ON ep.person_id = p.rowid"
+ " WHERE e.id = ?" + " GROUP BY e.id", selectionArgs);
try { try {
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
return toEvent(cursor); return toEvent(cursor);
@ -449,33 +436,18 @@ public class DatabaseManager {
*/ */
public Cursor getEvents(Day day, Track track) { public Cursor getEvents(Day day, Track track) {
String[] selectionArgs = new String[]{String.valueOf(day.getIndex()), track.getName(), track.getType().name()}; String[] selectionArgs = new String[]{String.valueOf(day.getIndex()), track.getName(), track.getType().name()};
Cursor cursor = helper Cursor cursor = helper.getReadableDatabase().rawQuery(
.getReadableDatabase()
.rawQuery(
"SELECT e.id AS _id, e.start_time, e.end_time, e.room_name, e.slug, et.title, et.subtitle, e.abstract, e.description, GROUP_CONCAT(p.name, ', '), e.day_index, d.date, t.name, t.type, b.event_id" "SELECT e.id AS _id, e.start_time, e.end_time, e.room_name, e.slug, et.title, et.subtitle, e.abstract, e.description, GROUP_CONCAT(p.name, ', '), e.day_index, d.date, t.name, t.type, b.event_id"
+ " FROM " + " FROM " + DatabaseHelper.EVENTS_TABLE_NAME + " e"
+ DatabaseHelper.EVENTS_TABLE_NAME + " JOIN " + DatabaseHelper.EVENTS_TITLES_TABLE_NAME + " et ON e.id = et.rowid"
+ " e" + " JOIN " + DatabaseHelper.DAYS_TABLE_NAME + " d ON e.day_index = d._index"
+ " JOIN " + " JOIN " + DatabaseHelper.TRACKS_TABLE_NAME + " t ON e.track_id = t.id"
+ DatabaseHelper.EVENTS_TITLES_TABLE_NAME + " LEFT JOIN " + DatabaseHelper.EVENTS_PERSONS_TABLE_NAME + " ep ON e.id = ep.event_id"
+ " et ON e.id = et.rowid" + " LEFT JOIN " + DatabaseHelper.PERSONS_TABLE_NAME + " p ON ep.person_id = p.rowid"
+ " JOIN " + " LEFT JOIN " + DatabaseHelper.BOOKMARKS_TABLE_NAME + " b ON e.id = b.event_id"
+ DatabaseHelper.DAYS_TABLE_NAME
+ " d ON e.day_index = d._index"
+ " JOIN "
+ DatabaseHelper.TRACKS_TABLE_NAME
+ " t ON e.track_id = t.id"
+ " LEFT JOIN "
+ DatabaseHelper.EVENTS_PERSONS_TABLE_NAME
+ " ep ON e.id = ep.event_id"
+ " LEFT JOIN "
+ DatabaseHelper.PERSONS_TABLE_NAME
+ " p ON ep.person_id = p.rowid"
+ " LEFT JOIN "
+ DatabaseHelper.BOOKMARKS_TABLE_NAME
+ " b ON e.id = b.event_id"
+ " WHERE e.day_index = ? AND t.name = ? AND t.type = ?" + " WHERE e.day_index = ? AND t.name = ? AND t.type = ?"
+ " GROUP BY e.id" + " ORDER BY e.start_time ASC", selectionArgs); + " GROUP BY e.id"
+ " ORDER BY e.start_time ASC", selectionArgs);
cursor.setNotificationUri(context.getContentResolver(), URI_EVENTS); cursor.setNotificationUri(context.getContentResolver(), URI_EVENTS);
return cursor; return cursor;
} }
@ -483,14 +455,10 @@ public class DatabaseManager {
/** /**
* Returns the events in the specified time window, ordered by start time. All parameters are optional but at least one must be provided. * Returns the events in the specified time window, ordered by start time. All parameters are optional but at least one must be provided.
* *
* @param minStartTime * @param minStartTime Minimum start time, or -1
* Minimum start time, or -1 * @param maxStartTime Maximum start time, or -1
* @param maxStartTime * @param minEndTime Minimum end time, or -1
* Maximum start time, or -1 * @param ascending If true, order results from start time ascending, else order from start time descending
* @param minEndTime
* Minimum end time, or -1
* @param ascending
* If true, order results from start time ascending, else order from start time descending
* @return * @return
*/ */
public Cursor getEvents(long minStartTime, long maxStartTime, long minEndTime, boolean ascending) { public Cursor getEvents(long minStartTime, long maxStartTime, long minEndTime, boolean ascending) {
@ -520,35 +488,19 @@ public class DatabaseManager {
} }
String ascendingString = ascending ? "ASC" : "DESC"; String ascendingString = ascending ? "ASC" : "DESC";
Cursor cursor = helper Cursor cursor = helper.getReadableDatabase().rawQuery(
.getReadableDatabase()
.rawQuery(
"SELECT e.id AS _id, e.start_time, e.end_time, e.room_name, e.slug, et.title, et.subtitle, e.abstract, e.description, GROUP_CONCAT(p.name, ', '), e.day_index, d.date, t.name, t.type, b.event_id" "SELECT e.id AS _id, e.start_time, e.end_time, e.room_name, e.slug, et.title, et.subtitle, e.abstract, e.description, GROUP_CONCAT(p.name, ', '), e.day_index, d.date, t.name, t.type, b.event_id"
+ " FROM " + " FROM " + DatabaseHelper.EVENTS_TABLE_NAME + " e"
+ DatabaseHelper.EVENTS_TABLE_NAME + " JOIN " + DatabaseHelper.EVENTS_TITLES_TABLE_NAME + " et ON e.id = et.rowid"
+ " e" + " JOIN " + DatabaseHelper.DAYS_TABLE_NAME + " d ON e.day_index = d._index"
+ " JOIN " + " JOIN " + DatabaseHelper.TRACKS_TABLE_NAME + " t ON e.track_id = t.id"
+ DatabaseHelper.EVENTS_TITLES_TABLE_NAME + " LEFT JOIN " + DatabaseHelper.EVENTS_PERSONS_TABLE_NAME + " ep ON e.id = ep.event_id"
+ " et ON e.id = et.rowid" + " LEFT JOIN " + DatabaseHelper.PERSONS_TABLE_NAME + " p ON ep.person_id = p.rowid"
+ " JOIN " + " LEFT JOIN " + DatabaseHelper.BOOKMARKS_TABLE_NAME + " b ON e.id = b.event_id"
+ DatabaseHelper.DAYS_TABLE_NAME + " WHERE " + whereCondition.toString()
+ " d ON e.day_index = d._index"
+ " JOIN "
+ DatabaseHelper.TRACKS_TABLE_NAME
+ " t ON e.track_id = t.id"
+ " LEFT JOIN "
+ DatabaseHelper.EVENTS_PERSONS_TABLE_NAME
+ " ep ON e.id = ep.event_id"
+ " LEFT JOIN "
+ DatabaseHelper.PERSONS_TABLE_NAME
+ " p ON ep.person_id = p.rowid"
+ " LEFT JOIN "
+ DatabaseHelper.BOOKMARKS_TABLE_NAME
+ " b ON e.id = b.event_id"
+ " WHERE "
+ whereCondition.toString()
+ " GROUP BY e.id" + " GROUP BY e.id"
+ " ORDER BY e.start_time " + ascendingString, selectionArgs.toArray(new String[selectionArgs.size()])); + " ORDER BY e.start_time " + ascendingString,
selectionArgs.toArray(new String[selectionArgs.size()]));
cursor.setNotificationUri(context.getContentResolver(), URI_EVENTS); cursor.setNotificationUri(context.getContentResolver(), URI_EVENTS);
return cursor; return cursor;
} }
@ -561,34 +513,19 @@ public class DatabaseManager {
*/ */
public Cursor getEvents(Person person) { public Cursor getEvents(Person person) {
String[] selectionArgs = new String[]{String.valueOf(person.getId())}; String[] selectionArgs = new String[]{String.valueOf(person.getId())};
Cursor cursor = helper Cursor cursor = helper.getReadableDatabase().rawQuery(
.getReadableDatabase()
.rawQuery(
"SELECT e.id AS _id, e.start_time, e.end_time, e.room_name, e.slug, et.title, et.subtitle, e.abstract, e.description, GROUP_CONCAT(p.name, ', '), e.day_index, d.date, t.name, t.type, b.event_id" "SELECT e.id AS _id, e.start_time, e.end_time, e.room_name, e.slug, et.title, et.subtitle, e.abstract, e.description, GROUP_CONCAT(p.name, ', '), e.day_index, d.date, t.name, t.type, b.event_id"
+ " FROM " + " FROM " + DatabaseHelper.EVENTS_TABLE_NAME + " e"
+ DatabaseHelper.EVENTS_TABLE_NAME + " JOIN " + DatabaseHelper.EVENTS_TITLES_TABLE_NAME + " et ON e.id = et.rowid"
+ " e" + " JOIN " + DatabaseHelper.DAYS_TABLE_NAME + " d ON e.day_index = d._index"
+ " JOIN " + " JOIN " + DatabaseHelper.TRACKS_TABLE_NAME + " t ON e.track_id = t.id"
+ DatabaseHelper.EVENTS_TITLES_TABLE_NAME + " LEFT JOIN " + DatabaseHelper.EVENTS_PERSONS_TABLE_NAME + " ep ON e.id = ep.event_id"
+ " et ON e.id = et.rowid" + " LEFT JOIN " + DatabaseHelper.PERSONS_TABLE_NAME + " p ON ep.person_id = p.rowid"
+ " JOIN " + " LEFT JOIN " + DatabaseHelper.BOOKMARKS_TABLE_NAME + " b ON e.id = b.event_id"
+ DatabaseHelper.DAYS_TABLE_NAME + " JOIN " + DatabaseHelper.EVENTS_PERSONS_TABLE_NAME + " ep2 ON e.id = ep2.event_id"
+ " d ON e.day_index = d._index" + " WHERE ep2.person_id = ?"
+ " JOIN " + " GROUP BY e.id"
+ DatabaseHelper.TRACKS_TABLE_NAME + " ORDER BY e.start_time ASC", selectionArgs);
+ " t ON e.track_id = t.id"
+ " LEFT JOIN "
+ DatabaseHelper.EVENTS_PERSONS_TABLE_NAME
+ " ep ON e.id = ep.event_id"
+ " LEFT JOIN "
+ DatabaseHelper.PERSONS_TABLE_NAME
+ " p ON ep.person_id = p.rowid"
+ " LEFT JOIN "
+ DatabaseHelper.BOOKMARKS_TABLE_NAME
+ " b ON e.id = b.event_id"
+ " JOIN "
+ DatabaseHelper.EVENTS_PERSONS_TABLE_NAME
+ " ep2 ON e.id = ep2.event_id" + " WHERE ep2.person_id = ?" + " GROUP BY e.id" + " ORDER BY e.start_time ASC", selectionArgs);
cursor.setNotificationUri(context.getContentResolver(), URI_EVENTS); cursor.setNotificationUri(context.getContentResolver(), URI_EVENTS);
return cursor; return cursor;
} }
@ -596,8 +533,7 @@ public class DatabaseManager {
/** /**
* Returns the bookmarks. * Returns the bookmarks.
* *
* @param minStartTime * @param minStartTime When positive, only return the events starting after this time.
* When positive, only return the events starting after this time.
* @return A cursor to Events * @return A cursor to Events
*/ */
public Cursor getBookmarks(long minStartTime) { public Cursor getBookmarks(long minStartTime) {
@ -611,31 +547,18 @@ public class DatabaseManager {
selectionArgs = null; selectionArgs = null;
} }
Cursor cursor = helper Cursor cursor = helper.getReadableDatabase().rawQuery(
.getReadableDatabase()
.rawQuery(
"SELECT e.id AS _id, e.start_time, e.end_time, e.room_name, e.slug, et.title, et.subtitle, e.abstract, e.description, GROUP_CONCAT(p.name, ', '), e.day_index, d.date, t.name, t.type, 1" "SELECT e.id AS _id, e.start_time, e.end_time, e.room_name, e.slug, et.title, et.subtitle, e.abstract, e.description, GROUP_CONCAT(p.name, ', '), e.day_index, d.date, t.name, t.type, 1"
+ " FROM " + " FROM " + DatabaseHelper.BOOKMARKS_TABLE_NAME + " b"
+ DatabaseHelper.BOOKMARKS_TABLE_NAME + " JOIN " + DatabaseHelper.EVENTS_TABLE_NAME + " e ON b.event_id = e.id"
+ " b" + " JOIN " + DatabaseHelper.EVENTS_TITLES_TABLE_NAME + " et ON e.id = et.rowid"
+ " JOIN " + " JOIN " + DatabaseHelper.DAYS_TABLE_NAME + " d ON e.day_index = d._index"
+ DatabaseHelper.EVENTS_TABLE_NAME + " JOIN " + DatabaseHelper.TRACKS_TABLE_NAME + " t ON e.track_id = t.id"
+ " e ON b.event_id = e.id" + " LEFT JOIN " + DatabaseHelper.EVENTS_PERSONS_TABLE_NAME + " ep ON e.id = ep.event_id"
+ " JOIN " + " LEFT JOIN " + DatabaseHelper.PERSONS_TABLE_NAME + " p ON ep.person_id = p.rowid"
+ DatabaseHelper.EVENTS_TITLES_TABLE_NAME + whereCondition
+ " et ON e.id = et.rowid" + " GROUP BY e.id"
+ " JOIN " + " ORDER BY e.start_time ASC", selectionArgs);
+ DatabaseHelper.DAYS_TABLE_NAME
+ " d ON e.day_index = d._index"
+ " JOIN "
+ DatabaseHelper.TRACKS_TABLE_NAME
+ " t ON e.track_id = t.id"
+ " LEFT JOIN "
+ DatabaseHelper.EVENTS_PERSONS_TABLE_NAME
+ " ep ON e.id = ep.event_id"
+ " LEFT JOIN "
+ DatabaseHelper.PERSONS_TABLE_NAME
+ " p ON ep.person_id = p.rowid" + whereCondition + " GROUP BY e.id" + " ORDER BY e.start_time ASC", selectionArgs);
cursor.setNotificationUri(context.getContentResolver(), URI_EVENTS); cursor.setNotificationUri(context.getContentResolver(), URI_EVENTS);
return cursor; return cursor;
} }
@ -650,80 +573,70 @@ public class DatabaseManager {
public Cursor getSearchResults(String query) { public Cursor getSearchResults(String query) {
final String matchQuery = query + "*"; final String matchQuery = query + "*";
String[] selectionArgs = new String[]{matchQuery, "%" + query + "%", matchQuery}; String[] selectionArgs = new String[]{matchQuery, "%" + query + "%", matchQuery};
Cursor cursor = helper Cursor cursor = helper.getReadableDatabase().rawQuery(
.getReadableDatabase()
.rawQuery(
"SELECT e.id AS _id, e.start_time, e.end_time, e.room_name, e.slug, et.title, et.subtitle, e.abstract, e.description, GROUP_CONCAT(p.name, ', '), e.day_index, d.date, t.name, t.type, b.event_id" "SELECT e.id AS _id, e.start_time, e.end_time, e.room_name, e.slug, et.title, et.subtitle, e.abstract, e.description, GROUP_CONCAT(p.name, ', '), e.day_index, d.date, t.name, t.type, b.event_id"
+ " FROM " + " FROM " + DatabaseHelper.EVENTS_TABLE_NAME + " e"
+ DatabaseHelper.EVENTS_TABLE_NAME + " JOIN " + DatabaseHelper.EVENTS_TITLES_TABLE_NAME + " et ON e.id = et.rowid"
+ " e" + " JOIN " + DatabaseHelper.DAYS_TABLE_NAME + " d ON e.day_index = d._index"
+ " JOIN " + " JOIN " + DatabaseHelper.TRACKS_TABLE_NAME + " t ON e.track_id = t.id"
+ DatabaseHelper.EVENTS_TITLES_TABLE_NAME + " LEFT JOIN " + DatabaseHelper.EVENTS_PERSONS_TABLE_NAME + " ep ON e.id = ep.event_id"
+ " et ON e.id = et.rowid" + " LEFT JOIN " + DatabaseHelper.PERSONS_TABLE_NAME + " p ON ep.person_id = p.rowid"
+ " JOIN " + " LEFT JOIN " + DatabaseHelper.BOOKMARKS_TABLE_NAME + " b ON e.id = b.event_id"
+ DatabaseHelper.DAYS_TABLE_NAME
+ " d ON e.day_index = d._index"
+ " JOIN "
+ DatabaseHelper.TRACKS_TABLE_NAME
+ " t ON e.track_id = t.id"
+ " LEFT JOIN "
+ DatabaseHelper.EVENTS_PERSONS_TABLE_NAME
+ " ep ON e.id = ep.event_id"
+ " LEFT JOIN "
+ DatabaseHelper.PERSONS_TABLE_NAME
+ " p ON ep.person_id = p.rowid"
+ " LEFT JOIN "
+ DatabaseHelper.BOOKMARKS_TABLE_NAME
+ " b ON e.id = b.event_id"
+ " WHERE e.id IN ( " + " WHERE e.id IN ( "
+ "SELECT rowid" + "SELECT rowid"
+ " FROM " + " FROM " + DatabaseHelper.EVENTS_TITLES_TABLE_NAME
+ DatabaseHelper.EVENTS_TITLES_TABLE_NAME + " WHERE " + DatabaseHelper.EVENTS_TITLES_TABLE_NAME + " MATCH ?"
+ " WHERE "
+ DatabaseHelper.EVENTS_TITLES_TABLE_NAME
+ " MATCH ?"
+ " UNION " + " UNION "
+ "SELECT e.id" + "SELECT e.id"
+ " FROM " + " FROM " + DatabaseHelper.EVENTS_TABLE_NAME + " e"
+ DatabaseHelper.EVENTS_TABLE_NAME + " JOIN " + DatabaseHelper.TRACKS_TABLE_NAME + " t ON e.track_id = t.id"
+ " e"
+ " JOIN "
+ DatabaseHelper.TRACKS_TABLE_NAME
+ " t ON e.track_id = t.id"
+ " WHERE t.name LIKE ?" + " WHERE t.name LIKE ?"
+ " UNION " + " UNION "
+ "SELECT ep.event_id" + "SELECT ep.event_id"
+ " FROM " + " FROM " + DatabaseHelper.EVENTS_PERSONS_TABLE_NAME + " ep"
+ DatabaseHelper.EVENTS_PERSONS_TABLE_NAME + " JOIN " + DatabaseHelper.PERSONS_TABLE_NAME + " p ON ep.person_id = p.rowid"
+ " ep" + " WHERE p.name MATCH ?"
+ " JOIN " + " )"
+ DatabaseHelper.PERSONS_TABLE_NAME + " GROUP BY e.id"
+ " p ON ep.person_id = p.rowid" + " WHERE p.name MATCH ?" + " )" + " GROUP BY e.id" + " ORDER BY e.start_time ASC", + " ORDER BY e.start_time ASC", selectionArgs);
selectionArgs);
cursor.setNotificationUri(context.getContentResolver(), URI_EVENTS); cursor.setNotificationUri(context.getContentResolver(), URI_EVENTS);
return cursor; return cursor;
} }
/** /**
* Method called by SearchSuggestionProvider to return search results in the format expected by the search framework. * Method called by SearchSuggestionProvider to return search results in the format expected by the search framework.
*
*/ */
public Cursor getSearchSuggestionResults(String query, int limit) { public Cursor getSearchSuggestionResults(String query, int limit) {
final String matchQuery = query + "*"; final String matchQuery = query + "*";
String[] selectionArgs = new String[]{matchQuery, "%" + query + "%", matchQuery, String.valueOf(limit)}; String[] selectionArgs = new String[]{matchQuery, "%" + query + "%", matchQuery, String.valueOf(limit)};
// Query is similar to getSearchResults but returns different columns, does not join the Day table or the Bookmark table and limits the result set. // Query is similar to getSearchResults but returns different columns, does not join the Day table or the Bookmark table and limits the result set.
Cursor cursor = helper.getReadableDatabase().rawQuery( Cursor cursor = helper.getReadableDatabase().rawQuery(
"SELECT e.id AS " + BaseColumns._ID + ", et.title AS " + SearchManager.SUGGEST_COLUMN_TEXT_1 "SELECT e.id AS " + BaseColumns._ID
+ ", IFNULL(GROUP_CONCAT(p.name, ', '), '') || ' - ' || t.name AS " + SearchManager.SUGGEST_COLUMN_TEXT_2 + ", e.id AS " + ", et.title AS " + SearchManager.SUGGEST_COLUMN_TEXT_1
+ SearchManager.SUGGEST_COLUMN_INTENT_DATA + " FROM " + DatabaseHelper.EVENTS_TABLE_NAME + " e" + " JOIN " + ", IFNULL(GROUP_CONCAT(p.name, ', '), '') || ' - ' || t.name AS " + SearchManager.SUGGEST_COLUMN_TEXT_2
+ DatabaseHelper.EVENTS_TITLES_TABLE_NAME + " et ON e.id = et.rowid" + " JOIN " + DatabaseHelper.TRACKS_TABLE_NAME + ", e.id AS " + SearchManager.SUGGEST_COLUMN_INTENT_DATA
+ " t ON e.track_id = t.id" + " LEFT JOIN " + DatabaseHelper.EVENTS_PERSONS_TABLE_NAME + " ep ON e.id = ep.event_id" + " LEFT JOIN " + " FROM " + DatabaseHelper.EVENTS_TABLE_NAME + " e"
+ DatabaseHelper.PERSONS_TABLE_NAME + " p ON ep.person_id = p.rowid" + " WHERE e.id IN ( " + "SELECT rowid" + " FROM " + " JOIN " + DatabaseHelper.EVENTS_TITLES_TABLE_NAME + " et ON e.id = et.rowid"
+ DatabaseHelper.EVENTS_TITLES_TABLE_NAME + " WHERE " + DatabaseHelper.EVENTS_TITLES_TABLE_NAME + " MATCH ?" + " UNION " + " JOIN " + DatabaseHelper.TRACKS_TABLE_NAME + " t ON e.track_id = t.id"
+ "SELECT e.id" + " FROM " + DatabaseHelper.EVENTS_TABLE_NAME + " e" + " JOIN " + DatabaseHelper.TRACKS_TABLE_NAME + " LEFT JOIN " + DatabaseHelper.EVENTS_PERSONS_TABLE_NAME + " ep ON e.id = ep.event_id"
+ " t ON e.track_id = t.id" + " WHERE t.name LIKE ?" + " UNION " + "SELECT ep.event_id" + " FROM " + " LEFT JOIN " + DatabaseHelper.PERSONS_TABLE_NAME + " p ON ep.person_id = p.rowid"
+ DatabaseHelper.EVENTS_PERSONS_TABLE_NAME + " ep" + " JOIN " + DatabaseHelper.PERSONS_TABLE_NAME + " p ON ep.person_id = p.rowid" + " WHERE e.id IN ( "
+ " WHERE p.name MATCH ?" + " )" + " GROUP BY e.id" + " ORDER BY e.start_time ASC LIMIT ?", selectionArgs); + "SELECT rowid"
+ " FROM " + DatabaseHelper.EVENTS_TITLES_TABLE_NAME
+ " WHERE " + DatabaseHelper.EVENTS_TITLES_TABLE_NAME + " MATCH ?"
+ " UNION "
+ "SELECT e.id"
+ " FROM " + DatabaseHelper.EVENTS_TABLE_NAME + " e"
+ " JOIN " + DatabaseHelper.TRACKS_TABLE_NAME + " t ON e.track_id = t.id"
+ " WHERE t.name LIKE ?"
+ " UNION "
+ "SELECT ep.event_id"
+ " FROM " + DatabaseHelper.EVENTS_PERSONS_TABLE_NAME + " ep"
+ " JOIN " + DatabaseHelper.PERSONS_TABLE_NAME + " p ON ep.person_id = p.rowid"
+ " WHERE p.name MATCH ?"
+ " )"
+ " GROUP BY e.id"
+ " ORDER BY e.start_time ASC LIMIT ?", selectionArgs);
return cursor; return cursor;
} }
@ -809,7 +722,9 @@ public class DatabaseManager {
*/ */
public Cursor getPersons() { public Cursor getPersons() {
Cursor cursor = helper.getReadableDatabase().rawQuery( Cursor cursor = helper.getReadableDatabase().rawQuery(
"SELECT rowid AS _id, name" + " FROM " + DatabaseHelper.PERSONS_TABLE_NAME + " ORDER BY name COLLATE NOCASE", null); "SELECT rowid AS _id, name"
+ " FROM " + DatabaseHelper.PERSONS_TABLE_NAME
+ " ORDER BY name COLLATE NOCASE", null);
cursor.setNotificationUri(context.getContentResolver(), URI_EVENTS); cursor.setNotificationUri(context.getContentResolver(), URI_EVENTS);
return cursor; return cursor;
} }
@ -822,8 +737,10 @@ public class DatabaseManager {
public List<Person> getPersons(Event event) { public List<Person> getPersons(Event event) {
String[] selectionArgs = new String[]{String.valueOf(event.getId())}; String[] selectionArgs = new String[]{String.valueOf(event.getId())};
Cursor cursor = helper.getReadableDatabase().rawQuery( Cursor cursor = helper.getReadableDatabase().rawQuery(
"SELECT p.rowid AS _id, p.name" + " FROM " + DatabaseHelper.PERSONS_TABLE_NAME + " p" + " JOIN " + DatabaseHelper.EVENTS_PERSONS_TABLE_NAME "SELECT p.rowid AS _id, p.name"
+ " ep ON p.rowid = ep.person_id" + " WHERE ep.event_id = ?", selectionArgs); + " FROM " + DatabaseHelper.PERSONS_TABLE_NAME + " p"
+ " JOIN " + DatabaseHelper.EVENTS_PERSONS_TABLE_NAME + " ep ON p.rowid = ep.person_id"
+ " WHERE ep.event_id = ?", selectionArgs);
try { try {
List<Person> result = new ArrayList<>(cursor.getCount()); List<Person> result = new ArrayList<>(cursor.getCount());
while (cursor.moveToNext()) { while (cursor.moveToNext()) {
@ -852,7 +769,10 @@ public class DatabaseManager {
public List<Link> getLinks(Event event) { public List<Link> getLinks(Event event) {
String[] selectionArgs = new String[]{String.valueOf(event.getId())}; String[] selectionArgs = new String[]{String.valueOf(event.getId())};
Cursor cursor = helper.getReadableDatabase().rawQuery( Cursor cursor = helper.getReadableDatabase().rawQuery(
"SELECT url, description" + " FROM " + DatabaseHelper.LINKS_TABLE_NAME + " WHERE event_id = ?" + " ORDER BY rowid ASC", selectionArgs); "SELECT url, description"
+ " FROM " + DatabaseHelper.LINKS_TABLE_NAME
+ " WHERE event_id = ?"
+ " ORDER BY rowid ASC", selectionArgs);
try { try {
List<Link> result = new ArrayList<>(cursor.getCount()); List<Link> result = new ArrayList<>(cursor.getCount());
while (cursor.moveToNext()) { while (cursor.moveToNext()) {