summaryrefslogtreecommitdiffstats
path: root/database/postgres/where.go
blob: 8f52d9e8ec864d31d315bc6f375b58b31ae1b817 (plain)
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
package postgres

import (
	"database/sql"
	"fmt"
	"strings"

	"github.com/NyaaPantsu/nyaa/common"
	"github.com/NyaaPantsu/nyaa/model"
)

// build sql query from SearchParam for torrent search
func searchParamToTorrentQuery(param *common.TorrentParam) (q sqlQuery) {
	counter := 1
	q.query = fmt.Sprintf("SELECT %s FROM %s ", torrentSelectColumnsFull, tableTorrents)
	if param.Category.IsSet() {
		q.query += fmt.Sprintf("WHERE category = $%d AND sub_category = $%d ", counter, counter+1)
		q.params = append(q.params, param.Category.Main, param.Category.Sub)
		counter += 2
	}
	if counter > 1 {
		q.query += "AND "
	} else {
		q.query += "WHERE "
	}

	q.query += fmt.Sprintf("status >= $%d ", counter)
	q.params = append(q.params, param.Status)
	counter++
	if param.UserID > 0 {
		q.query += fmt.Sprintf("AND uploader = $%d ", counter)
		q.params = append(q.params, param.UserID)
		counter++
	}

	notnulls := strings.Split(param.NotNull, ",")
	for idx := range notnulls {
		k := strings.ToLower(strings.TrimSpace(notnulls[idx]))
		switch k {
		case "date":
		case "downloads":
		case "filesize":
		case "website_link":
		case "deleted_at":
		case "seeders":
		case "leechers":
		case "completed":
		case "last_scrape":
			q.query += fmt.Sprintf("AND %s IS NOT NULL ", k)
			break
		default:
			break
		}
	}

	nulls := strings.Split(param.Null, ",")
	for idx := range nulls {
		k := strings.ToLower(strings.TrimSpace(nulls[idx]))
		switch k {
		case "date":
		case "downloads":
		case "filesize":
		case "website_link":
		case "deleted_at":
		case "seeders":
		case "leechers":
		case "completed":
		case "last_scrape":
			q.query += fmt.Sprintf("AND %s IS NULL ", k)
			break
		default:
			break
		}
	}

	nameLikes := strings.Split(param.NameLike, ",")
	for idx := range nameLikes {
		q.query += fmt.Sprintf("AND torrent_name ILIKE $%d", counter)
		q.params = append(q.params, strings.TrimSpace(nameLikes[idx]))
		counter++
	}

	var sort string
	switch param.Sort {
	case common.Name:
		sort = "torrent_name"
		break
	case common.Date:
		sort = "date"
		break
	case common.Downloads:
		sort = "downloads"
		break
	case common.Size:
		sort = "filesize"
		break
	case common.Seeders:
		sort = "seeders"
		break
	case common.Leechers:
		sort = "leechers"
		break
	case common.Completed:
		sort = "completed"
		break
	case common.ID:
	default:
		sort = "torrent_id"
	}

	q.query += fmt.Sprintf("ORDER BY %s ", sort)
	if param.Order {
		q.query += "ASC "
	} else {
		q.query += "DESC "
	}

	if param.Max > 0 {
		q.query += fmt.Sprintf("LIMIT $%d ", counter)
		q.params = append(q.params, param.Max)
		counter++
	}
	if param.Offset > 0 {
		q.query += fmt.Sprintf("OFFSET $%d ", counter)
		q.params = append(q.params, param.Offset)
		counter++
	}
	return
}

func (db *Database) GetTorrentsWhere(param *common.TorrentParam) (torrents []model.Torrent, err error) {
	if param.TorrentID > 0 {
		var torrent model.Torrent
		var has bool
		torrent, has, err = db.GetTorrentByID(param.TorrentID)
		if has {
			torrents = append(torrents, torrent)
		}
		return
	}

	if param.All {
		torrents, err = db.GetAllTorrents(param.Offset, param.Max)
		return
	}

	q := searchParamToTorrentQuery(param)
	err = q.Query(db.conn, func(rows *sql.Rows) error {
		if param.Max == 0 {
			torrents = make([]model.Torrent, 0, 128)
		} else {
			torrents = make([]model.Torrent, 0, param.Max)
		}

		for rows.Next() {
			var t model.Torrent
			scanTorrentColumnsFull(rows, &t)
			torrents = append(torrents, t)
			// grow as needed
			if len(torrents) >= cap(torrents) {
				newtorrents := make([]model.Torrent, cap(torrents), cap(torrents)*3/2) // XXX: adjust as needed
				copy(newtorrents, torrents)
				torrents = newtorrents
			}
		}
		return nil
	})
	return
}