2021-01-18 01:23:20 +00:00
|
|
|
package sqlite
|
|
|
|
|
|
|
|
import (
|
2022-05-19 07:49:32 +00:00
|
|
|
"context"
|
2021-01-18 01:23:20 +00:00
|
|
|
"database/sql"
|
Errorlint sweep + minor linter tweaks (#1796)
* Replace error assertions with Go 1.13 style
Use `errors.As(..)` over type assertions. This enables better use of
wrapped errors in the future, and lets us pass some errorlint checks
in the process.
The rewrite is entirely mechanical, and uses a standard idiom for
doing so.
* Use Go 1.13's errors.Is(..)
Rather than directly checking for error equality, use errors.Is(..).
This protects against error wrapping issues in the future.
Even though something like sql.ErrNoRows doesn't need the wrapping, do
so anyway, for the sake of consistency throughout the code base.
The change almost lets us pass the `errorlint` Go checker except for
a missing case in `js.go` which is to be handled separately; it isn't
mechanical, like these changes are.
* Remove goconst
goconst isn't a useful linter in many cases, because it's false positive
rate is high. It's 100% for the current code base.
* Avoid direct comparison of errors in recover()
Assert that we are catching an error from recover(). If we are,
check that the error caught matches errStop.
* Enable the "errorlint" checker
Configure the checker to avoid checking for errorf wraps. These are
often false positives since the suggestion is to blanket wrap errors
with %w, and that exposes the underlying API which you might not want
to do.
The other warnings are good however, and with the current patch stack,
the code base passes all these checks as well.
* Configure rowserrcheck
The project uses sqlx. Configure rowserrcheck to include said package.
* Mechanically rewrite a large set of errors
Mechanically search for errors that look like
fmt.Errorf("...%s", err.Error())
and rewrite those into
fmt.Errorf("...%v", err)
The `fmt` package is error-aware and knows how to call err.Error()
itself.
The rationale is that this is more idiomatic Go; it paves the
way for using error wrapping later with %w in some sites.
This patch only addresses the entirely mechanical rewriting caught by
a project-side search/replace. There are more individual sites not
addressed by this patch.
2021-10-12 03:03:08 +00:00
|
|
|
"errors"
|
2021-01-18 01:23:20 +00:00
|
|
|
"fmt"
|
2024-10-29 00:26:23 +00:00
|
|
|
"slices"
|
2021-01-18 01:23:20 +00:00
|
|
|
|
2022-08-12 02:21:46 +00:00
|
|
|
"github.com/doug-martin/goqu/v9"
|
2023-06-15 02:46:09 +00:00
|
|
|
"github.com/doug-martin/goqu/v9/exp"
|
2022-08-12 02:21:46 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
2023-06-15 02:46:09 +00:00
|
|
|
"gopkg.in/guregu/null.v4"
|
|
|
|
"gopkg.in/guregu/null.v4/zero"
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
"github.com/stashapp/stash/pkg/models"
|
|
|
|
)
|
|
|
|
|
2023-03-16 23:52:49 +00:00
|
|
|
const (
|
2024-07-30 04:14:16 +00:00
|
|
|
groupTable = "groups"
|
|
|
|
groupIDColumn = "group_id"
|
2023-03-16 23:52:49 +00:00
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
groupFrontImageBlobColumn = "front_image_blob"
|
|
|
|
groupBackImageBlobColumn = "back_image_blob"
|
2024-06-11 03:08:49 +00:00
|
|
|
|
2024-07-30 04:14:16 +00:00
|
|
|
groupsTagsTable = "groups_tags"
|
2024-06-18 01:24:15 +00:00
|
|
|
|
2024-07-30 04:14:16 +00:00
|
|
|
groupURLsTable = "group_urls"
|
2024-07-03 23:10:26 +00:00
|
|
|
groupURLColumn = "url"
|
2024-08-30 01:43:44 +00:00
|
|
|
|
|
|
|
groupRelationsTable = "groups_relations"
|
2023-03-16 23:52:49 +00:00
|
|
|
)
|
2021-01-18 01:23:20 +00:00
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
type groupRow struct {
|
2023-06-15 02:46:09 +00:00
|
|
|
ID int `db:"id" goqu:"skipinsert"`
|
|
|
|
Name zero.String `db:"name"`
|
|
|
|
Aliases zero.String `db:"aliases"`
|
|
|
|
Duration null.Int `db:"duration"`
|
|
|
|
Date NullDate `db:"date"`
|
|
|
|
// expressed as 1-100
|
2024-07-30 04:14:16 +00:00
|
|
|
Rating null.Int `db:"rating"`
|
|
|
|
StudioID null.Int `db:"studio_id,omitempty"`
|
|
|
|
Director zero.String `db:"director"`
|
|
|
|
Description zero.String `db:"description"`
|
|
|
|
CreatedAt Timestamp `db:"created_at"`
|
|
|
|
UpdatedAt Timestamp `db:"updated_at"`
|
2023-06-15 02:46:09 +00:00
|
|
|
|
|
|
|
// not used in resolutions or updates
|
|
|
|
FrontImageBlob zero.String `db:"front_image_blob"`
|
|
|
|
BackImageBlob zero.String `db:"back_image_blob"`
|
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
func (r *groupRow) fromGroup(o models.Group) {
|
2023-06-15 02:46:09 +00:00
|
|
|
r.ID = o.ID
|
|
|
|
r.Name = zero.StringFrom(o.Name)
|
|
|
|
r.Aliases = zero.StringFrom(o.Aliases)
|
|
|
|
r.Duration = intFromPtr(o.Duration)
|
|
|
|
r.Date = NullDateFromDatePtr(o.Date)
|
|
|
|
r.Rating = intFromPtr(o.Rating)
|
|
|
|
r.StudioID = intFromPtr(o.StudioID)
|
|
|
|
r.Director = zero.StringFrom(o.Director)
|
2024-07-30 04:14:16 +00:00
|
|
|
r.Description = zero.StringFrom(o.Synopsis)
|
2023-06-15 02:46:09 +00:00
|
|
|
r.CreatedAt = Timestamp{Timestamp: o.CreatedAt}
|
|
|
|
r.UpdatedAt = Timestamp{Timestamp: o.UpdatedAt}
|
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
func (r *groupRow) resolve() *models.Group {
|
|
|
|
ret := &models.Group{
|
2023-06-15 02:46:09 +00:00
|
|
|
ID: r.ID,
|
|
|
|
Name: r.Name.String,
|
|
|
|
Aliases: r.Aliases.String,
|
|
|
|
Duration: nullIntPtr(r.Duration),
|
|
|
|
Date: r.Date.DatePtr(),
|
|
|
|
Rating: nullIntPtr(r.Rating),
|
|
|
|
StudioID: nullIntPtr(r.StudioID),
|
|
|
|
Director: r.Director.String,
|
2024-07-30 04:14:16 +00:00
|
|
|
Synopsis: r.Description.String,
|
2023-06-15 02:46:09 +00:00
|
|
|
CreatedAt: r.CreatedAt.Timestamp,
|
|
|
|
UpdatedAt: r.UpdatedAt.Timestamp,
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
type groupRowRecord struct {
|
2023-06-15 02:46:09 +00:00
|
|
|
updateRecord
|
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
func (r *groupRowRecord) fromPartial(o models.GroupPartial) {
|
2023-06-15 02:46:09 +00:00
|
|
|
r.setNullString("name", o.Name)
|
|
|
|
r.setNullString("aliases", o.Aliases)
|
|
|
|
r.setNullInt("duration", o.Duration)
|
|
|
|
r.setNullDate("date", o.Date)
|
|
|
|
r.setNullInt("rating", o.Rating)
|
|
|
|
r.setNullInt("studio_id", o.StudioID)
|
|
|
|
r.setNullString("director", o.Director)
|
2024-07-30 04:14:16 +00:00
|
|
|
r.setNullString("description", o.Synopsis)
|
2023-06-15 02:46:09 +00:00
|
|
|
r.setTimestamp("created_at", o.CreatedAt)
|
|
|
|
r.setTimestamp("updated_at", o.UpdatedAt)
|
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
type groupRepositoryType struct {
|
2021-01-18 01:23:20 +00:00
|
|
|
repository
|
2024-06-11 01:34:38 +00:00
|
|
|
scenes repository
|
2024-06-18 01:24:15 +00:00
|
|
|
tags joinRepository
|
2024-06-11 01:34:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2024-07-03 23:10:26 +00:00
|
|
|
groupRepository = groupRepositoryType{
|
2024-06-11 01:34:38 +00:00
|
|
|
repository: repository{
|
2024-07-03 23:10:26 +00:00
|
|
|
tableName: groupTable,
|
2024-06-11 01:34:38 +00:00
|
|
|
idColumn: idColumn,
|
|
|
|
},
|
|
|
|
scenes: repository{
|
2024-07-03 23:10:26 +00:00
|
|
|
tableName: groupsScenesTable,
|
|
|
|
idColumn: groupIDColumn,
|
2024-06-11 01:34:38 +00:00
|
|
|
},
|
2024-06-18 01:24:15 +00:00
|
|
|
tags: joinRepository{
|
|
|
|
repository: repository{
|
2024-07-03 23:10:26 +00:00
|
|
|
tableName: groupsTagsTable,
|
|
|
|
idColumn: groupIDColumn,
|
2024-06-18 01:24:15 +00:00
|
|
|
},
|
|
|
|
fkColumn: tagIDColumn,
|
|
|
|
foreignTable: tagTable,
|
|
|
|
orderBy: "tags.name ASC",
|
|
|
|
},
|
2024-06-11 01:34:38 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
type GroupStore struct {
|
2023-03-16 23:52:49 +00:00
|
|
|
blobJoinQueryBuilder
|
2024-06-18 01:24:15 +00:00
|
|
|
tagRelationshipStore
|
2024-08-30 01:43:44 +00:00
|
|
|
groupRelationshipStore
|
2023-06-15 02:46:09 +00:00
|
|
|
|
|
|
|
tableMgr *table
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
func NewGroupStore(blobStore *BlobStore) *GroupStore {
|
|
|
|
return &GroupStore{
|
2023-06-15 02:46:09 +00:00
|
|
|
blobJoinQueryBuilder: blobJoinQueryBuilder{
|
2023-03-16 23:52:49 +00:00
|
|
|
blobStore: blobStore,
|
2024-07-03 23:10:26 +00:00
|
|
|
joinTable: groupTable,
|
2023-03-16 23:52:49 +00:00
|
|
|
},
|
2024-06-18 01:24:15 +00:00
|
|
|
tagRelationshipStore: tagRelationshipStore{
|
|
|
|
idRelationshipStore: idRelationshipStore{
|
2024-07-03 23:10:26 +00:00
|
|
|
joinTable: groupsTagsTableMgr,
|
2024-06-18 01:24:15 +00:00
|
|
|
},
|
|
|
|
},
|
2024-08-30 01:43:44 +00:00
|
|
|
groupRelationshipStore: groupRelationshipStore{
|
|
|
|
table: groupRelationshipTableMgr,
|
|
|
|
},
|
2023-06-15 02:46:09 +00:00
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
tableMgr: groupTableMgr,
|
2023-03-16 23:52:49 +00:00
|
|
|
}
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
func (qb *GroupStore) table() exp.IdentifierExpression {
|
2023-06-15 02:46:09 +00:00
|
|
|
return qb.tableMgr.table
|
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
func (qb *GroupStore) selectDataset() *goqu.SelectDataset {
|
2023-06-15 02:46:09 +00:00
|
|
|
return dialect.From(qb.table()).Select(qb.table().All())
|
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
func (qb *GroupStore) Create(ctx context.Context, newObject *models.Group) error {
|
|
|
|
var r groupRow
|
|
|
|
r.fromGroup(*newObject)
|
2023-06-15 02:46:09 +00:00
|
|
|
|
|
|
|
id, err := qb.tableMgr.insertID(ctx, r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-06-11 03:08:49 +00:00
|
|
|
if newObject.URLs.Loaded() {
|
|
|
|
const startPos = 0
|
2024-07-03 23:10:26 +00:00
|
|
|
if err := groupsURLsTableMgr.insertJoins(ctx, id, startPos, newObject.URLs.List()); err != nil {
|
2024-06-11 03:08:49 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-18 01:24:15 +00:00
|
|
|
if err := qb.tagRelationshipStore.createRelationships(ctx, id, newObject.TagIDs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-08-30 01:43:44 +00:00
|
|
|
if err := qb.groupRelationshipStore.createContainingRelationships(ctx, id, newObject.ContainingGroups); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := qb.groupRelationshipStore.createSubRelationships(ctx, id, newObject.SubGroups); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-06-15 02:46:09 +00:00
|
|
|
updated, err := qb.find(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("finding after create: %w", err)
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2023-06-15 02:46:09 +00:00
|
|
|
*newObject = *updated
|
|
|
|
|
|
|
|
return nil
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
func (qb *GroupStore) UpdatePartial(ctx context.Context, id int, partial models.GroupPartial) (*models.Group, error) {
|
|
|
|
r := groupRowRecord{
|
2023-06-15 02:46:09 +00:00
|
|
|
updateRecord{
|
|
|
|
Record: make(exp.Record),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
r.fromPartial(partial)
|
|
|
|
|
|
|
|
if len(r.Record) > 0 {
|
|
|
|
if err := qb.tableMgr.updateByID(ctx, id, r.Record); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2024-06-11 03:08:49 +00:00
|
|
|
if partial.URLs != nil {
|
2024-07-03 23:10:26 +00:00
|
|
|
if err := groupsURLsTableMgr.modifyJoins(ctx, id, partial.URLs.Values, partial.URLs.Mode); err != nil {
|
2024-06-11 03:08:49 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-18 01:24:15 +00:00
|
|
|
if err := qb.tagRelationshipStore.modifyRelationships(ctx, id, partial.TagIDs); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-08-30 01:43:44 +00:00
|
|
|
if err := qb.groupRelationshipStore.modifyContainingRelationships(ctx, id, partial.ContainingGroups); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := qb.groupRelationshipStore.modifySubRelationships(ctx, id, partial.SubGroups); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-06-15 02:46:09 +00:00
|
|
|
return qb.find(ctx, id)
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
func (qb *GroupStore) Update(ctx context.Context, updatedObject *models.Group) error {
|
|
|
|
var r groupRow
|
|
|
|
r.fromGroup(*updatedObject)
|
2023-06-15 02:46:09 +00:00
|
|
|
|
|
|
|
if err := qb.tableMgr.updateByID(ctx, updatedObject.ID, r); err != nil {
|
|
|
|
return err
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2024-06-11 03:08:49 +00:00
|
|
|
if updatedObject.URLs.Loaded() {
|
2024-07-03 23:10:26 +00:00
|
|
|
if err := groupsURLsTableMgr.replaceJoins(ctx, updatedObject.ID, updatedObject.URLs.List()); err != nil {
|
2024-06-11 03:08:49 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-18 01:24:15 +00:00
|
|
|
if err := qb.tagRelationshipStore.replaceRelationships(ctx, updatedObject.ID, updatedObject.TagIDs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-08-30 01:43:44 +00:00
|
|
|
if err := qb.groupRelationshipStore.replaceContainingRelationships(ctx, updatedObject.ID, updatedObject.ContainingGroups); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := qb.groupRelationshipStore.replaceSubRelationships(ctx, updatedObject.ID, updatedObject.SubGroups); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-06-15 02:46:09 +00:00
|
|
|
return nil
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
func (qb *GroupStore) Destroy(ctx context.Context, id int) error {
|
2023-03-16 23:52:49 +00:00
|
|
|
// must handle image checksums manually
|
|
|
|
if err := qb.destroyImages(ctx, id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
return groupRepository.destroyExisting(ctx, []int{id})
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2023-06-15 02:46:09 +00:00
|
|
|
// returns nil, nil if not found
|
2024-07-03 23:10:26 +00:00
|
|
|
func (qb *GroupStore) Find(ctx context.Context, id int) (*models.Group, error) {
|
2023-06-15 02:46:09 +00:00
|
|
|
ret, err := qb.find(ctx, id)
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return nil, nil
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
2023-06-15 02:46:09 +00:00
|
|
|
return ret, err
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
func (qb *GroupStore) FindMany(ctx context.Context, ids []int) ([]*models.Group, error) {
|
|
|
|
ret := make([]*models.Group, len(ids))
|
2022-08-12 02:21:46 +00:00
|
|
|
|
2023-06-15 02:46:09 +00:00
|
|
|
table := qb.table()
|
2023-03-15 22:08:21 +00:00
|
|
|
if err := batchExec(ids, defaultBatchSize, func(batch []int) error {
|
2023-06-15 02:46:09 +00:00
|
|
|
q := qb.selectDataset().Prepared(true).Where(table.Col(idColumn).In(batch))
|
2023-03-15 22:08:21 +00:00
|
|
|
unsorted, err := qb.getMany(ctx, q)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range unsorted {
|
2024-10-29 00:26:23 +00:00
|
|
|
i := slices.Index(ids, s.ID)
|
2023-03-15 22:08:21 +00:00
|
|
|
ret[i] = s
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
2022-08-12 02:21:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := range ret {
|
|
|
|
if ret[i] == nil {
|
2024-07-03 23:10:26 +00:00
|
|
|
return nil, fmt.Errorf("group with id %d not found", ids[i])
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
2022-08-12 02:21:46 +00:00
|
|
|
}
|
2021-01-18 01:23:20 +00:00
|
|
|
|
2022-08-12 02:21:46 +00:00
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2023-06-15 02:46:09 +00:00
|
|
|
// returns nil, sql.ErrNoRows if not found
|
2024-07-03 23:10:26 +00:00
|
|
|
func (qb *GroupStore) find(ctx context.Context, id int) (*models.Group, error) {
|
2023-06-15 02:46:09 +00:00
|
|
|
q := qb.selectDataset().Where(qb.tableMgr.byID(id))
|
|
|
|
|
|
|
|
ret, err := qb.get(ctx, q)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns nil, sql.ErrNoRows if not found
|
2024-07-03 23:10:26 +00:00
|
|
|
func (qb *GroupStore) get(ctx context.Context, q *goqu.SelectDataset) (*models.Group, error) {
|
2023-06-15 02:46:09 +00:00
|
|
|
ret, err := qb.getMany(ctx, q)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ret) == 0 {
|
|
|
|
return nil, sql.ErrNoRows
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret[0], nil
|
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
func (qb *GroupStore) getMany(ctx context.Context, q *goqu.SelectDataset) ([]*models.Group, error) {
|
2022-08-12 02:21:46 +00:00
|
|
|
const single = false
|
2024-07-03 23:10:26 +00:00
|
|
|
var ret []*models.Group
|
2022-08-12 02:21:46 +00:00
|
|
|
if err := queryFunc(ctx, q, single, func(r *sqlx.Rows) error {
|
2024-07-03 23:10:26 +00:00
|
|
|
var f groupRow
|
2022-08-12 02:21:46 +00:00
|
|
|
if err := r.StructScan(&f); err != nil {
|
|
|
|
return err
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2023-06-15 02:46:09 +00:00
|
|
|
s := f.resolve()
|
|
|
|
|
|
|
|
ret = append(ret, s)
|
2022-08-12 02:21:46 +00:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2022-08-12 02:21:46 +00:00
|
|
|
return ret, nil
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
func (qb *GroupStore) FindByName(ctx context.Context, name string, nocase bool) (*models.Group, error) {
|
2024-07-30 04:14:16 +00:00
|
|
|
// query := "SELECT * FROM groups WHERE name = ?"
|
2023-06-15 02:46:09 +00:00
|
|
|
// if nocase {
|
|
|
|
// query += " COLLATE NOCASE"
|
|
|
|
// }
|
|
|
|
// query += " LIMIT 1"
|
|
|
|
where := "name = ?"
|
2021-01-18 01:23:20 +00:00
|
|
|
if nocase {
|
2023-06-15 02:46:09 +00:00
|
|
|
where += " COLLATE NOCASE"
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
2023-06-15 02:46:09 +00:00
|
|
|
sq := qb.selectDataset().Prepared(true).Where(goqu.L(where, name)).Limit(1)
|
|
|
|
ret, err := qb.get(ctx, sq)
|
|
|
|
|
|
|
|
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
func (qb *GroupStore) FindByNames(ctx context.Context, names []string, nocase bool) ([]*models.Group, error) {
|
2024-07-30 04:14:16 +00:00
|
|
|
// query := "SELECT * FROM groups WHERE name"
|
2023-06-15 02:46:09 +00:00
|
|
|
// if nocase {
|
|
|
|
// query += " COLLATE NOCASE"
|
|
|
|
// }
|
|
|
|
// query += " IN " + getInBinding(len(names))
|
|
|
|
where := "name"
|
2021-01-18 01:23:20 +00:00
|
|
|
if nocase {
|
2023-06-15 02:46:09 +00:00
|
|
|
where += " COLLATE NOCASE"
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
2023-06-15 02:46:09 +00:00
|
|
|
where += " IN " + getInBinding(len(names))
|
2021-01-18 01:23:20 +00:00
|
|
|
var args []interface{}
|
|
|
|
for _, name := range names {
|
|
|
|
args = append(args, name)
|
|
|
|
}
|
2023-06-15 02:46:09 +00:00
|
|
|
sq := qb.selectDataset().Prepared(true).Where(goqu.L(where, args...))
|
|
|
|
ret, err := qb.getMany(ctx, sq)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
func (qb *GroupStore) Count(ctx context.Context) (int, error) {
|
2023-06-15 02:46:09 +00:00
|
|
|
q := dialect.Select(goqu.COUNT("*")).From(qb.table())
|
|
|
|
return count(ctx, q)
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
func (qb *GroupStore) All(ctx context.Context) ([]*models.Group, error) {
|
2023-06-15 02:46:09 +00:00
|
|
|
table := qb.table()
|
|
|
|
|
|
|
|
return qb.getMany(ctx, qb.selectDataset().Order(
|
|
|
|
table.Col("name").Asc(),
|
|
|
|
table.Col(idColumn).Asc(),
|
|
|
|
))
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
func (qb *GroupStore) makeQuery(ctx context.Context, groupFilter *models.GroupFilterType, findFilter *models.FindFilterType) (*queryBuilder, error) {
|
2021-01-18 01:23:20 +00:00
|
|
|
if findFilter == nil {
|
|
|
|
findFilter = &models.FindFilterType{}
|
|
|
|
}
|
2024-07-03 23:10:26 +00:00
|
|
|
if groupFilter == nil {
|
|
|
|
groupFilter = &models.GroupFilterType{}
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
query := groupRepository.newQuery()
|
|
|
|
distinctIDs(&query, groupTable)
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
if q := findFilter.Q; q != nil && *q != "" {
|
2024-07-30 04:14:16 +00:00
|
|
|
searchColumns := []string{"groups.name", "groups.aliases"}
|
2021-11-22 03:59:22 +00:00
|
|
|
query.parseQueryString(searchColumns, *q)
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
filter := filterBuilderFromHandler(ctx, &groupFilterHandler{
|
|
|
|
groupFilter: groupFilter,
|
2024-06-11 01:34:38 +00:00
|
|
|
})
|
2021-01-18 01:23:20 +00:00
|
|
|
|
2023-02-13 01:14:41 +00:00
|
|
|
if err := query.addFilter(filter); err != nil {
|
2023-06-16 00:46:14 +00:00
|
|
|
return nil, err
|
2023-02-13 01:14:41 +00:00
|
|
|
}
|
2021-04-09 05:05:11 +00:00
|
|
|
|
2024-08-30 01:43:44 +00:00
|
|
|
if err := qb.setGroupSort(&query, findFilter); err != nil {
|
2024-05-22 04:59:08 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
query.sortAndPagination += getPagination(findFilter)
|
2023-06-16 00:46:14 +00:00
|
|
|
|
|
|
|
return &query, nil
|
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
func (qb *GroupStore) Query(ctx context.Context, groupFilter *models.GroupFilterType, findFilter *models.FindFilterType) ([]*models.Group, int, error) {
|
|
|
|
query, err := qb.makeQuery(ctx, groupFilter, findFilter)
|
2023-06-16 00:46:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
idsResult, countResult, err := query.executeFind(ctx)
|
2021-01-18 01:23:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
groups, err := qb.FindMany(ctx, idsResult)
|
2022-08-12 02:21:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
return groups, countResult, nil
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
func (qb *GroupStore) QueryCount(ctx context.Context, groupFilter *models.GroupFilterType, findFilter *models.FindFilterType) (int, error) {
|
|
|
|
query, err := qb.makeQuery(ctx, groupFilter, findFilter)
|
2023-06-16 00:46:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return query.executeCount(ctx)
|
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
var groupSortOptions = sortOptions{
|
2024-05-22 04:59:08 +00:00
|
|
|
"created_at",
|
|
|
|
"date",
|
|
|
|
"duration",
|
|
|
|
"id",
|
|
|
|
"name",
|
|
|
|
"random",
|
|
|
|
"rating",
|
|
|
|
"scenes_count",
|
2024-08-30 01:43:44 +00:00
|
|
|
"sub_group_order",
|
2024-06-18 01:24:15 +00:00
|
|
|
"tag_count",
|
2024-05-22 04:59:08 +00:00
|
|
|
"updated_at",
|
|
|
|
}
|
|
|
|
|
2024-08-30 01:43:44 +00:00
|
|
|
func (qb *GroupStore) setGroupSort(query *queryBuilder, findFilter *models.FindFilterType) error {
|
2021-01-18 01:23:20 +00:00
|
|
|
var sort string
|
|
|
|
var direction string
|
|
|
|
if findFilter == nil {
|
|
|
|
sort = "name"
|
|
|
|
direction = "ASC"
|
|
|
|
} else {
|
|
|
|
sort = findFilter.GetSort("name")
|
|
|
|
direction = findFilter.GetDirection()
|
|
|
|
}
|
|
|
|
|
2024-05-22 04:59:08 +00:00
|
|
|
// CVE-2024-32231 - ensure sort is in the list of allowed sorts
|
2024-07-03 23:10:26 +00:00
|
|
|
if err := groupSortOptions.validateSort(sort); err != nil {
|
2024-08-30 01:43:44 +00:00
|
|
|
return err
|
2024-05-22 04:59:08 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 23:21:17 +00:00
|
|
|
switch sort {
|
2024-08-30 01:43:44 +00:00
|
|
|
case "sub_group_order":
|
|
|
|
// sub_group_order is a special sort that sorts by the order_index of the subgroups
|
|
|
|
if query.hasJoin("groups_parents") {
|
|
|
|
query.sortAndPagination += getSort("order_index", direction, "groups_parents")
|
|
|
|
} else {
|
|
|
|
// this will give unexpected results if the query is not filtered by a parent group and
|
|
|
|
// the group has multiple parents and order indexes
|
|
|
|
query.join(groupRelationsTable, "", "groups.id = groups_relations.sub_id")
|
|
|
|
query.sortAndPagination += getSort("order_index", direction, groupRelationsTable)
|
|
|
|
}
|
2024-06-18 01:24:15 +00:00
|
|
|
case "tag_count":
|
2024-08-30 01:43:44 +00:00
|
|
|
query.sortAndPagination += getCountSort(groupTable, groupsTagsTable, groupIDColumn, direction)
|
2021-06-03 23:21:17 +00:00
|
|
|
case "scenes_count": // generic getSort won't work for this
|
2024-08-30 01:43:44 +00:00
|
|
|
query.sortAndPagination += getCountSort(groupTable, groupsScenesTable, groupIDColumn, direction)
|
2021-06-03 23:21:17 +00:00
|
|
|
default:
|
2024-08-30 01:43:44 +00:00
|
|
|
query.sortAndPagination += getSort(sort, direction, "groups")
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
2023-04-17 05:21:13 +00:00
|
|
|
|
|
|
|
// Whatever the sorting, always use name/id as a final sort
|
2024-08-30 01:43:44 +00:00
|
|
|
query.sortAndPagination += ", COALESCE(groups.name, groups.id) COLLATE NATURAL_CI ASC"
|
|
|
|
return nil
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
func (qb *GroupStore) queryGroups(ctx context.Context, query string, args []interface{}) ([]*models.Group, error) {
|
2023-06-15 02:46:09 +00:00
|
|
|
const single = false
|
2024-07-03 23:10:26 +00:00
|
|
|
var ret []*models.Group
|
|
|
|
if err := groupRepository.queryFunc(ctx, query, args, single, func(r *sqlx.Rows) error {
|
|
|
|
var f groupRow
|
2023-06-15 02:46:09 +00:00
|
|
|
if err := r.StructScan(&f); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s := f.resolve()
|
2021-01-18 01:23:20 +00:00
|
|
|
|
2023-06-15 02:46:09 +00:00
|
|
|
ret = append(ret, s)
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2021-01-18 01:23:20 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-06-15 02:46:09 +00:00
|
|
|
return ret, nil
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
func (qb *GroupStore) UpdateFrontImage(ctx context.Context, groupID int, frontImage []byte) error {
|
|
|
|
return qb.UpdateImage(ctx, groupID, groupFrontImageBlobColumn, frontImage)
|
2023-03-16 23:52:49 +00:00
|
|
|
}
|
2021-01-18 01:23:20 +00:00
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
func (qb *GroupStore) UpdateBackImage(ctx context.Context, groupID int, backImage []byte) error {
|
|
|
|
return qb.UpdateImage(ctx, groupID, groupBackImageBlobColumn, backImage)
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
func (qb *GroupStore) destroyImages(ctx context.Context, groupID int) error {
|
|
|
|
if err := qb.DestroyImage(ctx, groupID, groupFrontImageBlobColumn); err != nil {
|
2021-01-18 01:23:20 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-07-03 23:10:26 +00:00
|
|
|
if err := qb.DestroyImage(ctx, groupID, groupBackImageBlobColumn); err != nil {
|
2023-03-16 23:52:49 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
func (qb *GroupStore) GetFrontImage(ctx context.Context, groupID int) ([]byte, error) {
|
|
|
|
return qb.GetImage(ctx, groupID, groupFrontImageBlobColumn)
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
func (qb *GroupStore) HasFrontImage(ctx context.Context, groupID int) (bool, error) {
|
|
|
|
return qb.HasImage(ctx, groupID, groupFrontImageBlobColumn)
|
2023-04-19 03:01:32 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
func (qb *GroupStore) GetBackImage(ctx context.Context, groupID int) ([]byte, error) {
|
|
|
|
return qb.GetImage(ctx, groupID, groupBackImageBlobColumn)
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
2021-09-27 01:31:49 +00:00
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
func (qb *GroupStore) HasBackImage(ctx context.Context, groupID int) (bool, error) {
|
|
|
|
return qb.HasImage(ctx, groupID, groupBackImageBlobColumn)
|
2023-03-25 23:56:32 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
func (qb *GroupStore) FindByPerformerID(ctx context.Context, performerID int) ([]*models.Group, error) {
|
2024-07-30 04:14:16 +00:00
|
|
|
query := `SELECT DISTINCT groups.*
|
|
|
|
FROM groups
|
|
|
|
INNER JOIN groups_scenes ON groups.id = groups_scenes.group_id
|
|
|
|
INNER JOIN performers_scenes ON performers_scenes.scene_id = groups_scenes.scene_id
|
2021-09-27 01:31:49 +00:00
|
|
|
WHERE performers_scenes.performer_id = ?
|
|
|
|
`
|
|
|
|
args := []interface{}{performerID}
|
2024-07-03 23:10:26 +00:00
|
|
|
return qb.queryGroups(ctx, query, args)
|
2021-09-27 01:31:49 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
func (qb *GroupStore) CountByPerformerID(ctx context.Context, performerID int) (int, error) {
|
2024-07-30 04:14:16 +00:00
|
|
|
query := `SELECT COUNT(DISTINCT groups_scenes.group_id) AS count
|
|
|
|
FROM groups_scenes
|
|
|
|
INNER JOIN performers_scenes ON performers_scenes.scene_id = groups_scenes.scene_id
|
2021-09-27 01:31:49 +00:00
|
|
|
WHERE performers_scenes.performer_id = ?
|
|
|
|
`
|
|
|
|
args := []interface{}{performerID}
|
2024-07-03 23:10:26 +00:00
|
|
|
return groupRepository.runCountQuery(ctx, query, args)
|
2021-09-27 01:31:49 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
func (qb *GroupStore) FindByStudioID(ctx context.Context, studioID int) ([]*models.Group, error) {
|
2024-07-30 04:14:16 +00:00
|
|
|
query := `SELECT groups.*
|
|
|
|
FROM groups
|
|
|
|
WHERE groups.studio_id = ?
|
2021-09-27 01:31:49 +00:00
|
|
|
`
|
|
|
|
args := []interface{}{studioID}
|
2024-07-03 23:10:26 +00:00
|
|
|
return qb.queryGroups(ctx, query, args)
|
2021-09-27 01:31:49 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
func (qb *GroupStore) CountByStudioID(ctx context.Context, studioID int) (int, error) {
|
2021-09-27 01:31:49 +00:00
|
|
|
query := `SELECT COUNT(1) AS count
|
2024-07-30 04:14:16 +00:00
|
|
|
FROM groups
|
|
|
|
WHERE groups.studio_id = ?
|
2021-09-27 01:31:49 +00:00
|
|
|
`
|
|
|
|
args := []interface{}{studioID}
|
2024-07-03 23:10:26 +00:00
|
|
|
return groupRepository.runCountQuery(ctx, query, args)
|
2021-09-27 01:31:49 +00:00
|
|
|
}
|
2024-06-11 03:08:49 +00:00
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
func (qb *GroupStore) GetURLs(ctx context.Context, groupID int) ([]string, error) {
|
|
|
|
return groupsURLsTableMgr.get(ctx, groupID)
|
2024-06-11 03:08:49 +00:00
|
|
|
}
|
2024-08-30 01:43:44 +00:00
|
|
|
|
|
|
|
// FindSubGroupIDs returns a list of group IDs where a group in the ids list is a sub-group of the parent group
|
|
|
|
func (qb *GroupStore) FindSubGroupIDs(ctx context.Context, containingID int, ids []int) ([]int, error) {
|
|
|
|
/*
|
|
|
|
SELECT gr.sub_id FROM groups_relations gr
|
|
|
|
WHERE gr.containing_id = :parentID AND gr.sub_id IN (:ids);
|
|
|
|
*/
|
|
|
|
table := groupRelationshipTableMgr.table
|
|
|
|
q := dialect.From(table).Prepared(true).
|
|
|
|
Select(table.Col("sub_id")).Where(
|
|
|
|
table.Col("containing_id").Eq(containingID),
|
|
|
|
table.Col("sub_id").In(ids),
|
|
|
|
)
|
|
|
|
|
|
|
|
const single = false
|
|
|
|
var ret []int
|
|
|
|
if err := queryFunc(ctx, q, single, func(r *sqlx.Rows) error {
|
|
|
|
var id int
|
|
|
|
if err := r.Scan(&id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = append(ret, id)
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindInAscestors returns a list of group IDs where a group in the ids list is an ascestor of the ancestor group IDs
|
|
|
|
func (qb *GroupStore) FindInAncestors(ctx context.Context, ascestorIDs []int, ids []int) ([]int, error) {
|
|
|
|
/*
|
|
|
|
WITH RECURSIVE ascestors AS (
|
|
|
|
SELECT g.id AS parent_id FROM groups g WHERE g.id IN (:ascestorIDs)
|
|
|
|
UNION
|
|
|
|
SELECT gr.containing_id FROM groups_relations gr INNER JOIN ascestors a ON a.parent_id = gr.sub_id
|
|
|
|
)
|
|
|
|
SELECT p.parent_id FROM ascestors p WHERE p.parent_id IN (:ids);
|
|
|
|
*/
|
|
|
|
table := qb.table()
|
|
|
|
const ascestors = "ancestors"
|
|
|
|
const parentID = "parent_id"
|
|
|
|
q := dialect.From(ascestors).Prepared(true).
|
|
|
|
WithRecursive(ascestors,
|
|
|
|
dialect.From(qb.table()).Select(table.Col(idColumn).As(parentID)).
|
|
|
|
Where(table.Col(idColumn).In(ascestorIDs)).
|
|
|
|
Union(
|
|
|
|
dialect.From(groupRelationsJoinTable).InnerJoin(
|
|
|
|
goqu.I(ascestors), goqu.On(goqu.I("parent_id").Eq(goqu.I("sub_id"))),
|
|
|
|
).Select("containing_id"),
|
|
|
|
),
|
|
|
|
).Select(parentID).Where(goqu.I(parentID).In(ids))
|
|
|
|
|
|
|
|
const single = false
|
|
|
|
var ret []int
|
|
|
|
if err := queryFunc(ctx, q, single, func(r *sqlx.Rows) error {
|
|
|
|
var id int
|
|
|
|
if err := r.Scan(&id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = append(ret, id)
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|