From 345901583c008aaea089ad15bcb122fc5cb215d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C4=85tkowski?= Date: Thu, 4 Aug 2022 17:40:59 +0200 Subject: [PATCH] QueryTorrents for postgreSQL - initail implementation --- pkg/persistence/postgres.go | 51 +++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/pkg/persistence/postgres.go b/pkg/persistence/postgres.go index 7a98278..656d168 100644 --- a/pkg/persistence/postgres.go +++ b/pkg/persistence/postgres.go @@ -197,7 +197,53 @@ func (db *postgresDatabase) QueryTorrents( lastOrderedValue *float64, lastID *uint64, ) ([]TorrentMetadata, error) { - return nil, NotImplementedError + + if query == "" && orderBy == ByRelevance { + return nil, fmt.Errorf("torrents cannot be ordered by relevance when the query is empty") + } + if (lastOrderedValue == nil) != (lastID == nil) { + return nil, fmt.Errorf("lastOrderedValue and lastID should be supplied together, if supplied") + } + + //doJoin := query != "" + //firstPage := lastID == nil + + // executeTemplate is used to prepare the SQL query, WITH PLACEHOLDERS FOR USER INPUT. + rows, err := db.conn.Query(` + 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) + + defer db.closeRows(rows) + if err != nil { + zap.L().Error(fmt.Sprint(err)) + return nil, errors.Wrap(err, "query error") + } + + torrents := make([]TorrentMetadata, 0) + for rows.Next() { + var torrent TorrentMetadata + err = rows.Scan( + &torrent.ID, + &torrent.InfoHash, + &torrent.Name, + &torrent.Size, + &torrent.DiscoveredOn, + &torrent.NFiles, + //&torrent.Relevance, + ) + if err != nil { + return nil, err + } + torrents = append(torrents, torrent) + } + + return torrents, nil } func (db *postgresDatabase) GetTorrent(infoHash []byte) (*TorrentMetadata, error) { @@ -234,7 +280,8 @@ func (db *postgresDatabase) GetFiles(infoHash []byte) ([]File, error) { SELECT f.size, f.path - FROM files f, torrents t WHERE f.torrent_id = t.id AND t.info_hash = $1;`, + FROM files f, torrents t + WHERE f.torrent_id = t.id AND t.info_hash = $1;`, infoHash, ) defer db.closeRows(rows)