FairEmail/app/src/main/java/eu/faircode/email/DB.java

360 lines
17 KiB
Java
Raw Normal View History

2018-08-02 13:33:06 +00:00
package eu.faircode.email;
import android.content.Context;
2018-08-24 09:20:14 +00:00
import android.database.Cursor;
2018-08-06 16:22:01 +00:00
import android.text.TextUtils;
2018-08-02 13:33:06 +00:00
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import javax.mail.Address;
import javax.mail.internet.InternetAddress;
2018-08-08 06:55:47 +00:00
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.room.TypeConverter;
import androidx.room.TypeConverters;
import androidx.room.migration.Migration;
import androidx.sqlite.db.SupportSQLiteDatabase;
2018-11-09 07:38:07 +00:00
import io.requery.android.database.sqlite.RequerySQLiteOpenHelperFactory;
2018-08-08 06:55:47 +00:00
2018-08-02 13:33:06 +00:00
/*
2018-08-14 05:53:24 +00:00
This file is part of FairEmail.
2018-08-02 13:33:06 +00:00
2018-08-14 05:53:24 +00:00
FairEmail is free software: you can redistribute it and/or modify
2018-08-02 13:33:06 +00:00
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.
2018-10-29 10:46:49 +00:00
FairEmail is distributed in the hope that it will be useful,
2018-08-02 13:33:06 +00:00
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
2018-10-29 10:46:49 +00:00
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
2018-08-02 13:33:06 +00:00
Copyright 2018 by Marcel Bokhorst (M66B)
*/
// https://developer.android.com/topic/libraries/architecture/room.html
@Database(
version = 20,
2018-08-02 13:33:06 +00:00
entities = {
EntityIdentity.class,
EntityAccount.class,
EntityFolder.class,
EntityMessage.class,
2018-08-03 12:07:51 +00:00
EntityAttachment.class,
EntityOperation.class,
EntityAnswer.class,
2018-09-03 19:11:16 +00:00
EntityLog.class
2018-08-08 10:22:12 +00:00
}
2018-08-02 13:33:06 +00:00
)
@TypeConverters({DB.Converters.class})
public abstract class DB extends RoomDatabase {
public abstract DaoIdentity identity();
public abstract DaoAccount account();
public abstract DaoFolder folder();
public abstract DaoMessage message();
2018-08-03 12:07:51 +00:00
public abstract DaoAttachment attachment();
2018-08-02 13:33:06 +00:00
public abstract DaoOperation operation();
public abstract DaoAnswer answer();
2018-09-03 19:11:16 +00:00
public abstract DaoLog log();
2018-08-02 13:33:06 +00:00
private static DB sInstance;
2018-11-13 09:05:33 +00:00
private static final String DB_NAME = "fairemail";
2018-08-02 13:33:06 +00:00
public static synchronized DB getInstance(Context context) {
if (sInstance == null) {
2018-08-24 07:58:04 +00:00
sInstance = migrate(Room
.databaseBuilder(context.getApplicationContext(), DB.class, DB_NAME)
2018-11-09 07:38:07 +00:00
.openHelperFactory(new RequerySQLiteOpenHelperFactory())
2018-08-24 07:58:04 +00:00
.setJournalMode(JournalMode.WRITE_AHEAD_LOGGING));
2018-08-24 10:15:54 +00:00
Log.i(Helper.TAG, "sqlite version=" + exec(sInstance, "SELECT sqlite_version() AS sqlite_version"));
Log.i(Helper.TAG, "sqlite sync=" + exec(sInstance, "PRAGMA synchronous"));
Log.i(Helper.TAG, "sqlite journal=" + exec(sInstance, "PRAGMA journal_mode"));
}
2018-08-02 13:33:06 +00:00
return sInstance;
}
2018-12-09 14:49:43 +00:00
private static String exec(DB db, String command) {
Cursor cursor = null;
try {
cursor = db.query(command, new Object[0]);
if (cursor.moveToNext())
return cursor.getString(0);
else
return null;
} finally {
if (cursor != null)
cursor.close();
}
2018-08-03 18:07:12 +00:00
}
2018-08-02 13:33:06 +00:00
private static DB migrate(RoomDatabase.Builder<DB> builder) {
2018-11-13 09:05:33 +00:00
// https://www.sqlite.org/lang_altertable.html
2018-08-02 13:33:06 +00:00
return builder
2018-08-08 10:22:12 +00:00
.addCallback(new Callback() {
@Override
public void onOpen(SupportSQLiteDatabase db) {
Log.i(Helper.TAG, "Database version=" + db.getVersion());
super.onOpen(db);
}
})
2018-08-23 09:48:27 +00:00
.addMigrations(new Migration(1, 2) {
@Override
public void migrate(SupportSQLiteDatabase db) {
Log.i(Helper.TAG, "DB migration from version " + startVersion + " to " + endVersion);
2018-11-14 09:49:59 +00:00
db.execSQL("ALTER TABLE `folder` RENAME COLUMN `after` TO `sync_days`");
db.execSQL("ALTER TABLE `folder` ADD COLUMN `keep_days` INTEGER NOT NULL DEFAULT 30");
db.execSQL("UPDATE `folder` SET keep_days = sync_days");
2018-11-12 13:45:02 +00:00
}
})
2018-11-14 13:51:50 +00:00
.addMigrations(new Migration(2, 3) {
@Override
public void migrate(SupportSQLiteDatabase db) {
Log.i(Helper.TAG, "DB migration from version " + startVersion + " to " + endVersion);
db.execSQL("ALTER TABLE `identity` ADD COLUMN `signature` TEXT");
db.execSQL("UPDATE `identity` SET signature =" +
" (SELECT account.signature FROM account WHERE account.id = identity.account)");
}
})
.addMigrations(new Migration(3, 4) {
@Override
public void migrate(SupportSQLiteDatabase db) {
Log.i(Helper.TAG, "DB migration from version " + startVersion + " to " + endVersion);
db.execSQL("ALTER TABLE `message` ADD COLUMN `forwarding` INTEGER" +
" REFERENCES `message`(`id`) ON UPDATE NO ACTION ON DELETE SET NULL");
2018-12-06 10:59:57 +00:00
db.execSQL("CREATE INDEX `index_message_forwarding` ON `message` (`forwarding`)");
}
})
2018-11-19 13:14:02 +00:00
.addMigrations(new Migration(4, 5) {
@Override
public void migrate(SupportSQLiteDatabase db) {
Log.i(Helper.TAG, "DB migration from version " + startVersion + " to " + endVersion);
db.execSQL("ALTER TABLE `account` ADD COLUMN `last_connected` INTEGER");
db.execSQL("ALTER TABLE `message` ADD COLUMN `last_attempt` INTEGER");
}
})
.addMigrations(new Migration(5, 6) {
@Override
public void migrate(SupportSQLiteDatabase db) {
Log.i(Helper.TAG, "DB migration from version " + startVersion + " to " + endVersion);
db.execSQL("ALTER TABLE `account` ADD COLUMN `notify` INTEGER NOT NULL DEFAULT 0");
}
})
2018-11-24 18:14:28 +00:00
.addMigrations(new Migration(6, 7) {
@Override
public void migrate(SupportSQLiteDatabase db) {
Log.i(Helper.TAG, "DB migration from version " + startVersion + " to " + endVersion);
db.execSQL("ALTER TABLE `message` ADD COLUMN `answered` INTEGER NOT NULL DEFAULT 0");
db.execSQL("ALTER TABLE `message` ADD COLUMN `ui_answered` INTEGER NOT NULL DEFAULT 0");
}
})
2018-11-25 12:34:08 +00:00
.addMigrations(new Migration(7, 8) {
@Override
public void migrate(SupportSQLiteDatabase db) {
Log.i(Helper.TAG, "DB migration from version " + startVersion + " to " + endVersion);
db.execSQL("ALTER TABLE `message` ADD COLUMN `keywords` TEXT");
}
})
2018-11-26 08:12:44 +00:00
.addMigrations(new Migration(8, 9) {
@Override
public void migrate(SupportSQLiteDatabase db) {
Log.i(Helper.TAG, "DB migration from version " + startVersion + " to " + endVersion);
db.execSQL("ALTER TABLE `folder` ADD COLUMN `keywords` TEXT");
}
})
2018-11-26 15:57:00 +00:00
.addMigrations(new Migration(9, 10) {
@Override
public void migrate(SupportSQLiteDatabase db) {
Log.i(Helper.TAG, "DB migration from version " + startVersion + " to " + endVersion);
db.execSQL("ALTER TABLE `message` ADD COLUMN `ui_browsed` INTEGER NOT NULL DEFAULT 0");
2018-12-06 10:59:57 +00:00
db.execSQL("CREATE INDEX `index_message_ui_browsed` ON `message` (`ui_browsed`)");
2018-11-26 15:57:00 +00:00
}
})
2018-12-01 14:13:57 +00:00
.addMigrations(new Migration(10, 11) {
@Override
public void migrate(SupportSQLiteDatabase db) {
Log.i(Helper.TAG, "DB migration from version " + startVersion + " to " + endVersion);
db.execSQL("ALTER TABLE `operation` ADD COLUMN `error` TEXT");
}
})
2018-12-02 13:19:54 +00:00
.addMigrations(new Migration(11, 12) {
@Override
public void migrate(SupportSQLiteDatabase db) {
Log.i(Helper.TAG, "DB migration from version " + startVersion + " to " + endVersion);
db.execSQL("DROP INDEX `index_operation_folder`");
db.execSQL("DROP INDEX `index_operation_message`");
db.execSQL("DROP TABLE `operation`");
db.execSQL("CREATE TABLE `operation`" +
" (`id` INTEGER PRIMARY KEY AUTOINCREMENT" +
", `folder` INTEGER NOT NULL" +
", `message` INTEGER" +
", `name` TEXT NOT NULL" +
", `args` TEXT NOT NULL" +
", `created` INTEGER NOT NULL" +
", `error` TEXT" +
", FOREIGN KEY(`folder`) REFERENCES `folder`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE" +
", FOREIGN KEY(`message`) REFERENCES `message`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE)");
db.execSQL("CREATE INDEX `index_operation_folder` ON `operation` (`folder`)");
db.execSQL("CREATE INDEX `index_operation_message` ON `operation` (`message`)");
}
})
.addMigrations(new Migration(12, 13) {
@Override
public void migrate(SupportSQLiteDatabase db) {
Log.i(Helper.TAG, "DB migration from version " + startVersion + " to " + endVersion);
db.execSQL("CREATE INDEX `index_message_ui_flagged` ON `message` (`ui_flagged`)");
}
})
2018-12-03 09:39:44 +00:00
.addMigrations(new Migration(13, 14) {
@Override
public void migrate(SupportSQLiteDatabase db) {
Log.i(Helper.TAG, "DB migration from version " + startVersion + " to " + endVersion);
db.execSQL("ALTER TABLE `folder` ADD COLUMN `level` INTEGER NOT NULL DEFAULT 0");
}
})
2018-12-03 12:41:36 +00:00
.addMigrations(new Migration(14, 15) {
@Override
public void migrate(SupportSQLiteDatabase db) {
Log.i(Helper.TAG, "DB migration from version " + startVersion + " to " + endVersion);
db.execSQL("ALTER TABLE `folder` ADD COLUMN `sync_state` TEXT");
}
})
.addMigrations(new Migration(15, 16) {
@Override
public void migrate(SupportSQLiteDatabase db) {
Log.i(Helper.TAG, "DB migration from version " + startVersion + " to " + endVersion);
db.execSQL("ALTER TABLE `folder` ADD COLUMN `poll` INTEGER NOT NULL DEFAULT 0");
}
})
2018-12-06 10:59:57 +00:00
.addMigrations(new Migration(16, 17) {
@Override
public void migrate(SupportSQLiteDatabase db) {
Log.i(Helper.TAG, "DB migration from version " + startVersion + " to " + endVersion);
db.execSQL("DELETE FROM `message` WHERE ui_found");
db.execSQL("DROP INDEX `index_message_folder_uid_ui_found`");
db.execSQL("DROP INDEX `index_message_msgid_folder_ui_found`");
db.execSQL("CREATE UNIQUE INDEX `index_message_folder_uid` ON `message` (`folder`, `uid`)");
db.execSQL("CREATE UNIQUE INDEX `index_message_msgid_folder` ON `message` (`msgid`, `folder`)");
}
})
.addMigrations(new Migration(17, 18) {
@Override
public void migrate(SupportSQLiteDatabase db) {
Log.i(Helper.TAG, "DB migration from version " + startVersion + " to " + endVersion);
db.execSQL("ALTER TABLE `account` ADD COLUMN `tbd` INTEGER");
db.execSQL("ALTER TABLE `identity` ADD COLUMN `tbd` INTEGER");
db.execSQL("ALTER TABLE `folder` ADD COLUMN `tbd` INTEGER");
}
})
.addMigrations(new Migration(18, 19) {
@Override
public void migrate(SupportSQLiteDatabase db) {
Log.i(Helper.TAG, "DB migration from version " + startVersion + " to " + endVersion);
db.execSQL("ALTER TABLE `identity` ADD COLUMN `delivery_receipt` INTEGER NOT NULL DEFAULT 0");
db.execSQL("ALTER TABLE `identity` ADD COLUMN `read_receipt` INTEGER NOT NULL DEFAULT 0");
}
})
.addMigrations(new Migration(19, 20) {
@Override
public void migrate(SupportSQLiteDatabase db) {
Log.i(Helper.TAG, "DB migration from version " + startVersion + " to " + endVersion);
db.execSQL("ALTER TABLE `folder` ADD COLUMN `notify` INTEGER NOT NULL DEFAULT 0");
db.execSQL("UPDATE `folder` SET notify = unified");
}
})
2018-08-02 13:33:06 +00:00
.build();
}
public static class Converters {
@TypeConverter
2018-11-25 12:34:08 +00:00
public static String[] toStringArray(String value) {
if (value == null)
return new String[0];
else
2018-11-25 17:29:11 +00:00
return TextUtils.split(value, " ");
2018-08-02 13:33:06 +00:00
}
@TypeConverter
2018-11-25 12:34:08 +00:00
public static String fromStringArray(String[] value) {
if (value == null || value.length == 0)
return null;
else
return TextUtils.join(" ", value);
2018-08-02 13:33:06 +00:00
}
@TypeConverter
public static String encodeAddresses(Address[] addresses) {
if (addresses == null)
return null;
JSONArray jaddresses = new JSONArray();
2018-12-09 14:49:43 +00:00
for (Address address : addresses)
try {
if (address instanceof InternetAddress) {
String a = ((InternetAddress) address).getAddress();
String p = ((InternetAddress) address).getPersonal();
JSONObject jaddress = new JSONObject();
if (a != null)
jaddress.put("address", a);
if (p != null)
jaddress.put("personal", p);
jaddresses.put(jaddress);
} else {
JSONObject jaddress = new JSONObject();
jaddress.put("address", address.toString());
jaddresses.put(jaddress);
}
2018-12-09 14:49:43 +00:00
} catch (JSONException ex) {
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
}
return jaddresses.toString();
}
@TypeConverter
public static Address[] decodeAddresses(String json) {
if (json == null)
return null;
List<Address> result = new ArrayList<>();
try {
JSONArray jaddresses = new JSONArray(json);
for (int i = 0; i < jaddresses.length(); i++) {
JSONObject jaddress = (JSONObject) jaddresses.get(i);
if (jaddress.has("personal"))
result.add(new InternetAddress(
jaddress.getString("address"),
jaddress.getString("personal")));
else
result.add(new InternetAddress(
jaddress.getString("address")));
}
} catch (Throwable ex) {
2018-08-29 05:31:00 +00:00
// Compose can store invalid addresses
Log.w(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
}
return result.toArray(new Address[0]);
}
2018-08-02 13:33:06 +00:00
}
}