parent
942154a61f
commit
a58f564d1e
1 changed files with 32 additions and 16 deletions
|
@ -1,6 +1,8 @@
|
||||||
package org.thoughtcrime.securesms.database;
|
package org.thoughtcrime.securesms.database;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.text.TextUtils;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
|
||||||
import com.annimon.stream.Stream;
|
import com.annimon.stream.Stream;
|
||||||
|
@ -9,9 +11,6 @@ import net.sqlcipher.Cursor;
|
||||||
import net.sqlcipher.database.SQLiteDatabase;
|
import net.sqlcipher.database.SQLiteDatabase;
|
||||||
|
|
||||||
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper;
|
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper;
|
||||||
import org.thoughtcrime.securesms.util.Util;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Contains all databases necessary for full-text search (FTS).
|
* Contains all databases necessary for full-text search (FTS).
|
||||||
|
@ -112,31 +111,48 @@ public class SearchDatabase extends Database {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Cursor queryMessages(@NonNull String query) {
|
public Cursor queryMessages(@NonNull String query) {
|
||||||
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
||||||
String prefixQuery = adjustQuery(query);
|
String fullTextSearchQuery = createFullTextSearchQuery(query);
|
||||||
|
|
||||||
|
if (TextUtils.isEmpty(fullTextSearchQuery)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Cursor cursor = db.rawQuery(MESSAGES_QUERY, new String[] { fullTextSearchQuery,
|
||||||
|
fullTextSearchQuery });
|
||||||
|
|
||||||
Cursor cursor = db.rawQuery(MESSAGES_QUERY, new String[] { prefixQuery, prefixQuery });
|
|
||||||
setNotifyConverationListListeners(cursor);
|
setNotifyConverationListListeners(cursor);
|
||||||
return cursor;
|
return cursor;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Cursor queryMessages(@NonNull String query, long threadId) {
|
public Cursor queryMessages(@NonNull String query, long threadId) {
|
||||||
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
||||||
String prefixQuery = adjustQuery(query);
|
String fullTextSearchQuery = createFullTextSearchQuery(query);
|
||||||
|
|
||||||
|
if (TextUtils.isEmpty(fullTextSearchQuery)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Cursor cursor = db.rawQuery(MESSAGES_FOR_THREAD_QUERY, new String[] { fullTextSearchQuery,
|
||||||
|
String.valueOf(threadId),
|
||||||
|
fullTextSearchQuery,
|
||||||
|
String.valueOf(threadId) });
|
||||||
|
|
||||||
Cursor cursor = db.rawQuery(MESSAGES_FOR_THREAD_QUERY, new String[] { prefixQuery, String.valueOf(threadId), prefixQuery, String.valueOf(threadId) });
|
|
||||||
setNotifyConverationListListeners(cursor);
|
setNotifyConverationListListeners(cursor);
|
||||||
return cursor;
|
return cursor;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String adjustQuery(@NonNull String query) {
|
private static String createFullTextSearchQuery(@NonNull String query) {
|
||||||
List<String> tokens = Stream.of(query.split(" ")).filter(s -> s.trim().length() > 0).toList();
|
return Stream.of(query.split(" "))
|
||||||
String prefixQuery = Util.join(tokens, "* ");
|
.map(String::trim)
|
||||||
|
.filter(s -> s.length() > 0)
|
||||||
|
.map(SearchDatabase::fullTextSearchEscape)
|
||||||
|
.collect(StringBuilder::new, (sb, s) -> sb.append(s).append("* "))
|
||||||
|
.toString();
|
||||||
|
}
|
||||||
|
|
||||||
prefixQuery += "*";
|
private static String fullTextSearchEscape(String s) {
|
||||||
|
return "\"" + s.replace("\"", "\"\"") + "\"";
|
||||||
return prefixQuery;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue