-
Notifications
You must be signed in to change notification settings - Fork 1
/
sqlstore.go
628 lines (541 loc) · 14.8 KB
/
sqlstore.go
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
package sqlstore
import (
"database/sql"
"encoding/json"
"fmt"
"os"
"regexp"
"strings"
"time"
"github.com/bcampbell/scrapeomat/store"
)
type nullLogger struct{}
func (l nullLogger) Printf(format string, v ...interface{}) {
}
type stderrLogger struct{}
func (l stderrLogger) Printf(format string, v ...interface{}) {
fmt.Fprintf(os.Stderr, format, v...)
}
// SQLStore stashes articles in an SQL database
type SQLStore struct {
db *sql.DB
driverName string
loc *time.Location
ErrLog store.Logger
DebugLog store.Logger
}
type SQLArtIter struct {
rows *sql.Rows
ss *SQLStore
current *store.Article
err error
}
// Which method to use to get last insert IDs
const (
DUNNO = iota
RESULT // use Result.LastInsertID()
RETURNING // use sql "RETURNING" clause
)
// eg "postgres", "postgres://username@localhost/dbname"
// eg "sqlite3", "/tmp/foo.db"
func New(driver string, connStr string) (*SQLStore, error) {
//db, err := sql.Open("postgres", connStr)
db, err := sql.Open(driver, connStr)
if err != nil {
return nil, err
}
return NewFromDB(driver, db)
}
func NewFromDB(driver string, db *sql.DB) (*SQLStore, error) {
err := db.Ping()
if err != nil {
db.Close()
return nil, err
}
// our assumed location for publication dates, when no timezone given
// TODO: this is the wrong place for it. Scraper should handle this on a per-publication basis
// loc, err := time.LoadLocation("Europe/London")
// if err != nil {
// return nil, err
// }
ss := SQLStore{
db: db,
driverName: driver,
loc: time.UTC,
ErrLog: nullLogger{}, // TODO: should log to stderr by default?
DebugLog: nullLogger{},
}
// TODO: would be nice to have logger set up before here...
err = ss.checkSchema()
if err != nil {
db.Close()
return nil, err
}
return &ss, nil
}
// Same as New(), but if driver or connStr is missing, will try and read them
// from environment vars: SCRAPEOMAT_DRIVER & SCRAPEOMAT_DB.
// If both driver and SCRAPEOMAT_DRIVER are empty, default is "sqlite3".
func NewWithEnv(driver string, connStr string) (*SQLStore, error) {
if connStr == "" {
connStr = os.Getenv("SCRAPEOMAT_DB")
}
if driver == "" {
driver = os.Getenv("SCRAPEOMAT_DRIVER")
if driver == "" {
driver = "sqlite3"
}
}
if connStr == "" {
return nil, fmt.Errorf("no database specified (set SCRAPEOMAT_DB?)")
}
return New(driver, connStr)
}
func (ss *SQLStore) Close() {
if ss.db != nil {
ss.db.Close()
ss.db = nil
}
}
func (ss *SQLStore) rebind(q string) string {
return rebind(bindType(ss.driverName), q)
}
// can we use Result.LastInsertID() or do we need to fiddle the SQL?
func (ss *SQLStore) insertIDType() int {
switch ss.driverName {
case "postgres", "pgx", "pq-timeouts", "cloudsqlpostgres", "ql":
return RETURNING
case "sqlite3", "mysql":
return RESULT
case "oci8", "ora", "goracle":
// ora: https://godoc.org/gopkg.in/rana/ora.v4#hdr-LastInsertId
return DUNNO
case "sqlserver":
// https://github.com/denisenkom/go-mssqldb#important-notes
return DUNNO
default:
return DUNNO
}
}
// return a string with sql fn to return current timestamp.
func (ss *SQLStore) nowSQL() string {
switch ss.driverName {
case "postgres", "pgx", "pq-timeouts", "cloudsqlpostgres", "ql":
return "NOW()"
case "sqlite3":
return "datetime('now','localtime')"
case "mysql":
return "PROPER_FN_GOES_HERE_PLEASE()"
case "oci8", "ora", "goracle":
return "PROPER_FN_GOES_HERE_PLEASE()"
case "sqlserver":
return "PROPER_FN_GOES_HERE_PLEASE()"
default:
return "PROPER_FN_GOES_HERE_PLEASE()"
}
}
var timeFmts = []string{
time.RFC3339,
"2006-01-02T15:04Z07:00",
// "2006-01-02T15:04:05Z",
"2006-01-02T15:04:05",
"2006-01-02T15:04",
"2006-01-02",
}
func (ss *SQLStore) cvtTime(timestamp string) sql.NullTime {
for _, layout := range timeFmts {
t, err := time.ParseInLocation(layout, timestamp, ss.loc)
if err == nil {
return sql.NullTime{Time: t, Valid: true}
}
}
return sql.NullTime{Valid: false}
}
var datePat = regexp.MustCompile(`^\d\d\d\d-\d\d-\d\d`)
// FindURLs Looks up article urls, returning a list of matching article IDs.
// usually you'd use this on the URLs for a single article, expecting zero or one IDs back,
// but there's no reason you can't look up a whole bunch of articles at once, although you won't
// know which ones match which URLs.
// remember that there can be multiple URLs for a single article, AND also multiple articles can
// share the same URL (hopefully much much more rare).
func (ss *SQLStore) FindURLs(urls []string) ([]int, error) {
params := make([]interface{}, len(urls))
placeholders := make([]string, len(urls))
for i, u := range urls {
params[i] = u
placeholders[i] = "?"
}
s := `SELECT distinct article_id FROM article_url WHERE url IN (` + strings.Join(placeholders, ",") + `)`
rows, err := ss.db.Query(ss.rebind(s), params...)
if err != nil {
return nil, err
}
defer rows.Close()
out := []int{}
for rows.Next() {
var artID int
if err := rows.Scan(&artID); err != nil {
return nil, err
}
out = append(out, artID)
}
if err := rows.Err(); err != nil {
return nil, err
}
return out, nil
}
// NOTE: remember article urls don't _have_ to be unique. If you only pass
// canonical urls in here you should be ok :-)
func (ss *SQLStore) WhichAreNew(artURLs []string) ([]string, error) {
stmt, err := ss.db.Prepare(ss.rebind(`SELECT article_id FROM article_url WHERE url=?`))
if err != nil {
return nil, err
}
newArts := []string{}
for _, u := range artURLs {
var artID int
err = stmt.QueryRow(u).Scan(&artID)
if err == sql.ErrNoRows {
newArts = append(newArts, u)
} else if err != nil {
return nil, err
}
}
return newArts, nil
}
// Build a WHERE clause from a filter.
func buildWhere(filt *store.Filter) (string, []interface{}) {
params := []interface{}{}
frags := []string{}
if !filt.PubFrom.IsZero() {
frags = append(frags, "a.published>=?")
params = append(params, filt.PubFrom)
}
if !filt.PubTo.IsZero() {
frags = append(frags, "a.published<?")
params = append(params, filt.PubTo)
}
if !filt.AddedFrom.IsZero() {
frags = append(frags, "a.added>=?")
params = append(params, filt.AddedFrom)
}
if !filt.AddedTo.IsZero() {
frags = append(frags, "a.added<?")
params = append(params, filt.AddedTo)
}
if filt.SinceID > 0 {
frags = append(frags, "a.id>?")
params = append(params, filt.SinceID)
}
if len(filt.PubCodes) > 0 {
foo := []string{}
bar := []interface{}{}
for _, code := range filt.PubCodes {
foo = append(foo, "?")
bar = append(bar, code)
}
frags = append(frags, "p.code IN ("+strings.Join(foo, ",")+")")
params = append(params, bar...)
}
if len(filt.XPubCodes) > 0 {
foo := []string{}
bar := []interface{}{}
for _, code := range filt.XPubCodes {
foo = append(foo, "?")
bar = append(bar, code)
}
frags = append(frags, "p.code NOT IN ("+strings.Join(foo, ",")+")")
params = append(params, bar...)
}
var whereClause string
if len(frags) > 0 {
whereClause = "WHERE " + strings.Join(frags, " AND ")
}
return whereClause, params
}
func (ss *SQLStore) FetchCount(filt *store.Filter) (int, error) {
whereClause, params := buildWhere(filt)
q := `SELECT COUNT(*)
FROM (article a INNER JOIN publication p ON a.publication_id=p.id)
` + whereClause
var cnt int
//ss.DebugLog.Printf("fetchcount: %s\n", q)
//ss.DebugLog.Printf("fetchcount params: %+v\n", params)
err := ss.db.QueryRow(ss.rebind(q), params...).Scan(&cnt)
return cnt, err
}
func (ss *SQLStore) Fetch(filt *store.Filter) store.ArtIter {
whereClause, params := buildWhere(filt)
q := `SELECT a.id,a.headline,a.canonical_url,a.content,a.published,a.updated,a.section,a.extra,p.code,p.name,p.domain
FROM (article a INNER JOIN publication p ON a.publication_id=p.id)
` + whereClause + ` ORDER BY a.id`
if filt.Count > 0 {
q += fmt.Sprintf(" LIMIT %d", filt.Count)
}
ss.DebugLog.Printf("fetch: %s\n", q)
ss.DebugLog.Printf("fetch params: %+v\n", params)
rows, err := ss.db.Query(ss.rebind(q), params...)
return &SQLArtIter{ss: ss, rows: rows, err: err}
}
func (it *SQLArtIter) Close() error {
// may not even have got as far as initing rows!
var err error
if it.rows != nil {
err = it.rows.Close()
it.rows = nil
}
return err
}
func (it *SQLArtIter) Err() error {
return it.err
}
// if it returns true there will be an article.
func (it *SQLArtIter) Next() bool {
it.current = nil
if it.err != nil {
return false // no more, if we're in error state
}
if !it.rows.Next() {
it.err = it.rows.Err()
return false // all done
}
art := &store.Article{}
var p = &art.Publication
var published, updated sql.NullTime
var extra []byte
err := it.rows.Scan(&art.ID, &art.Headline, &art.CanonicalURL, &art.Content, &published, &updated, &art.Section, &extra, &p.Code, &p.Name, &p.Domain)
if err != nil {
it.err = err
return false
}
if published.Valid {
art.Published = published.Time.Format(time.RFC3339)
}
if updated.Valid {
art.Updated = updated.Time.Format(time.RFC3339)
}
urls, err := it.ss.fetchURLs(art.ID)
if err != nil {
it.err = err
return false
}
art.URLs = urls
keywords, err := it.ss.fetchKeywords(art.ID)
if err != nil {
it.err = err
return false
}
art.Keywords = keywords
authors, err := it.ss.fetchAuthors(art.ID)
if err != nil {
it.err = err
return false
}
art.Authors = authors
// decode extra data
if len(extra) > 0 {
err = json.Unmarshal(extra, &art.Extra)
if err != nil {
it.err = err
return false
}
}
// if we get this far there's an article ready.
it.current = art
return true
}
func (it *SQLArtIter) Article() *store.Article {
return it.current
}
func (ss *SQLStore) fetchURLs(artID int) ([]string, error) {
q := `SELECT url FROM article_url WHERE article_id=?`
rows, err := ss.db.Query(ss.rebind(q), artID)
if err != nil {
return nil, err
}
defer rows.Close()
out := []string{}
for rows.Next() {
var u string
if err := rows.Scan(&u); err != nil {
return nil, err
}
out = append(out, u)
}
if err := rows.Err(); err != nil {
return nil, err
}
return out, nil
}
func (ss *SQLStore) fetchAuthors(artID int) ([]store.Author, error) {
q := `SELECT name,rel_link,email,twitter
FROM (author a INNER JOIN author_attr attr ON attr.author_id=a.id)
WHERE article_id=?`
rows, err := ss.db.Query(ss.rebind(q), artID)
if err != nil {
return nil, err
}
defer rows.Close()
out := []store.Author{}
for rows.Next() {
var a store.Author
if err := rows.Scan(&a.Name, &a.RelLink, &a.Email, &a.Twitter); err != nil {
return nil, err
}
out = append(out, a)
}
if err := rows.Err(); err != nil {
return nil, err
}
return out, nil
}
func (ss *SQLStore) fetchKeywords(artID int) ([]store.Keyword, error) {
q := `SELECT name,url
FROM article_keyword
WHERE article_id=?`
rows, err := ss.db.Query(ss.rebind(q), artID)
if err != nil {
return nil, err
}
defer rows.Close()
out := []store.Keyword{}
for rows.Next() {
var k store.Keyword
if err := rows.Scan(&k.Name, &k.URL); err != nil {
return nil, err
}
out = append(out, k)
}
if err := rows.Err(); err != nil {
return nil, err
}
return out, nil
}
func (ss *SQLStore) FetchPublications() ([]store.Publication, error) {
q := `SELECT code,name,domain FROM publication ORDER by code`
rows, err := ss.db.Query(ss.rebind(q))
if err != nil {
return nil, err
}
defer rows.Close()
out := []store.Publication{}
for rows.Next() {
var p store.Publication
if err := rows.Scan(&p.Code, &p.Name, &p.Domain); err != nil {
return nil, err
}
out = append(out, p)
}
if err := rows.Err(); err != nil {
return nil, err
}
return out, nil
}
func (ss *SQLStore) FetchSummary(filt *store.Filter, group string) ([]store.DatePubCount, error) {
// TODO: FetchSummary() should probably take a timezone, in order to properly
// group by day... for now, days are UTC days!
tz := time.UTC
whereClause, params := buildWhere(filt)
var dayField string
switch group {
case "published":
dayField = "a.published"
case "added":
dayField = "a.Added"
default:
return nil, fmt.Errorf("Bad group field (%s)", group)
}
var q string
q = `SELECT DATE(` + dayField + `) AS day, p.code, COUNT(*)
FROM (article a INNER JOIN publication p ON a.publication_id=p.id) ` +
whereClause + ` GROUP BY day, p.code ORDER BY day ASC ,p.code ASC;`
ss.DebugLog.Printf("summary: %s\n", q)
ss.DebugLog.Printf("summary params: %+v\n", params)
rows, err := ss.db.Query(ss.rebind(q), params...)
if err != nil {
return nil, err
}
defer rows.Close()
out := []store.DatePubCount{}
for rows.Next() {
foo := store.DatePubCount{}
if ss.driverName == "sqlite3" {
// TODO: sqlite3 driver can't seem to scan a DATE() to a time.Time (or sql.NullTime)
// TODO: INVESTIGATE!
// for now, workaround with string parsing.
var day sql.NullString
if err := rows.Scan(&day, &foo.PubCode, &foo.Count); err != nil {
return nil, err
}
if day.Valid {
t, err := time.ParseInLocation("2006-01-02", day.String, tz)
if err == nil {
foo.Date = t
}
}
} else {
// the non-sqlite3 version:
var day sql.NullTime
if err := rows.Scan(&day, &foo.PubCode, &foo.Count); err != nil {
return nil, err
}
if day.Valid {
foo.Date = day.Time.In(tz)
}
}
//ss.DebugLog.Printf("summary: %v\n", foo)
out = append(out, foo)
}
if err := rows.Err(); err != nil {
return nil, err
}
ss.DebugLog.Printf("summary out: %d\n", len(out))
return out, nil
}
// Fetch a single article by ID
func (ss *SQLStore) FetchArt(artID int) (*store.Article, error) {
q := `SELECT a.id,a.headline,a.canonical_url,a.content,a.published,a.updated,a.section,a.extra,p.code,p.name,p.domain
FROM (article a INNER JOIN publication p ON a.publication_id=p.id)
WHERE a.id=?`
ss.DebugLog.Printf("fetch: %s [%d]\n", q, artID)
row := ss.db.QueryRow(q, artID)
/* TODO: split scanning/augmenting out into function, to share with Fetch() */
var art store.Article
var p = &art.Publication
var published, updated sql.NullTime
var extra []byte
if err := row.Scan(&art.ID, &art.Headline, &art.CanonicalURL, &art.Content, &published, &updated, &art.Section, &extra, &p.Code, &p.Name, &p.Domain); err != nil {
return nil, err
}
if published.Valid {
art.Published = published.Time.Format(time.RFC3339)
}
if updated.Valid {
art.Updated = updated.Time.Format(time.RFC3339)
}
urls, err := ss.fetchURLs(art.ID)
if err != nil {
return nil, err
}
art.URLs = urls
keywords, err := ss.fetchKeywords(art.ID)
if err != nil {
return nil, err
}
art.Keywords = keywords
authors, err := ss.fetchAuthors(art.ID)
if err != nil {
return nil, err
}
art.Authors = authors
// decode extra data
if len(extra) > 0 {
err = json.Unmarshal(extra, &art.Extra)
if err != nil {
err = fmt.Errorf("error in 'Extra' (artid %d): %s", art.ID, err)
return nil, err
}
}
/* end scanning/augmenting */
return &art, nil
}