lure/internal/repos/find.go

83 lines
2.0 KiB
Go
Raw Normal View History

/*
* LURE - Linux User REpository
* Copyright (C) 2023 Arsen Musayelyan
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2022-12-01 02:26:25 +00:00
package repos
import (
2022-12-24 20:56:02 +00:00
"github.com/jmoiron/sqlx"
2022-12-01 02:26:25 +00:00
"go.arsenm.dev/lure/internal/db"
)
2022-12-01 06:15:34 +00:00
// FindPkgs looks for packages matching the inputs inside the database.
// It returns a map that maps the package name input to the packages found for it.
// It also returns a slice that contains the names of all packages that were not found.
2022-12-24 20:56:02 +00:00
func FindPkgs(gdb *sqlx.DB, pkgs []string) (map[string][]db.Package, []string, error) {
2022-12-01 02:26:25 +00:00
found := map[string][]db.Package{}
notFound := []string(nil)
2022-12-01 02:26:25 +00:00
for _, pkgName := range pkgs {
2022-12-29 20:01:54 +00:00
if pkgName == "" {
continue
}
2022-12-01 02:26:25 +00:00
result, err := db.GetPkgs(gdb, "name LIKE ?", pkgName)
if err != nil {
return nil, nil, err
2022-12-01 02:26:25 +00:00
}
added := 0
2022-12-24 20:56:02 +00:00
for result.Next() {
2022-12-01 02:26:25 +00:00
var pkg db.Package
2022-12-24 20:56:02 +00:00
err = result.StructScan(&pkg)
2022-12-01 02:26:25 +00:00
if err != nil {
2022-12-24 20:56:02 +00:00
return nil, nil, err
2022-12-01 02:26:25 +00:00
}
added++
found[pkgName] = append(found[pkgName], pkg)
}
2022-12-24 20:56:02 +00:00
result.Close()
2022-12-01 02:26:25 +00:00
if added == 0 {
result, err := db.GetPkgs(gdb, "json_array_contains(provides, ?)", pkgName)
2022-12-01 02:26:25 +00:00
if err != nil {
return nil, nil, err
2022-12-01 02:26:25 +00:00
}
2022-12-24 20:56:02 +00:00
for result.Next() {
2022-12-01 02:26:25 +00:00
var pkg db.Package
2022-12-24 20:56:02 +00:00
err = result.StructScan(&pkg)
2022-12-01 02:26:25 +00:00
if err != nil {
2022-12-24 20:56:02 +00:00
return nil, nil, err
2022-12-01 02:26:25 +00:00
}
added++
found[pkgName] = append(found[pkgName], pkg)
}
2022-12-24 20:56:02 +00:00
result.Close()
2022-12-01 02:26:25 +00:00
}
if added == 0 {
notFound = append(notFound, pkgName)
}
2022-12-01 02:26:25 +00:00
}
return found, notFound, nil
2022-12-01 02:26:25 +00:00
}