Make article number be unique only within newsgroup

This commit is contained in:
ChronosX88 2022-02-03 21:24:40 +03:00
parent fcd95a9411
commit 5023c13afd
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
3 changed files with 11 additions and 7 deletions

View File

@ -15,6 +15,7 @@ CREATE TABLE IF NOT EXISTS articles(
);
CREATE TABLE IF NOT EXISTS articles_to_groups(
article_id INTEGER NOT NULL,
article_number INTEGER,
group_id INTEGER NOT NULL,
FOREIGN KEY (article_id) REFERENCES articles(id) ON DELETE CASCADE,
FOREIGN KEY (group_id) REFERENCES groups(id) ON DELETE CASCADE

View File

@ -79,12 +79,12 @@ func (sb *SQLiteBackend) GetArticlesCount(g *models.Group) (int, error) {
func (sb *SQLiteBackend) GetGroupHighWaterMark(g *models.Group) (int, error) {
var waterMark int
return waterMark, sb.db.Get(&waterMark, "SELECT article_id FROM articles_to_groups WHERE group_id = ? ORDER BY article_id DESC LIMIT 1", g.ID)
return waterMark, sb.db.Get(&waterMark, "SELECT max(article_number) FROM articles_to_groups WHERE group_id = ?", g.ID)
}
func (sb *SQLiteBackend) GetGroupLowWaterMark(g *models.Group) (int, error) {
var waterMark int
return waterMark, sb.db.Get(&waterMark, "SELECT article_id FROM articles_to_groups WHERE group_id = ? ORDER BY article_id LIMIT 1", g.ID)
return waterMark, sb.db.Get(&waterMark, "SELECT min(article_number) FROM articles_to_groups WHERE group_id = ?", g.ID)
}
func (sb *SQLiteBackend) GetGroup(groupName string) (models.Group, error) {
@ -119,7 +119,7 @@ func (sb *SQLiteBackend) SaveArticle(a models.Article, groups []string) error {
}
for _, v := range groupIDs {
_, err = sb.db.Exec("INSERT INTO articles_to_groups (article_id, group_id) VALUES (?, ?)", articleID, v)
_, err = sb.db.Exec("INSERT INTO articles_to_groups (article_id, article_number, group_id) VALUES (?, (SELECT ifnull(max(article_number)+1, 1) FROM articles_to_groups), ?)", articleID, v)
if err != nil {
return err
}
@ -139,21 +139,21 @@ func (sb *SQLiteBackend) GetArticleNumbers(g *models.Group, low, high int64) ([]
var numbers []int64
if high == 0 && low == 0 {
if err := sb.db.Select(&numbers, "SELECT article_id FROM articles_to_groups WHERE group_id = ?", g.ID); err != nil {
if err := sb.db.Select(&numbers, "SELECT article_number FROM articles_to_groups WHERE group_id = ?", g.ID); err != nil {
return nil, err
}
} else if low == -1 && high != 0 {
if err := sb.db.Select(&numbers, "SELECT article_id FROM articles_to_groups WHERE group_id = ? AND article_id = ?", g.ID, high); err != nil {
if err := sb.db.Select(&numbers, "SELECT article_number FROM articles_to_groups WHERE group_id = ? AND article_number = ?", g.ID, high); err != nil {
return nil, err
}
} else if low != 0 && high == -1 {
if err := sb.db.Select(&numbers, "SELECT article_id FROM articles_to_groups WHERE group_id = ? AND article_id > ?", g.ID, low); err != nil {
if err := sb.db.Select(&numbers, "SELECT article_number FROM articles_to_groups WHERE group_id = ? AND article_number > ?", g.ID, low); err != nil {
return nil, err
}
} else if low == -1 && high == -1 {
return nil, nil
} else {
if err := sb.db.Select(&numbers, "SELECT article_id FROM articles_to_groups WHERE group_id = ? AND article_id > ? AND article_id < ?", g.ID, low, high); err != nil {
if err := sb.db.Select(&numbers, "SELECT article_number FROM articles_to_groups WHERE group_id = ? AND article_number > ? AND article_number < ?", g.ID, low, high); err != nil {
return nil, err
}
}

View File

@ -143,6 +143,9 @@ func (h *Handler) handleList(s *Session, arguments []string, id uint) error {
}
func (h *Handler) handleModeReader(s *Session, arguments []string, id uint) error {
s.tconn.StartResponse(id)
defer s.tconn.EndResponse(id)
if len(arguments) == 0 || arguments[0] != "READER" {
return s.tconn.PrintfLine(protocol.ErrSyntaxError.String())
}