From 9b1514e2f0d7436df500bedb709623fabd871904 Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 26 Jun 2019 22:08:40 +0200 Subject: [PATCH] Improved folder query --- .../java/eu/faircode/email/DaoFolder.java | 48 +++++++++++-------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/DaoFolder.java b/app/src/main/java/eu/faircode/email/DaoFolder.java index 53063c91ff..524754e853 100644 --- a/app/src/main/java/eu/faircode/email/DaoFolder.java +++ b/app/src/main/java/eu/faircode/email/DaoFolder.java @@ -33,14 +33,16 @@ public interface DaoFolder { @Query("SELECT folder.*" + ", account.id AS accountId, account.`order` AS accountOrder, account.name AS accountName, account.color AS accountColor, account.state AS accountState" + - ", (SELECT COUNT(rule.id) FROM rule WHERE rule.folder = folder.id AND rule.enabled) AS rules" + - ", COUNT(message.id) AS messages" + - ", SUM(CASE WHEN message.content = 1 THEN 1 ELSE 0 END) AS content" + - ", SUM(CASE WHEN message.ui_seen = 0 THEN 1 ELSE 0 END) AS unseen" + - ", (SELECT COUNT(operation.id) FROM operation WHERE operation.folder = folder.id AND operation.state = 'executing') AS executing" + + ", COUNT(DISTINCT rule.id) rules" + + ", COUNT(DISTINCT message.id) AS messages" + + ", COUNT(DISTINCT CASE WHEN message.content = 1 THEN message.id ELSE NULL END) AS content" + + ", COUNT(DISTINCT CASE WHEN message.ui_seen = 0 THEN message.id ELSE NULL END) AS unseen" + + ", COUNT(DISTINCT operation.id) AS executing" + " FROM folder" + " LEFT JOIN account ON account.id = folder.account" + " LEFT JOIN message ON message.folder = folder.id AND message.ui_hide = 0" + + " LEFT JOIN rule ON rule.folder = folder.id AND rule.enabled" + + " LEFT JOIN operation ON operation.folder = folder.id AND operation.state = 'executing'" + " WHERE folder.account = :account AND account.synchronize" + " GROUP BY folder.id") List getFoldersEx(long account); @@ -66,14 +68,16 @@ public interface DaoFolder { @Query("SELECT folder.*" + ", account.id AS accountId, account.`order` AS accountOrder, account.name AS accountName, account.color AS accountColor, account.state AS accountState" + - ", (SELECT COUNT(rule.id) FROM rule WHERE rule.folder = folder.id AND rule.enabled) AS rules" + - ", COUNT(message.id) AS messages" + - ", SUM(CASE WHEN message.content = 1 THEN 1 ELSE 0 END) AS content" + - ", SUM(CASE WHEN message.ui_seen = 0 THEN 1 ELSE 0 END) AS unseen" + - ", (SELECT COUNT(operation.id) FROM operation WHERE operation.folder = folder.id AND operation.state = 'executing') AS executing" + + ", COUNT(DISTINCT rule.id) rules" + + ", COUNT(DISTINCT message.id) AS messages" + + ", COUNT(DISTINCT CASE WHEN message.content = 1 THEN message.id ELSE NULL END) AS content" + + ", COUNT(DISTINCT CASE WHEN message.ui_seen = 0 THEN message.id ELSE NULL END) AS unseen" + + ", COUNT(DISTINCT operation.id) AS executing" + " FROM folder" + " LEFT JOIN account ON account.id = folder.account" + " LEFT JOIN message ON message.folder = folder.id AND message.ui_hide = 0" + + " LEFT JOIN rule ON rule.folder = folder.id AND rule.enabled" + + " LEFT JOIN operation ON operation.folder = folder.id AND operation.state = 'executing'" + " WHERE CASE WHEN :account IS NULL" + " THEN folder.unified AND account.synchronize" + " ELSE folder.account = :account AND account.synchronize" + @@ -83,14 +87,16 @@ public interface DaoFolder { @Query("SELECT folder.*" + ", account.id AS accountId, account.`order` AS accountOrder, account.name AS accountName, account.color AS accountColor, account.state AS accountState" + - ", (SELECT COUNT(rule.id) FROM rule WHERE rule.folder = folder.id AND rule.enabled) AS rules" + - ", COUNT(message.id) AS messages" + - ", SUM(CASE WHEN message.content = 1 THEN 1 ELSE 0 END) AS content" + - ", SUM(CASE WHEN message.ui_seen = 0 THEN 1 ELSE 0 END) AS unseen" + - ", (SELECT COUNT(operation.id) FROM operation WHERE operation.folder = folder.id AND operation.state = 'executing') AS executing" + + ", COUNT(DISTINCT rule.id) rules" + + ", COUNT(DISTINCT message.id) AS messages" + + ", COUNT(DISTINCT CASE WHEN message.content = 1 THEN message.id ELSE NULL END) AS content" + + ", COUNT(DISTINCT CASE WHEN message.ui_seen = 0 THEN message.id ELSE NULL END) AS unseen" + + ", COUNT(DISTINCT operation.id) AS executing" + " FROM folder" + " JOIN account ON account.id = folder.account" + " LEFT JOIN message ON message.folder = folder.id AND message.ui_hide = 0" + + " LEFT JOIN rule ON rule.folder = folder.id AND rule.enabled" + + " LEFT JOIN operation ON operation.folder = folder.id AND operation.state = 'executing'" + " WHERE account.`synchronize`" + " AND folder.unified" + " GROUP BY folder.id") @@ -123,14 +129,16 @@ public interface DaoFolder { @Query("SELECT folder.*" + ", account.id AS accountId, account.`order` AS accountOrder, account.name AS accountName, account.color AS accountColor, account.state AS accountState" + - ", (SELECT COUNT(rule.id) FROM rule WHERE rule.folder = folder.id AND rule.enabled) AS rules" + - ", COUNT(message.id) AS messages" + - ", SUM(CASE WHEN message.content = 1 THEN 1 ELSE 0 END) AS content" + - ", SUM(CASE WHEN message.ui_seen = 0 THEN 1 ELSE 0 END) AS unseen" + - ", (SELECT COUNT(operation.id) FROM operation WHERE operation.folder = folder.id AND operation.state = 'executing') AS executing" + + ", COUNT(DISTINCT rule.id) rules" + + ", COUNT(DISTINCT message.id) AS messages" + + ", COUNT(DISTINCT CASE WHEN message.content = 1 THEN message.id ELSE NULL END) AS content" + + ", COUNT(DISTINCT CASE WHEN message.ui_seen = 0 THEN message.id ELSE NULL END) AS unseen" + + ", COUNT(DISTINCT operation.id) AS executing" + " FROM folder" + " LEFT JOIN account ON account.id = folder.account" + " LEFT JOIN message ON message.folder = folder.id AND message.ui_hide = 0" + + " LEFT JOIN rule ON rule.folder = folder.id AND rule.enabled" + + " LEFT JOIN operation ON operation.folder = folder.id AND operation.state = 'executing'" + " WHERE folder.id = :id" + " GROUP BY folder.id") LiveData liveFolderEx(long id);