From fd2ee88e5dc68cbe3b556442a087f17fb412a230 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C4=85tkowski?= Date: Fri, 5 Aug 2022 01:16:48 +0200 Subject: [PATCH] QueryTorrents for postgreSQL - fix loading more torrents --- pkg/persistence/postgres.go | 41 ++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/pkg/persistence/postgres.go b/pkg/persistence/postgres.go index 656d168..6a12dbb 100644 --- a/pkg/persistence/postgres.go +++ b/pkg/persistence/postgres.go @@ -4,6 +4,7 @@ import ( "database/sql" "fmt" "net/url" + "text/template" "time" "unicode/utf8" @@ -205,20 +206,50 @@ func (db *postgresDatabase) QueryTorrents( return nil, fmt.Errorf("lastOrderedValue and lastID should be supplied together, if supplied") } - //doJoin := query != "" - //firstPage := lastID == nil + firstPage := lastID == nil // executeTemplate is used to prepare the SQL query, WITH PLACEHOLDERS FOR USER INPUT. - rows, err := db.conn.Query(` + sqlQuery := executeTemplate(` SELECT id , info_hash , name , total_size , discovered_on , (SELECT COUNT(*) FROM files WHERE torrents.id = files.torrent_id) AS n_files - FROM torrents - LIMIT $1;`, limit) + FROM torrents {{ if not .FirstPage }} + WHERE {{.OrderOn}} {{GTEorLTE .Ascending}} {{.LastOrderedValue}} {{ end }} + ORDER BY {{.OrderOn}} {{AscOrDesc .Ascending}} + LIMIT {{.Limit}}; + `, struct { + FirstPage bool + OrderOn string + Ascending bool + Limit uint + LastOrderedValue *float64 + }{ + FirstPage: firstPage, + OrderOn: orderOn(orderBy), + Ascending: ascending, + Limit: limit, + LastOrderedValue: lastOrderedValue, + }, template.FuncMap{ + "GTEorLTE": func(ascending bool) string { + if ascending { + return ">" + } else { + return "<" + } + }, + "AscOrDesc": func(ascending bool) string { + if ascending { + return "ASC" + } else { + return "DESC" + } + }, + }) + rows, err := db.conn.Query(sqlQuery) defer db.closeRows(rows) if err != nil { zap.L().Error(fmt.Sprint(err))