2018-08-02 13:33:06 +00:00
|
|
|
package eu.faircode.email;
|
|
|
|
|
|
|
|
|
|
/*
|
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
|
|
|
|
2021-01-01 08:56:36 +01:00
|
|
|
Copyright 2018-2021 by Marcel Bokhorst (M66B)
|
2018-08-02 13:33:06 +00:00
|
|
|
*/
|
|
|
|
|
|
2018-08-12 10:51:09 +00:00
|
|
|
import androidx.lifecycle.LiveData;
|
2018-08-08 06:55:47 +00:00
|
|
|
import androidx.room.Dao;
|
|
|
|
|
import androidx.room.Insert;
|
|
|
|
|
import androidx.room.Query;
|
2020-01-30 16:32:39 +01:00
|
|
|
import androidx.room.Transaction;
|
2018-08-08 06:55:47 +00:00
|
|
|
|
2019-04-17 20:21:44 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
@Dao
|
|
|
|
|
public interface DaoOperation {
|
2019-10-05 17:47:56 +02:00
|
|
|
String priority = "CASE" +
|
2020-01-24 19:35:39 +01:00
|
|
|
" WHEN operation.name = '" + EntityOperation.BODY + "' THEN -4" +
|
|
|
|
|
" WHEN operation.name = '" + EntityOperation.ATTACHMENT + "' THEN -3" +
|
|
|
|
|
" WHEN operation.name = '" + EntityOperation.HEADERS + "' THEN -2" +
|
2020-07-03 08:40:52 +02:00
|
|
|
" WHEN operation.name = '" + EntityOperation.RAW + "' THEN -2" +
|
|
|
|
|
" WHEN operation.name = '" + EntityOperation.SYNC + "' AND folder.account IS NULL THEN -1" + // Outbox
|
|
|
|
|
" WHEN operation.name = '" + EntityOperation.SYNC + "' AND folder.account IS NOT NULL THEN 1" +
|
|
|
|
|
// Other operations: add, delete, seen, answered, flag, keyword, label, subscribe, send
|
2020-01-25 12:36:34 +01:00
|
|
|
" WHEN operation.name = '" + EntityOperation.FETCH + "' THEN 2" +
|
2020-01-26 08:44:14 +01:00
|
|
|
" WHEN operation.name = '" + EntityOperation.EXISTS + "' THEN 3" +
|
2020-07-03 08:40:52 +02:00
|
|
|
" WHEN operation.name = '" + EntityOperation.COPY + "' THEN 4" +
|
2020-12-10 15:38:43 +01:00
|
|
|
" WHEN operation.name = '" + EntityOperation.MOVE + "' THEN 5" +
|
|
|
|
|
" WHEN operation.name = '" + EntityOperation.PURGE + "' THEN 6" +
|
|
|
|
|
" WHEN operation.name = '" + EntityOperation.DELETE + "' THEN 7" +
|
2021-02-12 12:51:34 +01:00
|
|
|
" WHEN operation.name = '" + EntityOperation.EXPUNGE + "' THEN 8" +
|
2019-10-05 17:47:56 +02:00
|
|
|
" ELSE 0" +
|
|
|
|
|
" END";
|
|
|
|
|
|
2020-01-30 16:32:39 +01:00
|
|
|
@Transaction
|
2020-01-25 10:49:59 +01:00
|
|
|
@Query("SELECT operation.*" +
|
|
|
|
|
", " + priority + " AS priority" +
|
|
|
|
|
", account.name AS accountName, folder.name AS folderName" +
|
2020-10-04 16:57:19 +02:00
|
|
|
", (account.synchronize IS NULL OR account.synchronize) AS synchronize" +
|
2019-01-29 11:06:53 +00:00
|
|
|
" FROM operation" +
|
2019-06-07 18:33:35 +02:00
|
|
|
" JOIN folder ON folder.id = operation.folder" +
|
2019-12-24 09:27:03 +01:00
|
|
|
" LEFT JOIN account ON account.id = operation.account" +
|
2019-10-05 17:47:56 +02:00
|
|
|
" ORDER BY " + priority + ", id")
|
2018-12-11 09:47:37 +01:00
|
|
|
LiveData<List<TupleOperationEx>> liveOperations();
|
2018-08-13 06:59:05 +00:00
|
|
|
|
2020-10-04 16:57:19 +02:00
|
|
|
@Transaction
|
|
|
|
|
@Query("SELECT operation.*" +
|
2020-01-25 10:49:59 +01:00
|
|
|
", " + priority + " AS priority" +
|
|
|
|
|
", account.name AS accountName, folder.name AS folderName" +
|
2020-10-04 16:57:19 +02:00
|
|
|
", account.synchronize" +
|
2020-01-25 10:49:59 +01:00
|
|
|
" FROM operation" +
|
2019-06-07 18:33:35 +02:00
|
|
|
" JOIN folder ON folder.id = operation.folder" +
|
2020-10-04 16:57:19 +02:00
|
|
|
" JOIN account ON account.id = operation.account" +
|
|
|
|
|
" WHERE operation.account = :account" +
|
|
|
|
|
" AND account.synchronize" +
|
|
|
|
|
" AND folder.account IS NOT NULL" + // not outbox
|
|
|
|
|
" ORDER BY " + priority + ", id")
|
|
|
|
|
LiveData<List<TupleOperationEx>> liveOperations(long account);
|
2019-01-29 15:57:05 +00:00
|
|
|
|
2020-02-26 14:30:59 +01:00
|
|
|
@Transaction
|
2020-10-04 16:57:19 +02:00
|
|
|
@Query("SELECT operation.*" +
|
|
|
|
|
" FROM operation" +
|
|
|
|
|
" JOIN folder ON folder.id = operation.folder" +
|
|
|
|
|
" WHERE folder.account IS NULL" + // outbox
|
|
|
|
|
" ORDER BY id")
|
|
|
|
|
LiveData<List<EntityOperation>> liveSend();
|
2018-12-02 14:19:54 +01:00
|
|
|
|
2019-05-04 22:38:11 +02:00
|
|
|
@Query("SELECT COUNT(operation.id) AS pending" +
|
|
|
|
|
", SUM(CASE WHEN operation.error IS NULL THEN 0 ELSE 1 END) AS errors" +
|
2019-06-13 13:43:34 +02:00
|
|
|
" FROM operation" +
|
|
|
|
|
" LEFT JOIN account ON account.id = operation.account" +
|
2020-10-04 16:57:19 +02:00
|
|
|
" WHERE (account.synchronize IS NULL OR account.synchronize)")
|
2019-05-04 22:38:11 +02:00
|
|
|
LiveData<TupleOperationStats> liveStats();
|
2019-04-29 10:14:55 +02:00
|
|
|
|
2020-03-11 10:09:52 +01:00
|
|
|
@Query("SELECT" +
|
|
|
|
|
" COUNT(operation.id) AS count" +
|
|
|
|
|
", SUM(CASE WHEN operation.state = 'executing' THEN 1 ELSE 0 END) AS busy" +
|
|
|
|
|
" FROM operation" +
|
2020-10-04 16:57:19 +02:00
|
|
|
" WHERE operation.name = '" + EntityOperation.SEND + "'")
|
2020-03-11 10:09:52 +01:00
|
|
|
LiveData<TupleUnsent> liveUnsent();
|
2019-02-28 08:11:53 +00:00
|
|
|
|
2018-12-01 15:38:02 +01:00
|
|
|
@Query("SELECT * FROM operation ORDER BY id")
|
|
|
|
|
List<EntityOperation> getOperations();
|
|
|
|
|
|
2019-03-03 12:43:09 +00:00
|
|
|
@Query("SELECT * FROM operation WHERE name = :name")
|
|
|
|
|
List<EntityOperation> getOperations(String name);
|
|
|
|
|
|
2020-03-11 09:11:35 +01:00
|
|
|
@Query("SELECT * FROM operation WHERE account = :account AND name = :name")
|
|
|
|
|
List<EntityOperation> getOperations(long account, String name);
|
|
|
|
|
|
2019-07-24 10:06:56 +02:00
|
|
|
@Query("SELECT * FROM operation WHERE id = :id")
|
|
|
|
|
EntityOperation getOperation(long id);
|
|
|
|
|
|
2020-09-17 16:33:52 +02:00
|
|
|
@Query("SELECT * FROM operation WHERE message = :message AND name = :name")
|
|
|
|
|
EntityOperation getOperation(long message, String name);
|
|
|
|
|
|
2018-12-11 18:59:02 +01:00
|
|
|
@Query("SELECT * FROM operation WHERE error IS NOT NULL")
|
|
|
|
|
List<EntityOperation> getOperationsError();
|
2018-12-11 09:47:37 +01:00
|
|
|
|
2019-06-07 18:33:35 +02:00
|
|
|
@Query("SELECT COUNT(id) FROM operation" +
|
2018-12-02 14:19:54 +01:00
|
|
|
" WHERE folder = :folder" +
|
2019-08-03 20:44:30 +02:00
|
|
|
" AND (:name IS NULL OR name = :name)")
|
2018-12-02 14:19:54 +01:00
|
|
|
int getOperationCount(long folder, String name);
|
2018-08-04 22:35:47 +00:00
|
|
|
|
2019-06-07 18:33:35 +02:00
|
|
|
@Query("SELECT COUNT(id) FROM operation" +
|
2018-12-25 15:01:21 +00:00
|
|
|
" WHERE folder = :folder" +
|
2019-08-03 20:44:30 +02:00
|
|
|
" AND message = :message")
|
2018-12-25 15:01:21 +00:00
|
|
|
int getOperationCount(long folder, long message);
|
|
|
|
|
|
2019-08-03 20:44:30 +02:00
|
|
|
@Query("SELECT COUNT(id) FROM operation" +
|
|
|
|
|
" WHERE folder = :folder" +
|
|
|
|
|
" AND message = :message" +
|
|
|
|
|
" AND name = :name")
|
|
|
|
|
int getOperationCount(long folder, long message, String name);
|
|
|
|
|
|
2020-11-29 12:14:01 +01:00
|
|
|
@Query("UPDATE operation SET tries = :tries WHERE id = :id AND NOT (tries IS :tries)")
|
2020-02-11 17:46:04 +01:00
|
|
|
int setOperationTries(long id, int tries);
|
|
|
|
|
|
2020-11-29 12:14:01 +01:00
|
|
|
@Query("UPDATE operation SET state = :state WHERE id = :id AND NOT (state IS :state)")
|
2019-05-11 21:04:27 +02:00
|
|
|
int setOperationState(long id, String state);
|
|
|
|
|
|
2020-12-31 14:12:21 +01:00
|
|
|
@Query("UPDATE operation SET state = NULL" +
|
|
|
|
|
" WHERE account = :account" +
|
|
|
|
|
" AND state IS NOT NULL" +
|
|
|
|
|
" AND name <> '" + EntityOperation.SEND + "'")
|
|
|
|
|
int resetOperationStates(long account);
|
2020-02-20 12:03:27 +01:00
|
|
|
|
2020-11-29 12:14:01 +01:00
|
|
|
@Query("UPDATE operation SET error = :error WHERE id = :id AND NOT (error IS :error)")
|
2018-12-01 15:13:57 +01:00
|
|
|
int setOperationError(long id, String error);
|
|
|
|
|
|
2018-08-12 08:16:41 +00:00
|
|
|
@Insert
|
|
|
|
|
long insertOperation(EntityOperation operation);
|
|
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
@Query("DELETE FROM operation WHERE id = :id")
|
2019-03-07 08:45:10 +00:00
|
|
|
int deleteOperation(long id);
|
2020-12-02 16:58:32 +01:00
|
|
|
|
|
|
|
|
@Query("DELETE FROM operation WHERE folder = :folder AND name = :name")
|
|
|
|
|
int deleteOperation(long folder, String name);
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|