forked from predbdotovh/pre-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre.go
122 lines (98 loc) · 2.76 KB
/
pre.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
package main
import (
"database/sql"
"log"
"strings"
)
type sphinxRow struct {
ID int `json:"id"`
Name string `json:"name"`
Team string `json:"team"`
Cat string `json:"cat"`
Genre string `json:"genre"`
URL string `json:"url"`
Size float64 `json:"size"`
Files int `json:"files"`
PreAt int `json:"preAt"`
Nuke *nuke `json:"nuke"`
}
var replacer = strings.NewReplacer("(", "\\(", ")", "\\)")
const preColumns = "id, name, team, cat, genre, url, size, files, pre_at"
const defaultMaxMatches = 1000
func (p *sphinxRow) proc() {
p.Size /= 1000
}
func scanPresRows(rows *sql.Rows, appendNukes bool) []sphinxRow {
res := make([]sphinxRow, 0)
for rows.Next() {
var r sphinxRow
err := rows.Scan(&r.ID, &r.Name, &r.Team, &r.Cat, &r.Genre, &r.URL, &r.Size, &r.Files, &r.PreAt)
if err != nil {
log.Print(err)
continue
}
r.proc()
if appendNukes {
r.fetchNuke(mysql)
}
res = append(res, r)
}
return res
}
func getPre(db *sql.DB, preID int, withNuke bool) (*sphinxRow, error) {
sqlQuery := "SELECT " + preColumns + " FROM " + sphinxTable +
" WHERE id = ? OPTION reverse_scan = 1"
var r sphinxRow
err := db.QueryRow(sqlQuery, preID).Scan(&r.ID, &r.Name, &r.Team, &r.Cat, &r.Genre, &r.URL, &r.Size, &r.Files, &r.PreAt)
if err != nil {
return nil, err
}
r.proc()
if withNuke {
r.fetchNuke(mysql)
}
return &r, nil
}
func searchPres(tx *sql.Tx, q string, offsetInt, countInt int, withNukes bool) ([]sphinxRow, error) {
sqlQuery := "SELECT " + preColumns + " FROM " + sphinxTable +
" WHERE MATCH(?) ORDER BY id DESC" +
" LIMIT " + string(offsetInt) + "," + string(countInt) +
" OPTION reverse_scan = 1"
if offsetInt+countInt > defaultMaxMatches {
sqlQuery += ", max_matches = " + string(offsetInt+countInt)
}
sqlRows, err := tx.Query(sqlQuery, replacer.Replace(q))
if err != nil {
return nil, err
}
defer sqlRows.Close()
return scanPresRows(sqlRows, withNukes), nil
}
func latestPres(tx *sql.Tx, offsetInt, countInt int, withNukes bool) ([]sphinxRow, error) {
sqlQuery := "SELECT " + preColumns + " FROM " + sphinxTable +
" ORDER BY id DESC" +
" LIMIT " + string(offsetInt) + "," + string(countInt) +
" OPTION reverse_scan = 1"
if offsetInt+countInt > defaultMaxMatches {
sqlQuery += ", max_matches = " + string(offsetInt+countInt)
}
sqlRows, err := tx.Query(sqlQuery)
if err != nil {
return nil, err
}
defer sqlRows.Close()
return scanPresRows(sqlRows, withNukes), nil
}
func (p *sphinxRow) fetchNuke(db *sql.DB) {
n, err := getNuke(db, p.ID)
if err != nil {
return
}
p.setNuke(n)
}
func (p *sphinxRow) setNuke(n *nuke) {
if n != nil {
n.setType()
p.Nuke = n
}
}