Files
FairEmail/app/src/main/java/eu/faircode/email/FtsDbHelper.java

137 lines
4.7 KiB
Java
Raw Normal View History

2020-01-14 21:58:27 +01:00
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018-2020 by Marcel Bokhorst (M66B)
*/
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.text.TextUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.mail.Address;
import io.requery.android.database.sqlite.SQLiteDatabase;
import io.requery.android.database.sqlite.SQLiteOpenHelper;
2020-01-15 13:16:52 +01:00
// https://www.sqlite.org/fts5.html
2020-01-14 21:58:27 +01:00
public class FtsDbHelper extends SQLiteOpenHelper {
2020-01-15 14:48:33 +01:00
private static FtsDbHelper instance = null;
2020-01-14 21:58:27 +01:00
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "fts.db";
2020-01-15 14:48:33 +01:00
private FtsDbHelper(Context context) {
2020-01-14 21:58:27 +01:00
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
2020-01-15 14:48:33 +01:00
static SQLiteDatabase getInstance(Context context) {
if (instance == null)
instance = new FtsDbHelper(context);
return instance.getWritableDatabase();
}
2020-01-14 21:58:27 +01:00
@Override
public void onCreate(SQLiteDatabase db) {
Log.i("FTS create");
2020-01-15 10:29:16 +01:00
db.execSQL("CREATE VIRTUAL TABLE `message`" +
2020-01-15 13:16:52 +01:00
" USING fts5 (`folder` UNINDEXED, `time` UNINDEXED, `address`, `subject`, `keyword`, `text`)");
2020-01-14 21:58:27 +01:00
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Do nothing
}
2020-01-15 14:48:33 +01:00
static void insert(SQLiteDatabase db, EntityMessage message, String text) {
Log.i("FTS insert id=" + message.id);
2020-01-14 21:58:27 +01:00
List<Address> address = new ArrayList<>();
if (message.from != null)
address.addAll(Arrays.asList(message.from));
if (message.to != null)
address.addAll(Arrays.asList(message.to));
if (message.cc != null)
address.addAll(Arrays.asList(message.cc));
2020-01-15 14:48:33 +01:00
delete(db, message.id);
ContentValues cv = new ContentValues();
cv.put("rowid", message.id);
cv.put("folder", message.folder);
cv.put("time", message.received);
cv.put("address", MessageHelper.formatAddresses(address.toArray(new Address[0]), true, false));
cv.put("subject", message.subject == null ? "" : message.subject);
cv.put("keyword", TextUtils.join(", ", message.keywords));
cv.put("text", text);
db.insert("message", SQLiteDatabase.CONFLICT_FAIL, cv);
2020-01-14 21:58:27 +01:00
}
2020-01-16 09:44:54 +01:00
static void delete(SQLiteDatabase db) {
db.delete("message", null, null);
}
2020-01-15 14:48:33 +01:00
static void delete(SQLiteDatabase db, long id) {
2020-01-15 13:16:52 +01:00
db.delete("message", "rowid = ?", new Object[]{id});
2020-01-15 10:29:16 +01:00
}
2020-01-15 19:07:51 +01:00
static List<Long> match(SQLiteDatabase db, Long folder, String query) {
String[] parts = query.split("\\s+");
StringBuilder sb = new StringBuilder();
for (String part : parts) {
if (sb.length() > 0)
sb.append(" AND ");
part = part.replaceAll("\"", "\"\"");
sb.append("\"").append(part).append("\"");
}
String search = sb.toString();
2020-01-14 22:39:35 +01:00
Log.i("FTS folder=" + folder + " search=" + search);
2020-01-14 21:58:27 +01:00
List<Long> result = new ArrayList<>();
2020-01-14 22:39:35 +01:00
try (Cursor cursor = db.query(
2020-01-15 13:16:52 +01:00
"message", new String[]{"rowid"},
2020-01-14 22:39:35 +01:00
folder == null ? "message MATCH ?" : "folder = ? AND message MATCH ?",
folder == null ? new Object[]{search} : new Object[]{folder, search},
null, null, "time DESC", null)) {
while (cursor != null && cursor.moveToNext())
result.add(cursor.getLong(0));
2020-01-14 21:58:27 +01:00
}
Log.i("FTS result=" + result.size());
return result;
}
2020-01-15 10:29:16 +01:00
2020-01-15 14:48:33 +01:00
static Cursor getIds(SQLiteDatabase db) {
2020-01-15 10:29:16 +01:00
return db.query(
2020-01-15 13:16:52 +01:00
"message", new String[]{"rowid"},
2020-01-15 10:29:16 +01:00
null, null,
null, null, "time");
}
2020-01-16 09:44:54 +01:00
static long size(Context context) {
return context.getDatabasePath(DATABASE_NAME).length();
}
static void optimize(SQLiteDatabase db) {
Log.i("FTS optimize");
db.execSQL("INSERT INTO message (message) VALUES ('optimize')");
}
2020-01-14 21:58:27 +01:00
}