1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
// 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.
package storage
import (
"fmt"
"github.com/miniflux/miniflux2/helper"
"github.com/miniflux/miniflux2/model"
"strings"
"time"
)
type EntryQueryBuilder struct {
store *Storage
feedID int64
userID int64
timezone string
categoryID int64
status string
order string
direction string
limit int
offset int
entryID int64
gtEntryID int64
ltEntryID int64
conditions []string
args []interface{}
}
func (e *EntryQueryBuilder) WithCondition(column, operator string, value interface{}) *EntryQueryBuilder {
e.args = append(e.args, value)
e.conditions = append(e.conditions, fmt.Sprintf("%s %s $%d", column, operator, len(e.args)+1))
return e
}
func (e *EntryQueryBuilder) WithEntryID(entryID int64) *EntryQueryBuilder {
e.entryID = entryID
return e
}
func (e *EntryQueryBuilder) WithEntryIDGreaterThan(entryID int64) *EntryQueryBuilder {
e.gtEntryID = entryID
return e
}
func (e *EntryQueryBuilder) WithEntryIDLowerThan(entryID int64) *EntryQueryBuilder {
e.ltEntryID = entryID
return e
}
func (e *EntryQueryBuilder) WithFeedID(feedID int64) *EntryQueryBuilder {
e.feedID = feedID
return e
}
func (e *EntryQueryBuilder) WithCategoryID(categoryID int64) *EntryQueryBuilder {
e.categoryID = categoryID
return e
}
func (e *EntryQueryBuilder) WithStatus(status string) *EntryQueryBuilder {
e.status = status
return e
}
func (e *EntryQueryBuilder) WithOrder(order string) *EntryQueryBuilder {
e.order = order
return e
}
func (e *EntryQueryBuilder) WithDirection(direction string) *EntryQueryBuilder {
e.direction = direction
return e
}
func (e *EntryQueryBuilder) WithLimit(limit int) *EntryQueryBuilder {
e.limit = limit
return e
}
func (e *EntryQueryBuilder) WithOffset(offset int) *EntryQueryBuilder {
e.offset = offset
return e
}
func (e *EntryQueryBuilder) CountEntries() (count int, err error) {
defer helper.ExecutionTime(
time.Now(),
fmt.Sprintf("[EntryQueryBuilder:CountEntries] userID=%d, feedID=%d, status=%s", e.userID, e.feedID, e.status),
)
query := `SELECT count(*) FROM entries e LEFT JOIN feeds f ON f.id=e.feed_id WHERE %s`
args, condition := e.buildCondition()
err = e.store.db.QueryRow(fmt.Sprintf(query, condition), args...).Scan(&count)
if err != nil {
return 0, fmt.Errorf("unable to count entries: %v", err)
}
return count, nil
}
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
}
func (e *EntryQueryBuilder) GetEntries() (model.Entries, error) {
debugStr := "[EntryQueryBuilder:GetEntries] userID=%d, feedID=%d, categoryID=%d, status=%s, order=%s, direction=%s, offset=%d, limit=%d"
defer helper.ExecutionTime(time.Now(), fmt.Sprintf(debugStr, e.userID, e.feedID, e.categoryID, e.status, e.order, e.direction, e.offset, e.limit))
query := `
SELECT
e.id, e.user_id, e.feed_id, e.hash, e.published_at at time zone '%s', e.title, e.url, e.author, e.content, e.status,
f.title as feed_title, f.feed_url, f.site_url, f.checked_at,
f.category_id, c.title as category_title,
fi.icon_id
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
WHERE %s %s
`
args, conditions := e.buildCondition()
query = fmt.Sprintf(query, e.timezone, conditions, e.buildSorting())
// log.Println(query)
rows, err := e.store.db.Query(query, args...)
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{}
entry.Feed = &model.Feed{UserID: e.userID}
entry.Feed.Category = &model.Category{UserID: e.userID}
entry.Feed.Icon = &model.FeedIcon{}
err := rows.Scan(
&entry.ID,
&entry.UserID,
&entry.FeedID,
&entry.Hash,
&entry.Date,
&entry.Title,
&entry.URL,
&entry.Author,
&entry.Content,
&entry.Status,
&entry.Feed.Title,
&entry.Feed.FeedURL,
&entry.Feed.SiteURL,
&entry.Feed.CheckedAt,
&entry.Feed.Category.ID,
&entry.Feed.Category.Title,
&iconID,
)
if err != nil {
return nil, fmt.Errorf("Unable to fetch entry row: %v", err)
}
if iconID == nil {
entry.Feed.Icon.IconID = 0
} else {
entry.Feed.Icon.IconID = iconID.(int64)
}
entry.Feed.ID = entry.FeedID
entry.Feed.Icon.FeedID = entry.FeedID
entries = append(entries, &entry)
}
return entries, nil
}
func (e *EntryQueryBuilder) buildCondition() ([]interface{}, string) {
args := []interface{}{e.userID}
conditions := []string{"e.user_id = $1"}
if len(e.conditions) > 0 {
conditions = append(conditions, e.conditions...)
args = append(args, e.args...)
}
if e.categoryID != 0 {
conditions = append(conditions, fmt.Sprintf("f.category_id=$%d", len(args)+1))
args = append(args, e.categoryID)
}
if e.feedID != 0 {
conditions = append(conditions, fmt.Sprintf("e.feed_id=$%d", len(args)+1))
args = append(args, e.feedID)
}
if e.entryID != 0 {
conditions = append(conditions, fmt.Sprintf("e.id=$%d", len(args)+1))
args = append(args, e.entryID)
}
if e.gtEntryID != 0 {
conditions = append(conditions, fmt.Sprintf("e.id > $%d", len(args)+1))
args = append(args, e.gtEntryID)
}
if e.ltEntryID != 0 {
conditions = append(conditions, fmt.Sprintf("e.id < $%d", len(args)+1))
args = append(args, e.ltEntryID)
}
if e.status != "" {
conditions = append(conditions, fmt.Sprintf("e.status=$%d", len(args)+1))
args = append(args, e.status)
}
return args, strings.Join(conditions, " AND ")
}
func (e *EntryQueryBuilder) buildSorting() string {
var queries []string
if e.order != "" {
queries = append(queries, fmt.Sprintf(`ORDER BY "%s"`, e.order))
}
if e.direction != "" {
queries = append(queries, fmt.Sprintf(`%s`, e.direction))
}
if e.limit != 0 {
queries = append(queries, fmt.Sprintf(`LIMIT %d`, e.limit))
}
if e.offset != 0 {
queries = append(queries, fmt.Sprintf(`OFFSET %d`, e.offset))
}
return strings.Join(queries, " ")
}
func NewEntryQueryBuilder(store *Storage, userID int64, timezone string) *EntryQueryBuilder {
return &EntryQueryBuilder{
store: store,
userID: userID,
timezone: timezone,
}
}
|