Changed DeleteScannedAssets to work correct and added db tag

This commit is contained in:
David Allen 2024-09-18 14:46:52 -06:00 committed by David Allen
parent 75f1db7145
commit 0f32d7ec0d
Signed by: towk
GPG key ID: 0430CDBE22619155

View file

@ -2,7 +2,6 @@ package sqlite
import ( import (
"fmt" "fmt"
"strings"
"github.com/davidallendj/magellan/internal/util" "github.com/davidallendj/magellan/internal/util"
magellan "github.com/davidallendj/magellan/pkg" magellan "github.com/davidallendj/magellan/pkg"
@ -78,22 +77,7 @@ func DeleteScannedAssets(path string, assets ...magellan.RemoteAsset) error {
} }
tx = db.MustBegin() tx = db.MustBegin()
for _, asset := range assets { for _, asset := range assets {
// skip if neither host nor port are specified sql := fmt.Sprintf(`DELETE FROM %s WHERE host=:host AND port=:port;`, TABLE_NAME)
if asset.Host == "" && asset.Port <= 0 {
continue
}
sql := fmt.Sprintf(`DELETE FROM %s`, TABLE_NAME)
where := []string{}
if asset.Port > 0 {
where = append(where, "port=:port")
}
if asset.Host != "" {
where = append(where, "host=:host")
}
if len(where) <= 0 {
continue
}
sql += fmt.Sprintf(" WHERE %s;", strings.Join(where, " AND "))
_, err := tx.NamedExec(sql, &asset) _, err := tx.NamedExec(sql, &asset)
if err != nil { if err != nil {
fmt.Printf("failed to execute DELETE transaction: %v\n", err) fmt.Printf("failed to execute DELETE transaction: %v\n", err)