Simple email application for Android. Original source code: https://framagit.org/dystopia-project/simple-email
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
7.1 KiB

6 years ago
6 years ago
6 years ago
6 years ago
  1. package eu.faircode.email;
  2. /*
  3. This file is part of FairEmail.
  4. FairEmail is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. NetGuard is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with NetGuard. If not, see <http://www.gnu.org/licenses/>.
  14. Copyright 2018 by Marcel Bokhorst (M66B)
  15. */
  16. import java.util.List;
  17. import androidx.lifecycle.LiveData;
  18. import androidx.paging.DataSource;
  19. import androidx.room.Dao;
  20. import androidx.room.Insert;
  21. import androidx.room.Query;
  22. import androidx.room.Update;
  23. @Dao
  24. public interface DaoMessage {
  25. // About 'dummy': "When the min() or max() aggregate functions are used in an aggregate query,
  26. // all bare columns in the result set take values from the input row which also contains the minimum or maximum."
  27. // https://www.sqlite.org/lang_select.html
  28. @Query("SELECT message.*, account.name AS accountName, folder.name as folderName, folder.type as folderType" +
  29. ", COUNT(message.id) as count" +
  30. ", SUM(CASE WHEN message.ui_seen THEN 0 ELSE 1 END) as unseen" +
  31. ", (SELECT COUNT(a.id) FROM attachment a WHERE a.message = message.id) AS attachments" +
  32. ", MAX(CASE WHEN folder.type = '" + EntityFolder.INBOX + "' THEN message.id ELSE 0 END) as dummy" +
  33. " FROM message" +
  34. " LEFT JOIN account ON account.id = message.account" +
  35. " JOIN folder ON folder.id = message.folder" +
  36. " WHERE (NOT message.ui_hide OR :debug)" +
  37. " GROUP BY CASE WHEN message.thread IS NULL THEN message.id ELSE message.thread END" +
  38. " HAVING SUM(CASE WHEN folder.type = '" + EntityFolder.INBOX + "' THEN 1 ELSE 0 END) > 0" +
  39. " ORDER BY message.received DESC")
  40. DataSource.Factory<Integer, TupleMessageEx> pagedUnifiedInbox(boolean debug);
  41. @Query("SELECT message.*, account.name AS accountName, folder.name as folderName, folder.type as folderType" +
  42. ", COUNT(message.id) as count" +
  43. ", SUM(CASE WHEN message.ui_seen THEN 0 ELSE 1 END) as unseen" +
  44. ", (SELECT COUNT(a.id) FROM attachment a WHERE a.message = message.id) AS attachments" +
  45. ", MAX(CASE WHEN folder.id = :folder THEN message.id ELSE 0 END) as dummy" +
  46. " FROM message" +
  47. " LEFT JOIN account ON account.id = message.account" +
  48. " JOIN folder ON folder.id = message.folder" +
  49. " LEFT JOIN folder f ON f.id = :folder" +
  50. " WHERE (NOT message.ui_hide OR :debug)" +
  51. " GROUP BY CASE WHEN message.thread IS NULL THEN message.id ELSE message.thread END" +
  52. " HAVING SUM(CASE WHEN folder.id = :folder THEN 1 ELSE 0 END) > 0" +
  53. " ORDER BY message.received DESC, message.sent DESC")
  54. DataSource.Factory<Integer, TupleMessageEx> pagedFolder(long folder, boolean debug);
  55. @Query("SELECT message.*, account.name AS accountName, folder.name as folderName, folder.type as folderType" +
  56. ", 1 AS count" +
  57. ", CASE WHEN message.ui_seen THEN 0 ELSE 1 END AS unseen" +
  58. ", (SELECT COUNT(a.id) FROM attachment a WHERE a.message = message.id) AS attachments" +
  59. " FROM message" +
  60. " LEFT JOIN account ON account.id = message.account" +
  61. " JOIN folder ON folder.id = message.folder" +
  62. " WHERE (NOT message.ui_hide OR :debug)" +
  63. " AND message.account = (SELECT m1.account FROM message m1 WHERE m1.id = :msgid)" +
  64. " AND message.thread = (SELECT m2.thread FROM message m2 WHERE m2.id = :msgid)" +
  65. " ORDER BY message.received DESC, message.sent DESC")
  66. DataSource.Factory<Integer, TupleMessageEx> pagedThread(long msgid, boolean debug);
  67. @Query("SELECT *" +
  68. " FROM message" +
  69. " WHERE id = :id")
  70. EntityMessage getMessage(long id);
  71. @Query("SELECT COUNT(id)" +
  72. " FROM message" +
  73. " WHERE id = :id")
  74. int countMessage(long id);
  75. @Query("SELECT *" +
  76. " FROM message" +
  77. " WHERE folder = :folder" +
  78. " AND uid = :uid")
  79. EntityMessage getMessageByUid(long folder, long uid);
  80. @Query("SELECT message.* FROM message" +
  81. " JOIN folder ON folder.id = message.folder" +
  82. " WHERE message.account = :account" +
  83. " AND message.msgid = :msgid")
  84. List<EntityMessage> getMessageByMsgId(long account, String msgid);
  85. @Query("SELECT message.* FROM message" +
  86. " JOIN folder ON folder.id = message.folder" +
  87. " WHERE message.account = :account" +
  88. " AND message.thread = :thread" +
  89. " AND folder.type <> '" + EntityFolder.OUTBOX + "'")
  90. List<EntityMessage> getMessageByThread(long account, String thread);
  91. @Query("SELECT message.*, account.name AS accountName, folder.name as folderName, folder.type as folderType" +
  92. ", (SELECT COUNT(m1.id) FROM message m1 WHERE m1.account = message.account AND m1.thread = message.thread AND NOT m1.ui_hide) AS count" +
  93. ", (SELECT COUNT(m2.id) FROM message m2 WHERE m2.account = message.account AND m2.thread = message.thread AND NOT m2.ui_hide AND NOT m2.ui_seen) AS unseen" +
  94. ", (SELECT COUNT(a.id) FROM attachment a WHERE a.message = message.id) AS attachments" +
  95. " FROM message" +
  96. " LEFT JOIN account ON account.id = message.account" +
  97. " JOIN folder ON folder.id = message.folder" +
  98. " WHERE message.id = :id")
  99. LiveData<TupleMessageEx> liveMessage(long id);
  100. @Query("SELECT uid FROM message WHERE folder = :folder AND received >= :received AND NOT uid IS NULL")
  101. List<Long> getUids(long folder, long received);
  102. @Insert
  103. long insertMessage(EntityMessage message);
  104. @Update
  105. void updateMessage(EntityMessage message);
  106. @Query("UPDATE message SET uid = :uid WHERE id = :id")
  107. int setMessageUid(long id, Long uid);
  108. @Query("UPDATE message SET seen = :seen WHERE id = :id")
  109. int setMessageSeen(long id, boolean seen);
  110. @Query("UPDATE message SET ui_seen = :ui_seen WHERE id = :id")
  111. int setMessageUiSeen(long id, boolean ui_seen);
  112. @Query("UPDATE message SET ui_hide = :ui_hide WHERE id = :id")
  113. int setMessageUiHide(long id, boolean ui_hide);
  114. @Query("UPDATE message SET error = :error WHERE id = :id")
  115. int setMessageError(long id, String error);
  116. @Query("DELETE FROM message WHERE id = :id")
  117. int deleteMessage(long id);
  118. @Query("DELETE FROM message WHERE folder = :folder AND uid = :uid")
  119. int deleteMessage(long folder, long uid);
  120. @Query("DELETE FROM message WHERE folder = :folder")
  121. void deleteMessages(long folder);
  122. @Query("DELETE FROM message WHERE folder = :folder AND received < :received AND NOT uid IS NULL")
  123. int deleteMessagesBefore(long folder, long received);
  124. }