Changed DeleteScannedAssets to work correct and added db tag

This commit is contained in:
David Allen 2024-09-18 14:46:52 -06:00
parent 20f7c9f043
commit 21a10533ef
Signed by: towk
GPG key ID: 793B2924A49B3A3F
2 changed files with 11 additions and 11 deletions

View file

@ -59,8 +59,8 @@ func InsertScannedAssets(path string, assets ...magellan.RemoteAsset) error {
return nil return nil
} }
func DeleteScannedAssets(path string, results ...magellan.RemoteAsset) error { func DeleteScannedAssets(path string, assets ...magellan.RemoteAsset) error {
if results == nil { if assets == nil {
return fmt.Errorf("no assets found") return fmt.Errorf("no assets found")
} }
db, err := sqlx.Open("sqlite3", path) db, err := sqlx.Open("sqlite3", path)
@ -68,11 +68,11 @@ func DeleteScannedAssets(path string, results ...magellan.RemoteAsset) error {
return fmt.Errorf("failed to open database: %v", err) return fmt.Errorf("failed to open database: %v", err)
} }
tx := db.MustBegin() tx := db.MustBegin()
for _, state := range results { for _, asset := range assets {
sql := fmt.Sprintf(`DELETE FROM %s WHERE host = :host, port = :port;`, TABLE_NAME) sql := fmt.Sprintf(`DELETE FROM %s WHERE host=:host AND port=:port;`, TABLE_NAME)
_, err := tx.NamedExec(sql, &state) _, err := tx.NamedExec(sql, &asset)
if err != nil { if err != nil {
fmt.Printf("failed to execute transaction: %v\n", err) fmt.Printf("failed to execute DELETE transaction: %v\n", err)
} }
} }

View file

@ -16,11 +16,11 @@ import (
) )
type RemoteAsset struct { type RemoteAsset struct {
Host string `json:"host"` Host string `db:"host" json:"host"`
Port int `json:"port"` Port int `db:"port" json:"port"`
Protocol string `json:"protocol"` Protocol string `db:"protocol" json:"protocol"`
State bool `json:"state"` State bool `db:"state" json:"state"`
Timestamp time.Time `json:"timestamp"` Timestamp time.Time `db:"timestamp" json:"timestamp"`
} }
// ScanParams is a collection of commom parameters passed to the CLI // ScanParams is a collection of commom parameters passed to the CLI