2021-01-18 01:23:20 +00:00
|
|
|
package sqlite
|
2019-02-09 12:30:49 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
2020-09-15 07:28:53 +00:00
|
|
|
"fmt"
|
2021-04-26 02:51:31 +00:00
|
|
|
"strings"
|
2019-08-14 21:40:51 +00:00
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
"github.com/stashapp/stash/pkg/models"
|
2021-06-06 05:05:05 +00:00
|
|
|
"github.com/stashapp/stash/pkg/utils"
|
2019-02-09 12:30:49 +00:00
|
|
|
)
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
const performerTable = "performers"
|
|
|
|
const performerIDColumn = "performer_id"
|
2021-03-10 01:25:51 +00:00
|
|
|
const performersTagsTable = "performers_tags"
|
2021-05-24 07:45:51 +00:00
|
|
|
const performersImageTable = "performers_image" // performer cover image
|
2021-03-10 01:25:51 +00:00
|
|
|
|
|
|
|
var countPerformersForTagQuery = `
|
|
|
|
SELECT tag_id AS id FROM performers_tags
|
|
|
|
WHERE performers_tags.tag_id = ?
|
|
|
|
GROUP BY performers_tags.performer_id
|
|
|
|
`
|
2019-02-09 12:30:49 +00:00
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
type performerQueryBuilder struct {
|
|
|
|
repository
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func NewPerformerReaderWriter(tx dbi) *performerQueryBuilder {
|
|
|
|
return &performerQueryBuilder{
|
|
|
|
repository{
|
|
|
|
tx: tx,
|
|
|
|
tableName: performerTable,
|
|
|
|
idColumn: idColumn,
|
|
|
|
},
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
2019-02-09 12:30:49 +00:00
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *performerQueryBuilder) Create(newObject models.Performer) (*models.Performer, error) {
|
|
|
|
var ret models.Performer
|
|
|
|
if err := qb.insertObject(newObject, &ret); err != nil {
|
2019-02-09 12:30:49 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
return &ret, nil
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *performerQueryBuilder) Update(updatedObject models.PerformerPartial) (*models.Performer, error) {
|
|
|
|
const partial = true
|
|
|
|
if err := qb.update(updatedObject.ID, updatedObject, partial); err != nil {
|
2020-12-04 01:42:56 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
var ret models.Performer
|
|
|
|
if err := qb.get(updatedObject.ID, &ret); err != nil {
|
2020-12-04 01:42:56 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-18 01:23:20 +00:00
|
|
|
|
2020-12-04 01:42:56 +00:00
|
|
|
return &ret, nil
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *performerQueryBuilder) UpdateFull(updatedObject models.Performer) (*models.Performer, error) {
|
|
|
|
const partial = false
|
|
|
|
if err := qb.update(updatedObject.ID, updatedObject, partial); err != nil {
|
2019-02-09 12:30:49 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
var ret models.Performer
|
|
|
|
if err := qb.get(updatedObject.ID, &ret); err != nil {
|
2019-02-09 12:30:49 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
return &ret, nil
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *performerQueryBuilder) Destroy(id int) error {
|
|
|
|
// TODO - add on delete cascade to performers_scenes
|
|
|
|
_, err := qb.tx.Exec("DELETE FROM performers_scenes WHERE performer_id = ?", id)
|
2019-08-14 21:40:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
return qb.destroyExisting([]int{id})
|
2019-08-14 21:40:51 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *performerQueryBuilder) Find(id int) (*models.Performer, error) {
|
|
|
|
var ret models.Performer
|
|
|
|
if err := qb.get(id, &ret); err != nil {
|
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
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2021-01-18 01:23:20 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
2019-02-09 12:30:49 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-18 01:23:20 +00:00
|
|
|
return &ret, nil
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *performerQueryBuilder) FindMany(ids []int) ([]*models.Performer, error) {
|
|
|
|
var performers []*models.Performer
|
2020-09-15 07:28:53 +00:00
|
|
|
for _, id := range ids {
|
|
|
|
performer, err := qb.Find(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if performer == nil {
|
|
|
|
return nil, fmt.Errorf("performer with id %d not found", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
performers = append(performers, performer)
|
|
|
|
}
|
|
|
|
|
|
|
|
return performers, nil
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *performerQueryBuilder) FindBySceneID(sceneID int) ([]*models.Performer, error) {
|
2020-05-11 05:19:11 +00:00
|
|
|
query := selectAll("performers") + `
|
2019-02-09 12:30:49 +00:00
|
|
|
LEFT JOIN performers_scenes as scenes_join on scenes_join.performer_id = performers.id
|
2020-05-11 05:19:11 +00:00
|
|
|
WHERE scenes_join.scene_id = ?
|
2019-02-09 12:30:49 +00:00
|
|
|
`
|
|
|
|
args := []interface{}{sceneID}
|
2021-01-18 01:23:20 +00:00
|
|
|
return qb.queryPerformers(query, args)
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *performerQueryBuilder) FindByImageID(imageID int) ([]*models.Performer, error) {
|
2020-10-12 23:12:46 +00:00
|
|
|
query := selectAll("performers") + `
|
|
|
|
LEFT JOIN performers_images as images_join on images_join.performer_id = performers.id
|
|
|
|
WHERE images_join.image_id = ?
|
|
|
|
`
|
|
|
|
args := []interface{}{imageID}
|
2021-01-18 01:23:20 +00:00
|
|
|
return qb.queryPerformers(query, args)
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *performerQueryBuilder) FindByGalleryID(galleryID int) ([]*models.Performer, error) {
|
2020-10-12 23:12:46 +00:00
|
|
|
query := selectAll("performers") + `
|
|
|
|
LEFT JOIN performers_galleries as galleries_join on galleries_join.performer_id = performers.id
|
|
|
|
WHERE galleries_join.gallery_id = ?
|
|
|
|
`
|
|
|
|
args := []interface{}{galleryID}
|
2021-01-18 01:23:20 +00:00
|
|
|
return qb.queryPerformers(query, args)
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *performerQueryBuilder) FindNamesBySceneID(sceneID int) ([]*models.Performer, error) {
|
2020-04-24 02:52:21 +00:00
|
|
|
query := `
|
|
|
|
SELECT performers.name FROM performers
|
|
|
|
LEFT JOIN performers_scenes as scenes_join on scenes_join.performer_id = performers.id
|
|
|
|
WHERE scenes_join.scene_id = ?
|
|
|
|
`
|
|
|
|
args := []interface{}{sceneID}
|
2021-01-18 01:23:20 +00:00
|
|
|
return qb.queryPerformers(query, args)
|
2020-04-24 02:52:21 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *performerQueryBuilder) FindByNames(names []string, nocase bool) ([]*models.Performer, error) {
|
2020-05-24 06:19:22 +00:00
|
|
|
query := "SELECT * FROM performers WHERE name"
|
|
|
|
if nocase {
|
|
|
|
query += " COLLATE NOCASE"
|
|
|
|
}
|
|
|
|
query += " IN " + getInBinding(len(names))
|
|
|
|
|
2019-02-09 12:30:49 +00:00
|
|
|
var args []interface{}
|
|
|
|
for _, name := range names {
|
|
|
|
args = append(args, name)
|
|
|
|
}
|
2021-01-18 01:23:20 +00:00
|
|
|
return qb.queryPerformers(query, args)
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 01:25:51 +00:00
|
|
|
func (qb *performerQueryBuilder) CountByTagID(tagID int) (int, error) {
|
|
|
|
args := []interface{}{tagID}
|
|
|
|
return qb.runCountQuery(qb.buildCountQuery(countPerformersForTagQuery), args)
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *performerQueryBuilder) Count() (int, error) {
|
|
|
|
return qb.runCountQuery(qb.buildCountQuery("SELECT performers.id FROM performers"), nil)
|
2019-02-11 20:36:10 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *performerQueryBuilder) All() ([]*models.Performer, error) {
|
|
|
|
return qb.queryPerformers(selectAll("performers")+qb.getPerformerSort(nil), nil)
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2021-04-26 02:51:31 +00:00
|
|
|
func (qb *performerQueryBuilder) QueryForAutoTag(words []string) ([]*models.Performer, error) {
|
|
|
|
// TODO - Query needs to be changed to support queries of this type, and
|
|
|
|
// this method should be removed
|
|
|
|
query := selectAll(performerTable)
|
|
|
|
|
|
|
|
var whereClauses []string
|
|
|
|
var args []interface{}
|
|
|
|
|
2021-10-11 12:06:06 +00:00
|
|
|
whereClauses = append(whereClauses, "name regexp ?")
|
|
|
|
args = append(args, "^[\\w][.\\-_ ]")
|
|
|
|
|
2021-04-26 02:51:31 +00:00
|
|
|
for _, w := range words {
|
|
|
|
whereClauses = append(whereClauses, "name like ?")
|
2021-06-08 00:47:22 +00:00
|
|
|
args = append(args, w+"%")
|
2021-10-11 12:06:06 +00:00
|
|
|
// TODO - commented out until alias matching works both ways
|
|
|
|
// whereClauses = append(whereClauses, "aliases like ?")
|
|
|
|
// args = append(args, w+"%")
|
2021-04-26 02:51:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
where := strings.Join(whereClauses, " OR ")
|
|
|
|
return qb.queryPerformers(query+" WHERE "+where, args)
|
|
|
|
}
|
|
|
|
|
2021-05-22 07:07:03 +00:00
|
|
|
func (qb *performerQueryBuilder) validateFilter(filter *models.PerformerFilterType) error {
|
|
|
|
const and = "AND"
|
|
|
|
const or = "OR"
|
|
|
|
const not = "NOT"
|
|
|
|
|
|
|
|
if filter.And != nil {
|
|
|
|
if filter.Or != nil {
|
|
|
|
return illegalFilterCombination(and, or)
|
|
|
|
}
|
|
|
|
if filter.Not != nil {
|
|
|
|
return illegalFilterCombination(and, not)
|
|
|
|
}
|
|
|
|
|
|
|
|
return qb.validateFilter(filter.And)
|
|
|
|
}
|
|
|
|
|
|
|
|
if filter.Or != nil {
|
|
|
|
if filter.Not != nil {
|
|
|
|
return illegalFilterCombination(or, not)
|
|
|
|
}
|
|
|
|
|
|
|
|
return qb.validateFilter(filter.Or)
|
|
|
|
}
|
|
|
|
|
|
|
|
if filter.Not != nil {
|
|
|
|
return qb.validateFilter(filter.Not)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *performerQueryBuilder) makeFilter(filter *models.PerformerFilterType) *filterBuilder {
|
|
|
|
query := &filterBuilder{}
|
|
|
|
|
|
|
|
if filter.And != nil {
|
|
|
|
query.and(qb.makeFilter(filter.And))
|
|
|
|
}
|
|
|
|
if filter.Or != nil {
|
|
|
|
query.or(qb.makeFilter(filter.Or))
|
|
|
|
}
|
|
|
|
if filter.Not != nil {
|
|
|
|
query.not(qb.makeFilter(filter.Not))
|
|
|
|
}
|
|
|
|
|
|
|
|
const tableName = performerTable
|
2021-06-22 23:10:20 +00:00
|
|
|
query.handleCriterion(stringCriterionHandler(filter.Name, tableName+".name"))
|
|
|
|
query.handleCriterion(stringCriterionHandler(filter.Details, tableName+".details"))
|
|
|
|
|
2021-06-21 05:48:28 +00:00
|
|
|
query.handleCriterion(boolCriterionHandler(filter.FilterFavorites, tableName+".favorite"))
|
2021-05-22 07:07:03 +00:00
|
|
|
|
2021-06-21 05:48:28 +00:00
|
|
|
query.handleCriterion(yearFilterCriterionHandler(filter.BirthYear, tableName+".birthdate"))
|
|
|
|
query.handleCriterion(yearFilterCriterionHandler(filter.DeathYear, tableName+".death_date"))
|
2021-05-22 07:07:03 +00:00
|
|
|
|
2021-06-21 05:48:28 +00:00
|
|
|
query.handleCriterion(performerAgeFilterCriterionHandler(filter.Age))
|
2021-05-22 07:07:03 +00:00
|
|
|
|
2021-06-21 05:48:28 +00:00
|
|
|
query.handleCriterion(criterionHandlerFunc(func(f *filterBuilder) {
|
2021-05-22 07:07:03 +00:00
|
|
|
if gender := filter.Gender; gender != nil {
|
|
|
|
f.addWhere(tableName+".gender = ?", gender.Value.String())
|
|
|
|
}
|
2021-06-21 05:48:28 +00:00
|
|
|
}))
|
|
|
|
|
|
|
|
query.handleCriterion(performerIsMissingCriterionHandler(qb, filter.IsMissing))
|
|
|
|
query.handleCriterion(stringCriterionHandler(filter.Ethnicity, tableName+".ethnicity"))
|
|
|
|
query.handleCriterion(stringCriterionHandler(filter.Country, tableName+".country"))
|
|
|
|
query.handleCriterion(stringCriterionHandler(filter.EyeColor, tableName+".eye_color"))
|
|
|
|
query.handleCriterion(stringCriterionHandler(filter.Height, tableName+".height"))
|
|
|
|
query.handleCriterion(stringCriterionHandler(filter.Measurements, tableName+".measurements"))
|
|
|
|
query.handleCriterion(stringCriterionHandler(filter.FakeTits, tableName+".fake_tits"))
|
|
|
|
query.handleCriterion(stringCriterionHandler(filter.CareerLength, tableName+".career_length"))
|
|
|
|
query.handleCriterion(stringCriterionHandler(filter.Tattoos, tableName+".tattoos"))
|
|
|
|
query.handleCriterion(stringCriterionHandler(filter.Piercings, tableName+".piercings"))
|
|
|
|
query.handleCriterion(intCriterionHandler(filter.Rating, tableName+".rating"))
|
|
|
|
query.handleCriterion(stringCriterionHandler(filter.HairColor, tableName+".hair_color"))
|
|
|
|
query.handleCriterion(stringCriterionHandler(filter.URL, tableName+".url"))
|
|
|
|
query.handleCriterion(intCriterionHandler(filter.Weight, tableName+".weight"))
|
|
|
|
query.handleCriterion(criterionHandlerFunc(func(f *filterBuilder) {
|
2021-05-22 07:07:03 +00:00
|
|
|
if filter.StashID != nil {
|
|
|
|
qb.stashIDRepository().join(f, "performer_stash_ids", "performers.id")
|
|
|
|
stringCriterionHandler(filter.StashID, "performer_stash_ids.stash_id")(f)
|
|
|
|
}
|
2021-06-21 05:48:28 +00:00
|
|
|
}))
|
2021-05-22 07:07:03 +00:00
|
|
|
|
|
|
|
// TODO - need better handling of aliases
|
2021-06-21 05:48:28 +00:00
|
|
|
query.handleCriterion(stringCriterionHandler(filter.Aliases, tableName+".aliases"))
|
2021-05-22 07:07:03 +00:00
|
|
|
|
2021-06-21 05:48:28 +00:00
|
|
|
query.handleCriterion(performerTagsCriterionHandler(qb, filter.Tags))
|
2021-05-22 07:07:03 +00:00
|
|
|
|
Tag hierarchy (#1519)
* Add migration script for tag relations table
* Expand hierarchical filter features
Expand the features of the hierarchical multi input filter with support
for using a relations table, which only has parent_id and child_id
columns, and support adding an additional intermediate table to join on,
for example for scenes and tags which are linked by the scenes_tags
table as well.
* Add hierarchical filtering for tags
* Add hierarchical tags support to scene markers
Refactor filtering of scene markers to filterBuilder and in the process
add support for hierarchical tags as well.
* List parent and child tags on tag details page
* Support setting parent and child tags
Add support for setting parent and child tags during tag creation and
tag updates.
* Validate no loops are created in tags hierarchy
* Update tag merging to support tag hierarcy
* Add unit tests for tags.EnsureUniqueHierarchy
* Fix applying recursive to with clause
The SQL `RECURSIVE` of a `WITH` clause only needs to be applied once,
imediately after the `WITH`. So this fixes the query building to do just
that, automatically applying the `RECURSIVE` keyword when any added with
clause is added as recursive.
* Rename hierarchical root id column
* Rewrite hierarchical filtering for performance
Completely rewrite the hierarchical filtering to optimize for
performance. Doing the recursive query in combination with a complex
query seems to break SQLite optimizing some things which means that the
recursive part might be 2,5 second slower than adding a static
`VALUES()` list. This is mostly noticable in case of the tag hierarchy
where setting an exclusion with any depth (or depth: all) being applied
has this performance impact of 2,5 second. "Include" also suffered this
issue, but some rewritten query by joining in the *_tags table in one
pass and applying a `WHERE x IS NOT NULL` filter did seem to optimize
that case. But that optimization isn't applied to the `IS NULL` filter
of "exclude". Running a simple query beforehand to get all (recursive)
items and then applying them to the query doesn't have this performance
penalty.
* Remove UI references to child studios and tags
* Add parents to tag export
* Support importing of parent relationship for tags
* Assign stable ids to parent / child badges
* Silence Apollo warning on parents/children fields on tags
Silence warning triggered by Apollo GraphQL by explicitly instructing it
to use the incoming parents/children values. By default it already does
this, but it triggers a warning as it might be unintended that it uses
the incoming values (instead of for example merging both arrays).
Setting merge to false still applies the same behaviour (use only
incoming values) but silences the warning as it's explicitly configured
to work like this.
* Rework detecting unique tag hierarchy
Completely rework the unique tag hierarchy to detect invalid hierarchies
for which a tag is "added in the middle". So when there are tags A <- B
and A <- C, you could previously edit tag B and add tag C as a sub tag
without it being noticed as parent A being applied twice (to tag C).
While afterwards saving tag C would fail as tag A was applied as parent
twice. The updated code correctly detects this scenario as well.
Furthermore the error messaging has been reworked a bit and the message
now mentions both the direct parent / sub tag as well as the tag which
would results in the error. So in aboves example it would now show the
message that tag C can't be applied because tag A already is a parent.
* Update relations on cached tags when needed
Update the relations on cached tags when a tag is created / updated /
deleted so these always reflect the correct state. Otherwise (re)opening
a tag might still show the old relations untill the page is fully
reloaded or the list is navigated. But this obviously is strange when
you for example have tag A, create or update tag B to have a relation to
tag A, and from tags B page click through to tag A and it doesn't show
that it is linked to tag B.
2021-09-09 04:58:43 +00:00
|
|
|
query.handleCriterion(performerStudiosCriterionHandler(qb, filter.Studios))
|
2021-05-22 07:07:03 +00:00
|
|
|
|
2021-06-21 05:48:28 +00:00
|
|
|
query.handleCriterion(performerTagCountCriterionHandler(qb, filter.TagCount))
|
|
|
|
query.handleCriterion(performerSceneCountCriterionHandler(qb, filter.SceneCount))
|
|
|
|
query.handleCriterion(performerImageCountCriterionHandler(qb, filter.ImageCount))
|
|
|
|
query.handleCriterion(performerGalleryCountCriterionHandler(qb, filter.GalleryCount))
|
2021-05-22 07:07:03 +00:00
|
|
|
|
|
|
|
return query
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *performerQueryBuilder) Query(performerFilter *models.PerformerFilterType, findFilter *models.FindFilterType) ([]*models.Performer, int, error) {
|
2019-02-09 12:30:49 +00:00
|
|
|
if performerFilter == nil {
|
2021-01-18 01:23:20 +00:00
|
|
|
performerFilter = &models.PerformerFilterType{}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
if findFilter == nil {
|
2021-01-18 01:23:20 +00:00
|
|
|
findFilter = &models.FindFilterType{}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
query := qb.newQuery()
|
2021-10-25 00:40:13 +00:00
|
|
|
distinctIDs(&query, performerTable)
|
2019-02-09 12:30:49 +00:00
|
|
|
|
|
|
|
if q := findFilter.Q; q != nil && *q != "" {
|
2021-03-30 03:04:57 +00:00
|
|
|
searchColumns := []string{"performers.name", "performers.aliases"}
|
2019-11-07 04:36:48 +00:00
|
|
|
clause, thisArgs := getSearchBinding(searchColumns, *q, false)
|
|
|
|
query.addWhere(clause)
|
|
|
|
query.addArg(thisArgs...)
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 07:07:03 +00:00
|
|
|
if err := qb.validateFilter(performerFilter); err != nil {
|
|
|
|
return nil, 0, err
|
2019-11-07 04:36:48 +00:00
|
|
|
}
|
2021-05-22 07:07:03 +00:00
|
|
|
filter := qb.makeFilter(performerFilter)
|
2019-11-07 04:36:48 +00:00
|
|
|
|
2021-05-22 07:07:03 +00:00
|
|
|
query.addFilter(filter)
|
|
|
|
|
|
|
|
query.sortAndPagination = qb.getPerformerSort(findFilter) + getPagination(findFilter)
|
|
|
|
idsResult, countResult, err := query.executeFind()
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
2021-04-16 06:06:35 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 07:07:03 +00:00
|
|
|
var performers []*models.Performer
|
|
|
|
for _, id := range idsResult {
|
|
|
|
performer, err := qb.Find(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
performers = append(performers, performer)
|
2019-11-07 04:36:48 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 07:07:03 +00:00
|
|
|
return performers, countResult, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func performerIsMissingCriterionHandler(qb *performerQueryBuilder, isMissing *string) criterionHandlerFunc {
|
|
|
|
return func(f *filterBuilder) {
|
|
|
|
if isMissing != nil && *isMissing != "" {
|
|
|
|
switch *isMissing {
|
2021-05-25 00:45:26 +00:00
|
|
|
case "scenes": // Deprecated: use `scene_count == 0` filter instead
|
2021-05-22 07:07:03 +00:00
|
|
|
f.addJoin(performersScenesTable, "scenes_join", "scenes_join.performer_id = performers.id")
|
|
|
|
f.addWhere("scenes_join.scene_id IS NULL")
|
|
|
|
case "image":
|
2021-05-24 07:45:51 +00:00
|
|
|
f.addJoin(performersImageTable, "image_join", "image_join.performer_id = performers.id")
|
|
|
|
f.addWhere("image_join.performer_id IS NULL")
|
2021-08-30 01:46:41 +00:00
|
|
|
case "stash_id":
|
|
|
|
qb.stashIDRepository().join(f, "performer_stash_ids", "performers.id")
|
|
|
|
f.addWhere("performer_stash_ids.performer_id IS NULL")
|
2021-05-22 07:07:03 +00:00
|
|
|
default:
|
|
|
|
f.addWhere("(performers." + *isMissing + " IS NULL OR TRIM(performers." + *isMissing + ") = '')")
|
|
|
|
}
|
|
|
|
}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
2021-05-22 07:07:03 +00:00
|
|
|
}
|
2019-02-09 12:30:49 +00:00
|
|
|
|
2021-05-22 07:07:03 +00:00
|
|
|
func yearFilterCriterionHandler(year *models.IntCriterionInput, col string) criterionHandlerFunc {
|
|
|
|
return func(f *filterBuilder) {
|
|
|
|
if year != nil && year.Modifier.IsValid() {
|
2021-08-12 00:24:16 +00:00
|
|
|
clause, args := getIntCriterionWhereClause("cast(strftime('%Y', "+col+") as int)", *year)
|
|
|
|
f.addWhere(clause, args...)
|
2021-05-22 07:07:03 +00:00
|
|
|
}
|
2020-03-31 22:36:38 +00:00
|
|
|
}
|
2021-05-22 07:07:03 +00:00
|
|
|
}
|
2020-03-31 22:36:38 +00:00
|
|
|
|
2021-05-22 07:07:03 +00:00
|
|
|
func performerAgeFilterCriterionHandler(age *models.IntCriterionInput) criterionHandlerFunc {
|
|
|
|
return func(f *filterBuilder) {
|
|
|
|
if age != nil && age.Modifier.IsValid() {
|
2021-08-12 00:24:16 +00:00
|
|
|
clause, args := getIntCriterionWhereClause(
|
|
|
|
"cast(IFNULL(strftime('%Y.%m%d', performers.death_date), strftime('%Y.%m%d', 'now')) - strftime('%Y.%m%d', performers.birthdate) as int)",
|
|
|
|
*age,
|
|
|
|
)
|
|
|
|
f.addWhere(clause, args...)
|
2020-04-08 04:21:05 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-22 07:07:03 +00:00
|
|
|
}
|
2020-04-08 04:21:05 +00:00
|
|
|
|
Tag hierarchy (#1519)
* Add migration script for tag relations table
* Expand hierarchical filter features
Expand the features of the hierarchical multi input filter with support
for using a relations table, which only has parent_id and child_id
columns, and support adding an additional intermediate table to join on,
for example for scenes and tags which are linked by the scenes_tags
table as well.
* Add hierarchical filtering for tags
* Add hierarchical tags support to scene markers
Refactor filtering of scene markers to filterBuilder and in the process
add support for hierarchical tags as well.
* List parent and child tags on tag details page
* Support setting parent and child tags
Add support for setting parent and child tags during tag creation and
tag updates.
* Validate no loops are created in tags hierarchy
* Update tag merging to support tag hierarcy
* Add unit tests for tags.EnsureUniqueHierarchy
* Fix applying recursive to with clause
The SQL `RECURSIVE` of a `WITH` clause only needs to be applied once,
imediately after the `WITH`. So this fixes the query building to do just
that, automatically applying the `RECURSIVE` keyword when any added with
clause is added as recursive.
* Rename hierarchical root id column
* Rewrite hierarchical filtering for performance
Completely rewrite the hierarchical filtering to optimize for
performance. Doing the recursive query in combination with a complex
query seems to break SQLite optimizing some things which means that the
recursive part might be 2,5 second slower than adding a static
`VALUES()` list. This is mostly noticable in case of the tag hierarchy
where setting an exclusion with any depth (or depth: all) being applied
has this performance impact of 2,5 second. "Include" also suffered this
issue, but some rewritten query by joining in the *_tags table in one
pass and applying a `WHERE x IS NOT NULL` filter did seem to optimize
that case. But that optimization isn't applied to the `IS NULL` filter
of "exclude". Running a simple query beforehand to get all (recursive)
items and then applying them to the query doesn't have this performance
penalty.
* Remove UI references to child studios and tags
* Add parents to tag export
* Support importing of parent relationship for tags
* Assign stable ids to parent / child badges
* Silence Apollo warning on parents/children fields on tags
Silence warning triggered by Apollo GraphQL by explicitly instructing it
to use the incoming parents/children values. By default it already does
this, but it triggers a warning as it might be unintended that it uses
the incoming values (instead of for example merging both arrays).
Setting merge to false still applies the same behaviour (use only
incoming values) but silences the warning as it's explicitly configured
to work like this.
* Rework detecting unique tag hierarchy
Completely rework the unique tag hierarchy to detect invalid hierarchies
for which a tag is "added in the middle". So when there are tags A <- B
and A <- C, you could previously edit tag B and add tag C as a sub tag
without it being noticed as parent A being applied twice (to tag C).
While afterwards saving tag C would fail as tag A was applied as parent
twice. The updated code correctly detects this scenario as well.
Furthermore the error messaging has been reworked a bit and the message
now mentions both the direct parent / sub tag as well as the tag which
would results in the error. So in aboves example it would now show the
message that tag C can't be applied because tag A already is a parent.
* Update relations on cached tags when needed
Update the relations on cached tags when a tag is created / updated /
deleted so these always reflect the correct state. Otherwise (re)opening
a tag might still show the old relations untill the page is fully
reloaded or the list is navigated. But this obviously is strange when
you for example have tag A, create or update tag B to have a relation to
tag A, and from tags B page click through to tag A and it doesn't show
that it is linked to tag B.
2021-09-09 04:58:43 +00:00
|
|
|
func performerTagsCriterionHandler(qb *performerQueryBuilder, tags *models.HierarchicalMultiCriterionInput) criterionHandlerFunc {
|
|
|
|
h := joinedHierarchicalMultiCriterionHandlerBuilder{
|
|
|
|
tx: qb.tx,
|
|
|
|
|
2021-05-22 07:07:03 +00:00
|
|
|
primaryTable: performerTable,
|
Tag hierarchy (#1519)
* Add migration script for tag relations table
* Expand hierarchical filter features
Expand the features of the hierarchical multi input filter with support
for using a relations table, which only has parent_id and child_id
columns, and support adding an additional intermediate table to join on,
for example for scenes and tags which are linked by the scenes_tags
table as well.
* Add hierarchical filtering for tags
* Add hierarchical tags support to scene markers
Refactor filtering of scene markers to filterBuilder and in the process
add support for hierarchical tags as well.
* List parent and child tags on tag details page
* Support setting parent and child tags
Add support for setting parent and child tags during tag creation and
tag updates.
* Validate no loops are created in tags hierarchy
* Update tag merging to support tag hierarcy
* Add unit tests for tags.EnsureUniqueHierarchy
* Fix applying recursive to with clause
The SQL `RECURSIVE` of a `WITH` clause only needs to be applied once,
imediately after the `WITH`. So this fixes the query building to do just
that, automatically applying the `RECURSIVE` keyword when any added with
clause is added as recursive.
* Rename hierarchical root id column
* Rewrite hierarchical filtering for performance
Completely rewrite the hierarchical filtering to optimize for
performance. Doing the recursive query in combination with a complex
query seems to break SQLite optimizing some things which means that the
recursive part might be 2,5 second slower than adding a static
`VALUES()` list. This is mostly noticable in case of the tag hierarchy
where setting an exclusion with any depth (or depth: all) being applied
has this performance impact of 2,5 second. "Include" also suffered this
issue, but some rewritten query by joining in the *_tags table in one
pass and applying a `WHERE x IS NOT NULL` filter did seem to optimize
that case. But that optimization isn't applied to the `IS NULL` filter
of "exclude". Running a simple query beforehand to get all (recursive)
items and then applying them to the query doesn't have this performance
penalty.
* Remove UI references to child studios and tags
* Add parents to tag export
* Support importing of parent relationship for tags
* Assign stable ids to parent / child badges
* Silence Apollo warning on parents/children fields on tags
Silence warning triggered by Apollo GraphQL by explicitly instructing it
to use the incoming parents/children values. By default it already does
this, but it triggers a warning as it might be unintended that it uses
the incoming values (instead of for example merging both arrays).
Setting merge to false still applies the same behaviour (use only
incoming values) but silences the warning as it's explicitly configured
to work like this.
* Rework detecting unique tag hierarchy
Completely rework the unique tag hierarchy to detect invalid hierarchies
for which a tag is "added in the middle". So when there are tags A <- B
and A <- C, you could previously edit tag B and add tag C as a sub tag
without it being noticed as parent A being applied twice (to tag C).
While afterwards saving tag C would fail as tag A was applied as parent
twice. The updated code correctly detects this scenario as well.
Furthermore the error messaging has been reworked a bit and the message
now mentions both the direct parent / sub tag as well as the tag which
would results in the error. So in aboves example it would now show the
message that tag C can't be applied because tag A already is a parent.
* Update relations on cached tags when needed
Update the relations on cached tags when a tag is created / updated /
deleted so these always reflect the correct state. Otherwise (re)opening
a tag might still show the old relations untill the page is fully
reloaded or the list is navigated. But this obviously is strange when
you for example have tag A, create or update tag B to have a relation to
tag A, and from tags B page click through to tag A and it doesn't show
that it is linked to tag B.
2021-09-09 04:58:43 +00:00
|
|
|
foreignTable: tagTable,
|
|
|
|
foreignFK: "tag_id",
|
2019-11-07 04:36:48 +00:00
|
|
|
|
Tag hierarchy (#1519)
* Add migration script for tag relations table
* Expand hierarchical filter features
Expand the features of the hierarchical multi input filter with support
for using a relations table, which only has parent_id and child_id
columns, and support adding an additional intermediate table to join on,
for example for scenes and tags which are linked by the scenes_tags
table as well.
* Add hierarchical filtering for tags
* Add hierarchical tags support to scene markers
Refactor filtering of scene markers to filterBuilder and in the process
add support for hierarchical tags as well.
* List parent and child tags on tag details page
* Support setting parent and child tags
Add support for setting parent and child tags during tag creation and
tag updates.
* Validate no loops are created in tags hierarchy
* Update tag merging to support tag hierarcy
* Add unit tests for tags.EnsureUniqueHierarchy
* Fix applying recursive to with clause
The SQL `RECURSIVE` of a `WITH` clause only needs to be applied once,
imediately after the `WITH`. So this fixes the query building to do just
that, automatically applying the `RECURSIVE` keyword when any added with
clause is added as recursive.
* Rename hierarchical root id column
* Rewrite hierarchical filtering for performance
Completely rewrite the hierarchical filtering to optimize for
performance. Doing the recursive query in combination with a complex
query seems to break SQLite optimizing some things which means that the
recursive part might be 2,5 second slower than adding a static
`VALUES()` list. This is mostly noticable in case of the tag hierarchy
where setting an exclusion with any depth (or depth: all) being applied
has this performance impact of 2,5 second. "Include" also suffered this
issue, but some rewritten query by joining in the *_tags table in one
pass and applying a `WHERE x IS NOT NULL` filter did seem to optimize
that case. But that optimization isn't applied to the `IS NULL` filter
of "exclude". Running a simple query beforehand to get all (recursive)
items and then applying them to the query doesn't have this performance
penalty.
* Remove UI references to child studios and tags
* Add parents to tag export
* Support importing of parent relationship for tags
* Assign stable ids to parent / child badges
* Silence Apollo warning on parents/children fields on tags
Silence warning triggered by Apollo GraphQL by explicitly instructing it
to use the incoming parents/children values. By default it already does
this, but it triggers a warning as it might be unintended that it uses
the incoming values (instead of for example merging both arrays).
Setting merge to false still applies the same behaviour (use only
incoming values) but silences the warning as it's explicitly configured
to work like this.
* Rework detecting unique tag hierarchy
Completely rework the unique tag hierarchy to detect invalid hierarchies
for which a tag is "added in the middle". So when there are tags A <- B
and A <- C, you could previously edit tag B and add tag C as a sub tag
without it being noticed as parent A being applied twice (to tag C).
While afterwards saving tag C would fail as tag A was applied as parent
twice. The updated code correctly detects this scenario as well.
Furthermore the error messaging has been reworked a bit and the message
now mentions both the direct parent / sub tag as well as the tag which
would results in the error. So in aboves example it would now show the
message that tag C can't be applied because tag A already is a parent.
* Update relations on cached tags when needed
Update the relations on cached tags when a tag is created / updated /
deleted so these always reflect the correct state. Otherwise (re)opening
a tag might still show the old relations untill the page is fully
reloaded or the list is navigated. But this obviously is strange when
you for example have tag A, create or update tag B to have a relation to
tag A, and from tags B page click through to tag A and it doesn't show
that it is linked to tag B.
2021-09-09 04:58:43 +00:00
|
|
|
relationsTable: "tags_relations",
|
|
|
|
joinAs: "image_tag",
|
|
|
|
joinTable: performersTagsTable,
|
|
|
|
primaryFK: performerIDColumn,
|
2021-05-22 07:07:03 +00:00
|
|
|
}
|
2019-11-07 04:36:48 +00:00
|
|
|
|
2021-05-22 07:07:03 +00:00
|
|
|
return h.handler(tags)
|
|
|
|
}
|
2021-03-10 01:25:51 +00:00
|
|
|
|
2021-05-22 07:07:03 +00:00
|
|
|
func performerTagCountCriterionHandler(qb *performerQueryBuilder, count *models.IntCriterionInput) criterionHandlerFunc {
|
|
|
|
h := countCriterionHandlerBuilder{
|
|
|
|
primaryTable: performerTable,
|
|
|
|
joinTable: performersTagsTable,
|
|
|
|
primaryFK: performerIDColumn,
|
2021-03-10 01:25:51 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 07:07:03 +00:00
|
|
|
return h.handler(count)
|
|
|
|
}
|
2021-04-09 08:46:00 +00:00
|
|
|
|
2021-05-22 07:07:03 +00:00
|
|
|
func performerSceneCountCriterionHandler(qb *performerQueryBuilder, count *models.IntCriterionInput) criterionHandlerFunc {
|
|
|
|
h := countCriterionHandlerBuilder{
|
|
|
|
primaryTable: performerTable,
|
|
|
|
joinTable: performersScenesTable,
|
|
|
|
primaryFK: performerIDColumn,
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
2019-02-09 12:30:49 +00:00
|
|
|
|
2021-05-22 07:07:03 +00:00
|
|
|
return h.handler(count)
|
|
|
|
}
|
|
|
|
|
|
|
|
func performerImageCountCriterionHandler(qb *performerQueryBuilder, count *models.IntCriterionInput) criterionHandlerFunc {
|
|
|
|
h := countCriterionHandlerBuilder{
|
|
|
|
primaryTable: performerTable,
|
|
|
|
joinTable: performersImagesTable,
|
|
|
|
primaryFK: performerIDColumn,
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 07:07:03 +00:00
|
|
|
return h.handler(count)
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 07:07:03 +00:00
|
|
|
func performerGalleryCountCriterionHandler(qb *performerQueryBuilder, count *models.IntCriterionInput) criterionHandlerFunc {
|
|
|
|
h := countCriterionHandlerBuilder{
|
|
|
|
primaryTable: performerTable,
|
|
|
|
joinTable: performersGalleriesTable,
|
|
|
|
primaryFK: performerIDColumn,
|
2019-11-07 04:36:48 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 07:07:03 +00:00
|
|
|
return h.handler(count)
|
2019-11-07 04:36:48 +00:00
|
|
|
}
|
|
|
|
|
Tag hierarchy (#1519)
* Add migration script for tag relations table
* Expand hierarchical filter features
Expand the features of the hierarchical multi input filter with support
for using a relations table, which only has parent_id and child_id
columns, and support adding an additional intermediate table to join on,
for example for scenes and tags which are linked by the scenes_tags
table as well.
* Add hierarchical filtering for tags
* Add hierarchical tags support to scene markers
Refactor filtering of scene markers to filterBuilder and in the process
add support for hierarchical tags as well.
* List parent and child tags on tag details page
* Support setting parent and child tags
Add support for setting parent and child tags during tag creation and
tag updates.
* Validate no loops are created in tags hierarchy
* Update tag merging to support tag hierarcy
* Add unit tests for tags.EnsureUniqueHierarchy
* Fix applying recursive to with clause
The SQL `RECURSIVE` of a `WITH` clause only needs to be applied once,
imediately after the `WITH`. So this fixes the query building to do just
that, automatically applying the `RECURSIVE` keyword when any added with
clause is added as recursive.
* Rename hierarchical root id column
* Rewrite hierarchical filtering for performance
Completely rewrite the hierarchical filtering to optimize for
performance. Doing the recursive query in combination with a complex
query seems to break SQLite optimizing some things which means that the
recursive part might be 2,5 second slower than adding a static
`VALUES()` list. This is mostly noticable in case of the tag hierarchy
where setting an exclusion with any depth (or depth: all) being applied
has this performance impact of 2,5 second. "Include" also suffered this
issue, but some rewritten query by joining in the *_tags table in one
pass and applying a `WHERE x IS NOT NULL` filter did seem to optimize
that case. But that optimization isn't applied to the `IS NULL` filter
of "exclude". Running a simple query beforehand to get all (recursive)
items and then applying them to the query doesn't have this performance
penalty.
* Remove UI references to child studios and tags
* Add parents to tag export
* Support importing of parent relationship for tags
* Assign stable ids to parent / child badges
* Silence Apollo warning on parents/children fields on tags
Silence warning triggered by Apollo GraphQL by explicitly instructing it
to use the incoming parents/children values. By default it already does
this, but it triggers a warning as it might be unintended that it uses
the incoming values (instead of for example merging both arrays).
Setting merge to false still applies the same behaviour (use only
incoming values) but silences the warning as it's explicitly configured
to work like this.
* Rework detecting unique tag hierarchy
Completely rework the unique tag hierarchy to detect invalid hierarchies
for which a tag is "added in the middle". So when there are tags A <- B
and A <- C, you could previously edit tag B and add tag C as a sub tag
without it being noticed as parent A being applied twice (to tag C).
While afterwards saving tag C would fail as tag A was applied as parent
twice. The updated code correctly detects this scenario as well.
Furthermore the error messaging has been reworked a bit and the message
now mentions both the direct parent / sub tag as well as the tag which
would results in the error. So in aboves example it would now show the
message that tag C can't be applied because tag A already is a parent.
* Update relations on cached tags when needed
Update the relations on cached tags when a tag is created / updated /
deleted so these always reflect the correct state. Otherwise (re)opening
a tag might still show the old relations untill the page is fully
reloaded or the list is navigated. But this obviously is strange when
you for example have tag A, create or update tag B to have a relation to
tag A, and from tags B page click through to tag A and it doesn't show
that it is linked to tag B.
2021-09-09 04:58:43 +00:00
|
|
|
func performerStudiosCriterionHandler(qb *performerQueryBuilder, studios *models.HierarchicalMultiCriterionInput) criterionHandlerFunc {
|
2021-05-22 07:07:03 +00:00
|
|
|
return func(f *filterBuilder) {
|
|
|
|
if studios != nil {
|
2021-06-06 05:05:05 +00:00
|
|
|
formatMaps := []utils.StrFormatMap{
|
|
|
|
{
|
|
|
|
"primaryTable": sceneTable,
|
|
|
|
"joinTable": performersScenesTable,
|
|
|
|
"primaryFK": sceneIDColumn,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"primaryTable": imageTable,
|
|
|
|
"joinTable": performersImagesTable,
|
|
|
|
"primaryFK": imageIDColumn,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"primaryTable": galleryTable,
|
|
|
|
"joinTable": performersGalleriesTable,
|
|
|
|
"primaryFK": galleryIDColumn,
|
|
|
|
},
|
2021-05-22 07:07:03 +00:00
|
|
|
}
|
|
|
|
|
2021-11-06 22:34:33 +00:00
|
|
|
if studios.Modifier == models.CriterionModifierIsNull || studios.Modifier == models.CriterionModifierNotNull {
|
|
|
|
var notClause string
|
|
|
|
if studios.Modifier == models.CriterionModifierNotNull {
|
|
|
|
notClause = "NOT"
|
|
|
|
}
|
|
|
|
|
|
|
|
var conditions []string
|
|
|
|
for _, c := range formatMaps {
|
|
|
|
f.addJoin(c["joinTable"].(string), "", fmt.Sprintf("%s.performer_id = performers.id", c["joinTable"]))
|
|
|
|
f.addJoin(c["primaryTable"].(string), "", fmt.Sprintf("%s.%s = %s.id", c["joinTable"], c["primaryFK"], c["primaryTable"]))
|
|
|
|
|
|
|
|
conditions = append(conditions, fmt.Sprintf("%s.studio_id IS NULL", c["primaryTable"]))
|
|
|
|
}
|
|
|
|
|
|
|
|
f.addWhere(fmt.Sprintf("%s (%s)", notClause, strings.Join(conditions, " AND ")))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(studios.Value) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var clauseCondition string
|
|
|
|
|
|
|
|
switch studios.Modifier {
|
|
|
|
case models.CriterionModifierIncludes:
|
|
|
|
// return performers who appear in scenes/images/galleries with any of the given studios
|
|
|
|
clauseCondition = "NOT"
|
|
|
|
case models.CriterionModifierExcludes:
|
|
|
|
// exclude performers who appear in scenes/images/galleries with any of the given studios
|
|
|
|
clauseCondition = ""
|
|
|
|
default:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-21 04:17:43 +00:00
|
|
|
const derivedPerformerStudioTable = "performer_studio"
|
Tag hierarchy (#1519)
* Add migration script for tag relations table
* Expand hierarchical filter features
Expand the features of the hierarchical multi input filter with support
for using a relations table, which only has parent_id and child_id
columns, and support adding an additional intermediate table to join on,
for example for scenes and tags which are linked by the scenes_tags
table as well.
* Add hierarchical filtering for tags
* Add hierarchical tags support to scene markers
Refactor filtering of scene markers to filterBuilder and in the process
add support for hierarchical tags as well.
* List parent and child tags on tag details page
* Support setting parent and child tags
Add support for setting parent and child tags during tag creation and
tag updates.
* Validate no loops are created in tags hierarchy
* Update tag merging to support tag hierarcy
* Add unit tests for tags.EnsureUniqueHierarchy
* Fix applying recursive to with clause
The SQL `RECURSIVE` of a `WITH` clause only needs to be applied once,
imediately after the `WITH`. So this fixes the query building to do just
that, automatically applying the `RECURSIVE` keyword when any added with
clause is added as recursive.
* Rename hierarchical root id column
* Rewrite hierarchical filtering for performance
Completely rewrite the hierarchical filtering to optimize for
performance. Doing the recursive query in combination with a complex
query seems to break SQLite optimizing some things which means that the
recursive part might be 2,5 second slower than adding a static
`VALUES()` list. This is mostly noticable in case of the tag hierarchy
where setting an exclusion with any depth (or depth: all) being applied
has this performance impact of 2,5 second. "Include" also suffered this
issue, but some rewritten query by joining in the *_tags table in one
pass and applying a `WHERE x IS NOT NULL` filter did seem to optimize
that case. But that optimization isn't applied to the `IS NULL` filter
of "exclude". Running a simple query beforehand to get all (recursive)
items and then applying them to the query doesn't have this performance
penalty.
* Remove UI references to child studios and tags
* Add parents to tag export
* Support importing of parent relationship for tags
* Assign stable ids to parent / child badges
* Silence Apollo warning on parents/children fields on tags
Silence warning triggered by Apollo GraphQL by explicitly instructing it
to use the incoming parents/children values. By default it already does
this, but it triggers a warning as it might be unintended that it uses
the incoming values (instead of for example merging both arrays).
Setting merge to false still applies the same behaviour (use only
incoming values) but silences the warning as it's explicitly configured
to work like this.
* Rework detecting unique tag hierarchy
Completely rework the unique tag hierarchy to detect invalid hierarchies
for which a tag is "added in the middle". So when there are tags A <- B
and A <- C, you could previously edit tag B and add tag C as a sub tag
without it being noticed as parent A being applied twice (to tag C).
While afterwards saving tag C would fail as tag A was applied as parent
twice. The updated code correctly detects this scenario as well.
Furthermore the error messaging has been reworked a bit and the message
now mentions both the direct parent / sub tag as well as the tag which
would results in the error. So in aboves example it would now show the
message that tag C can't be applied because tag A already is a parent.
* Update relations on cached tags when needed
Update the relations on cached tags when a tag is created / updated /
deleted so these always reflect the correct state. Otherwise (re)opening
a tag might still show the old relations untill the page is fully
reloaded or the list is navigated. But this obviously is strange when
you for example have tag A, create or update tag B to have a relation to
tag A, and from tags B page click through to tag A and it doesn't show
that it is linked to tag B.
2021-09-09 04:58:43 +00:00
|
|
|
valuesClause := getHierarchicalValues(qb.tx, studios.Value, studioTable, "", "parent_id", studios.Depth)
|
|
|
|
f.addWith("studio(root_id, item_id) AS (" + valuesClause + ")")
|
2021-05-22 07:07:03 +00:00
|
|
|
|
2021-06-21 04:17:43 +00:00
|
|
|
templStr := `SELECT performer_id FROM {primaryTable}
|
|
|
|
INNER JOIN {joinTable} ON {primaryTable}.id = {joinTable}.{primaryFK}
|
Tag hierarchy (#1519)
* Add migration script for tag relations table
* Expand hierarchical filter features
Expand the features of the hierarchical multi input filter with support
for using a relations table, which only has parent_id and child_id
columns, and support adding an additional intermediate table to join on,
for example for scenes and tags which are linked by the scenes_tags
table as well.
* Add hierarchical filtering for tags
* Add hierarchical tags support to scene markers
Refactor filtering of scene markers to filterBuilder and in the process
add support for hierarchical tags as well.
* List parent and child tags on tag details page
* Support setting parent and child tags
Add support for setting parent and child tags during tag creation and
tag updates.
* Validate no loops are created in tags hierarchy
* Update tag merging to support tag hierarcy
* Add unit tests for tags.EnsureUniqueHierarchy
* Fix applying recursive to with clause
The SQL `RECURSIVE` of a `WITH` clause only needs to be applied once,
imediately after the `WITH`. So this fixes the query building to do just
that, automatically applying the `RECURSIVE` keyword when any added with
clause is added as recursive.
* Rename hierarchical root id column
* Rewrite hierarchical filtering for performance
Completely rewrite the hierarchical filtering to optimize for
performance. Doing the recursive query in combination with a complex
query seems to break SQLite optimizing some things which means that the
recursive part might be 2,5 second slower than adding a static
`VALUES()` list. This is mostly noticable in case of the tag hierarchy
where setting an exclusion with any depth (or depth: all) being applied
has this performance impact of 2,5 second. "Include" also suffered this
issue, but some rewritten query by joining in the *_tags table in one
pass and applying a `WHERE x IS NOT NULL` filter did seem to optimize
that case. But that optimization isn't applied to the `IS NULL` filter
of "exclude". Running a simple query beforehand to get all (recursive)
items and then applying them to the query doesn't have this performance
penalty.
* Remove UI references to child studios and tags
* Add parents to tag export
* Support importing of parent relationship for tags
* Assign stable ids to parent / child badges
* Silence Apollo warning on parents/children fields on tags
Silence warning triggered by Apollo GraphQL by explicitly instructing it
to use the incoming parents/children values. By default it already does
this, but it triggers a warning as it might be unintended that it uses
the incoming values (instead of for example merging both arrays).
Setting merge to false still applies the same behaviour (use only
incoming values) but silences the warning as it's explicitly configured
to work like this.
* Rework detecting unique tag hierarchy
Completely rework the unique tag hierarchy to detect invalid hierarchies
for which a tag is "added in the middle". So when there are tags A <- B
and A <- C, you could previously edit tag B and add tag C as a sub tag
without it being noticed as parent A being applied twice (to tag C).
While afterwards saving tag C would fail as tag A was applied as parent
twice. The updated code correctly detects this scenario as well.
Furthermore the error messaging has been reworked a bit and the message
now mentions both the direct parent / sub tag as well as the tag which
would results in the error. So in aboves example it would now show the
message that tag C can't be applied because tag A already is a parent.
* Update relations on cached tags when needed
Update the relations on cached tags when a tag is created / updated /
deleted so these always reflect the correct state. Otherwise (re)opening
a tag might still show the old relations untill the page is fully
reloaded or the list is navigated. But this obviously is strange when
you for example have tag A, create or update tag B to have a relation to
tag A, and from tags B page click through to tag A and it doesn't show
that it is linked to tag B.
2021-09-09 04:58:43 +00:00
|
|
|
INNER JOIN studio ON {primaryTable}.studio_id = studio.item_id`
|
2021-05-22 07:07:03 +00:00
|
|
|
|
2021-06-21 04:17:43 +00:00
|
|
|
var unions []string
|
|
|
|
for _, c := range formatMaps {
|
|
|
|
unions = append(unions, utils.StrFormat(templStr, c))
|
|
|
|
}
|
2021-06-06 05:05:05 +00:00
|
|
|
|
2021-11-06 22:34:33 +00:00
|
|
|
f.addWith(fmt.Sprintf("%s AS (%s)", derivedPerformerStudioTable, strings.Join(unions, " UNION ")))
|
2021-06-06 05:05:05 +00:00
|
|
|
|
2021-06-21 04:17:43 +00:00
|
|
|
f.addJoin(derivedPerformerStudioTable, "", fmt.Sprintf("performers.id = %s.performer_id", derivedPerformerStudioTable))
|
|
|
|
f.addWhere(fmt.Sprintf("%s.performer_id IS %s NULL", derivedPerformerStudioTable, clauseCondition))
|
2019-11-07 04:36:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *performerQueryBuilder) getPerformerSort(findFilter *models.FindFilterType) string {
|
2019-02-09 12:30:49 +00:00
|
|
|
var sort string
|
|
|
|
var direction string
|
|
|
|
if findFilter == nil {
|
|
|
|
sort = "name"
|
|
|
|
direction = "ASC"
|
|
|
|
} else {
|
|
|
|
sort = findFilter.GetSort("name")
|
|
|
|
direction = findFilter.GetDirection()
|
|
|
|
}
|
2021-04-09 08:46:00 +00:00
|
|
|
|
|
|
|
if sort == "tag_count" {
|
|
|
|
return getCountSort(performerTable, performersTagsTable, performerIDColumn, direction)
|
|
|
|
}
|
2021-05-24 07:45:51 +00:00
|
|
|
if sort == "scenes_count" {
|
|
|
|
return getCountSort(performerTable, performersScenesTable, performerIDColumn, direction)
|
|
|
|
}
|
2021-08-25 23:41:18 +00:00
|
|
|
if sort == "images_count" {
|
|
|
|
return getCountSort(performerTable, performersImagesTable, performerIDColumn, direction)
|
|
|
|
}
|
|
|
|
if sort == "galleries_count" {
|
|
|
|
return getCountSort(performerTable, performersGalleriesTable, performerIDColumn, direction)
|
|
|
|
}
|
2021-04-09 08:46:00 +00:00
|
|
|
|
2019-02-09 12:30:49 +00:00
|
|
|
return getSort(sort, direction, "performers")
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *performerQueryBuilder) queryPerformers(query string, args []interface{}) ([]*models.Performer, error) {
|
|
|
|
var ret models.Performers
|
|
|
|
if err := qb.query(query, args, &ret); err != nil {
|
2019-02-09 12:30:49 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
return []*models.Performer(ret), nil
|
2019-02-14 22:53:32 +00:00
|
|
|
}
|
2020-06-22 23:19:19 +00:00
|
|
|
|
2021-03-10 01:25:51 +00:00
|
|
|
func (qb *performerQueryBuilder) tagsRepository() *joinRepository {
|
|
|
|
return &joinRepository{
|
|
|
|
repository: repository{
|
|
|
|
tx: qb.tx,
|
|
|
|
tableName: performersTagsTable,
|
|
|
|
idColumn: performerIDColumn,
|
|
|
|
},
|
|
|
|
fkColumn: tagIDColumn,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *performerQueryBuilder) GetTagIDs(id int) ([]int, error) {
|
|
|
|
return qb.tagsRepository().getIDs(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *performerQueryBuilder) UpdateTags(id int, tagIDs []int) error {
|
|
|
|
// Delete the existing joins and then create new ones
|
|
|
|
return qb.tagsRepository().replace(id, tagIDs)
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *performerQueryBuilder) imageRepository() *imageRepository {
|
|
|
|
return &imageRepository{
|
|
|
|
repository: repository{
|
|
|
|
tx: qb.tx,
|
|
|
|
tableName: "performers_image",
|
|
|
|
idColumn: performerIDColumn,
|
|
|
|
},
|
|
|
|
imageColumn: "image",
|
2020-06-22 23:19:19 +00:00
|
|
|
}
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
2020-06-22 23:19:19 +00:00
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *performerQueryBuilder) GetImage(performerID int) ([]byte, error) {
|
|
|
|
return qb.imageRepository().get(performerID)
|
|
|
|
}
|
2020-06-22 23:19:19 +00:00
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *performerQueryBuilder) UpdateImage(performerID int, image []byte) error {
|
|
|
|
return qb.imageRepository().replace(performerID, image)
|
2020-06-22 23:19:19 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *performerQueryBuilder) DestroyImage(performerID int) error {
|
|
|
|
return qb.imageRepository().destroy([]int{performerID})
|
|
|
|
}
|
2020-06-22 23:19:19 +00:00
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *performerQueryBuilder) stashIDRepository() *stashIDRepository {
|
|
|
|
return &stashIDRepository{
|
|
|
|
repository{
|
|
|
|
tx: qb.tx,
|
|
|
|
tableName: "performer_stash_ids",
|
|
|
|
idColumn: performerIDColumn,
|
|
|
|
},
|
2020-06-22 23:19:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *performerQueryBuilder) GetStashIDs(performerID int) ([]*models.StashID, error) {
|
|
|
|
return qb.stashIDRepository().get(performerID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *performerQueryBuilder) UpdateStashIDs(performerID int, stashIDs []models.StashID) error {
|
|
|
|
return qb.stashIDRepository().replace(performerID, stashIDs)
|
2020-06-22 23:19:19 +00:00
|
|
|
}
|
2021-05-03 04:21:20 +00:00
|
|
|
|
2021-11-14 20:51:52 +00:00
|
|
|
func (qb *performerQueryBuilder) FindByStashID(stashID models.StashID) ([]*models.Performer, error) {
|
|
|
|
query := selectAll("performers") + `
|
|
|
|
LEFT JOIN performer_stash_ids on performer_stash_ids.performer_id = performers.id
|
|
|
|
WHERE performer_stash_ids.stash_id = ?
|
|
|
|
AND performer_stash_ids.endpoint = ?
|
|
|
|
`
|
|
|
|
args := []interface{}{stashID.StashID, stashID.Endpoint}
|
|
|
|
return qb.queryPerformers(query, args)
|
|
|
|
}
|
|
|
|
|
2021-05-03 04:21:20 +00:00
|
|
|
func (qb *performerQueryBuilder) FindByStashIDStatus(hasStashID bool, stashboxEndpoint string) ([]*models.Performer, error) {
|
|
|
|
query := selectAll("performers") + `
|
|
|
|
LEFT JOIN performer_stash_ids on performer_stash_ids.performer_id = performers.id
|
|
|
|
`
|
|
|
|
|
|
|
|
if hasStashID {
|
|
|
|
query += `
|
|
|
|
WHERE performer_stash_ids.stash_id IS NOT NULL
|
|
|
|
AND performer_stash_ids.endpoint = ?
|
|
|
|
`
|
|
|
|
} else {
|
|
|
|
query += `
|
|
|
|
WHERE performer_stash_ids.stash_id IS NULL
|
|
|
|
`
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []interface{}{stashboxEndpoint}
|
|
|
|
return qb.queryPerformers(query, args)
|
|
|
|
}
|