|
| 1 | +package database |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "database/sql" |
| 6 | + "fmt" |
| 7 | + "log/slog" |
| 8 | + |
| 9 | + "github.com/go-shiori/shiori/internal/model" |
| 10 | + "github.com/huandu/go-sqlbuilder" |
| 11 | +) |
| 12 | + |
| 13 | +// GetTags returns a list of tags from the database. |
| 14 | +// If opts.WithBookmarkCount is true, the result will include the number of bookmarks for each tag. |
| 15 | +// If opts.BookmarkID is not 0, the result will include only the tags for the specified bookmark. |
| 16 | +// If opts.OrderBy is set, the result will be ordered by the specified column. |
| 17 | +func (db *dbbase) GetTags(ctx context.Context, opts model.DBListTagsOptions) ([]model.TagDTO, error) { |
| 18 | + sb := db.Flavor().NewSelectBuilder() |
| 19 | + |
| 20 | + sb.Select("t.id", "t.name") |
| 21 | + sb.From("tag t") |
| 22 | + |
| 23 | + // Treat the case where we want the bookmark count and filter by bookmark ID as a special case: |
| 24 | + // If we only want one of them, we can use a JOIN and GROUP BY. |
| 25 | + // If we want both, we need to use a subquery to get the count of bookmarks for each tag filtered |
| 26 | + // by bookmark ID. |
| 27 | + if opts.WithBookmarkCount && opts.BookmarkID == 0 { |
| 28 | + // Join with bookmark_tag and group by tag ID to get the count of bookmarks for each tag |
| 29 | + sb.JoinWithOption(sqlbuilder.LeftJoin, "bookmark_tag bt", "bt.tag_id = t.id") |
| 30 | + sb.SelectMore("COUNT(bt.tag_id) AS bookmark_count") |
| 31 | + sb.GroupBy("t.id") |
| 32 | + } else if opts.BookmarkID > 0 { |
| 33 | + // If we want the bookmark count, we need to use a subquery to get the count of bookmarks for each tag |
| 34 | + if opts.WithBookmarkCount { |
| 35 | + sb.SelectMore( |
| 36 | + sb.BuilderAs( |
| 37 | + db.Flavor().NewSelectBuilder().Select("COUNT(bt2.tag_id)").From("bookmark_tag bt2").Where("bt2.tag_id = t.id"), |
| 38 | + "bookmark_count", |
| 39 | + ), |
| 40 | + ) |
| 41 | + } |
| 42 | + |
| 43 | + // Join with bookmark_tag and filter by bookmark ID to get the tags for a specific bookmark |
| 44 | + sb.JoinWithOption(sqlbuilder.RightJoin, "bookmark_tag bt", |
| 45 | + sb.And( |
| 46 | + "bt.tag_id = t.id", |
| 47 | + sb.Equal("bt.bookmark_id", opts.BookmarkID), |
| 48 | + ), |
| 49 | + ) |
| 50 | + sb.Where(sb.IsNotNull("t.id")) |
| 51 | + } |
| 52 | + |
| 53 | + if opts.OrderBy == model.DBTagOrderByTagName { |
| 54 | + sb.OrderBy("t.name") |
| 55 | + } |
| 56 | + |
| 57 | + query, args := sb.Build() |
| 58 | + query = db.ReaderDB().Rebind(query) |
| 59 | + |
| 60 | + slog.Info("GetTags query", "query", query, "args", args) |
| 61 | + |
| 62 | + tags := []model.TagDTO{} |
| 63 | + err := db.ReaderDB().SelectContext(ctx, &tags, query, args...) |
| 64 | + if err != nil && err != sql.ErrNoRows { |
| 65 | + return nil, fmt.Errorf("failed to get tags: %w", err) |
| 66 | + } |
| 67 | + |
| 68 | + return tags, nil |
| 69 | +} |
0 commit comments