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-07-19 01:59:18 +00:00
|
|
|
"fmt"
|
2019-10-12 11:32:01 +00:00
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
"github.com/stashapp/stash/pkg/models"
|
2019-02-09 12:30:49 +00:00
|
|
|
)
|
|
|
|
|
2020-06-21 11:43:57 +00:00
|
|
|
const galleryTable = "galleries"
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
const performersGalleriesTable = "performers_galleries"
|
|
|
|
const galleriesTagsTable = "galleries_tags"
|
|
|
|
const galleriesImagesTable = "galleries_images"
|
2021-02-01 20:56:54 +00:00
|
|
|
const galleriesScenesTable = "scenes_galleries"
|
2021-01-18 01:23:20 +00:00
|
|
|
const galleryIDColumn = "gallery_id"
|
2019-02-09 12:30:49 +00:00
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
type galleryQueryBuilder struct {
|
|
|
|
repository
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func NewGalleryReaderWriter(tx dbi) *galleryQueryBuilder {
|
|
|
|
return &galleryQueryBuilder{
|
|
|
|
repository{
|
|
|
|
tx: tx,
|
|
|
|
tableName: galleryTable,
|
|
|
|
idColumn: idColumn,
|
|
|
|
},
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *galleryQueryBuilder) Create(newObject models.Gallery) (*models.Gallery, error) {
|
|
|
|
var ret models.Gallery
|
|
|
|
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 *galleryQueryBuilder) Update(updatedObject models.Gallery) (*models.Gallery, error) {
|
|
|
|
const partial = false
|
|
|
|
if err := qb.update(updatedObject.ID, updatedObject, partial); err != nil {
|
2020-10-12 23:12:46 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
return qb.Find(updatedObject.ID)
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *galleryQueryBuilder) UpdatePartial(updatedObject models.GalleryPartial) (*models.Gallery, error) {
|
|
|
|
const partial = true
|
|
|
|
if err := qb.update(updatedObject.ID, updatedObject, partial); err != nil {
|
|
|
|
return nil, err
|
2020-11-04 23:26:51 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
return qb.Find(updatedObject.ID)
|
2020-11-04 23:26:51 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *galleryQueryBuilder) UpdateChecksum(id int, checksum string) error {
|
|
|
|
return qb.updateMap(id, map[string]interface{}{
|
|
|
|
"checksum": checksum,
|
|
|
|
})
|
|
|
|
}
|
2020-11-04 23:26:51 +00:00
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *galleryQueryBuilder) UpdateFileModTime(id int, modTime models.NullSQLiteTimestamp) error {
|
|
|
|
return qb.updateMap(id, map[string]interface{}{
|
|
|
|
"file_mod_time": modTime,
|
|
|
|
})
|
2020-11-04 23:26:51 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *galleryQueryBuilder) Destroy(id int) error {
|
|
|
|
return qb.destroyExisting([]int{id})
|
2020-04-24 23:32:55 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *galleryQueryBuilder) Find(id int) (*models.Gallery, error) {
|
|
|
|
var ret models.Gallery
|
|
|
|
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
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &ret, nil
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *galleryQueryBuilder) FindMany(ids []int) ([]*models.Gallery, error) {
|
|
|
|
var galleries []*models.Gallery
|
2020-07-19 01:59:18 +00:00
|
|
|
for _, id := range ids {
|
2021-01-18 01:23:20 +00:00
|
|
|
gallery, err := qb.Find(id)
|
2020-07-19 01:59:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if gallery == nil {
|
|
|
|
return nil, fmt.Errorf("gallery with id %d not found", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
galleries = append(galleries, gallery)
|
|
|
|
}
|
|
|
|
|
|
|
|
return galleries, nil
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *galleryQueryBuilder) FindByChecksum(checksum string) (*models.Gallery, error) {
|
2019-02-09 12:30:49 +00:00
|
|
|
query := "SELECT * FROM galleries WHERE checksum = ? LIMIT 1"
|
|
|
|
args := []interface{}{checksum}
|
2021-01-18 01:23:20 +00:00
|
|
|
return qb.queryGallery(query, args)
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 20:56:54 +00:00
|
|
|
func (qb *galleryQueryBuilder) FindByChecksums(checksums []string) ([]*models.Gallery, error) {
|
|
|
|
query := "SELECT * FROM galleries WHERE checksum IN " + getInBinding(len(checksums))
|
|
|
|
var args []interface{}
|
|
|
|
for _, checksum := range checksums {
|
|
|
|
args = append(args, checksum)
|
|
|
|
}
|
|
|
|
return qb.queryGalleries(query, args)
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *galleryQueryBuilder) FindByPath(path string) (*models.Gallery, error) {
|
2019-02-09 12:30:49 +00:00
|
|
|
query := "SELECT * FROM galleries WHERE path = ? LIMIT 1"
|
|
|
|
args := []interface{}{path}
|
2021-01-18 01:23:20 +00:00
|
|
|
return qb.queryGallery(query, args)
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 20:56:54 +00:00
|
|
|
func (qb *galleryQueryBuilder) FindBySceneID(sceneID int) ([]*models.Gallery, error) {
|
|
|
|
query := selectAll(galleryTable) + `
|
|
|
|
LEFT JOIN scenes_galleries as scenes_join on scenes_join.gallery_id = galleries.id
|
|
|
|
WHERE scenes_join.scene_id = ?
|
|
|
|
GROUP BY galleries.id
|
|
|
|
`
|
2019-02-09 12:30:49 +00:00
|
|
|
args := []interface{}{sceneID}
|
2021-02-01 20:56:54 +00:00
|
|
|
return qb.queryGalleries(query, args)
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *galleryQueryBuilder) FindByImageID(imageID int) ([]*models.Gallery, error) {
|
2020-10-12 23:12:46 +00:00
|
|
|
query := selectAll(galleryTable) + `
|
|
|
|
LEFT JOIN galleries_images as images_join on images_join.gallery_id = galleries.id
|
|
|
|
WHERE images_join.image_id = ?
|
|
|
|
GROUP BY galleries.id
|
|
|
|
`
|
|
|
|
args := []interface{}{imageID}
|
2021-01-18 01:23:20 +00:00
|
|
|
return qb.queryGalleries(query, args)
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *galleryQueryBuilder) CountByImageID(imageID int) (int, error) {
|
2020-10-12 23:12:46 +00:00
|
|
|
query := `SELECT image_id FROM galleries_images
|
|
|
|
WHERE image_id = ?
|
|
|
|
GROUP BY gallery_id`
|
|
|
|
args := []interface{}{imageID}
|
2021-01-18 01:23:20 +00:00
|
|
|
return qb.runCountQuery(qb.buildCountQuery(query), args)
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *galleryQueryBuilder) Count() (int, error) {
|
|
|
|
return qb.runCountQuery(qb.buildCountQuery("SELECT galleries.id FROM galleries"), nil)
|
2019-02-11 20:36:10 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *galleryQueryBuilder) All() ([]*models.Gallery, error) {
|
|
|
|
return qb.queryGalleries(selectAll("galleries")+qb.getGallerySort(nil), nil)
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 03:09:46 +00:00
|
|
|
func (qb *galleryQueryBuilder) validateFilter(galleryFilter *models.GalleryFilterType) error {
|
|
|
|
const and = "AND"
|
|
|
|
const or = "OR"
|
|
|
|
const not = "NOT"
|
|
|
|
|
|
|
|
if galleryFilter.And != nil {
|
|
|
|
if galleryFilter.Or != nil {
|
|
|
|
return illegalFilterCombination(and, or)
|
|
|
|
}
|
|
|
|
if galleryFilter.Not != nil {
|
|
|
|
return illegalFilterCombination(and, not)
|
|
|
|
}
|
2019-02-09 12:30:49 +00:00
|
|
|
|
2021-05-03 03:09:46 +00:00
|
|
|
return qb.validateFilter(galleryFilter.And)
|
2020-06-21 11:43:57 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 03:09:46 +00:00
|
|
|
if galleryFilter.Or != nil {
|
|
|
|
if galleryFilter.Not != nil {
|
|
|
|
return illegalFilterCombination(or, not)
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 03:09:46 +00:00
|
|
|
return qb.validateFilter(galleryFilter.Or)
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 03:09:46 +00:00
|
|
|
if galleryFilter.Not != nil {
|
|
|
|
return qb.validateFilter(galleryFilter.Not)
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 03:09:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-04-09 08:46:00 +00:00
|
|
|
|
2021-05-03 03:09:46 +00:00
|
|
|
func (qb *galleryQueryBuilder) makeFilter(galleryFilter *models.GalleryFilterType) *filterBuilder {
|
|
|
|
query := &filterBuilder{}
|
2021-04-09 08:46:00 +00:00
|
|
|
|
2021-05-03 03:09:46 +00:00
|
|
|
if galleryFilter.And != nil {
|
|
|
|
query.and(qb.makeFilter(galleryFilter.And))
|
|
|
|
}
|
|
|
|
if galleryFilter.Or != nil {
|
|
|
|
query.or(qb.makeFilter(galleryFilter.Or))
|
|
|
|
}
|
|
|
|
if galleryFilter.Not != nil {
|
|
|
|
query.not(qb.makeFilter(galleryFilter.Not))
|
2021-04-09 08:46:00 +00:00
|
|
|
}
|
|
|
|
|
2021-06-22 23:10:20 +00:00
|
|
|
query.handleCriterion(stringCriterionHandler(galleryFilter.Title, "galleries.title"))
|
|
|
|
query.handleCriterion(stringCriterionHandler(galleryFilter.Details, "galleries.details"))
|
|
|
|
query.handleCriterion(stringCriterionHandler(galleryFilter.Checksum, "galleries.checksum"))
|
2021-06-21 05:48:28 +00:00
|
|
|
query.handleCriterion(boolCriterionHandler(galleryFilter.IsZip, "galleries.zip"))
|
|
|
|
query.handleCriterion(stringCriterionHandler(galleryFilter.Path, "galleries.path"))
|
|
|
|
query.handleCriterion(intCriterionHandler(galleryFilter.Rating, "galleries.rating"))
|
|
|
|
query.handleCriterion(stringCriterionHandler(galleryFilter.URL, "galleries.url"))
|
|
|
|
query.handleCriterion(boolCriterionHandler(galleryFilter.Organized, "galleries.organized"))
|
|
|
|
query.handleCriterion(galleryIsMissingCriterionHandler(qb, galleryFilter.IsMissing))
|
|
|
|
query.handleCriterion(galleryTagsCriterionHandler(qb, galleryFilter.Tags))
|
|
|
|
query.handleCriterion(galleryTagCountCriterionHandler(qb, galleryFilter.TagCount))
|
|
|
|
query.handleCriterion(galleryPerformersCriterionHandler(qb, galleryFilter.Performers))
|
|
|
|
query.handleCriterion(galleryPerformerCountCriterionHandler(qb, galleryFilter.PerformerCount))
|
|
|
|
query.handleCriterion(galleryStudioCriterionHandler(qb, galleryFilter.Studios))
|
|
|
|
query.handleCriterion(galleryPerformerTagsCriterionHandler(qb, galleryFilter.PerformerTags))
|
|
|
|
query.handleCriterion(galleryAverageResolutionCriterionHandler(qb, galleryFilter.AverageResolution))
|
|
|
|
query.handleCriterion(galleryImageCountCriterionHandler(qb, galleryFilter.ImageCount))
|
2020-10-12 23:12:46 +00:00
|
|
|
|
2021-05-03 03:09:46 +00:00
|
|
|
return query
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *galleryQueryBuilder) makeQuery(galleryFilter *models.GalleryFilterType, findFilter *models.FindFilterType) (*queryBuilder, error) {
|
|
|
|
if galleryFilter == nil {
|
|
|
|
galleryFilter = &models.GalleryFilterType{}
|
|
|
|
}
|
|
|
|
if findFilter == nil {
|
|
|
|
findFilter = &models.FindFilterType{}
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 03:09:46 +00:00
|
|
|
query := qb.newQuery()
|
2021-04-09 08:46:00 +00:00
|
|
|
|
2021-05-03 03:09:46 +00:00
|
|
|
query.body = selectDistinctIDs(galleryTable)
|
2021-04-09 08:46:00 +00:00
|
|
|
|
2021-05-03 03:09:46 +00:00
|
|
|
if q := findFilter.Q; q != nil && *q != "" {
|
|
|
|
searchColumns := []string{"galleries.title", "galleries.path", "galleries.checksum"}
|
|
|
|
clause, thisArgs := getSearchBinding(searchColumns, *q, false)
|
2021-04-09 08:46:00 +00:00
|
|
|
query.addWhere(clause)
|
2021-05-03 03:09:46 +00:00
|
|
|
query.addArg(thisArgs...)
|
2021-04-09 08:46:00 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 03:09:46 +00:00
|
|
|
if err := qb.validateFilter(galleryFilter); err != nil {
|
|
|
|
return nil, err
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
2021-05-03 03:09:46 +00:00
|
|
|
filter := qb.makeFilter(galleryFilter)
|
2019-02-09 12:30:49 +00:00
|
|
|
|
2021-05-03 03:09:46 +00:00
|
|
|
query.addFilter(filter)
|
2021-03-10 01:25:51 +00:00
|
|
|
|
2020-06-21 11:43:57 +00:00
|
|
|
query.sortAndPagination = qb.getGallerySort(findFilter) + getPagination(findFilter)
|
2021-04-15 00:46:31 +00:00
|
|
|
|
2021-05-03 03:09:46 +00:00
|
|
|
return &query, nil
|
2021-04-15 00:46:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *galleryQueryBuilder) Query(galleryFilter *models.GalleryFilterType, findFilter *models.FindFilterType) ([]*models.Gallery, int, error) {
|
2021-05-03 03:09:46 +00:00
|
|
|
query, err := qb.makeQuery(galleryFilter, findFilter)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
2021-04-15 00:46:31 +00:00
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
idsResult, countResult, err := query.executeFind()
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
2019-02-09 12:30:49 +00:00
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
var galleries []*models.Gallery
|
2019-02-09 12:30:49 +00:00
|
|
|
for _, id := range idsResult {
|
2021-01-18 01:23:20 +00:00
|
|
|
gallery, err := qb.Find(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
2019-05-27 19:34:26 +00:00
|
|
|
galleries = append(galleries, gallery)
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
return galleries, countResult, nil
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2021-04-15 00:46:31 +00:00
|
|
|
func (qb *galleryQueryBuilder) QueryCount(galleryFilter *models.GalleryFilterType, findFilter *models.FindFilterType) (int, error) {
|
2021-05-03 03:09:46 +00:00
|
|
|
query, err := qb.makeQuery(galleryFilter, findFilter)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2021-04-15 00:46:31 +00:00
|
|
|
|
|
|
|
return query.executeCount()
|
|
|
|
}
|
|
|
|
|
2021-05-03 03:09:46 +00:00
|
|
|
func galleryIsMissingCriterionHandler(qb *galleryQueryBuilder, isMissing *string) criterionHandlerFunc {
|
|
|
|
return func(f *filterBuilder) {
|
|
|
|
if isMissing != nil && *isMissing != "" {
|
|
|
|
switch *isMissing {
|
|
|
|
case "scenes":
|
|
|
|
f.addJoin("scenes_galleries", "scenes_join", "scenes_join.gallery_id = galleries.id")
|
|
|
|
f.addWhere("scenes_join.gallery_id IS NULL")
|
|
|
|
case "studio":
|
|
|
|
f.addWhere("galleries.studio_id IS NULL")
|
|
|
|
case "performers":
|
|
|
|
qb.performersRepository().join(f, "performers_join", "galleries.id")
|
|
|
|
f.addWhere("performers_join.gallery_id IS NULL")
|
|
|
|
case "date":
|
|
|
|
f.addWhere("galleries.date IS \"\" OR galleries.date IS \"0001-01-01\"")
|
|
|
|
case "tags":
|
|
|
|
qb.tagsRepository().join(f, "tags_join", "galleries.id")
|
|
|
|
f.addWhere("tags_join.gallery_id IS NULL")
|
|
|
|
default:
|
|
|
|
f.addWhere("(galleries." + *isMissing + " IS NULL OR TRIM(galleries." + *isMissing + ") = '')")
|
|
|
|
}
|
2020-10-19 23:11:15 +00:00
|
|
|
}
|
2021-05-03 03:09:46 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-19 23:11:15 +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 galleryTagsCriterionHandler(qb *galleryQueryBuilder, tags *models.HierarchicalMultiCriterionInput) criterionHandlerFunc {
|
|
|
|
h := joinedHierarchicalMultiCriterionHandlerBuilder{
|
|
|
|
tx: qb.tx,
|
|
|
|
|
2021-05-09 09:25:57 +00:00
|
|
|
primaryTable: galleryTable,
|
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",
|
2021-05-09 09:25:57 +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: galleriesTagsTable,
|
|
|
|
primaryFK: galleryIDColumn,
|
2021-05-03 03:09:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return h.handler(tags)
|
|
|
|
}
|
|
|
|
|
|
|
|
func galleryTagCountCriterionHandler(qb *galleryQueryBuilder, tagCount *models.IntCriterionInput) criterionHandlerFunc {
|
|
|
|
h := countCriterionHandlerBuilder{
|
|
|
|
primaryTable: galleryTable,
|
|
|
|
joinTable: galleriesTagsTable,
|
|
|
|
primaryFK: galleryIDColumn,
|
|
|
|
}
|
|
|
|
|
|
|
|
return h.handler(tagCount)
|
|
|
|
}
|
|
|
|
|
|
|
|
func galleryPerformersCriterionHandler(qb *galleryQueryBuilder, performers *models.MultiCriterionInput) criterionHandlerFunc {
|
2021-05-09 09:25:57 +00:00
|
|
|
h := joinedMultiCriterionHandlerBuilder{
|
|
|
|
primaryTable: galleryTable,
|
|
|
|
joinTable: performersGalleriesTable,
|
|
|
|
joinAs: "performers_join",
|
|
|
|
primaryFK: galleryIDColumn,
|
|
|
|
foreignFK: performerIDColumn,
|
|
|
|
|
|
|
|
addJoinTable: func(f *filterBuilder) {
|
|
|
|
qb.performersRepository().join(f, "performers_join", "galleries.id")
|
|
|
|
},
|
2021-05-03 03:09:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return h.handler(performers)
|
|
|
|
}
|
|
|
|
|
|
|
|
func galleryPerformerCountCriterionHandler(qb *galleryQueryBuilder, performerCount *models.IntCriterionInput) criterionHandlerFunc {
|
|
|
|
h := countCriterionHandlerBuilder{
|
|
|
|
primaryTable: galleryTable,
|
|
|
|
joinTable: performersGalleriesTable,
|
|
|
|
primaryFK: galleryIDColumn,
|
|
|
|
}
|
|
|
|
|
|
|
|
return h.handler(performerCount)
|
|
|
|
}
|
|
|
|
|
|
|
|
func galleryImageCountCriterionHandler(qb *galleryQueryBuilder, imageCount *models.IntCriterionInput) criterionHandlerFunc {
|
|
|
|
h := countCriterionHandlerBuilder{
|
|
|
|
primaryTable: galleryTable,
|
|
|
|
joinTable: galleriesImagesTable,
|
|
|
|
primaryFK: galleryIDColumn,
|
|
|
|
}
|
|
|
|
|
|
|
|
return h.handler(imageCount)
|
|
|
|
}
|
|
|
|
|
2021-06-03 10:52:19 +00:00
|
|
|
func galleryStudioCriterionHandler(qb *galleryQueryBuilder, studios *models.HierarchicalMultiCriterionInput) criterionHandlerFunc {
|
|
|
|
h := hierarchicalMultiCriterionHandlerBuilder{
|
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
|
|
|
tx: qb.tx,
|
|
|
|
|
2021-06-03 10:52:19 +00:00
|
|
|
primaryTable: galleryTable,
|
|
|
|
foreignTable: studioTable,
|
|
|
|
foreignFK: studioIDColumn,
|
|
|
|
derivedTable: "studio",
|
|
|
|
parentFK: "parent_id",
|
2021-05-03 03:09:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return h.handler(studios)
|
|
|
|
}
|
|
|
|
|
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 galleryPerformerTagsCriterionHandler(qb *galleryQueryBuilder, tags *models.HierarchicalMultiCriterionInput) criterionHandlerFunc {
|
2021-05-03 03:09:46 +00:00
|
|
|
return func(f *filterBuilder) {
|
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
|
|
|
if tags != nil && len(tags.Value) > 0 {
|
|
|
|
valuesClause := getHierarchicalValues(qb.tx, tags.Value, tagTable, "tags_relations", "", tags.Depth)
|
2021-05-03 03:09:46 +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
|
|
|
f.addWith(`performer_tags AS (
|
|
|
|
SELECT pg.gallery_id, t.column1 AS root_tag_id FROM performers_galleries pg
|
|
|
|
INNER JOIN performers_tags pt ON pt.performer_id = pg.performer_id
|
|
|
|
INNER JOIN (` + valuesClause + `) t ON t.column2 = pt.tag_id
|
|
|
|
)`)
|
2020-10-19 23:11:15 +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
|
|
|
f.addJoin("performer_tags", "", "performer_tags.gallery_id = galleries.id")
|
|
|
|
|
|
|
|
addHierarchicalConditionClauses(f, tags, "performer_tags", "root_tag_id")
|
2020-10-19 23:11:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-02 03:22:39 +00:00
|
|
|
func galleryAverageResolutionCriterionHandler(qb *galleryQueryBuilder, resolution *models.ResolutionCriterionInput) criterionHandlerFunc {
|
2021-05-03 03:09:46 +00:00
|
|
|
return func(f *filterBuilder) {
|
2021-08-02 03:22:39 +00:00
|
|
|
if resolution != nil && resolution.Value.IsValid() {
|
2021-05-03 03:09:46 +00:00
|
|
|
qb.imagesRepository().join(f, "images_join", "galleries.id")
|
|
|
|
f.addJoin("images", "", "images_join.image_id = images.id")
|
2021-03-10 01:25:51 +00:00
|
|
|
|
2021-08-02 03:22:39 +00:00
|
|
|
min := resolution.Value.GetMinResolution()
|
|
|
|
max := resolution.Value.GetMaxResolution()
|
2021-05-03 03:09:46 +00:00
|
|
|
|
|
|
|
const widthHeight = "avg(MIN(images.width, images.height))"
|
|
|
|
|
2021-08-02 03:22:39 +00:00
|
|
|
if resolution.Modifier == models.CriterionModifierEquals {
|
|
|
|
f.addHaving(fmt.Sprintf("%s BETWEEN %d AND %d", widthHeight, min, max))
|
|
|
|
} else if resolution.Modifier == models.CriterionModifierNotEquals {
|
|
|
|
f.addHaving(fmt.Sprintf("%s NOT BETWEEN %d AND %d", widthHeight, min, max))
|
|
|
|
} else if resolution.Modifier == models.CriterionModifierLessThan {
|
|
|
|
f.addHaving(fmt.Sprintf("%s < %d", widthHeight, min))
|
|
|
|
} else if resolution.Modifier == models.CriterionModifierGreaterThan {
|
|
|
|
f.addHaving(fmt.Sprintf("%s > %d", widthHeight, max))
|
2021-05-03 03:09:46 +00:00
|
|
|
}
|
2021-03-10 01:25:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *galleryQueryBuilder) getGallerySort(findFilter *models.FindFilterType) string {
|
2019-02-09 12:30:49 +00:00
|
|
|
var sort string
|
|
|
|
var direction string
|
2020-04-03 03:40:37 +00:00
|
|
|
if findFilter == nil {
|
|
|
|
sort = "path"
|
|
|
|
direction = "ASC"
|
|
|
|
} else {
|
|
|
|
sort = findFilter.GetSort("path")
|
|
|
|
direction = findFilter.GetDirection()
|
|
|
|
}
|
2021-04-09 08:46:00 +00:00
|
|
|
|
|
|
|
switch sort {
|
2021-05-03 03:09:46 +00:00
|
|
|
case "images_count":
|
|
|
|
return getCountSort(galleryTable, galleriesImagesTable, galleryIDColumn, direction)
|
2021-04-09 08:46:00 +00:00
|
|
|
case "tag_count":
|
|
|
|
return getCountSort(galleryTable, galleriesTagsTable, galleryIDColumn, direction)
|
|
|
|
case "performer_count":
|
|
|
|
return getCountSort(galleryTable, performersGalleriesTable, galleryIDColumn, direction)
|
|
|
|
default:
|
|
|
|
return getSort(sort, direction, "galleries")
|
|
|
|
}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *galleryQueryBuilder) queryGallery(query string, args []interface{}) (*models.Gallery, error) {
|
|
|
|
results, err := qb.queryGalleries(query, args)
|
2019-02-09 12:30:49 +00:00
|
|
|
if err != nil || len(results) < 1 {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-05-27 19:34:26 +00:00
|
|
|
return results[0], nil
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *galleryQueryBuilder) queryGalleries(query string, args []interface{}) ([]*models.Gallery, error) {
|
|
|
|
var ret models.Galleries
|
|
|
|
if err := qb.query(query, args, &ret); err != nil {
|
|
|
|
return nil, err
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
return []*models.Gallery(ret), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *galleryQueryBuilder) performersRepository() *joinRepository {
|
|
|
|
return &joinRepository{
|
|
|
|
repository: repository{
|
|
|
|
tx: qb.tx,
|
|
|
|
tableName: performersGalleriesTable,
|
|
|
|
idColumn: galleryIDColumn,
|
|
|
|
},
|
|
|
|
fkColumn: "performer_id",
|
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 *galleryQueryBuilder) GetPerformerIDs(galleryID int) ([]int, error) {
|
|
|
|
return qb.performersRepository().getIDs(galleryID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *galleryQueryBuilder) UpdatePerformers(galleryID int, performerIDs []int) error {
|
|
|
|
// Delete the existing joins and then create new ones
|
|
|
|
return qb.performersRepository().replace(galleryID, performerIDs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *galleryQueryBuilder) tagsRepository() *joinRepository {
|
|
|
|
return &joinRepository{
|
|
|
|
repository: repository{
|
|
|
|
tx: qb.tx,
|
|
|
|
tableName: galleriesTagsTable,
|
|
|
|
idColumn: galleryIDColumn,
|
|
|
|
},
|
|
|
|
fkColumn: "tag_id",
|
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 *galleryQueryBuilder) GetTagIDs(galleryID int) ([]int, error) {
|
|
|
|
return qb.tagsRepository().getIDs(galleryID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *galleryQueryBuilder) UpdateTags(galleryID int, tagIDs []int) error {
|
|
|
|
// Delete the existing joins and then create new ones
|
|
|
|
return qb.tagsRepository().replace(galleryID, tagIDs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *galleryQueryBuilder) imagesRepository() *joinRepository {
|
|
|
|
return &joinRepository{
|
|
|
|
repository: repository{
|
|
|
|
tx: qb.tx,
|
|
|
|
tableName: galleriesImagesTable,
|
|
|
|
idColumn: galleryIDColumn,
|
|
|
|
},
|
|
|
|
fkColumn: "image_id",
|
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 *galleryQueryBuilder) GetImageIDs(galleryID int) ([]int, error) {
|
|
|
|
return qb.imagesRepository().getIDs(galleryID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *galleryQueryBuilder) UpdateImages(galleryID int, imageIDs []int) error {
|
|
|
|
// Delete the existing joins and then create new ones
|
|
|
|
return qb.imagesRepository().replace(galleryID, imageIDs)
|
2019-02-14 22:53:32 +00:00
|
|
|
}
|
2021-02-01 20:56:54 +00:00
|
|
|
|
|
|
|
func (qb *galleryQueryBuilder) scenesRepository() *joinRepository {
|
|
|
|
return &joinRepository{
|
|
|
|
repository: repository{
|
|
|
|
tx: qb.tx,
|
|
|
|
tableName: galleriesScenesTable,
|
|
|
|
idColumn: galleryIDColumn,
|
|
|
|
},
|
|
|
|
fkColumn: sceneIDColumn,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *galleryQueryBuilder) GetSceneIDs(galleryID int) ([]int, error) {
|
|
|
|
return qb.scenesRepository().getIDs(galleryID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *galleryQueryBuilder) UpdateScenes(galleryID int, sceneIDs []int) error {
|
|
|
|
// Delete the existing joins and then create new ones
|
|
|
|
return qb.scenesRepository().replace(galleryID, sceneIDs)
|
|
|
|
}
|