From c1c74cfcb78dbdbc73b248a8df8ba0058547bc3b Mon Sep 17 00:00:00 2001 From: Rene Saarsoo Date: Wed, 22 Aug 2012 19:21:44 +0300 Subject: [PATCH] Add visible_comments view. Because almost all queries need to exclude the deleted comments, it helps having a view that excludes these. --- comments/database.sql | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/comments/database.sql b/comments/database.sql index 5b57a0c1..90c27cc4 100644 --- a/comments/database.sql +++ b/comments/database.sql @@ -65,18 +65,18 @@ CREATE TABLE read_comments ( FOREIGN KEY (comment_id) REFERENCES comments (id) ); +CREATE VIEW visible_comments AS SELECT * FROM comments WHERE deleted = 'N'; -- Example queries -- get comments for a particular member -SELECT * FROM comments JOIN targets ON comments.target_id = targets.id +SELECT * FROM visible_comments c JOIN targets ON c.target_id = targets.id WHERE targets.domain = ? AND targets.type = ? AND targets.cls = ? AND targets.member = ? -AND comments.deleted = 'N' -- get 100 most recent comments -SELECT * FROM comments JOIN targets ON comments.target_id = targets.id +SELECT * FROM visible_comments c JOIN targets ON c.target_id = targets.id WHERE targets.domain = ? ORDER BY created_at DESC LIMIT 100 @@ -87,7 +87,7 @@ SELECT target.cls AS cls, target.member AS member, count(*) AS cnt -FROM comments JOIN targets ON comments.target_id = targets.id +FROM visible_comments c JOIN targets ON c.target_id = targets.id WHERE target.domain = ? GROUP BY target.id @@ -96,7 +96,7 @@ GROUP BY target.id SELECT target.cls AS cls, count(*) AS cnt -FROM comments JOIN targets ON comments.target_id = targets.id +FROM visible_comments c JOIN targets ON c.target_id = targets.id WHERE target.domain = ? AND target.type = 'class' GROUP BY target.cls @@ -104,7 +104,7 @@ GROUP BY target.cls SELECT user.name, - SUM(comments.rating) -FROM users JOIN comments ON comments.user_id = users.id + SUM(c.rating) +FROM users JOIN visible_comments c ON c.user_id = users.id GROUP BY user.id -- GitLab