2022-07-13 06:30:54 +00:00
|
|
|
package sqlite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/doug-martin/goqu/v9"
|
|
|
|
"github.com/doug-martin/goqu/v9/exp"
|
2023-09-01 00:39:29 +00:00
|
|
|
"github.com/stashapp/stash/pkg/models"
|
2022-07-13 06:30:54 +00:00
|
|
|
"gopkg.in/guregu/null.v4"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
fingerprintTable = "files_fingerprints"
|
|
|
|
)
|
|
|
|
|
|
|
|
type fingerprintQueryRow struct {
|
|
|
|
Type null.String `db:"fingerprint_type"`
|
|
|
|
Fingerprint interface{} `db:"fingerprint"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r fingerprintQueryRow) valid() bool {
|
|
|
|
return r.Type.Valid
|
|
|
|
}
|
|
|
|
|
2023-09-01 00:39:29 +00:00
|
|
|
func (r *fingerprintQueryRow) resolve() models.Fingerprint {
|
|
|
|
return models.Fingerprint{
|
2022-07-13 06:30:54 +00:00
|
|
|
Type: r.Type.String,
|
|
|
|
Fingerprint: r.Fingerprint,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type fingerprintQueryBuilder struct {
|
|
|
|
repository
|
|
|
|
|
|
|
|
tableMgr *table
|
|
|
|
}
|
|
|
|
|
|
|
|
var FingerprintReaderWriter = &fingerprintQueryBuilder{
|
|
|
|
repository: repository{
|
|
|
|
tableName: fingerprintTable,
|
|
|
|
idColumn: fileIDColumn,
|
|
|
|
},
|
|
|
|
|
|
|
|
tableMgr: fingerprintTableMgr,
|
|
|
|
}
|
|
|
|
|
2023-09-01 00:39:29 +00:00
|
|
|
func (qb *fingerprintQueryBuilder) insert(ctx context.Context, fileID models.FileID, f models.Fingerprint) error {
|
2022-07-13 06:30:54 +00:00
|
|
|
table := qb.table()
|
|
|
|
q := dialect.Insert(table).Cols(fileIDColumn, "type", "fingerprint").Vals(
|
|
|
|
goqu.Vals{fileID, f.Type, f.Fingerprint},
|
|
|
|
)
|
|
|
|
_, err := exec(ctx, q)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("inserting into %s: %w", table.GetTable(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-01 00:39:29 +00:00
|
|
|
func (qb *fingerprintQueryBuilder) insertJoins(ctx context.Context, fileID models.FileID, f []models.Fingerprint) error {
|
2022-07-13 06:30:54 +00:00
|
|
|
for _, ff := range f {
|
|
|
|
if err := qb.insert(ctx, fileID, ff); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-22 03:07:10 +00:00
|
|
|
func (qb *fingerprintQueryBuilder) upsertJoins(ctx context.Context, fileID models.FileID, f []models.Fingerprint) error {
|
|
|
|
types := make([]string, len(f))
|
|
|
|
for i, ff := range f {
|
|
|
|
types[i] = ff.Type
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := qb.destroyJoins(ctx, fileID, types); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ff := range f {
|
|
|
|
if err := qb.insert(ctx, fileID, ff); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-01 00:39:29 +00:00
|
|
|
func (qb *fingerprintQueryBuilder) replaceJoins(ctx context.Context, fileID models.FileID, f []models.Fingerprint) error {
|
2022-07-13 06:30:54 +00:00
|
|
|
if err := qb.destroy(ctx, []int{int(fileID)}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return qb.insertJoins(ctx, fileID, f)
|
|
|
|
}
|
|
|
|
|
2023-12-22 03:07:10 +00:00
|
|
|
func (qb *fingerprintQueryBuilder) destroyJoins(ctx context.Context, fileID models.FileID, types []string) error {
|
|
|
|
table := qb.table()
|
|
|
|
q := dialect.Delete(table).Where(
|
|
|
|
table.Col(fileIDColumn).Eq(fileID),
|
|
|
|
table.Col("type").In(types),
|
|
|
|
)
|
|
|
|
|
|
|
|
_, err := exec(ctx, q)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("deleting from %s: %w", table.GetTable(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
func (qb *fingerprintQueryBuilder) table() exp.IdentifierExpression {
|
|
|
|
return qb.tableMgr.table
|
|
|
|
}
|