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

1042 lines
52 KiB
Java
Raw Normal View History

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
2024-01-01 07:50:49 +00:00
Copyright 2018-2024 by Marcel Bokhorst (M66B)
2018-08-02 13:33:06 +00:00
*/
2019-07-12 11:57:41 +00:00
import android.database.Cursor;
2018-08-08 06:55:47 +00:00
import androidx.lifecycle.LiveData;
import androidx.paging.DataSource;
import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.Query;
2018-11-19 04:49:24 +00:00
import androidx.room.RoomWarnings;
2020-01-12 10:29:23 +00:00
import androidx.room.Transaction;
2018-08-08 06:55:47 +00:00
import androidx.room.Update;
import java.util.List;
2018-08-02 13:33:06 +00:00
@Dao
public interface DaoMessage {
2018-08-07 15:06:18 +00:00
// About 'dummy': "When the min() or max() aggregate functions are used in an aggregate query,
// all bare columns in the result set take values from the input row which also contains the minimum or maximum."
// https://www.sqlite.org/lang_select.html
2019-09-11 10:37:50 +00:00
String is_drafts = "folder.type = '" + EntityFolder.DRAFTS + "'";
String is_outbox = "folder.type = '" + EntityFolder.OUTBOX + "'";
String is_sent = "folder.type = '" + EntityFolder.SENT + "'";
2021-12-30 11:19:26 +00:00
String is_outgoing = is_drafts + " OR " + is_outbox + " OR " + is_sent;
2019-09-11 10:37:50 +00:00
2020-06-04 16:19:10 +00:00
@Transaction
@SuppressWarnings(RoomWarnings.CURSOR_MISMATCH)
2018-12-11 13:14:26 +00:00
@Query("SELECT message.*" +
2021-10-04 08:41:03 +00:00
", account.pop AS accountProtocol, account.name AS accountName, account.category AS accountCategory, COALESCE(identity.color, folder.color, account.color) AS accountColor" +
", account.notify AS accountNotify, account.summary AS accountSummary, account.leave_deleted AS accountLeaveDeleted, account.auto_seen AS accountAutoSeen" +
2022-08-12 11:57:15 +00:00
", folder.name AS folderName, folder.color AS folderColor, folder.display AS folderDisplay, folder.type AS folderType, NULL AS folderInheritedType, folder.unified AS folderUnified, folder.read_only AS folderReadOnly" +
2021-04-24 15:55:21 +00:00
", IFNULL(identity.display, identity.name) AS identityName, identity.email AS identityEmail, identity.color AS identityColor, identity.synchronize AS identitySynchronize" +
", '[' || substr(group_concat(message.`from`, ','), 0, 2048) || ']' AS senders" +
", '[' || substr(group_concat(message.`to`, ','), 0, 2048) || ']' AS recipients" +
2018-12-30 07:58:02 +00:00
", COUNT(message.id) AS count" +
2019-07-24 17:56:00 +00:00
", SUM(1 - message.ui_seen) AS unseen" +
", SUM(1 - message.ui_flagged) AS unflagged" +
2019-11-17 11:35:21 +00:00
", SUM(folder.type = '" + EntityFolder.DRAFTS + "') AS drafts" +
", COUNT(DISTINCT" +
2020-04-05 06:46:10 +00:00
" CASE WHEN NOT message.hash IS NULL THEN message.hash" +
" WHEN NOT message.msgid IS NULL THEN message.msgid" +
" ELSE message.id END) AS visible" +
", COUNT(DISTINCT" +
" CASE WHEN message.ui_seen THEN NULL" +
" WHEN NOT message.hash IS NULL THEN message.hash" +
" WHEN NOT message.msgid IS NULL THEN message.msgid" +
" ELSE message.id END) AS visible_unseen" +
2022-02-26 14:55:48 +00:00
", SUM(message.attachments) AS totalAttachments" +
2019-09-30 19:00:28 +00:00
", SUM(message.total) AS totalSize" +
2020-04-30 07:46:58 +00:00
", message.priority AS ui_priority" +
", message.importance AS ui_importance" +
", MAX(CASE WHEN" +
2021-12-30 11:19:26 +00:00
" (:found AND folder.type <> '" + EntityFolder.ARCHIVE + "' AND NOT (" + is_outgoing + "))" +
" OR (NOT :found AND :type IS NULL AND folder.unified)" +
2021-12-30 11:19:26 +00:00
" OR (NOT :found AND folder.type = :type)" +
" THEN message.received ELSE 0 END) AS dummy" +
2023-12-20 14:05:28 +00:00
" FROM (SELECT * FROM message" +
" WHERE message.thread IN" +
" (SELECT DISTINCT mm.thread FROM folder ff" +
" JOIN message mm ON mm.folder = ff.id" +
" WHERE ((:found AND mm.ui_found)" +
" OR (NOT :found AND :type IS NULL AND ff.unified)" +
" OR (NOT :found AND :type IS NOT NULL AND ff.type = :type))" +
" AND (NOT mm.ui_hide OR :debug))" +
" ORDER BY received DESC) AS message" + // group_concat
2020-01-22 18:53:17 +00:00
" JOIN account_view AS account ON account.id = message.account" +
" LEFT JOIN identity_view AS identity ON identity.id = message.identity" +
2020-01-22 15:18:21 +00:00
" JOIN folder_view AS folder ON folder.id = message.folder" +
" WHERE account.`synchronize`" +
2019-12-26 18:13:41 +00:00
" AND (:threading OR (:type IS NULL AND (folder.unified OR :found)) OR (:type IS NOT NULL AND folder.type = :type))" +
2019-09-27 16:25:55 +00:00
" AND (NOT message.ui_hide OR :debug)" +
" AND (NOT :found OR message.ui_found = :found)" +
" GROUP BY account.id, CASE WHEN message.thread IS NULL OR NOT :threading THEN message.id ELSE message.thread END" +
2020-05-08 14:56:01 +00:00
" HAVING (SUM((:found AND message.ui_found)" +
" OR (NOT :found AND :type IS NULL AND folder.unified)" +
" OR (NOT :found AND :type IS NOT NULL AND folder.type = :type)) > 0)" + // thread can be the same in different accounts
2020-05-08 11:34:14 +00:00
" AND (NOT :filter_seen OR SUM(1 - message.ui_seen) > 0)" +
2019-07-24 17:56:00 +00:00
" AND (NOT :filter_unflagged OR COUNT(message.id) - SUM(1 - message.ui_flagged) > 0)" +
2020-02-06 07:15:44 +00:00
" AND (NOT :filter_unknown OR SUM(message.avatar IS NOT NULL AND message.sender <> identity.email) > 0)" +
2019-09-11 10:37:50 +00:00
" AND (NOT :filter_snoozed OR message.ui_snoozed IS NULL OR " + is_drafts + ")" +
2021-12-26 20:37:54 +00:00
" AND (NOT :filter_deleted OR NOT message.ui_deleted)" +
" AND (:filter_language IS NULL OR SUM(message.language = :filter_language) > 0)" +
" ORDER BY CASE WHEN :found THEN 0 ELSE -IFNULL(message.importance, 1) END" +
", CASE WHEN :group_category THEN account.category ELSE '' END COLLATE NOCASE" +
2020-02-01 12:51:32 +00:00
", CASE" +
" WHEN 'unread' = :sort THEN SUM(1 - message.ui_seen) = 0" +
" WHEN 'starred' = :sort THEN COUNT(message.id) - SUM(1 - message.ui_flagged) = 0" +
2020-04-30 07:46:58 +00:00
" WHEN 'priority' = :sort THEN -IFNULL(message.priority, 1)" +
2020-02-01 12:51:32 +00:00
" WHEN 'sender' = :sort THEN LOWER(message.sender)" +
" WHEN 'subject' = :sort THEN LOWER(message.subject)" +
" WHEN 'size' = :sort THEN -SUM(message.total)" +
" WHEN 'attachments' = :sort THEN -SUM(message.attachments)" +
" WHEN 'snoozed' = :sort THEN SUM(CASE WHEN message.ui_snoozed IS NULL THEN 0 ELSE 1 END) = 0" +
" ELSE 0" +
" END" +
2023-12-11 16:04:35 +00:00
", CASE WHEN :ascending THEN message.received ELSE -message.received END")
2019-07-19 06:27:44 +00:00
DataSource.Factory<Integer, TupleMessageEx> pagedUnified(
String type,
boolean threading, boolean group_category,
String sort, boolean ascending,
2021-12-26 20:37:54 +00:00
boolean filter_seen, boolean filter_unflagged, boolean filter_unknown, boolean filter_snoozed, boolean filter_deleted, String filter_language,
2019-05-13 10:57:20 +00:00
boolean found,
boolean debug);
2018-08-02 13:33:06 +00:00
2020-06-04 16:19:10 +00:00
@Transaction
@SuppressWarnings(RoomWarnings.CURSOR_MISMATCH)
2018-12-11 13:14:26 +00:00
@Query("SELECT message.*" +
2021-10-04 08:41:03 +00:00
", account.pop AS accountProtocol, account.name AS accountName, account.category AS accountCategory, COALESCE(identity.color, folder.color, account.color) AS accountColor" +
", account.notify AS accountNotify, account.summary AS accountSummary, account.leave_deleted AS accountLeaveDeleted, account.auto_seen AS accountAutoSeen" +
2022-08-12 11:57:15 +00:00
", folder.name AS folderName, folder.color AS folderColor, folder.display AS folderDisplay, folder.type AS folderType, f.inherited_type AS folderInheritedType, folder.unified AS folderUnified, folder.read_only AS folderReadOnly" +
2021-04-24 15:55:21 +00:00
", IFNULL(identity.display, identity.name) AS identityName, identity.email AS identityEmail, identity.color AS identityColor, identity.synchronize AS identitySynchronize" +
", '[' || substr(group_concat(message.`from`, ','), 0, 2048) || ']' AS senders" +
", '[' || substr(group_concat(message.`to`, ','), 0, 2048) || ']' AS recipients" +
2018-12-30 07:58:02 +00:00
", COUNT(message.id) AS count" +
2019-07-24 17:56:00 +00:00
", SUM(1 - message.ui_seen) AS unseen" +
", SUM(1 - message.ui_flagged) AS unflagged" +
2019-11-17 11:35:21 +00:00
", SUM(folder.type = '" + EntityFolder.DRAFTS + "') AS drafts" +
", COUNT(DISTINCT" +
2020-04-05 06:46:10 +00:00
" CASE WHEN NOT message.hash IS NULL THEN message.hash" +
" WHEN NOT message.msgid IS NULL THEN message.msgid" +
" ELSE message.id END) AS visible" +
", COUNT(DISTINCT" +
" CASE WHEN message.ui_seen THEN NULL" +
" WHEN NOT message.hash IS NULL THEN message.hash" +
" WHEN NOT message.msgid IS NULL THEN message.msgid" +
" ELSE message.id END) AS visible_unseen" +
2022-02-26 14:55:48 +00:00
", SUM(message.attachments) AS totalAttachments" +
2019-09-30 19:00:28 +00:00
", SUM(message.total) AS totalSize" +
2020-04-30 07:46:58 +00:00
", message.priority AS ui_priority" +
", message.importance AS ui_importance" +
2021-12-30 11:19:26 +00:00
", MAX(CASE WHEN" +
" (:found AND folder.type <> '" + EntityFolder.ARCHIVE + "' AND NOT (" + is_outgoing + "))" +
" OR (NOT :found AND folder.id = :folder)" +
" THEN message.received ELSE 0 END) AS dummy" +
2023-12-20 14:05:28 +00:00
" FROM (SELECT * FROM message" +
" WHERE message.thread IN" +
" (SELECT DISTINCT mm.thread FROM message mm" +
" WHERE mm.folder = :folder" +
" AND (NOT mm.ui_hide OR :debug)" +
" AND (NOT :found OR mm.ui_found))" +
" ORDER BY received DESC) AS message" + // group_concat
2020-01-22 18:53:17 +00:00
" JOIN account_view AS account ON account.id = message.account" +
" LEFT JOIN identity_view AS identity ON identity.id = message.identity" +
2020-01-22 15:18:21 +00:00
" JOIN folder_view AS folder ON folder.id = message.folder" +
" JOIN folder_view AS f ON f.id = :folder" +
2022-04-04 17:40:38 +00:00
" WHERE (message.account = f.account OR message.account = identity.account OR " + is_outbox + ")" +
" AND (:threading OR folder.id = :folder)" +
2019-09-27 16:25:55 +00:00
" AND (NOT message.ui_hide OR :debug)" +
" AND (NOT :found OR message.ui_found = :found)" +
" GROUP BY CASE WHEN message.thread IS NULL OR NOT :threading THEN message.id ELSE message.thread END" +
2020-05-01 15:30:40 +00:00
" HAVING (NOT :filter_seen OR SUM(1 - message.ui_seen) > 0 OR " + is_outbox + ")" +
2019-07-24 17:56:00 +00:00
" AND (NOT :filter_unflagged OR COUNT(message.id) - SUM(1 - message.ui_flagged) > 0 OR " + is_outbox + ")" +
2020-02-06 07:15:44 +00:00
" AND (NOT :filter_unknown OR SUM(message.avatar IS NOT NULL AND message.sender <> identity.email) > 0" +
" OR " + is_outbox + " OR " + is_drafts + " OR " + is_sent + ")" +
2019-09-11 10:37:50 +00:00
" AND (NOT :filter_snoozed OR message.ui_snoozed IS NULL OR " + is_outbox + " OR " + is_drafts + ")" +
2021-12-26 20:37:54 +00:00
" AND (NOT :filter_deleted OR NOT message.ui_deleted)" +
2020-03-28 11:34:41 +00:00
" AND (:filter_language IS NULL OR SUM(message.language = :filter_language) > 0 OR " + is_outbox + ")" +
" ORDER BY CASE WHEN :found THEN 0 ELSE -IFNULL(message.importance, 1) END" +
2020-02-01 12:51:32 +00:00
", CASE" +
" WHEN 'unread' = :sort THEN SUM(1 - message.ui_seen) = 0" +
" WHEN 'starred' = :sort THEN COUNT(message.id) - SUM(1 - message.ui_flagged) = 0" +
2020-04-30 07:46:58 +00:00
" WHEN 'priority' = :sort THEN -IFNULL(message.priority, 1)" +
2020-02-01 12:51:32 +00:00
" WHEN 'sender' = :sort THEN LOWER(message.sender)" +
" WHEN 'subject' = :sort THEN LOWER(message.subject)" +
" WHEN 'size' = :sort THEN -SUM(message.total)" +
" WHEN 'attachments' = :sort THEN -SUM(message.attachments)" +
" WHEN 'snoozed' = :sort THEN SUM(CASE WHEN message.ui_snoozed IS NULL THEN 0 ELSE 1 END) = 0" +
" ELSE 0" +
" END" +
2023-12-11 16:04:35 +00:00
", CASE WHEN :ascending THEN message.received ELSE -message.received END")
2019-04-28 13:16:26 +00:00
DataSource.Factory<Integer, TupleMessageEx> pagedFolder(
2019-05-13 10:57:20 +00:00
long folder, boolean threading,
String sort, boolean ascending,
2021-12-26 20:37:54 +00:00
boolean filter_seen, boolean filter_unflagged, boolean filter_unknown, boolean filter_snoozed, boolean filter_deleted, String filter_language,
2019-05-13 10:57:20 +00:00
boolean found,
boolean debug);
2018-08-02 13:33:06 +00:00
2020-06-04 16:19:10 +00:00
@Transaction
2018-09-12 16:54:48 +00:00
@Query("SELECT message.*" +
2021-10-04 08:41:03 +00:00
", account.pop AS accountProtocol, account.name AS accountName, account.category AS accountCategory, COALESCE(identity.color, folder.color, account.color) AS accountColor" +
", account.notify AS accountNotify, account.summary AS accountSummary, account.leave_deleted AS accountLeaveDeleted, account.auto_seen AS accountAutoSeen" +
2022-08-12 11:57:15 +00:00
", folder.name AS folderName, folder.color AS folderColor, folder.display AS folderDisplay, folder.type AS folderType, NULL AS folderInheritedType, folder.unified AS folderUnified, folder.read_only AS folderReadOnly" +
2021-04-24 15:55:21 +00:00
", IFNULL(identity.display, identity.name) AS identityName, identity.email AS identityEmail, identity.color AS identityColor, identity.synchronize AS identitySynchronize" +
", message.`from` AS senders" +
", message.`to` AS recipients" +
2018-12-30 07:58:02 +00:00
", 1 AS count" +
2018-09-28 15:10:18 +00:00
", CASE WHEN message.ui_seen THEN 0 ELSE 1 END AS unseen" +
2018-09-28 15:12:51 +00:00
", CASE WHEN message.ui_flagged THEN 0 ELSE 1 END AS unflagged" +
2019-11-17 11:35:21 +00:00
", (folder.type = '" + EntityFolder.DRAFTS + "') AS drafts" +
2018-12-30 07:58:02 +00:00
", 1 AS visible" +
", NOT message.ui_seen AS visible_unseen" +
2022-02-26 14:55:48 +00:00
", attachments AS totalAttachments" +
2019-09-30 19:00:28 +00:00
", message.total AS totalSize" +
2020-02-01 13:29:47 +00:00
", message.priority AS ui_priority" +
", message.importance AS ui_importance" +
2018-08-02 13:33:06 +00:00
" FROM message" +
2020-01-22 18:53:17 +00:00
" JOIN account_view AS account ON account.id = message.account" +
" LEFT JOIN identity_view AS identity ON identity.id = message.identity" +
2020-01-22 15:18:21 +00:00
" JOIN folder_view AS folder ON folder.id = message.folder" +
2018-10-17 09:15:44 +00:00
" WHERE message.account = :account" +
" AND message.thread = :thread" +
" AND (:id IS NULL OR message.id = :id)" +
2020-07-27 12:41:58 +00:00
" AND (NOT :filter_archive" +
" OR folder.type <> '" + EntityFolder.ARCHIVE + "'" +
" OR NOT EXISTS" +
" (SELECT * FROM message m" +
" WHERE m.id <> message.id" +
" AND m.thread = message.thread" +
2020-04-26 15:32:44 +00:00
" AND (m.hash = message.hash OR m.msgid = message.msgid)" +
2020-07-27 12:41:58 +00:00
" AND NOT m.ui_hide))" +
2019-09-27 16:25:55 +00:00
" AND (NOT message.ui_hide OR :debug)" +
" ORDER BY CASE WHEN :ascending THEN message.received ELSE -message.received END" +
2019-10-02 07:35:06 +00:00
", CASE" +
" WHEN folder.type = '" + EntityFolder.INBOX + "' THEN 1" +
" WHEN folder.type = '" + EntityFolder.OUTBOX + "' THEN 2" +
" WHEN folder.type = '" + EntityFolder.DRAFTS + "' THEN 3" +
" WHEN folder.type = '" + EntityFolder.SENT + "' THEN 4" +
2020-02-10 10:33:57 +00:00
" WHEN folder.type = '" + EntityFolder.TRASH + "' THEN 5" +
" WHEN folder.type = '" + EntityFolder.JUNK + "' THEN 6" +
" WHEN folder.type = '" + EntityFolder.SYSTEM + "' THEN 7" +
" WHEN folder.type = '" + EntityFolder.USER + "' THEN 8" +
" WHEN folder.type = '" + EntityFolder.ARCHIVE + "' THEN 9" +
2019-10-02 07:35:06 +00:00
" ELSE 999 END")
DataSource.Factory<Integer, TupleMessageEx> pagedThread(
long account, String thread, Long id,
boolean filter_archive,
boolean ascending, boolean debug);
2018-08-02 13:33:06 +00:00
2021-07-03 13:13:20 +00:00
@Query("SELECT COUNT(*) AS pending, SUM(message.error IS NOT NULL) AS errors" +
" FROM message" +
2021-03-31 16:07:58 +00:00
" JOIN folder_view AS folder ON folder.id = message.folder" +
" WHERE " + is_outbox +
2021-07-04 18:45:26 +00:00
" AND NOT message.ui_hide" +
2021-07-03 13:13:20 +00:00
" AND (NOT message.ui_snoozed IS NULL OR message.error IS NOT NULL)")
LiveData<TupleOutboxStats> liveOutboxPending();
2021-03-31 16:07:58 +00:00
2019-05-05 11:58:15 +00:00
@Query("SELECT account.name AS accountName" +
", COUNT(message.id) AS count" +
", SUM(message.ui_seen) AS seen" +
" FROM message" +
2020-01-22 18:53:17 +00:00
" JOIN account_view AS account ON account.id = message.account" +
2020-04-01 09:09:45 +00:00
" JOIN folder_view AS folder ON folder.id = message.folder" +
2019-05-05 11:58:15 +00:00
" WHERE message.account = :account" +
" AND message.thread = :thread" +
" AND (:id IS NULL OR message.id = :id)" +
2020-04-01 09:09:45 +00:00
" AND (NOT :filter_archive OR folder.type <> '" + EntityFolder.ARCHIVE +
"' OR (SELECT COUNT(m.id) FROM message m" +
" WHERE m.account = message.account" +
" AND (m.hash = message.hash OR m.msgid = message.msgid)) = 1)" +
2019-09-27 16:25:55 +00:00
" AND NOT message.ui_hide" +
2019-05-05 11:58:15 +00:00
" GROUP BY account.id")
2020-04-01 09:09:45 +00:00
LiveData<TupleThreadStats> liveThreadStats(long account, String thread, Long id, boolean filter_archive);
2019-05-05 11:58:15 +00:00
2019-04-11 15:12:20 +00:00
@Query("SELECT id FROM message" +
" WHERE account = :account" +
" AND thread = :thread" +
2019-09-27 16:25:55 +00:00
" AND ui_hide")
LiveData<List<Long>> liveHiddenThread(long account, String thread);
2019-04-11 15:12:20 +00:00
@Query("SELECT message.id FROM message" +
2021-10-14 07:41:21 +00:00
" JOIN folder_view AS folder ON folder.id = message.folder" +
" WHERE message.account = :account" +
" AND message.thread = :thread" +
2021-10-15 05:35:48 +00:00
" AND folder.type <> '" + EntityFolder.DRAFTS + "'" +
" AND folder.type <> '" + EntityFolder.OUTBOX + "'" +
" AND folder.type <> '" + EntityFolder.SENT + "'" +
2021-10-14 07:41:21 +00:00
" AND folder.type <> '" + EntityFolder.ARCHIVE + "'" +
" AND NOT ui_seen" +
" AND NOT ui_hide")
LiveData<List<Long>> liveUnreadThread(long account, String thread);
2021-10-14 07:41:21 +00:00
2022-10-14 07:02:53 +00:00
static String FTS_STATS = "SELECT SUM(fts) AS fts, COUNT(*) AS total FROM message" +
2020-01-22 15:18:21 +00:00
" JOIN folder_view AS folder ON folder.id = message.folder" +
" JOIN account_view AS account ON account.id = message.account" +
2020-01-15 09:29:16 +00:00
" WHERE content" +
" AND account.synchronize" +
2022-10-14 07:02:53 +00:00
" AND folder.type <> '" + EntityFolder.OUTBOX + "'";
@Query(FTS_STATS)
2020-01-15 09:29:16 +00:00
LiveData<TupleFtsStats> liveFts();
2022-10-14 07:02:53 +00:00
@Query(FTS_STATS)
TupleFtsStats getFts();
2021-03-20 16:10:15 +00:00
@Query("SELECT COUNT(*) FROM message" +
" WHERE id IN (:ids)" +
2022-01-31 18:41:16 +00:00
" AND (raw IS NULL OR NOT raw)")
2021-03-20 16:10:15 +00:00
LiveData<Integer> liveRaw(long[] ids);
@Query("SELECT *" +
2018-08-21 14:37:46 +00:00
" FROM message" +
" WHERE id = :id")
EntityMessage getMessage(long id);
2018-08-21 14:37:46 +00:00
@Query("SELECT *" +
" FROM message" +
" WHERE folder = :folder" +
2018-12-06 10:59:57 +00:00
" AND uid = :uid")
EntityMessage getMessageByUid(long folder, long uid);
2018-08-02 13:33:06 +00:00
2018-11-04 08:59:09 +00:00
@Query("SELECT id" +
" FROM message" +
2018-10-20 17:56:09 +00:00
" WHERE folder = :folder" +
2019-09-27 16:25:55 +00:00
" AND NOT ui_hide" +
" ORDER BY message.received DESC")
2018-12-06 10:59:57 +00:00
List<Long> getMessageByFolder(long folder);
2019-01-23 10:57:52 +00:00
@Query("SELECT id" +
2019-10-23 18:01:43 +00:00
" FROM message" +
" WHERE (:folder IS NULL OR folder = :folder)" +
" AND NOT ui_hide" +
" ORDER BY message.received DESC")
List<Long> getMessageIdsByFolder(Long folder);
2021-04-06 09:58:54 +00:00
@Transaction
2020-01-15 09:29:16 +00:00
@Query("SELECT message.id FROM message" +
2020-01-22 15:18:21 +00:00
" JOIN folder_view AS folder ON folder.id = message.folder" +
" JOIN account_view AS account ON account.id = message.account" +
2022-11-04 07:15:03 +00:00
" WHERE NOT fts" +
" AND account.synchronize" +
2020-01-15 09:29:16 +00:00
" AND folder.type <> '" + EntityFolder.OUTBOX + "'" +
2020-01-15 12:16:52 +00:00
" ORDER BY message.received")
2022-02-15 09:17:38 +00:00
Cursor getMessageFts();
2020-01-14 21:39:35 +00:00
2020-06-19 13:18:43 +00:00
@Query("SELECT message.id, account, thread, (:find IS NULL" +
2022-10-12 06:54:06 +00:00
//" OR (:senders AND `from` LIKE :find COLLATE NOCASE)" + // no index
//" OR (:recipients AND `to` LIKE :find COLLATE NOCASE)" + // no index
//" OR (:recipients AND `cc` LIKE :find COLLATE NOCASE)" + // no index
//" OR (:recipients AND `bcc` LIKE :find COLLATE NOCASE)" + // no index
//" OR (:subject AND `subject` LIKE :find COLLATE NOCASE)" + // unsuitable index
//" OR (:keywords AND `keywords` LIKE :find COLLATE NOCASE)" + // no index
//" OR (:message AND `preview` LIKE :find COLLATE NOCASE)" + // no index
//" OR (:notes AND `notes` LIKE :find COLLATE NOCASE)" + // no index
//" OR (:headers AND `headers` LIKE :find COLLATE NOCASE)" + // no index
2023-01-24 09:48:34 +00:00
//" OR (attachment.name LIKE :find COLLATE NOCASE)" + // no index
//" OR (attachment.type LIKE :find COLLATE NOCASE)" +
") AS matched" + // no index
2019-01-23 10:57:52 +00:00
" FROM message" +
2020-06-19 13:18:43 +00:00
" LEFT JOIN attachment ON attachment.message = message.id" +
2019-11-09 18:39:41 +00:00
" WHERE NOT ui_hide" +
2020-02-02 16:17:58 +00:00
" AND (:account IS NULL OR account = :account)" +
2019-11-09 18:39:41 +00:00
" AND (:folder IS NULL OR folder = :folder)" +
2020-04-09 15:50:29 +00:00
" AND (NOT :unseen OR NOT ui_seen)" +
" AND (NOT :flagged OR ui_flagged)" +
" AND (NOT :hidden OR NOT ui_snoozed IS NULL)" +
" AND (NOT :encrypted OR ui_encrypt > 0)" +
" AND (NOT :with_attachments OR attachments > 0)" +
" AND (NOT :with_notes OR NOT `notes` IS NULL)" +
2020-06-19 15:39:02 +00:00
" AND (:type_count = 0 OR attachment.type IN (:types))" +
2020-06-13 05:01:00 +00:00
" AND (:size IS NULL OR total > :size)" +
2020-04-10 10:26:23 +00:00
" AND (:after IS NULL OR received > :after)" +
" AND (:before IS NULL OR received < :before)" +
" AND NOT message.folder IN (:exclude)" +
2021-03-14 19:52:49 +00:00
" GROUP BY message.id" +
2022-10-11 17:01:43 +00:00
" ORDER BY received DESC" +
2019-10-29 08:46:39 +00:00
" LIMIT :limit OFFSET :offset")
List<TupleMatch> matchMessages(
Long account, Long folder, long[] exclude, String find,
2022-10-12 06:54:06 +00:00
//boolean senders, boolean recipients, boolean subject, boolean keywords, boolean message, boolean notes, boolean headers,
boolean unseen, boolean flagged, boolean hidden, boolean encrypted, boolean with_attachments, boolean with_notes,
2020-06-19 15:39:02 +00:00
int type_count, String[] types,
2020-06-12 16:49:46 +00:00
Integer size,
2020-04-10 10:26:23 +00:00
Long after, Long before,
int limit, int offset);
2019-01-23 10:57:52 +00:00
@Query("SELECT id" +
" FROM message" +
" WHERE content" +
" ORDER BY message.received DESC")
2023-12-09 21:27:09 +00:00
Cursor getMessageWithContent();
2020-06-17 14:16:11 +00:00
@Query("SELECT message.id" +
" FROM message" +
" JOIN account ON account.id = message.account" +
2020-06-17 14:16:11 +00:00
" LEFT JOIN identity_view AS identity ON identity.id = message.identity" +
" JOIN folder_view AS folder ON folder.id = message.folder" +
" WHERE account.`synchronize`" +
" AND CASE" +
2020-06-17 14:16:11 +00:00
" WHEN :folder IS NOT NULL THEN folder.id = :folder" +
" WHEN :type IS NOT NULL THEN folder.type = :type" +
" ELSE folder.unified" +
" END" +
" AND NOT ui_seen" +
" AND (NOT :filter_unflagged OR message.ui_flagged)" +
" AND (NOT :filter_unknown OR (message.avatar IS NOT NULL AND message.sender <> identity.email))" +
" AND (NOT :filter_snoozed OR message.ui_snoozed IS NULL OR " + is_drafts + ")" +
" AND (:filter_language IS NULL OR message.language = :filter_language)")
List<Long> getMessageUnseen(
Long folder, String type,
boolean filter_unflagged, boolean filter_unknown, boolean filter_snoozed, String filter_language);
2019-11-11 13:44:55 +00:00
@Query("SELECT message.*" +
" FROM message" +
2020-01-22 18:53:17 +00:00
" LEFT JOIN account_view AS account ON account.id = message.account" +
" WHERE account = :account" +
" AND thread = :thread" +
" AND (:id IS NULL OR message.id = :id)" +
2018-12-27 11:38:34 +00:00
" AND (:folder IS NULL OR message.folder = :folder)" +
2019-11-23 12:48:59 +00:00
" AND (NOT uid IS NULL OR account.pop <> " + EntityAccount.TYPE_IMAP + ")" +
2019-09-27 16:25:55 +00:00
" AND NOT ui_hide")
2019-07-25 13:38:14 +00:00
List<EntityMessage> getMessagesByThread(long account, String thread, Long id, Long folder);
2019-01-04 18:37:56 +00:00
@Query("SELECT * FROM message" +
" WHERE account = :account" +
" AND msgid = :msgid")
2019-11-06 16:38:55 +00:00
List<EntityMessage> getMessagesByMsgId(long account, String msgid);
2018-08-09 20:45:42 +00:00
@Query("SELECT message.* FROM message" +
" JOIN folder ON folder.id = message.folder" +
" WHERE message.account = :account" +
" AND folder.type = :folderType" +
" AND message.msgid = :msgid")
EntityMessage getMessage(long account, String folderType, String msgid);
2020-05-01 09:14:09 +00:00
@Query("SELECT * FROM message" +
" WHERE account = :account" +
" AND inreplyto = :inreplyto")
List<EntityMessage> getMessagesByInReplyTo(long account, String inreplyto);
2022-01-08 10:33:20 +00:00
@Query("SELECT thread, msgid, hash, inreplyto FROM message" +
" WHERE account = :account" +
2022-03-09 16:35:19 +00:00
" AND (msgid IN (:msgids) OR inreplyto IN (:msgids))" +
2022-03-11 07:53:40 +00:00
" AND (:from IS NULL OR received IS NULL OR received > :from)" +
" AND (:to IS NULL OR received IS NULL OR received < :to)")
2022-03-09 18:59:29 +00:00
List<TupleThreadInfo> getThreadInfo(long account, List<String> msgids, Long from, Long to);
2022-01-08 10:33:20 +00:00
@Query("SELECT * FROM message" +
" WHERE account = :account" +
" AND sender = :sender" +
2021-12-17 19:48:30 +00:00
" AND subject = :subject" +
" AND received >= :since")
List<EntityMessage> getMessagesBySubject(long account, String sender, String subject, long since);
2021-12-10 09:09:21 +00:00
@Query("SELECT message.* FROM message" +
2021-12-08 10:10:33 +00:00
" LEFT JOIN message AS base ON base.id = :id" +
" WHERE message.account = :account" +
" AND (message.id = :id" +
2022-12-14 16:00:43 +00:00
" OR (message.msgid = :msgid AND message.folder <> base.folder)" +
" OR (NOT :hash IS NULL AND message.hash IS :hash))")
List<EntityMessage> getMessagesBySimilarity(long account, long id, String msgid, String hash);
2019-01-04 18:37:56 +00:00
@Query("SELECT COUNT(*) FROM message" +
" WHERE folder = :folder" +
2022-10-11 06:41:44 +00:00
" AND msgid = :msgid" +
" AND (:hidden OR NOT message.ui_hide)")
int countMessageByMsgId(long folder, String msgid, boolean hidden);
2019-01-04 18:37:56 +00:00
2020-05-31 12:27:09 +00:00
@Query("SELECT COUNT(*) FROM message" +
" JOIN folder_view AS folder ON folder.id = message.folder" +
" WHERE message.id = :id" +
" AND NOT message.ui_hide" +
" AND (NOT :filter_seen OR NOT message.ui_seen)" +
" AND (NOT :filter_unflagged OR message.ui_flagged)" +
" AND (NOT :filter_snoozed OR message.ui_snoozed IS NULL OR " + is_drafts + ")")
int countVisible(long id, boolean filter_seen, boolean filter_unflagged, boolean filter_snoozed);
2020-05-31 12:27:09 +00:00
2020-06-23 11:35:04 +00:00
@Query("SELECT COUNT(id)" +
" FROM message" +
" WHERE folder = :folder" +
" AND sender = :sender")
int countSender(long folder, String sender);
2022-03-20 06:44:12 +00:00
@Query("SELECT COUNT(*) FROM message" +
" JOIN folder ON folder.id = message.folder" +
" WHERE folder.account IS NULL")
int countOutbox();
2022-01-12 16:13:25 +00:00
@Query("SELECT COUNT(*) FROM message")
int countTotal();
2022-01-22 08:29:57 +00:00
@Query("SELECT COUNT(*) FROM message" +
" WHERE folder = :folder" +
" AND NOT ui_seen")
int countUnseen(long folder);
2023-02-24 16:12:13 +00:00
@Query("SELECT COUNT(*) FROM message" +
" WHERE folder = :folder" +
" AND ui_hide")
int countHidden(long folder);
2022-01-22 08:29:57 +00:00
@Query("SELECT COUNT(*)" +
" FROM message" +
" WHERE folder = :folder" +
" AND notifying <> 0" +
" AND notifying <> " + EntityMessage.NOTIFYING_IGNORE +
" AND NOT (message.ui_seen OR message.ui_ignored OR message.ui_hide)")
int countNotifying(long folder);
2018-09-12 16:54:48 +00:00
@Query("SELECT message.*" +
2021-10-04 08:41:03 +00:00
", account.pop AS accountProtocol, account.name AS accountName, account.category AS accountCategory, identity.color AS accountColor" +
", account.notify AS accountNotify, account.summary AS accountSummary, account.leave_deleted AS accountLeaveDeleted, account.auto_seen AS accountAutoSeen" +
2022-08-12 11:57:15 +00:00
", folder.name AS folderName, folder.color AS folderColor, folder.display AS folderDisplay, folder.type AS folderType, NULL AS folderInheritedType, folder.unified AS folderUnified, folder.read_only AS folderReadOnly" +
2021-04-24 15:55:21 +00:00
", IFNULL(identity.display, identity.name) AS identityName, identity.email AS identityEmail, identity.color AS identityColor, identity.synchronize AS identitySynchronize" +
", message.`from` AS senders" +
", message.`to` AS recipients" +
2018-12-30 07:58:02 +00:00
", 1 AS count" +
2018-09-28 15:12:51 +00:00
", CASE WHEN message.ui_seen THEN 0 ELSE 1 END AS unseen" +
", CASE WHEN message.ui_flagged THEN 0 ELSE 1 END AS unflagged" +
2019-11-17 11:35:21 +00:00
", (folder.type = '" + EntityFolder.DRAFTS + "') AS drafts" +
2018-12-30 07:58:02 +00:00
", 1 AS visible" +
", NOT message.ui_seen AS visible_unseen" +
2022-02-26 14:55:48 +00:00
", message.attachments AS totalAttachments" +
2019-09-30 19:00:28 +00:00
", message.total AS totalSize" +
2020-02-01 13:29:47 +00:00
", message.priority AS ui_priority" +
", message.importance AS ui_importance" +
2018-08-02 13:33:06 +00:00
" FROM message" +
2020-01-22 18:53:17 +00:00
" JOIN account_view AS account ON account.id = message.account" +
" LEFT JOIN identity_view AS identity ON identity.id = message.identity" +
2020-01-22 15:18:21 +00:00
" JOIN folder_view AS folder ON folder.id = message.folder" +
2018-08-02 13:33:06 +00:00
" WHERE message.id = :id")
LiveData<TupleMessageEx> liveMessage(long id);
@Query("SELECT message.keywords AS selected, folder.keywords AS available" +
" FROM message" +
" JOIN folder ON folder.id = message.folder" +
" WHERE message.id = :id")
LiveData<TupleKeyword.Persisted> liveMessageKeywords(long id);
2020-01-12 10:29:23 +00:00
@Transaction
2019-02-27 18:52:38 +00:00
@Query("SELECT message.*" +
2021-10-04 08:41:03 +00:00
", account.pop AS accountProtocol, account.name AS accountName, account.category AS accountCategory, COALESCE(identity.color, folder.color, account.color) AS accountColor" +
", account.notify AS accountNotify, account.summary AS accountSummary, account.leave_deleted AS accountLeaveDeleted, account.auto_seen AS accountAutoSeen" +
2022-08-12 11:57:15 +00:00
", folder.name AS folderName, folder.color AS folderColor, folder.display AS folderDisplay, folder.type AS folderType, NULL AS folderInheritedType, folder.unified AS folderUnified, folder.read_only AS folderReadOnly" +
2021-04-24 15:55:21 +00:00
", IFNULL(identity.display, identity.name) AS identityName, identity.email AS identityEmail, identity.color AS identityColor, identity.synchronize AS identitySynchronize" +
", message.`from` AS senders" +
", message.`to` AS recipients" +
2018-11-06 17:00:07 +00:00
", 1 AS count" +
", 1 AS unseen" +
", 0 AS unflagged" +
2019-01-01 14:26:30 +00:00
", 0 AS drafts" +
2018-12-30 07:58:02 +00:00
", 1 AS visible" +
", NOT message.ui_seen AS visible_unseen" +
2022-02-26 14:55:48 +00:00
", message.attachments AS totalAttachments" +
2019-09-30 19:00:28 +00:00
", message.total AS totalSize" +
2020-02-01 13:29:47 +00:00
", message.priority AS ui_priority" +
", message.importance AS ui_importance" +
2018-11-06 17:00:07 +00:00
" FROM message" +
2020-01-22 18:53:17 +00:00
" JOIN account_view AS account ON account.id = message.account" +
" LEFT JOIN identity_view AS identity ON identity.id = message.identity" +
2020-01-22 15:18:21 +00:00
" JOIN folder_view AS folder ON folder.id = message.folder" +
" WHERE account.`synchronize`" +
" AND folder.notify" +
2021-07-19 15:34:54 +00:00
" AND (account.created IS NULL" +
" OR message.received > account.created" +
" OR message.sent > account.created" +
" OR message.ui_unsnoozed)" +
" AND (uid IS NOT NULL OR account.pop <> " + EntityAccount.TYPE_IMAP + ")" +
2021-01-27 14:48:49 +00:00
" AND message.notifying <> " + EntityMessage.NOTIFYING_IGNORE +
" AND (message.notifying <> 0 OR NOT (message.ui_seen OR message.ui_ignored OR message.ui_hide))" +
" ORDER BY message.received DESC")
LiveData<List<TupleMessageEx>> liveUnseenNotify();
2020-03-28 09:02:00 +00:00
@Transaction
2023-11-05 09:17:59 +00:00
@Query("SELECT account.id AS account, folder.id AS folder," +
2021-02-24 13:05:23 +00:00
" COUNT(message.id) AS unseen," +
" SUM(CASE WHEN account.created IS NULL OR message.received > account.created OR message.sent > account.created THEN NOT ui_ignored ELSE 0 END) AS notifying" +
2020-03-28 09:02:00 +00:00
" FROM message" +
" JOIN account_view AS account ON account.id = message.account" +
" JOIN folder_view AS folder ON folder.id = message.folder" +
" WHERE (:account IS NULL OR account.id = :account)" +
" AND account.`synchronize`" +
" AND folder.notify" +
2021-01-27 14:48:49 +00:00
" AND message.notifying <> " + EntityMessage.NOTIFYING_IGNORE +
2020-03-28 09:02:00 +00:00
" AND NOT (message.ui_seen OR message.ui_hide)" +
2023-11-05 09:17:59 +00:00
" GROUP BY folder.id" +
" ORDER BY folder.id")
2020-03-28 09:02:00 +00:00
LiveData<List<TupleMessageStats>> liveWidgetUnseen(Long account);
2023-11-05 09:17:59 +00:00
@Query("SELECT :account AS account, folder.id AS folder," +
2021-02-24 13:05:23 +00:00
" COUNT(message.id) AS unseen," +
" SUM(CASE WHEN account.created IS NULL OR message.received > account.created OR message.sent > account.created THEN NOT ui_ignored ELSE 0 END) AS notifying" +
2020-03-28 09:02:00 +00:00
" FROM message" +
" JOIN account_view AS account ON account.id = message.account" +
" JOIN folder_view AS folder ON folder.id = message.folder" +
" WHERE (:account IS NULL OR account.id = :account)" +
" AND account.`synchronize`" +
2023-11-05 09:17:59 +00:00
" AND (:folder IS NULL OR folder.id = :folder)" +
2020-03-28 09:02:00 +00:00
" AND folder.notify" +
2021-01-27 14:48:49 +00:00
" AND message.notifying <> " + EntityMessage.NOTIFYING_IGNORE +
2020-03-28 09:02:00 +00:00
" AND NOT (message.ui_seen OR message.ui_hide)")
2023-11-05 09:17:59 +00:00
TupleMessageStats getWidgetUnseen(Long account, Long folder);
2020-03-28 09:02:00 +00:00
2020-01-12 10:29:23 +00:00
@Transaction
@Query("SELECT folder, COUNT(*) AS total" +
2019-11-18 09:14:37 +00:00
", SUM(ui_seen) AS seen" +
", SUM(ui_flagged) AS flagged" +
" FROM message" +
" WHERE NOT ui_hide" +
" AND message.ui_snoozed IS NULL" +
" GROUP BY folder" +
" ORDER BY folder")
LiveData<List<TupleMessageWidgetCount>> liveWidgetUnified();
2019-11-18 09:14:37 +00:00
2022-01-09 13:11:06 +00:00
@SuppressWarnings(RoomWarnings.CURSOR_MISMATCH)
2019-11-18 09:14:37 +00:00
@Query("SELECT message.*" +
", account.name AS accountName, COALESCE(identity.color, folder.color, account.color) AS accountColor" +
", SUM(1 - message.ui_seen) AS unseen" +
", COUNT(message.id) - SUM(message.ui_flagged) AS unflagged" +
", MAX(message.received) AS dummy" +
2019-07-25 13:33:21 +00:00
" FROM message" +
2020-01-22 18:53:17 +00:00
" JOIN account_view AS account ON account.id = message.account" +
2020-01-22 15:18:21 +00:00
" JOIN folder_view AS folder ON folder.id = message.folder" +
" LEFT JOIN identity ON identity.id = message.identity" +
2019-07-25 13:33:21 +00:00
" WHERE account.`synchronize`" +
" AND (:account IS NULL OR account.id = :account)" +
2019-11-18 09:14:37 +00:00
" AND ((:folder IS NULL AND folder.unified) OR folder.id = :folder)" +
2019-09-27 16:25:55 +00:00
" AND NOT message.ui_hide" +
2019-07-25 13:33:21 +00:00
" AND message.ui_snoozed IS NULL" +
" AND (NOT :unseen OR NOT message.ui_seen)" +
" AND (NOT :flagged OR message.ui_flagged)" +
2019-09-21 07:18:17 +00:00
" GROUP BY account.id" +
", CASE WHEN message.thread IS NULL OR NOT :threading THEN message.id ELSE message.thread END" +
2022-05-02 09:07:36 +00:00
" ORDER BY message.received DESC" +
" LIMIT :limit")
List<TupleMessageWidget> getWidgetUnified(Long account, Long folder, boolean threading, boolean unseen, boolean flagged, int limit);
2019-07-25 15:22:00 +00:00
2018-09-01 16:34:16 +00:00
@Query("SELECT uid FROM message" +
" WHERE folder = :folder" +
2019-01-02 09:44:02 +00:00
" AND (:received IS NULL OR received >= :received)" +
2021-10-16 19:45:54 +00:00
" AND NOT uid IS NULL" +
" ORDER BY uid")
2019-01-01 17:43:53 +00:00
List<Long> getUids(long folder, Long received);
2018-08-02 13:33:06 +00:00
2021-08-06 11:36:52 +00:00
@Query("SELECT * FROM message" +
" WHERE folder = :folder" +
" AND (:received IS NULL OR received >= :received)" +
" AND NOT uid IS NULL" +
" AND NOT content")
List<EntityMessage> getMessagesWithoutContent(long folder, Long received);
2022-03-26 12:16:53 +00:00
@Query("SELECT uid FROM message" +
" WHERE folder = :folder" +
" AND ui_deleted" +
" AND NOT uid IS NULL")
List<Long> getDeletedUids(long folder);
2021-05-14 17:03:15 +00:00
@Query("SELECT uid FROM message" +
" WHERE folder = :folder" +
" AND NOT uid IS NULL" +
" AND ((NOT ui_busy IS NULL AND ui_busy > :time)" +
" OR (EXISTS (SELECT * FROM operation WHERE operation.id = message.id)))")
2021-05-14 17:03:15 +00:00
List<Long> getBusyUids(long folder, long time);
2022-10-22 15:33:45 +00:00
@Query("SELECT id, uidl, msgid, ui_hide, ui_busy, ui_flagged FROM message" +
2019-09-20 07:45:36 +00:00
" WHERE folder = :folder")
2020-05-09 06:49:04 +00:00
List<TupleUidl> getUidls(long folder);
2019-09-20 07:45:36 +00:00
2020-04-02 19:13:27 +00:00
@Query("SELECT * FROM message" +
" WHERE (:folder IS NULL OR folder = :folder)" +
" AND NOT ui_snoozed IS NULL")
List<EntityMessage> getSnoozed(Long folder);
2019-01-07 15:05:24 +00:00
2022-07-01 19:54:27 +00:00
@Query("SELECT COUNT(*) FROM message" +
" WHERE NOT ui_snoozed IS NULL" +
" AND ui_snoozed <> " + Long.MAX_VALUE)
int getSnoozedCount();
2019-07-12 11:57:41 +00:00
@Query("SELECT id AS _id, subject AS suggestion FROM message" +
2022-12-22 07:19:39 +00:00
" WHERE :subject" +
" AND (:account IS NULL OR message.account = :account)" +
" AND (:folder IS NULL OR message.folder = :folder)" +
" AND subject LIKE :query" +
" AND NOT message.ui_hide" +
2019-07-12 11:57:41 +00:00
" GROUP BY subject" +
2019-07-12 11:57:41 +00:00
" UNION" +
2019-07-12 11:57:41 +00:00
" SELECT id AS _id, sender AS suggestion FROM message" +
2022-12-22 07:19:39 +00:00
" WHERE :senders" +
" AND (:account IS NULL OR message.account = :account)" +
" AND (:folder IS NULL OR message.folder = :folder)" +
" AND sender LIKE :query" +
" AND NOT message.ui_hide" +
2019-07-12 11:57:41 +00:00
" GROUP BY sender" +
2020-04-09 18:43:28 +00:00
" ORDER BY sender, subject" +
2021-06-07 19:23:56 +00:00
" LIMIT :limit")
2022-12-22 07:19:39 +00:00
Cursor getSuggestions(boolean subject, boolean senders, Long account, Long folder, String query, int limit);
2019-03-31 07:09:32 +00:00
2020-03-27 09:47:49 +00:00
@Query("SELECT language FROM message" +
" WHERE (:account IS NULL OR message.account = :account)" +
" AND (:folder IS NULL OR message.folder = :folder)" +
" AND NOT message.ui_hide" +
2020-03-27 09:47:49 +00:00
" AND NOT message.language IS NULL" +
2021-06-27 16:43:45 +00:00
" GROUP BY language")
2020-03-27 09:47:49 +00:00
List<String> getLanguages(Long account, Long folder);
2018-08-12 15:31:43 +00:00
@Insert
2018-08-02 13:33:06 +00:00
long insertMessage(EntityMessage message);
@Update
2018-09-26 12:53:04 +00:00
int updateMessage(EntityMessage message);
2018-08-02 13:33:06 +00:00
2020-05-01 09:14:09 +00:00
@Query("UPDATE message SET thread = :thread" +
2021-12-17 19:48:30 +00:00
" WHERE account = :account" +
" AND thread = :old AND NOT (:old IS :thread)" +
" AND (:since IS NULL OR received >= :since)")
int updateMessageThread(long account, String old, String thread, Long since);
2019-10-13 18:13:00 +00:00
2020-11-29 11:14:01 +00:00
@Query("UPDATE message SET uid = :uid WHERE id = :id AND NOT (uid IS :uid)")
2019-03-25 17:39:28 +00:00
int setMessageUid(long id, Long uid);
2019-01-28 16:54:14 +00:00
2020-11-29 11:14:01 +00:00
@Query("UPDATE message SET msgid = :msgid WHERE id = :id AND NOT (msgid IS :msgid)")
2019-06-14 06:31:37 +00:00
int setMessageMsgId(long id, String msgid);
2020-11-29 11:14:01 +00:00
@Query("UPDATE message SET hash = :hash WHERE id = :id AND NOT (hash IS :hash)")
2020-04-01 14:17:54 +00:00
int setMessageHash(long id, String hash);
2020-11-29 11:14:01 +00:00
@Query("UPDATE message SET priority = :priority WHERE id = :id AND NOT (priority IS :priority)")
2019-09-30 14:55:58 +00:00
int setMessagePriority(long id, Integer priority);
@Query("UPDATE message SET sensitivity = :sensitivity WHERE id = :id AND NOT (sensitivity IS :sensitivity)")
int setMessageSensitivity(long id, Integer sensitivity);
2020-11-29 11:14:01 +00:00
@Query("UPDATE message SET importance = :importance WHERE id = :id AND NOT (importance IS :importance)")
2020-02-01 12:51:32 +00:00
int setMessageImportance(long id, Integer importance);
2020-11-29 11:14:01 +00:00
@Query("UPDATE message SET receipt_request = :receipt_request WHERE id = :id AND NOT (receipt_request IS :receipt_request)")
2019-10-01 11:56:48 +00:00
int setMessageReceiptRequest(long id, Boolean receipt_request);
2023-03-02 13:08:24 +00:00
@Transaction
2020-11-29 11:14:01 +00:00
@Query("UPDATE message SET notifying = :notifying WHERE id = :id AND NOT (notifying IS :notifying)")
2019-07-06 08:16:42 +00:00
int setMessageNotifying(long id, int notifying);
2020-11-29 11:14:01 +00:00
@Query("UPDATE message SET fts = :fts WHERE id = :id AND NOT (fts IS :fts)")
2020-01-14 21:39:35 +00:00
int setMessageFts(long id, boolean fts);
2020-11-29 11:14:01 +00:00
@Query("UPDATE message SET received = :received WHERE id = :id AND NOT (received IS :received)")
2019-09-01 19:00:15 +00:00
int setMessageReceived(long id, long received);
2020-11-29 11:14:01 +00:00
@Query("UPDATE message SET subject = :subject WHERE id = :id AND NOT (subject IS :subject)")
2020-11-05 13:16:48 +00:00
int setMessageSubject(long id, String subject);
2022-05-28 09:25:08 +00:00
@Query("UPDATE message SET recent = :recent WHERE id = :id AND NOT (recent IS :recent)")
int setMessageRecent(long id, boolean recent);
2020-11-29 11:14:01 +00:00
@Query("UPDATE message SET seen = :seen WHERE id = :id AND NOT (seen IS :seen)")
2018-08-13 14:44:47 +00:00
int setMessageSeen(long id, boolean seen);
2020-11-29 11:14:01 +00:00
@Query("UPDATE message SET flagged = :flagged WHERE id = :id AND NOT (flagged IS :flagged)")
2018-11-25 12:34:08 +00:00
int setMessageFlagged(long id, boolean flagged);
2018-08-13 14:44:47 +00:00
2021-02-11 09:41:07 +00:00
@Query("UPDATE message SET deleted = :deleted WHERE id = :id AND NOT (deleted IS :deleted)")
int setMessageDeleted(long id, boolean deleted);
2020-11-29 11:14:01 +00:00
@Query("UPDATE message SET answered = :answered WHERE id = :id AND NOT (answered IS :answered)")
2018-11-24 18:14:28 +00:00
int setMessageAnswered(long id, boolean answered);
2020-11-29 11:14:01 +00:00
@Query("UPDATE message SET keywords = :keywords WHERE id = :id AND NOT (keywords IS :keywords)")
2018-11-25 12:34:08 +00:00
int setMessageKeywords(long id, String keywords);
2018-11-24 18:14:28 +00:00
2020-11-29 11:14:01 +00:00
@Query("UPDATE message SET labels = :labels WHERE id = :id AND NOT (labels IS :labels)")
2020-06-25 15:55:59 +00:00
int setMessageLabels(long id, String labels);
2020-11-29 11:14:01 +00:00
@Query("UPDATE message SET ui_seen = :ui_seen WHERE id = :id AND NOT (ui_seen IS :ui_seen)")
2018-11-25 12:34:08 +00:00
int setMessageUiSeen(long id, boolean ui_seen);
2018-09-07 15:12:43 +00:00
2020-11-29 09:03:47 +00:00
@Query("UPDATE message" +
" SET ui_flagged = :ui_flagged, color = :color" +
" WHERE id = :id" +
2020-11-29 11:14:01 +00:00
" AND (NOT (ui_flagged IS :ui_flagged) OR NOT (color IS :color))")
2019-09-28 18:25:19 +00:00
int setMessageUiFlagged(long id, boolean ui_flagged, Integer color);
2018-09-07 15:12:43 +00:00
2021-02-11 08:51:48 +00:00
@Query("UPDATE message SET ui_deleted = :ui_deleted WHERE id = :id AND NOT (ui_deleted IS :ui_deleted)")
int setMessageUiDeleted(long id, boolean ui_deleted);
2020-11-29 11:14:01 +00:00
@Query("UPDATE message SET ui_answered = :ui_answered WHERE id = :id AND NOT (ui_answered IS :ui_answered)")
2018-11-25 12:34:08 +00:00
int setMessageUiAnswered(long id, boolean ui_answered);
2020-11-29 11:14:01 +00:00
@Query("UPDATE message SET ui_hide = :ui_hide WHERE id = :id AND NOT (ui_hide IS :ui_hide)")
2019-09-27 16:25:55 +00:00
int setMessageUiHide(long id, Boolean ui_hide);
2018-08-13 14:44:47 +00:00
2022-05-07 14:00:30 +00:00
@Transaction
@Query("UPDATE message SET ui_hide = 1" +
" WHERE folder = :folder" +
2023-04-04 17:15:59 +00:00
" AND NOT ui_flagged" +
2022-05-07 14:00:30 +00:00
" AND id NOT IN (" +
" SELECT id FROM message" +
" WHERE folder = :folder" +
" ORDER BY received DESC" +
2022-05-07 14:00:30 +00:00
" LIMIT :keep)")
int setMessagesUiHide(long folder, int keep);
2022-05-07 14:00:30 +00:00
2023-03-02 13:08:24 +00:00
@Transaction
2020-11-29 11:14:01 +00:00
@Query("UPDATE message SET ui_ignored = :ui_ignored WHERE id = :id AND NOT (ui_ignored IS :ui_ignored)")
2018-10-16 11:29:12 +00:00
int setMessageUiIgnored(long id, boolean ui_ignored);
2023-03-02 13:08:24 +00:00
@Transaction
2021-02-15 07:58:05 +00:00
@Query("UPDATE message SET ui_silent = :ui_silent WHERE id = :id AND NOT (ui_silent IS :ui_silent)")
int setMessageUiSilent(long id, boolean ui_silent);
@Query("UPDATE message SET ui_local_only = :ui_local_only WHERE id = :id AND NOT (ui_local_only IS :ui_local_only)")
int setMessageUiLocalOnly(long id, boolean ui_local_only);
2020-11-29 11:14:01 +00:00
@Query("UPDATE message SET ui_busy = :busy WHERE id = :id AND NOT (ui_busy IS :busy)")
2020-04-14 13:29:01 +00:00
int setMessageUiBusy(long id, Long busy);
2019-10-14 09:29:46 +00:00
2020-11-29 09:03:47 +00:00
@Query("UPDATE message" +
" SET received = :sent, sent = :sent" +
" WHERE id = :id" +
2020-11-29 11:14:01 +00:00
" AND (NOT (received IS :sent) OR NOT (sent IS :sent))")
int setMessageSent(long id, Long sent);
2020-11-29 11:14:01 +00:00
@Query("UPDATE message SET error = :error WHERE id = :id AND NOT (error IS :error)")
2018-08-13 14:44:47 +00:00
int setMessageError(long id, String error);
2020-11-29 11:14:01 +00:00
@Query("UPDATE message SET identity = :identity WHERE id = :id AND NOT (identity IS :identity)")
2020-08-01 06:05:06 +00:00
int setMessageIdentity(long id, Long identity);
2020-11-29 11:14:01 +00:00
@Query("UPDATE message SET revision = :revision WHERE id = :id AND NOT (revision IS :revision)")
2019-04-18 07:12:30 +00:00
int setMessageRevision(long id, Integer revision);
2020-11-29 11:14:01 +00:00
@Query("UPDATE message SET revisions = :revisions WHERE id = :id AND NOT (revisions IS :revisions)")
2019-04-18 09:29:15 +00:00
int setMessageRevisions(long id, Integer revisions);
2020-01-15 11:07:45 +00:00
@Query("UPDATE message" +
2020-03-26 14:25:44 +00:00
" SET content = 0, fts = 0, language = NULL, plain_only = NULL, preview = NULL" +
2020-11-29 09:03:47 +00:00
" WHERE id = :id" +
2020-11-29 11:14:01 +00:00
" AND (NOT (content IS 0)" +
" OR NOT (fts IS 0)" +
" OR language IS NOT NULL" +
" OR plain_only IS NOT NULL" +
" OR preview IS NOT NULL)")
2020-03-26 14:25:44 +00:00
int resetMessageContent(long id);
2020-01-15 11:07:45 +00:00
@Query("UPDATE message" +
2020-03-26 14:25:44 +00:00
" SET content = :content" +
", fts = 0" +
", language = :language" +
", plain_only = :plain_only" +
", preview = :preview" +
", warning = :warning" +
2020-11-29 09:03:47 +00:00
" WHERE id = :id" +
2020-11-29 11:14:01 +00:00
" AND (NOT (content IS :content)" +
" OR NOT (fts IS 0)" +
" OR NOT (language IS :language)" +
" OR NOT (plain_only IS :plain_only)" +
" OR NOT (preview IS :preview)" +
" OR NOT (warning IS :warning))")
2022-02-13 10:36:38 +00:00
int setMessageContent(long id, boolean content, String language, Integer plain_only, String preview, String warning);
2018-09-15 07:22:42 +00:00
2021-04-08 07:40:21 +00:00
@Query("UPDATE message" +
2022-09-29 11:35:53 +00:00
" SET notes = :notes, notes_color = :color, fts = 0" +
2021-04-08 07:40:21 +00:00
" WHERE id = :id" +
" AND NOT (notes IS :notes AND notes_color IS :color)")
int setMessageNotes(long id, String notes, Integer color);
2021-01-21 15:23:00 +00:00
2020-11-29 09:03:47 +00:00
@Query("UPDATE message" +
" SET size = :size, total = :total" +
" WHERE id = :id" +
2020-11-29 11:14:01 +00:00
" AND (NOT (size IS :size) OR NOT (total IS :total))")
2019-09-30 19:00:28 +00:00
int setMessageSize(long id, Long size, Long total);
2019-04-05 07:45:51 +00:00
2020-11-29 11:14:01 +00:00
@Query("UPDATE message SET headers = :headers WHERE id = :id AND NOT (headers IS :headers)")
2018-09-05 07:23:51 +00:00
int setMessageHeaders(long id, String headers);
2020-11-29 11:14:01 +00:00
@Query("UPDATE message SET raw = :raw WHERE id = :id AND NOT (raw IS :raw)")
int setMessageRaw(long id, Boolean raw);
2020-11-29 11:14:01 +00:00
@Query("UPDATE message SET stored = :stored WHERE id = :id AND NOT (stored IS :stored)")
2018-10-28 12:49:13 +00:00
int setMessageStored(long id, long stored);
2020-11-29 11:14:01 +00:00
@Query("UPDATE message SET plain_only = :plain_only WHERE id = :id AND NOT (plain_only IS :plain_only)")
2022-02-13 10:36:38 +00:00
int setMessagePlainOnly(long id, Integer plain_only);
2023-11-10 09:03:28 +00:00
@Query("UPDATE message SET write_below = :write_below WHERE id = :id AND NOT (write_below IS :write_below)")
int setMessageWriteBelow(long id, Boolean write_below);
2020-11-29 11:14:01 +00:00
@Query("UPDATE message SET encrypt = :encrypt WHERE id = :id AND NOT (encrypt IS :encrypt)")
2019-11-27 09:40:43 +00:00
int setMessageEncrypt(long id, Integer encrypt);
2020-11-29 11:14:01 +00:00
@Query("UPDATE message SET ui_encrypt = :ui_encrypt WHERE id = :id AND NOT (ui_encrypt IS :ui_encrypt)")
2020-04-02 09:33:01 +00:00
int setMessageUiEncrypt(long id, Integer ui_encrypt);
2020-01-18 10:28:37 +00:00
2020-11-29 11:14:01 +00:00
@Query("UPDATE message SET verified = :verified WHERE id = :id AND NOT (verified IS :verified)")
2020-06-06 10:14:32 +00:00
int setMessageVerified(long id, boolean verified);
2020-11-29 11:14:01 +00:00
@Query("UPDATE message SET last_attempt = :last_attempt WHERE id = :id AND NOT (last_attempt IS :last_attempt)")
2018-11-19 13:14:02 +00:00
int setMessageLastAttempt(long id, long last_attempt);
@Query("UPDATE message SET ui_ignored = 1" +
2021-07-06 06:13:31 +00:00
" WHERE NOT ui_ignored" +
" AND account IN (" +
" SELECT id FROM account" +
" WHERE :folder IS NOT NULL" +
2021-09-12 06:14:33 +00:00
" OR :type IS NOT NULL" +
2021-07-06 06:13:31 +00:00
" OR id = :account" +
2021-09-12 06:14:33 +00:00
" OR (:account IS NULL AND NOT notify))" +
" AND folder IN (" +
" SELECT id FROM folder" +
2021-09-12 06:14:33 +00:00
" WHERE notify" +
" AND (id = :folder" +
" OR (type = :type AND type <> '" + EntityFolder.OUTBOX + "')" +
" OR (:folder IS NULL AND :type IS NULL AND unified)))")
int ignoreAll(Long account, Long folder, String type);
2021-12-30 07:32:51 +00:00
@Query("UPDATE message SET ui_found = :found WHERE id = :id AND NOT (ui_found IS :found)")
int setMessageFound(long id, boolean found);
2020-11-29 11:14:01 +00:00
@Query("UPDATE message SET ui_found = 0 WHERE NOT (ui_found IS 0)")
2018-12-06 10:59:57 +00:00
int resetSearch();
2020-11-29 11:14:01 +00:00
@Query("UPDATE message SET ui_snoozed = :wakeup WHERE id = :id AND NOT (ui_snoozed IS :wakeup)")
2019-01-07 15:05:24 +00:00
int setMessageSnoozed(long id, Long wakeup);
2020-11-29 11:14:01 +00:00
@Query("UPDATE message SET ui_unsnoozed = :unsnoozed WHERE id = :id AND NOT (ui_unsnoozed IS :unsnoozed)")
2020-05-19 10:46:35 +00:00
int setMessageUnsnoozed(long id, boolean unsnoozed);
2021-05-30 06:51:14 +00:00
@Query("UPDATE message SET show_images = :show_images WHERE id = :id AND NOT (show_images IS :show_images)")
int setMessageShowImages(long id, boolean show_images);
@Query("UPDATE message SET show_full = :show_full WHERE id = :id AND NOT (show_full IS :show_full)")
int setMessageShowFull(long id, boolean show_full);
2020-11-29 11:14:01 +00:00
@Query("UPDATE message SET notifying = 0 WHERE NOT (notifying IS 0)")
2019-07-21 17:29:46 +00:00
int clearNotifyingMessages();
2020-06-28 12:12:11 +00:00
@Query("UPDATE message SET headers = NULL" +
" WHERE headers IS NOT NULL" +
" AND account IN (SELECT id FROM account WHERE pop = " + EntityAccount.TYPE_IMAP + ")")
int clearMessageHeaders();
2022-01-17 09:37:35 +00:00
@Query("UPDATE message SET raw = NULL" +
" WHERE raw IS NOT NULL" +
" AND account IN (SELECT id FROM account WHERE pop = " + EntityAccount.TYPE_IMAP + ")")
int clearRawMessages();
@Query("UPDATE message SET fts = 0 WHERE NOT (fts IS 0)")
int resetFts();
2020-01-15 09:29:16 +00:00
2018-08-02 13:33:06 +00:00
@Query("DELETE FROM message WHERE id = :id")
2018-08-09 20:45:42 +00:00
int deleteMessage(long id);
2018-08-02 13:33:06 +00:00
@Query("DELETE FROM message" +
" WHERE folder = :folder" +
" AND uid = :uid")
int deleteMessage(long folder, long uid);
2018-08-02 13:33:06 +00:00
@Query("DELETE FROM message" +
" WHERE folder = :folder" +
" AND NOT uid IS NULL")
int deleteLocalMessages(long folder);
2018-08-02 13:33:06 +00:00
2019-03-09 14:09:30 +00:00
@Query("DELETE FROM message" +
" WHERE folder = :folder" +
" AND (ui_browsed OR received < :before)" +
2019-03-09 14:09:30 +00:00
" AND NOT uid IS NULL")
int deleteBrowsedMessages(long folder, long before);
2019-03-09 14:09:30 +00:00
2022-12-09 18:45:23 +00:00
@Query("DELETE FROM message WHERE id IN (" +
" SELECT id FROM message" +
2020-07-26 17:58:13 +00:00
" WHERE folder = :folder" +
2022-12-09 18:45:23 +00:00
" AND ui_hide" +
" LIMIT :limit)")
int deleteHiddenMessages(long folder, int limit);
2020-07-26 17:58:13 +00:00
2019-01-07 08:19:48 +00:00
@Query("DELETE FROM message" +
" WHERE folder = :folder" +
2019-01-19 16:09:52 +00:00
" AND uid IS NULL" +
2021-05-10 15:51:54 +00:00
" AND (ui_busy IS NULL OR ui_busy < :now)" +
" AND NOT EXISTS" +
" (SELECT * FROM operation" +
" WHERE operation.message = message.id" +
2021-01-21 15:23:00 +00:00
" AND operation.name = '" + EntityOperation.ADD + "')" +
" AND NOT EXISTS" +
" (SELECT * FROM operation o" +
" JOIN message m ON m.id = o.message" +
" WHERE o.account = message.account" +
2021-12-10 13:20:06 +00:00
" AND o.name IN ('" + EntityOperation.MOVE + "', '" + EntityOperation.COPY + "')" +
" AND m.msgid = message.msgid)"
)
2021-05-10 15:51:54 +00:00
int deleteOrphans(long folder, long now);
2019-01-07 08:19:48 +00:00
2023-04-17 07:21:02 +00:00
@Query("SELECT * FROM message" +
" WHERE folder = :folder" +
" AND uid IS NULL" +
" AND NOT EXISTS" +
" (SELECT * FROM operation" +
" WHERE operation.message = message.id)")
List<EntityMessage> getDraftOrphans(long folder);
2021-02-05 08:17:34 +00:00
@Query("SELECT * FROM message" +
" WHERE folder = :folder" +
" AND uid IS NULL" +
" AND NOT EXISTS" +
" (SELECT * FROM operation" +
" WHERE operation.message = message.id" +
" AND operation.name = '" + EntityOperation.EXISTS + "')")
List<EntityMessage> getSentOrphans(long folder);
@Query("SELECT id FROM message" +
" WHERE folder = :folder" +
2022-02-08 19:59:35 +00:00
" AND received < :keep_time" +
" AND NOT uid IS NULL" +
2019-05-05 11:11:19 +00:00
" AND (ui_seen OR :unseen)" +
2019-05-08 08:35:34 +00:00
" AND NOT ui_flagged" +
2022-02-08 19:59:35 +00:00
" AND stored < :sync_time" + // moved, browsed
2023-09-03 06:00:34 +00:00
" AND (ui_snoozed IS NULL OR ui_snoozed =" + Long.MAX_VALUE + ")")
2022-02-08 19:59:35 +00:00
List<Long> getMessagesBefore(long folder, long sync_time, long keep_time, boolean unseen);
2018-11-26 15:57:00 +00:00
@Query("DELETE FROM message" +
" WHERE folder = :folder" +
2021-05-26 17:39:08 +00:00
" AND received < :keep_time" +
2018-11-26 15:57:00 +00:00
" AND NOT uid IS NULL" +
" AND (ui_seen OR :unseen)" +
2019-05-08 08:35:34 +00:00
" AND NOT ui_flagged" +
2021-05-26 17:39:08 +00:00
" AND stored < :sync_time" + // moved, browsed
2023-07-26 21:25:31 +00:00
" AND (ui_snoozed IS NULL OR ui_snoozed = " + Long.MAX_VALUE + ")")
int deleteMessagesBefore(long folder, long sync_time, long keep_time, boolean unseen);
2022-05-07 14:00:30 +00:00
@Transaction
@Query("DELETE FROM message" +
" WHERE folder = :folder" +
2023-04-04 17:15:59 +00:00
" AND NOT ui_flagged" +
2022-05-07 14:00:30 +00:00
" AND id NOT IN (" +
" SELECT id FROM message" +
" WHERE folder = :folder" +
" ORDER BY received DESC" +
2022-05-07 14:00:30 +00:00
" LIMIT :keep)")
int deleteMessagesKeep(long folder, int keep);
2018-08-02 13:33:06 +00:00
}