-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwpub-sql.go
425 lines (375 loc) · 10.2 KB
/
jwpub-sql.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
package main
import (
"database/sql"
"fmt"
"time"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
)
const jwpubDateFormat = "20060102"
func getMEPSDocuments(db *sql.DB, mdocid string) (mepsDocuments []mepsDocument, err error) {
// get all docIDs
sqlQuery := fmt.Sprintf(`SELECT Multimedia.MimeType,
Multimedia.FilePath,
Multimedia.Track,
Multimedia.KeySymbol,
Multimedia.MepsDocumentId,
Multimedia.IssueTagNumber
FROM Document
INNER JOIN DocumentMultimedia
ON Document.DocumentId = DocumentMultimedia.DocumentId
INNER JOIN Multimedia
ON DocumentMultimedia.MultimediaId = Multimedia.MultimediaId
WHERE Document.MepsDocumentId = %s`, mdocid)
rows, err := db.Query(sqlQuery)
if err != nil {
return nil, fmt.Errorf("unable to get allDocs: %v", err)
}
defer rows.Close()
for rows.Next() {
var mepsDoc mepsDocument
err = rows.Scan(
&mepsDoc.MimeType,
&mepsDoc.Name,
&mepsDoc.Track,
&mepsDoc.KeySymbol,
&mepsDoc.MepsDocumentID,
&mepsDoc.IssueTagNumber,
)
if err != nil {
return nil, fmt.Errorf("unable to scan allDocs row: %v", err)
}
mepsDocuments = append(mepsDocuments, mepsDoc)
}
err = rows.Err()
if err != nil {
return nil, fmt.Errorf("row error after allDocs query: %v", err)
}
return
}
func getMWBDocuments(db *sql.DB) (mwbDocuments []Document, err error) {
// get all docIDs
allDocs := make([]Document, 0)
sqlQuery := `SELECT DocumentId AS LastDocId
FROM Document
ORDER BY DocumentId ASC`
rows, err := db.Query(sqlQuery)
if err != nil {
return nil, fmt.Errorf("unable to get allDocs: %v", err)
}
defer rows.Close()
for rows.Next() {
var mepsDoc Document
err = rows.Scan(
&mepsDoc.ID,
)
if err != nil {
return nil, fmt.Errorf("unable to scan allDocs row: %v", err)
}
allDocs = append(allDocs, mepsDoc)
}
err = rows.Err()
if err != nil {
return nil, fmt.Errorf("row error after allDocs query: %v", err)
}
// Get primary docIDs w/ dates
primaryDocs := make([]Document, 0)
sqlQuery = `SELECT DocumentId, CAST(FirstDateOffset AS TEXT)
FROM DatedText`
rows, err = db.Query(sqlQuery)
if err != nil {
return nil, fmt.Errorf("unable to get documents: %v", err)
}
defer rows.Close()
for rows.Next() {
var mepsDoc Document
var date string
err = rows.Scan(
&mepsDoc.ID,
&date,
)
if err != nil {
return nil, fmt.Errorf("unable to scan document row: %v", err)
}
mepsDoc.Date, err = time.Parse(jwpubDateFormat, date)
if err != nil {
return
}
primaryDocs = append(primaryDocs, mepsDoc)
}
err = rows.Err()
if err != nil {
return nil, fmt.Errorf("row error after document query: %v", err)
}
// glue it all together
var docDate time.Time
for _, doc := range allDocs {
for _, pdoc := range primaryDocs {
if pdoc.ID == doc.ID {
docDate = pdoc.Date
}
}
doc.Date = docDate
mwbDocuments = append(mwbDocuments, doc)
}
return
}
func getWTDocuments(db *sql.DB) (wtDocumentIDs []int, err error) {
wtDocumentIDs = make([]int, 0)
sqlQuery := `SELECT DocumentId
FROM Document
WHERE Class=40
ORDER BY DocumentId ASC`
rows, err := db.Query(sqlQuery)
if err != nil {
return nil, fmt.Errorf("unable to get documents: %v", err)
}
defer rows.Close()
for rows.Next() {
var docID int
err = rows.Scan(
&docID,
)
if err != nil {
return nil, fmt.Errorf("unable to scan document row: %v", err)
}
wtDocumentIDs = append(wtDocumentIDs, docID)
}
err = rows.Err()
if err != nil {
return nil, fmt.Errorf("row error after document query: %v", err)
}
return
}
func getWTDates(db *sql.DB) (wtDates []time.Time, err error) {
wtDates = make([]time.Time, 0)
sqlQuery := `SELECT CAST(FirstDateOffset AS TEXT) AS Date
FROM DatedText
ORDER BY DatedTextId ASC`
rows, err := db.Query(sqlQuery)
if err != nil {
log.Fatal("unable to get dates: ", err)
}
defer rows.Close()
for rows.Next() {
var date string
err = rows.Scan(
&date,
)
if err != nil {
log.Fatal("unable to scan date row: ", err)
}
wdate, err := time.Parse(jwpubDateFormat, date)
if err != nil {
return nil, err
}
wtDates = append(wtDates, wdate)
}
err = rows.Err()
if err != nil {
log.Fatal("row error after date query: ", err)
}
return
}
func getMWBSongs(db *sql.DB, docIDs []Document) (songs []string) {
var whereDID string
for i, did := range docIDs {
if i != 0 {
whereDID += " OR "
}
whereDID += fmt.Sprintf("DocumentId=%v", did.ID)
}
sqlQuery := fmt.Sprintf(`SELECT Track
FROM DocumentMultimedia
INNER JOIN Multimedia
ON DocumentMultimedia.MultimediaId = Multimedia.MultimediaId
WHERE (%s)
AND BeginParagraphOrdinal IS NOT NULL
AND KeySymbol='sjjm'
ORDER BY DocumentMultimediaId ASC;`, whereDID)
rows, err := db.Query(sqlQuery)
if err != nil {
log.Fatal("unable to get songs: ", err)
}
defer rows.Close()
for rows.Next() {
var mm Multimedia
err = rows.Scan(
&mm.Track,
)
if err != nil {
log.Fatal("unable to scan song row: ", err)
}
songs = append(songs, mm.Track)
}
err = rows.Err()
if err != nil {
log.Fatal("row error after song query: ", err)
}
logrus.Debug("getMWBSongs()", songs)
return
}
func getMWBVideos(db *sql.DB, docIDs []Document) (videos []video) {
var whereDID string
for i, did := range docIDs {
if i != 0 {
whereDID += " OR "
}
whereDID += fmt.Sprintf("DocumentId=%v", did.ID)
}
sqlQuery := fmt.Sprintf(`SELECT Track, KeySymbol, MepsDocumentId, IssueTagNumber
FROM DocumentMultimedia
INNER JOIN Multimedia
ON DocumentMultimedia.MultimediaId = Multimedia.MultimediaId
WHERE (%s)
AND Multimedia.MimeType="video/mp4"
AND ( MepsDocumentId IS NOT NULL OR IssueTagNumber != 0)
ORDER BY DocumentMultimediaId ASC;`, whereDID)
rows, err := db.Query(sqlQuery)
if err != nil {
log.Fatal("unable to get videos: ", err)
}
defer rows.Close()
for rows.Next() {
var v video
err = rows.Scan(
&v.Track,
&v.KeySymbol,
&v.MepsDocumentID,
&v.IssueTagNumber,
)
if err != nil {
log.Fatal("unable to scan video row: ", err)
}
videos = append(videos, v)
}
err = rows.Err()
if err != nil {
log.Fatal("row error after song query: ", err)
}
logrus.Debug("getMWBSongs()", videos)
return
}
func getWTSongs(db *sql.DB, date time.Time) (songs []string) {
d := date.Format(jwpubDateFormat)
sqlQuery := fmt.Sprintf(`SELECT Multimedia.Track
FROM DatedText
INNER JOIN Multimedia
ON DatedText.BeginParagraphOrdinal = Multimedia.MultimediaId
WHERE FirstDateOffset=%s
AND KeySymbol='sjjm'
UNION ALL
SELECT Multimedia.Track
FROM DatedText
INNER JOIN Multimedia
ON DatedText.EndParagraphOrdinal = Multimedia.MultimediaId
WHERE FirstDateOffset=%s
AND KeySymbol='sjjm';
`, d, d)
rows, err := db.Query(sqlQuery)
if err != nil {
log.Fatal("unable to get wtsongs: ", err)
}
defer rows.Close()
for rows.Next() {
var mm Multimedia
err = rows.Scan(
&mm.Track,
)
if err != nil {
log.Fatal("unable to scan wtsong row: ", err)
}
songs = append(songs, mm.Track)
}
err = rows.Err()
if err != nil {
log.Fatal("row error after wtsong query: ", err)
}
logrus.Debug("getWTSongs()", songs)
return
}
func getImageNames(db *sql.DB, docIDs []Document) (files []string) {
var whereDID string
for i, did := range docIDs {
if i != 0 {
whereDID += " OR "
}
whereDID += fmt.Sprintf("DocumentId=%v", did.ID)
}
sqlQuery := fmt.Sprintf(`SELECT FilePath
FROM DocumentMultimedia
INNER JOIN Multimedia
ON DocumentMultimedia.MultimediaId = Multimedia.MultimediaId
WHERE (%s)
AND BeginParagraphOrdinal IS NOT NULL
AND FilePath!=''
ORDER BY DocumentMultimediaId ASC;`, whereDID)
rows, err := db.Query(sqlQuery)
if err != nil {
log.Fatal("unable to get documents: ", err)
}
defer rows.Close()
for rows.Next() {
var mm Multimedia
err = rows.Scan(
&mm.FilePath,
)
if err != nil {
log.Fatal("unable to scan document row: ", err)
}
files = append(files, mm.FilePath)
}
err = rows.Err()
if err != nil {
log.Fatal("row error after document query: ", err)
}
logrus.Debug("getImageNames()", files)
return
}
func (c *Config) getLinkedDocs(db *sql.DB, docIDs []Document) (docs []LinkedDocument) {
var whereDID string
for i, did := range docIDs {
if i != 0 {
whereDID += " OR "
}
whereDID += fmt.Sprintf("DocumentExtract.DocumentId=%v", did.ID)
}
var refSymbol string
for i, pubSymbol := range c.PubSymbols {
if i != 0 {
refSymbol += " OR "
}
refSymbol += fmt.Sprintf("RefPublication.UndatedSymbol == '%s'", pubSymbol)
}
sqlQuery := fmt.Sprintf(`SELECT RefPublication.UndatedSymbol, Extract.RefMepsDocumentId
FROM DocumentExtract
INNER JOIN Extract
ON DocumentExtract.ExtractId = Extract.ExtractId
INNER JOIN RefPublication
ON Extract.RefPublicationId = RefPublication.RefPublicationId
WHERE (%s)
AND (%s);`,
whereDID, refSymbol)
rows, err := db.Query(sqlQuery)
if err != nil {
log.Fatal("unable to get lined documents: ", err)
}
defer rows.Close()
for rows.Next() {
var ld LinkedDocument
err = rows.Scan(
&ld.PublicationSymbol,
&ld.MepsDocumentID,
)
if err != nil {
log.Fatal("unable to scan lined document row: ", err)
}
docs = append(docs, ld)
}
err = rows.Err()
if err != nil {
log.Fatal("row error after lined document query: ", err)
}
logrus.Debug("getLinkedDocs()", docs)
return
}