Added FTS expressions

pull/184/head
M66B 5 years ago
parent f0b792f9e1
commit a540c33397

@ -194,8 +194,6 @@ public class BoundaryCallbackMessages extends PagedList.BoundaryCallback<TupleMe
DB db = DB.getInstance(context); DB db = DB.getInstance(context);
int found = 0; int found = 0;
if (criteria.isExpression())
return found;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean fts = prefs.getBoolean("fts", false); boolean fts = prefs.getBoolean("fts", false);
@ -227,6 +225,9 @@ public class BoundaryCallbackMessages extends PagedList.BoundaryCallback<TupleMe
return found; return found;
} }
if (criteria.isExpression())
return found;
try { try {
db.beginTransaction(); db.beginTransaction();
@ -708,34 +709,28 @@ public class BoundaryCallbackMessages extends PagedList.BoundaryCallback<TupleMe
List<String> plus = new ArrayList<>(); List<String> plus = new ArrayList<>();
List<String> minus = new ArrayList<>(); List<String> minus = new ArrayList<>();
List<String> opt = new ArrayList<>(); List<String> opt = new ArrayList<>();
StringBuilder sb = new StringBuilder(); StringBuilder all = new StringBuilder();
for (String w : search.trim().split("\\s+")) { for (String w : search.trim().split("\\s+")) {
if (sb.length() > 0) if (all.length() > 0)
sb.append(' '); all.append(' ');
if (w.length() > 1 && w.startsWith("+")) { if (w.length() > 1 && w.startsWith("+")) {
plus.add(w.substring(1)); plus.add(w.substring(1));
sb.append(w.substring(1)); all.append(w.substring(1));
} else if (w.length() > 1 && w.startsWith("-")) { } else if (w.length() > 1 && w.startsWith("-")) {
minus.add(w.substring(1)); minus.add(w.substring(1));
sb.append(w.substring(1)); all.append(w.substring(1));
} else if (w.length() > 1 && w.startsWith("?")) { } else if (w.length() > 1 && w.startsWith("?")) {
opt.add(w.substring(1)); opt.add(w.substring(1));
sb.append(w.substring(1)); all.append(w.substring(1));
} else { } else {
word.add(w); word.add(w);
sb.append(w); all.append(w);
} }
} }
if (plus.size() + minus.size() + opt.size() > 0) { if (plus.size() + minus.size() + opt.size() > 0)
search = sb.toString(); search = all.toString();
Log.i("SEARCH word=" + TextUtils.join(",", word));
Log.i("SEARCH plus=" + TextUtils.join(",", plus));
Log.i("SEARCH minus=" + TextUtils.join(",", minus));
Log.i("SEARCH opt=" + TextUtils.join(",", opt));
Log.i("SEARCH full=" + search);
}
// Yahoo! does not support keyword search, but uses the flags $Forwarded $Junk $NotJunk // Yahoo! does not support keyword search, but uses the flags $Forwarded $Junk $NotJunk
boolean hasKeywords = false; boolean hasKeywords = false;

@ -334,10 +334,6 @@ public class FragmentDialogSearch extends FragmentDialogBase {
Log.unexpectedError(getParentFragmentManager(), ex); Log.unexpectedError(getParentFragmentManager(), ex);
} }
}.execute(getContext(), getViewLifecycleOwner(), getArguments(), "search:raw"); }.execute(getContext(), getViewLifecycleOwner(), getArguments(), "search:raw");
else if (criteria.isExpression() && folder > 0)
FragmentMessages.search(
getContext(), getViewLifecycleOwner(), getParentFragmentManager(),
account, folder, true, criteria);
else else
FragmentMessages.search( FragmentMessages.search(
getContext(), getViewLifecycleOwner(), getParentFragmentManager(), getContext(), getViewLifecycleOwner(), getParentFragmentManager(),

@ -111,29 +111,61 @@ public class FtsDbHelper extends SQLiteOpenHelper {
SQLiteDatabase db, SQLiteDatabase db,
Long account, Long folder, Long account, Long folder,
BoundaryCallbackMessages.SearchCriteria criteria) { BoundaryCallbackMessages.SearchCriteria criteria) {
List<String> word = new ArrayList<>();
List<String> plus = new ArrayList<>();
List<String> minus = new ArrayList<>();
List<String> opt = new ArrayList<>();
StringBuilder all = new StringBuilder();
for (String w : criteria.query.trim().split("\\s+")) {
if (all.length() > 0)
all.append(' ');
if (w.length() > 1 && w.startsWith("+")) {
plus.add(w.substring(1));
all.append(w.substring(1));
} else if (w.length() > 1 && w.startsWith("-")) {
minus.add(w.substring(1));
all.append(w.substring(1));
} else if (w.length() > 1 && w.startsWith("?")) {
opt.add(w.substring(1));
all.append(w.substring(1));
} else {
word.add(w);
all.append(w);
}
}
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (String or : criteria.query.split(",")) { if (plus.size() + minus.size() + opt.size() > 0) {
if (sb.length() > 0) if (word.size() > 0)
sb.append(" OR "); sb.append(escape(TextUtils.join(" ", word)));
boolean first = true; for (String p : plus) {
sb.append("("); if (sb.length() > 0)
for (String and : or.trim().split("\\s+")) {
if (first)
first = false;
else
sb.append(" AND "); sb.append(" AND ");
sb.append(escape(p));
}
// Escape quotes for (String m : minus) {
String term = and.replaceAll("\"", "\"\""); if (sb.length() > 0)
sb.append(" NOT ");
sb.append(escape(m));
}
if (sb.length() > 0) {
sb.insert(0, '(');
sb.append(')');
}
// Quote search term for (String o : opt) {
sb.append("\"").append(term).append("\""); if (sb.length() > 0)
sb.append(" OR ");
sb.append(escape(o));
} }
sb.append(")");
} }
String search = sb.toString(); String search = (sb.length() > 0 ? sb.toString() : escape(criteria.query));
String select = ""; String select = "";
if (account != null) if (account != null)
@ -159,6 +191,10 @@ public class FtsDbHelper extends SQLiteOpenHelper {
return result; return result;
} }
private static String escape(String word) {
return "\"" + word.replaceAll("\"", "\"\"") + "\"";
}
static Cursor getIds(SQLiteDatabase db) { static Cursor getIds(SQLiteDatabase db) {
return db.query( return db.query(
"message", new String[]{"rowid"}, "message", new String[]{"rowid"},

Loading…
Cancel
Save