diff --git a/database/conference.go b/database/conference.go index 1b93e2a..7efb64f 100644 --- a/database/conference.go +++ b/database/conference.go @@ -580,7 +580,7 @@ func (c *Conference) UnreadMessages(ctx context.Context, u *User) (int32, error) return 0, nil } var rc int32 - err = amdb.GetContext(ctx, &rc, `SELECT SUM(t.top_message - IFNULL(s.last_message,-1)) + err = amdb.GetContext(ctx, &rc, `SELECT SUM(t.top_message - COALESCE(s.last_message,-1)) FROM topics t LEFT JOIN topicsettings s ON t.topicid = s.topicid AND s.uid = ? WHERE t.confid = ? AND t.archived = 0 AND (s.hidden IS NULL OR s.hidden = 0)`, u.Uid, c.ConfId) return rc, err @@ -800,7 +800,7 @@ func (c *Conference) GetActiveUserEMailAddrs(ctx context.Context, userSelect, da // Stats retrieves the number of topics and posts in this conference. func (c *Conference) Stats(ctx context.Context) (int, int, error) { - row := amdb.QueryRowContext(ctx, "SELECT COUNT(*), SUM(top_message + 1) FROM topics WHERE confid = ?", c.ConfId) + row := amdb.QueryRowContext(ctx, "SELECT COUNT(*), COALESCE(SUM(top_message + 1), 0) FROM topics WHERE confid = ?", c.ConfId) ntopic := 0 npost := 0 err := row.Scan(&ntopic, &npost) diff --git a/database/post.go b/database/post.go index 0c28cf8..8b8c27d 100644 --- a/database/post.go +++ b/database/post.go @@ -800,7 +800,7 @@ func AmSearchPosts(ctx context.Context, searchTerms string, u *User, offset, max JOIN commmember m ON m.commid = q.commid JOIN users u ON u.uid = m.uid JOIN commftrs f ON f.commid = q.commid JOIN topics t ON t.confid = c.confid JOIN posts p ON p.topicid = t.topicid JOIN postdata d ON d.postid = p.postid JOIN users u2 ON u2.uid = p.creator_uid LEFT JOIN confmember x ON (c.confid = x.confid AND u.uid = x.uid) - WHERE u.uid = ? AND f.ftr_code = ? AND GREATEST(u.base_lvl,m.granted_lvl,s.granted_lvl,IFNULL(x.granted_lvl,0)) >= c.read_lvl + WHERE u.uid = ? AND f.ftr_code = ? AND GREATEST(u.base_lvl,m.granted_lvl,s.granted_lvl,COALESCE(x.granted_lvl,0)) >= c.read_lvl AND p.scribble_uid IS NULL AND MATCH(d.data) AGAINST (?)`, u.Uid, confService, searchTerms) case "community": err = amdb.GetContext(ctx, &count, `SELECT COUNT(*) @@ -808,7 +808,7 @@ func AmSearchPosts(ctx context.Context, searchTerms string, u *User, offset, max JOIN commmember m ON m.commid = q.commid JOIN users u ON u.uid = m.uid JOIN commftrs f ON f.commid = q.commid JOIN topics t ON t.confid = c.confid JOIN posts p ON p.topicid = t.topicid JOIN postdata d ON d.postid = p.postid JOIN users u2 ON u2.uid = p.creator_uid LEFT JOIN confmember x ON (c.confid = x.confid AND u.uid = x.uid) - WHERE u.uid = ? AND f.ftr_code = ? AND GREATEST(u.base_lvl,m.granted_lvl,s.granted_lvl,IFNULL(x.granted_lvl,0)) >= c.read_lvl + WHERE u.uid = ? AND f.ftr_code = ? AND GREATEST(u.base_lvl,m.granted_lvl,s.granted_lvl,COALESCE(x.granted_lvl,0)) >= c.read_lvl AND q.commid = ? AND p.scribble_uid IS NULL AND MATCH(d.data) AGAINST (?)`, u.Uid, confService, comm.Id, searchTerms) case "conference": err = amdb.GetContext(ctx, &count, `SELECT COUNT(*) @@ -816,7 +816,7 @@ func AmSearchPosts(ctx context.Context, searchTerms string, u *User, offset, max JOIN commmember m ON m.commid = q.commid JOIN users u ON u.uid = m.uid JOIN commftrs f ON f.commid = q.commid JOIN topics t ON t.confid = c.confid JOIN posts p ON p.topicid = t.topicid JOIN postdata d ON d.postid = p.postid JOIN users u2 ON u2.uid = p.creator_uid LEFT JOIN confmember x ON (c.confid = x.confid AND u.uid = x.uid) - WHERE u.uid = ? AND f.ftr_code = ? AND GREATEST(u.base_lvl,m.granted_lvl,s.granted_lvl,IFNULL(x.granted_lvl,0)) >= c.read_lvl + WHERE u.uid = ? AND f.ftr_code = ? AND GREATEST(u.base_lvl,m.granted_lvl,s.granted_lvl,COALESCE(x.granted_lvl,0)) >= c.read_lvl AND q.commid = ? AND c.confid = ? AND p.scribble_uid IS NULL AND MATCH(d.data) AGAINST (?)`, u.Uid, confService, comm.Id, conf.ConfId, searchTerms) case "topic": err = amdb.GetContext(ctx, &count, `SELECT COUNT(*) @@ -824,7 +824,7 @@ func AmSearchPosts(ctx context.Context, searchTerms string, u *User, offset, max JOIN commmember m ON m.commid = q.commid JOIN users u ON u.uid = m.uid JOIN commftrs f ON f.commid = q.commid JOIN topics t ON t.confid = c.confid JOIN posts p ON p.topicid = t.topicid JOIN postdata d ON d.postid = p.postid JOIN users u2 ON u2.uid = p.creator_uid LEFT JOIN confmember x ON (c.confid = x.confid AND u.uid = x.uid) - WHERE u.uid = ? AND f.ftr_code = ? AND GREATEST(u.base_lvl,m.granted_lvl,s.granted_lvl,IFNULL(x.granted_lvl,0)) >= c.read_lvl + WHERE u.uid = ? AND f.ftr_code = ? AND GREATEST(u.base_lvl,m.granted_lvl,s.granted_lvl,COALESCE(x.granted_lvl,0)) >= c.read_lvl AND q.commid = ? AND c.confid = ? AND t.topicid = ? AND p.scribble_uid IS NULL AND MATCH(d.data) AGAINST (?)`, u.Uid, confService, comm.Id, conf.ConfId, topic.TopicId, searchTerms) } @@ -842,7 +842,7 @@ func AmSearchPosts(ctx context.Context, searchTerms string, u *User, offset, max JOIN commmember m ON m.commid = q.commid JOIN users u ON u.uid = m.uid JOIN commftrs f ON f.commid = q.commid JOIN topics t ON t.confid = c.confid JOIN posts p ON p.topicid = t.topicid JOIN postdata d ON d.postid = p.postid JOIN users u2 ON u2.uid = p.creator_uid LEFT JOIN confmember x ON (c.confid = x.confid AND u.uid = x.uid) - WHERE u.uid = ? AND f.ftr_code = ? AND GREATEST(u.base_lvl,m.granted_lvl,s.granted_lvl,IFNULL(x.granted_lvl,0)) >= c.read_lvl + WHERE u.uid = ? AND f.ftr_code = ? AND GREATEST(u.base_lvl,m.granted_lvl,s.granted_lvl,COALESCE(x.granted_lvl,0)) >= c.read_lvl AND p.scribble_uid IS NULL AND MATCH(d.data) AGAINST (?) ORDER BY q.commname, c.name, t.num, p.num LIMIT ? OFFSET ?`, u.Uid, confService, searchTerms, max, offset) case "community": @@ -851,7 +851,7 @@ func AmSearchPosts(ctx context.Context, searchTerms string, u *User, offset, max JOIN commmember m ON m.commid = q.commid JOIN users u ON u.uid = m.uid JOIN commftrs f ON f.commid = q.commid JOIN topics t ON t.confid = c.confid JOIN posts p ON p.topicid = t.topicid JOIN postdata d ON d.postid = p.postid JOIN users u2 ON u2.uid = p.creator_uid LEFT JOIN confmember x ON (c.confid = x.confid AND u.uid = x.uid) - WHERE u.uid = ? AND f.ftr_code = ? AND GREATEST(u.base_lvl,m.granted_lvl,s.granted_lvl,IFNULL(x.granted_lvl,0)) >= c.read_lvl + WHERE u.uid = ? AND f.ftr_code = ? AND GREATEST(u.base_lvl,m.granted_lvl,s.granted_lvl,COALESCE(x.granted_lvl,0)) >= c.read_lvl AND q.commid = ? AND p.scribble_uid IS NULL AND MATCH(d.data) AGAINST (?) ORDER BY q.commname, c.name, t.num, p.num LIMIT ? OFFSET ?`, u.Uid, confService, comm.Id, searchTerms, max, offset) case "conference": @@ -860,7 +860,7 @@ func AmSearchPosts(ctx context.Context, searchTerms string, u *User, offset, max JOIN commmember m ON m.commid = q.commid JOIN users u ON u.uid = m.uid JOIN commftrs f ON f.commid = q.commid JOIN topics t ON t.confid = c.confid JOIN posts p ON p.topicid = t.topicid JOIN postdata d ON d.postid = p.postid JOIN users u2 ON u2.uid = p.creator_uid LEFT JOIN confmember x ON (c.confid = x.confid AND u.uid = x.uid) - WHERE u.uid = ? AND f.ftr_code = ? AND GREATEST(u.base_lvl,m.granted_lvl,s.granted_lvl,IFNULL(x.granted_lvl,0)) >= c.read_lvl + WHERE u.uid = ? AND f.ftr_code = ? AND GREATEST(u.base_lvl,m.granted_lvl,s.granted_lvl,COALESCE(x.granted_lvl,0)) >= c.read_lvl AND q.commid = ? AND c.confid = ? AND p.scribble_uid IS NULL AND MATCH(d.data) AGAINST (?) ORDER BY q.commname, c.name, t.num, p.num LIMIT ? OFFSET ?`, u.Uid, confService, comm.Id, conf.ConfId, searchTerms, max, offset) case "topic": @@ -869,7 +869,7 @@ func AmSearchPosts(ctx context.Context, searchTerms string, u *User, offset, max JOIN commmember m ON m.commid = q.commid JOIN users u ON u.uid = m.uid JOIN commftrs f ON f.commid = q.commid JOIN topics t ON t.confid = c.confid JOIN posts p ON p.topicid = t.topicid JOIN postdata d ON d.postid = p.postid JOIN users u2 ON u2.uid = p.creator_uid LEFT JOIN confmember x ON (c.confid = x.confid AND u.uid = x.uid) - WHERE u.uid = ? AND f.ftr_code = ? AND GREATEST(u.base_lvl,m.granted_lvl,s.granted_lvl,IFNULL(x.granted_lvl,0)) >= c.read_lvl + WHERE u.uid = ? AND f.ftr_code = ? AND GREATEST(u.base_lvl,m.granted_lvl,s.granted_lvl,COALESCE(x.granted_lvl,0)) >= c.read_lvl AND q.commid = ? AND c.confid = ? AND t.topicid = ? AND p.scribble_uid IS NULL AND MATCH(d.data) AGAINST (?) ORDER BY q.commname, c.name, t.num, p.num LIMIT ? OFFSET ?`, u.Uid, confService, comm.Id, conf.ConfId, topic.TopicId, searchTerms, max, offset) } diff --git a/database/topic.go b/database/topic.go index 2623629..2ec7d69 100644 --- a/database/topic.go +++ b/database/topic.go @@ -610,7 +610,7 @@ func AmListTopics(ctx context.Context, confid int32, uid int32, viewOption int, case TopicViewAll: whereClause = "" case TopicViewNew: - tail := "t.top_message > IFNULL(s.last_message,-1)" + tail := "t.top_message > COALESCE(s.last_message,-1)" if !ignoreSticky { tail = "(t.sticky = 1 OR " + tail + ")" } @@ -673,9 +673,9 @@ func AmListTopics(ctx context.Context, confid int32, uid int32, viewOption int, // Build the full SQL statement var fullStatement strings.Builder - fullStatement.WriteString("SELECT t.topicid, t.num, t.name, (t.top_message - IFNULL(s.last_message,-1)) AS unread, ") - fullStatement.WriteString("(t.top_message + 1) AS total, t.lastupdate, t.frozen, t.archived, IFNULL(s.subscribe,0) AS subscribe, ") - fullStatement.WriteString("IFNULL(s.hidden,0) AS hidden, t.sticky, GREATEST(SIGN(t.top_message - IFNULL(s.last_message,-1)),0) AS newflag ") + fullStatement.WriteString("SELECT t.topicid, t.num, t.name, (t.top_message - COALESCE(s.last_message,-1)) AS unread, ") + fullStatement.WriteString("(t.top_message + 1) AS total, t.lastupdate, t.frozen, t.archived, COALESCE(s.subscribe,0) AS subscribe, ") + fullStatement.WriteString("COALESCE(s.hidden,0) AS hidden, t.sticky, GREATEST(SIGN(t.top_message - COALESCE(s.last_message,-1)),0) AS newflag ") fullStatement.WriteString("FROM topics t LEFT JOIN topicsettings s ON t.topicid = s.topicid AND s.uid = ? WHERE t.confid = ? ") if whereClause != "" { fullStatement.WriteString("AND ")