2017-11-20 06:10:04 +01:00
|
|
|
// Copyright 2017 Frédéric Guillot. All rights reserved.
|
|
|
|
// Use of this source code is governed by the Apache 2.0
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2018-08-25 06:51:50 +02:00
|
|
|
package storage // import "miniflux.app/storage"
|
2017-11-20 06:10:04 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2017-11-22 02:40:29 +01:00
|
|
|
|
2017-12-04 02:44:27 +01:00
|
|
|
"github.com/lib/pq"
|
|
|
|
|
2018-08-25 06:51:50 +02:00
|
|
|
"miniflux.app/model"
|
|
|
|
"miniflux.app/timer"
|
|
|
|
"miniflux.app/timezone"
|
2017-11-20 06:10:04 +01:00
|
|
|
)
|
|
|
|
|
2017-11-22 02:40:29 +01:00
|
|
|
// EntryQueryBuilder builds a SQL query to fetch entries.
|
2017-11-20 06:10:04 +01:00
|
|
|
type EntryQueryBuilder struct {
|
2018-06-09 03:24:41 +02:00
|
|
|
store *Storage
|
|
|
|
args []interface{}
|
|
|
|
conditions []string
|
|
|
|
order string
|
|
|
|
direction string
|
|
|
|
limit int
|
|
|
|
offset int
|
2017-12-22 20:33:01 +01:00
|
|
|
}
|
|
|
|
|
2018-07-05 02:40:03 +02:00
|
|
|
// WithSearchQuery adds full-text search query to the condition.
|
|
|
|
func (e *EntryQueryBuilder) WithSearchQuery(query string) *EntryQueryBuilder {
|
|
|
|
if query != "" {
|
|
|
|
e.conditions = append(e.conditions, fmt.Sprintf("e.document_vectors @@ plainto_tsquery($%d)", len(e.args)+1))
|
|
|
|
e.args = append(e.args, query)
|
|
|
|
}
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-12-22 20:33:01 +01:00
|
|
|
// WithStarred adds starred filter.
|
|
|
|
func (e *EntryQueryBuilder) WithStarred() *EntryQueryBuilder {
|
2018-06-09 03:24:41 +02:00
|
|
|
e.conditions = append(e.conditions, "e.starred is true")
|
2017-12-22 20:33:01 +01:00
|
|
|
return e
|
2017-12-04 02:44:27 +01:00
|
|
|
}
|
|
|
|
|
2018-06-09 22:40:20 +02:00
|
|
|
// BeforeDate adds a condition < published_at
|
|
|
|
func (e *EntryQueryBuilder) BeforeDate(date time.Time) *EntryQueryBuilder {
|
2018-06-09 03:24:41 +02:00
|
|
|
e.conditions = append(e.conditions, fmt.Sprintf("e.published_at < $%d", len(e.args)+1))
|
|
|
|
e.args = append(e.args, date)
|
2017-12-04 02:44:27 +01:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2018-06-09 22:40:20 +02:00
|
|
|
// AfterDate adds a condition > published_at
|
|
|
|
func (e *EntryQueryBuilder) AfterDate(date time.Time) *EntryQueryBuilder {
|
|
|
|
e.conditions = append(e.conditions, fmt.Sprintf("e.published_at > $%d", len(e.args)+1))
|
|
|
|
e.args = append(e.args, date)
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// BeforeEntryID adds a condition < entryID.
|
|
|
|
func (e *EntryQueryBuilder) BeforeEntryID(entryID int64) *EntryQueryBuilder {
|
|
|
|
if entryID != 0 {
|
|
|
|
e.conditions = append(e.conditions, fmt.Sprintf("e.id < $%d", len(e.args)+1))
|
|
|
|
e.args = append(e.args, entryID)
|
|
|
|
}
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// AfterEntryID adds a condition > entryID.
|
|
|
|
func (e *EntryQueryBuilder) AfterEntryID(entryID int64) *EntryQueryBuilder {
|
2018-06-09 03:24:41 +02:00
|
|
|
if entryID != 0 {
|
|
|
|
e.conditions = append(e.conditions, fmt.Sprintf("e.id > $%d", len(e.args)+1))
|
|
|
|
e.args = append(e.args, entryID)
|
|
|
|
}
|
2017-12-04 02:44:27 +01:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithEntryIDs adds a condition to fetch only the given entry IDs.
|
|
|
|
func (e *EntryQueryBuilder) WithEntryIDs(entryIDs []int64) *EntryQueryBuilder {
|
2018-06-09 03:24:41 +02:00
|
|
|
e.conditions = append(e.conditions, fmt.Sprintf("e.id = ANY($%d)", len(e.args)+1))
|
|
|
|
e.args = append(e.args, pq.Array(entryIDs))
|
2017-12-04 02:44:27 +01:00
|
|
|
return e
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-22 02:40:29 +01:00
|
|
|
// WithEntryID set the entryID.
|
2017-11-20 06:10:04 +01:00
|
|
|
func (e *EntryQueryBuilder) WithEntryID(entryID int64) *EntryQueryBuilder {
|
2018-06-09 03:24:41 +02:00
|
|
|
if entryID != 0 {
|
|
|
|
e.conditions = append(e.conditions, fmt.Sprintf("e.id = $%d", len(e.args)+1))
|
|
|
|
e.args = append(e.args, entryID)
|
|
|
|
}
|
2017-11-20 06:10:04 +01:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-11-22 02:40:29 +01:00
|
|
|
// WithFeedID set the feedID.
|
2017-11-20 06:10:04 +01:00
|
|
|
func (e *EntryQueryBuilder) WithFeedID(feedID int64) *EntryQueryBuilder {
|
2018-06-09 03:24:41 +02:00
|
|
|
if feedID != 0 {
|
|
|
|
e.conditions = append(e.conditions, fmt.Sprintf("e.feed_id = $%d", len(e.args)+1))
|
|
|
|
e.args = append(e.args, feedID)
|
|
|
|
}
|
2017-11-20 06:10:04 +01:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-11-22 02:40:29 +01:00
|
|
|
// WithCategoryID set the categoryID.
|
2017-11-20 06:10:04 +01:00
|
|
|
func (e *EntryQueryBuilder) WithCategoryID(categoryID int64) *EntryQueryBuilder {
|
2018-06-09 03:24:41 +02:00
|
|
|
if categoryID != 0 {
|
|
|
|
e.conditions = append(e.conditions, fmt.Sprintf("f.category_id = $%d", len(e.args)+1))
|
|
|
|
e.args = append(e.args, categoryID)
|
|
|
|
}
|
2017-11-20 06:10:04 +01:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-11-22 02:40:29 +01:00
|
|
|
// WithStatus set the entry status.
|
2017-11-20 06:10:04 +01:00
|
|
|
func (e *EntryQueryBuilder) WithStatus(status string) *EntryQueryBuilder {
|
2018-06-09 03:24:41 +02:00
|
|
|
if status != "" {
|
|
|
|
e.conditions = append(e.conditions, fmt.Sprintf("e.status = $%d", len(e.args)+1))
|
|
|
|
e.args = append(e.args, status)
|
|
|
|
}
|
2017-11-20 06:10:04 +01:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-11-22 02:40:29 +01:00
|
|
|
// WithoutStatus set the entry status that should not be returned.
|
|
|
|
func (e *EntryQueryBuilder) WithoutStatus(status string) *EntryQueryBuilder {
|
2018-06-09 03:24:41 +02:00
|
|
|
if status != "" {
|
|
|
|
e.conditions = append(e.conditions, fmt.Sprintf("e.status <> $%d", len(e.args)+1))
|
|
|
|
e.args = append(e.args, status)
|
|
|
|
}
|
2017-11-22 02:40:29 +01:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithOrder set the sorting order.
|
2017-11-20 06:10:04 +01:00
|
|
|
func (e *EntryQueryBuilder) WithOrder(order string) *EntryQueryBuilder {
|
|
|
|
e.order = order
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-11-22 02:40:29 +01:00
|
|
|
// WithDirection set the sorting direction.
|
2017-11-20 06:10:04 +01:00
|
|
|
func (e *EntryQueryBuilder) WithDirection(direction string) *EntryQueryBuilder {
|
|
|
|
e.direction = direction
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-11-22 02:40:29 +01:00
|
|
|
// WithLimit set the limit.
|
2017-11-20 06:10:04 +01:00
|
|
|
func (e *EntryQueryBuilder) WithLimit(limit int) *EntryQueryBuilder {
|
|
|
|
e.limit = limit
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-11-22 02:40:29 +01:00
|
|
|
// WithOffset set the offset.
|
2017-11-20 06:10:04 +01:00
|
|
|
func (e *EntryQueryBuilder) WithOffset(offset int) *EntryQueryBuilder {
|
|
|
|
e.offset = offset
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-11-22 02:40:29 +01:00
|
|
|
// CountEntries count the number of entries that match the condition.
|
2017-11-20 06:10:04 +01:00
|
|
|
func (e *EntryQueryBuilder) CountEntries() (count int, err error) {
|
|
|
|
query := `SELECT count(*) FROM entries e LEFT JOIN feeds f ON f.id=e.feed_id WHERE %s`
|
2018-06-09 03:24:41 +02:00
|
|
|
condition := e.buildCondition()
|
|
|
|
|
2018-07-05 02:40:03 +02:00
|
|
|
defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[EntryQueryBuilder:CountEntries] %s, args=%v", condition, e.args))
|
2018-06-09 03:24:41 +02:00
|
|
|
|
|
|
|
err = e.store.db.QueryRow(fmt.Sprintf(query, condition), e.args...).Scan(&count)
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("unable to count entries: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return count, nil
|
|
|
|
}
|
|
|
|
|
2017-11-22 02:40:29 +01:00
|
|
|
// GetEntry returns a single entry that match the condition.
|
2017-11-20 06:10:04 +01:00
|
|
|
func (e *EntryQueryBuilder) GetEntry() (*model.Entry, error) {
|
|
|
|
e.limit = 1
|
|
|
|
entries, err := e.GetEntries()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(entries) != 1 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
entries[0].Enclosures, err = e.store.GetEnclosures(entries[0].ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return entries[0], nil
|
|
|
|
}
|
|
|
|
|
2017-11-22 02:40:29 +01:00
|
|
|
// GetEntries returns a list of entries that match the condition.
|
2017-11-20 06:10:04 +01:00
|
|
|
func (e *EntryQueryBuilder) GetEntries() (model.Entries, error) {
|
|
|
|
query := `
|
|
|
|
SELECT
|
2017-12-29 04:20:14 +01:00
|
|
|
e.id, e.user_id, e.feed_id, e.hash, e.published_at at time zone u.timezone, e.title,
|
2018-04-07 22:50:45 +02:00
|
|
|
e.url, e.comments_url, e.author, e.content, e.status, e.starred,
|
2017-11-20 06:10:04 +01:00
|
|
|
f.title as feed_title, f.feed_url, f.site_url, f.checked_at,
|
2017-12-13 04:19:36 +01:00
|
|
|
f.category_id, c.title as category_title, f.scraper_rules, f.rewrite_rules, f.crawler,
|
2018-03-02 06:24:58 +01:00
|
|
|
fi.icon_id,
|
|
|
|
u.timezone
|
2017-11-20 06:10:04 +01:00
|
|
|
FROM entries e
|
|
|
|
LEFT JOIN feeds f ON f.id=e.feed_id
|
|
|
|
LEFT JOIN categories c ON c.id=f.category_id
|
|
|
|
LEFT JOIN feed_icons fi ON fi.feed_id=f.id
|
2017-12-29 04:20:14 +01:00
|
|
|
LEFT JOIN users u ON u.id=e.user_id
|
2017-11-20 06:10:04 +01:00
|
|
|
WHERE %s %s
|
|
|
|
`
|
|
|
|
|
2018-06-09 03:24:41 +02:00
|
|
|
condition := e.buildCondition()
|
2018-06-09 22:40:20 +02:00
|
|
|
sorting := e.buildSorting()
|
|
|
|
query = fmt.Sprintf(query, condition, sorting)
|
2017-11-20 06:10:04 +01:00
|
|
|
|
2018-06-09 22:40:20 +02:00
|
|
|
defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[EntryQueryBuilder:GetEntries] %s, args=%v, sorting=%s", condition, e.args, sorting))
|
2018-06-09 03:24:41 +02:00
|
|
|
|
|
|
|
rows, err := e.store.db.Query(query, e.args...)
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to get entries: %v", err)
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
entries := make(model.Entries, 0)
|
|
|
|
for rows.Next() {
|
|
|
|
var entry model.Entry
|
|
|
|
var iconID interface{}
|
2018-03-02 06:24:58 +01:00
|
|
|
var tz string
|
2017-11-20 06:10:04 +01:00
|
|
|
|
2018-06-09 03:24:41 +02:00
|
|
|
entry.Feed = &model.Feed{}
|
|
|
|
entry.Feed.Category = &model.Category{}
|
2017-11-20 06:10:04 +01:00
|
|
|
entry.Feed.Icon = &model.FeedIcon{}
|
|
|
|
|
|
|
|
err := rows.Scan(
|
|
|
|
&entry.ID,
|
|
|
|
&entry.UserID,
|
|
|
|
&entry.FeedID,
|
|
|
|
&entry.Hash,
|
|
|
|
&entry.Date,
|
|
|
|
&entry.Title,
|
|
|
|
&entry.URL,
|
2018-04-07 22:50:45 +02:00
|
|
|
&entry.CommentsURL,
|
2017-11-20 06:10:04 +01:00
|
|
|
&entry.Author,
|
|
|
|
&entry.Content,
|
|
|
|
&entry.Status,
|
2017-12-22 20:33:01 +01:00
|
|
|
&entry.Starred,
|
2017-11-20 06:10:04 +01:00
|
|
|
&entry.Feed.Title,
|
|
|
|
&entry.Feed.FeedURL,
|
|
|
|
&entry.Feed.SiteURL,
|
|
|
|
&entry.Feed.CheckedAt,
|
|
|
|
&entry.Feed.Category.ID,
|
|
|
|
&entry.Feed.Category.Title,
|
2017-12-11 05:51:04 +01:00
|
|
|
&entry.Feed.ScraperRules,
|
2017-12-12 07:16:32 +01:00
|
|
|
&entry.Feed.RewriteRules,
|
2017-12-13 04:19:36 +01:00
|
|
|
&entry.Feed.Crawler,
|
2017-11-20 06:10:04 +01:00
|
|
|
&iconID,
|
2018-03-02 06:24:58 +01:00
|
|
|
&tz,
|
2017-11-20 06:10:04 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
2017-12-03 02:04:01 +01:00
|
|
|
return nil, fmt.Errorf("unable to fetch entry row: %v", err)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if iconID == nil {
|
|
|
|
entry.Feed.Icon.IconID = 0
|
|
|
|
} else {
|
|
|
|
entry.Feed.Icon.IconID = iconID.(int64)
|
|
|
|
}
|
|
|
|
|
2018-03-02 06:24:58 +01:00
|
|
|
// Make sure that timestamp fields contains timezone information (API)
|
|
|
|
entry.Date = timezone.Convert(tz, entry.Date)
|
|
|
|
entry.Feed.CheckedAt = timezone.Convert(tz, entry.Feed.CheckedAt)
|
|
|
|
|
2017-11-20 06:10:04 +01:00
|
|
|
entry.Feed.ID = entry.FeedID
|
2018-06-09 03:24:41 +02:00
|
|
|
entry.Feed.UserID = entry.UserID
|
2017-11-20 06:10:04 +01:00
|
|
|
entry.Feed.Icon.FeedID = entry.FeedID
|
2018-06-09 03:24:41 +02:00
|
|
|
entry.Feed.Category.UserID = entry.UserID
|
2017-11-20 06:10:04 +01:00
|
|
|
entries = append(entries, &entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
return entries, nil
|
|
|
|
}
|
|
|
|
|
2017-12-04 02:44:27 +01:00
|
|
|
// GetEntryIDs returns a list of entry IDs that match the condition.
|
|
|
|
func (e *EntryQueryBuilder) GetEntryIDs() ([]int64, error) {
|
2018-06-09 03:24:41 +02:00
|
|
|
query := `SELECT e.id FROM entries e LEFT JOIN feeds f ON f.id=e.feed_id WHERE %s %s`
|
2017-12-04 02:44:27 +01:00
|
|
|
|
2018-06-09 03:24:41 +02:00
|
|
|
condition := e.buildCondition()
|
|
|
|
query = fmt.Sprintf(query, condition, e.buildSorting())
|
2017-12-04 02:44:27 +01:00
|
|
|
// log.Println(query)
|
|
|
|
|
2018-06-09 03:24:41 +02:00
|
|
|
defer timer.ExecutionTime(time.Now(), fmt.Sprintf("[EntryQueryBuilder:GetEntryIDs] condition=%s, args=%v", condition, e.args))
|
|
|
|
|
|
|
|
rows, err := e.store.db.Query(query, e.args...)
|
2017-12-04 02:44:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to get entries: %v", err)
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var entryIDs []int64
|
|
|
|
for rows.Next() {
|
|
|
|
var entryID int64
|
|
|
|
|
|
|
|
err := rows.Scan(&entryID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to fetch entry row: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
entryIDs = append(entryIDs, entryID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return entryIDs, nil
|
|
|
|
}
|
|
|
|
|
2018-06-09 03:24:41 +02:00
|
|
|
func (e *EntryQueryBuilder) buildCondition() string {
|
|
|
|
return strings.Join(e.conditions, " AND ")
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *EntryQueryBuilder) buildSorting() string {
|
2018-06-09 22:40:20 +02:00
|
|
|
var parts []string
|
2017-11-20 06:10:04 +01:00
|
|
|
|
|
|
|
if e.order != "" {
|
2018-06-09 22:40:20 +02:00
|
|
|
parts = append(parts, fmt.Sprintf(`ORDER BY "%s"`, e.order))
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if e.direction != "" {
|
2018-06-09 22:40:20 +02:00
|
|
|
parts = append(parts, fmt.Sprintf(`%s`, e.direction))
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if e.limit != 0 {
|
2018-06-09 22:40:20 +02:00
|
|
|
parts = append(parts, fmt.Sprintf(`LIMIT %d`, e.limit))
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if e.offset != 0 {
|
2018-06-09 22:40:20 +02:00
|
|
|
parts = append(parts, fmt.Sprintf(`OFFSET %d`, e.offset))
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2018-06-09 22:40:20 +02:00
|
|
|
return strings.Join(parts, " ")
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-22 02:40:29 +01:00
|
|
|
// NewEntryQueryBuilder returns a new EntryQueryBuilder.
|
2017-12-29 04:20:14 +01:00
|
|
|
func NewEntryQueryBuilder(store *Storage, userID int64) *EntryQueryBuilder {
|
2017-11-20 06:10:04 +01:00
|
|
|
return &EntryQueryBuilder{
|
2018-06-09 03:24:41 +02:00
|
|
|
store: store,
|
|
|
|
args: []interface{}{userID},
|
|
|
|
conditions: []string{"e.user_id = $1"},
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
}
|