mirror of
https://github.com/miniflux/v2.git
synced 2025-08-01 17:38:37 +00:00
Refactor entry/feed query builder sorting to match SQL semantic
This commit is contained in:
parent
095bec072c
commit
28775f5e10
15 changed files with 44 additions and 79 deletions
|
@ -18,13 +18,12 @@ import (
|
|||
|
||||
// EntryQueryBuilder builds a SQL query to fetch entries.
|
||||
type EntryQueryBuilder struct {
|
||||
store *Storage
|
||||
args []interface{}
|
||||
conditions []string
|
||||
order string
|
||||
direction string
|
||||
limit int
|
||||
offset int
|
||||
store *Storage
|
||||
args []interface{}
|
||||
conditions []string
|
||||
sortExpressions []string
|
||||
limit int
|
||||
offset int
|
||||
}
|
||||
|
||||
// WithSearchQuery adds full-text search query to the condition.
|
||||
|
@ -35,8 +34,10 @@ func (e *EntryQueryBuilder) WithSearchQuery(query string) *EntryQueryBuilder {
|
|||
e.args = append(e.args, query)
|
||||
|
||||
// 0.0000001 = 0.1 / (seconds_in_a_day)
|
||||
e.WithOrder(fmt.Sprintf("ts_rank(document_vectors, plainto_tsquery($%d)) - extract (epoch from now() - published_at)::float * 0.0000001", nArgs))
|
||||
e.WithDirection("DESC")
|
||||
e.WithSorting(
|
||||
fmt.Sprintf("ts_rank(document_vectors, plainto_tsquery($%d)) - extract (epoch from now() - published_at)::float * 0.0000001", nArgs),
|
||||
"DESC",
|
||||
)
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
@ -168,15 +169,9 @@ func (e *EntryQueryBuilder) WithShareCodeNotEmpty() *EntryQueryBuilder {
|
|||
return e
|
||||
}
|
||||
|
||||
// WithOrder set the sorting order.
|
||||
func (e *EntryQueryBuilder) WithOrder(order string) *EntryQueryBuilder {
|
||||
e.order = order
|
||||
return e
|
||||
}
|
||||
|
||||
// WithDirection set the sorting direction.
|
||||
func (e *EntryQueryBuilder) WithDirection(direction string) *EntryQueryBuilder {
|
||||
e.direction = direction
|
||||
// WithSorting add a sort expression.
|
||||
func (e *EntryQueryBuilder) WithSorting(column, direction string) *EntryQueryBuilder {
|
||||
e.sortExpressions = append(e.sortExpressions, fmt.Sprintf("%s %s", column, direction))
|
||||
return e
|
||||
}
|
||||
|
||||
|
@ -403,12 +398,8 @@ func (e *EntryQueryBuilder) buildCondition() string {
|
|||
func (e *EntryQueryBuilder) buildSorting() string {
|
||||
var parts []string
|
||||
|
||||
if e.order != "" {
|
||||
parts = append(parts, fmt.Sprintf(`ORDER BY %s`, e.order))
|
||||
}
|
||||
|
||||
if e.direction != "" {
|
||||
parts = append(parts, e.direction)
|
||||
if len(e.sortExpressions) > 0 {
|
||||
parts = append(parts, fmt.Sprintf(`ORDER BY %s`, strings.Join(e.sortExpressions, ", ")))
|
||||
}
|
||||
|
||||
if e.limit > 0 {
|
||||
|
|
|
@ -135,8 +135,7 @@ func (s *Storage) CountAllFeedsWithErrors() int {
|
|||
// Feeds returns all feeds that belongs to the given user.
|
||||
func (s *Storage) Feeds(userID int64) (model.Feeds, error) {
|
||||
builder := NewFeedQueryBuilder(s, userID)
|
||||
builder.WithOrder(model.DefaultFeedSorting)
|
||||
builder.WithDirection(model.DefaultFeedSortingDirection)
|
||||
builder.WithSorting(model.DefaultFeedSorting, model.DefaultFeedSortingDirection)
|
||||
return builder.GetFeeds()
|
||||
}
|
||||
|
||||
|
@ -153,8 +152,7 @@ func getFeedsSorted(builder *FeedQueryBuilder) (model.Feeds, error) {
|
|||
func (s *Storage) FeedsWithCounters(userID int64) (model.Feeds, error) {
|
||||
builder := NewFeedQueryBuilder(s, userID)
|
||||
builder.WithCounters()
|
||||
builder.WithOrder(model.DefaultFeedSorting)
|
||||
builder.WithDirection(model.DefaultFeedSortingDirection)
|
||||
builder.WithSorting(model.DefaultFeedSorting, model.DefaultFeedSortingDirection)
|
||||
return getFeedsSorted(builder)
|
||||
}
|
||||
|
||||
|
@ -171,8 +169,7 @@ func (s *Storage) FeedsByCategoryWithCounters(userID, categoryID int64) (model.F
|
|||
builder := NewFeedQueryBuilder(s, userID)
|
||||
builder.WithCategoryID(categoryID)
|
||||
builder.WithCounters()
|
||||
builder.WithOrder(model.DefaultFeedSorting)
|
||||
builder.WithDirection(model.DefaultFeedSortingDirection)
|
||||
builder.WithSorting(model.DefaultFeedSorting, model.DefaultFeedSortingDirection)
|
||||
return getFeedsSorted(builder)
|
||||
}
|
||||
|
||||
|
|
|
@ -18,8 +18,7 @@ type FeedQueryBuilder struct {
|
|||
store *Storage
|
||||
args []interface{}
|
||||
conditions []string
|
||||
order string
|
||||
direction string
|
||||
sortExpressions []string
|
||||
limit int
|
||||
offset int
|
||||
withCounters bool
|
||||
|
@ -66,15 +65,9 @@ func (f *FeedQueryBuilder) WithCounters() *FeedQueryBuilder {
|
|||
return f
|
||||
}
|
||||
|
||||
// WithOrder set the sorting order.
|
||||
func (f *FeedQueryBuilder) WithOrder(order string) *FeedQueryBuilder {
|
||||
f.order = order
|
||||
return f
|
||||
}
|
||||
|
||||
// WithDirection set the sorting direction.
|
||||
func (f *FeedQueryBuilder) WithDirection(direction string) *FeedQueryBuilder {
|
||||
f.direction = direction
|
||||
// WithSorting add a sort expression.
|
||||
func (f *FeedQueryBuilder) WithSorting(column, direction string) *FeedQueryBuilder {
|
||||
f.sortExpressions = append(f.sortExpressions, fmt.Sprintf("%s %s", column, direction))
|
||||
return f
|
||||
}
|
||||
|
||||
|
@ -101,12 +94,8 @@ func (f *FeedQueryBuilder) buildCounterCondition() string {
|
|||
func (f *FeedQueryBuilder) buildSorting() string {
|
||||
var parts []string
|
||||
|
||||
if f.order != "" {
|
||||
parts = append(parts, fmt.Sprintf(`ORDER BY %s`, f.order))
|
||||
}
|
||||
|
||||
if f.direction != "" {
|
||||
parts = append(parts, f.direction)
|
||||
if len(f.sortExpressions) > 0 {
|
||||
parts = append(parts, fmt.Sprintf(`ORDER BY %s`, strings.Join(f.sortExpressions, ", ")))
|
||||
}
|
||||
|
||||
if len(parts) > 0 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue