2021-01-18 01:23:20 +00:00
|
|
|
package sqlite
|
2019-02-09 12:30:49 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2019-11-07 12:49:08 +00:00
|
|
|
"errors"
|
2020-09-15 07:28:53 +00:00
|
|
|
"fmt"
|
2021-04-26 02:51:31 +00:00
|
|
|
"strings"
|
2019-11-07 12:49:08 +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-07-07 00:35:43 +00:00
|
|
|
const tagTable = "tags"
|
2021-01-18 01:23:20 +00:00
|
|
|
const tagIDColumn = "tag_id"
|
2021-05-26 04:36:05 +00:00
|
|
|
const tagAliasesTable = "tag_aliases"
|
|
|
|
const tagAliasColumn = "alias"
|
2020-07-07 00:35:43 +00:00
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
type tagQueryBuilder struct {
|
|
|
|
repository
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func NewTagReaderWriter(tx dbi) *tagQueryBuilder {
|
|
|
|
return &tagQueryBuilder{
|
|
|
|
repository{
|
|
|
|
tx: tx,
|
|
|
|
tableName: tagTable,
|
|
|
|
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 *tagQueryBuilder) Create(newObject models.Tag) (*models.Tag, error) {
|
|
|
|
var ret models.Tag
|
|
|
|
if err := qb.insertObject(newObject, &ret); err != nil {
|
2019-02-09 12:30:49 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-19 02:03:51 +00:00
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
return &ret, nil
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2021-05-26 04:36:05 +00:00
|
|
|
func (qb *tagQueryBuilder) Update(updatedObject models.TagPartial) (*models.Tag, error) {
|
|
|
|
const partial = true
|
|
|
|
if err := qb.update(updatedObject.ID, updatedObject, partial); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return qb.Find(updatedObject.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *tagQueryBuilder) UpdateFull(updatedObject models.Tag) (*models.Tag, error) {
|
2021-01-18 01:23:20 +00:00
|
|
|
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
|
|
|
return qb.Find(updatedObject.ID)
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *tagQueryBuilder) Destroy(id int) error {
|
|
|
|
// TODO - add delete cascade to foreign key
|
2019-08-12 23:03:16 +00:00
|
|
|
// delete tag from scenes and markers first
|
2021-01-18 01:23:20 +00:00
|
|
|
_, err := qb.tx.Exec("DELETE FROM scenes_tags WHERE tag_id = ?", id)
|
2019-08-12 23:03:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
// TODO - add delete cascade to foreign key
|
|
|
|
_, err = qb.tx.Exec("DELETE FROM scene_markers_tags WHERE tag_id = ?", id)
|
2019-08-12 23:03:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-17 00:56:08 +00:00
|
|
|
// cannot unset primary_tag_id in scene_markers because it is not nullable
|
|
|
|
countQuery := "SELECT COUNT(*) as count FROM scene_markers where primary_tag_id = ?"
|
|
|
|
args := []interface{}{id}
|
2021-01-18 01:23:20 +00:00
|
|
|
primaryMarkers, err := qb.runCountQuery(countQuery, args)
|
2019-10-17 00:56:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if primaryMarkers > 0 {
|
2021-09-08 01:23:10 +00:00
|
|
|
return errors.New("cannot delete tag used as a primary tag in scene markers")
|
2019-10-17 00:56:08 +00:00
|
|
|
}
|
2019-08-12 23:03:16 +00:00
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
return qb.destroyExisting([]int{id})
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *tagQueryBuilder) Find(id int) (*models.Tag, error) {
|
|
|
|
var ret models.Tag
|
|
|
|
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 *tagQueryBuilder) FindMany(ids []int) ([]*models.Tag, error) {
|
|
|
|
var tags []*models.Tag
|
2020-09-15 07:28:53 +00:00
|
|
|
for _, id := range ids {
|
2021-01-18 01:23:20 +00:00
|
|
|
tag, err := qb.Find(id)
|
2020-09-15 07:28:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if tag == nil {
|
|
|
|
return nil, fmt.Errorf("tag with id %d not found", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
tags = append(tags, tag)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tags, nil
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *tagQueryBuilder) FindBySceneID(sceneID int) ([]*models.Tag, error) {
|
2019-02-09 12:30:49 +00:00
|
|
|
query := `
|
|
|
|
SELECT tags.* FROM tags
|
|
|
|
LEFT JOIN scenes_tags as scenes_join on scenes_join.tag_id = tags.id
|
2020-04-24 02:52:21 +00:00
|
|
|
WHERE scenes_join.scene_id = ?
|
2019-02-09 12:30:49 +00:00
|
|
|
GROUP BY tags.id
|
|
|
|
`
|
2021-03-11 11:17:37 +00:00
|
|
|
query += qb.getDefaultTagSort()
|
2019-02-09 12:30:49 +00:00
|
|
|
args := []interface{}{sceneID}
|
2021-01-18 01:23:20 +00:00
|
|
|
return qb.queryTags(query, args)
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 01:25:51 +00:00
|
|
|
func (qb *tagQueryBuilder) FindByPerformerID(performerID int) ([]*models.Tag, error) {
|
|
|
|
query := `
|
|
|
|
SELECT tags.* FROM tags
|
|
|
|
LEFT JOIN performers_tags as performers_join on performers_join.tag_id = tags.id
|
|
|
|
WHERE performers_join.performer_id = ?
|
|
|
|
GROUP BY tags.id
|
|
|
|
`
|
2021-03-11 11:17:37 +00:00
|
|
|
query += qb.getDefaultTagSort()
|
2021-03-10 01:25:51 +00:00
|
|
|
args := []interface{}{performerID}
|
|
|
|
return qb.queryTags(query, args)
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *tagQueryBuilder) FindByImageID(imageID int) ([]*models.Tag, error) {
|
2020-10-12 23:12:46 +00:00
|
|
|
query := `
|
|
|
|
SELECT tags.* FROM tags
|
|
|
|
LEFT JOIN images_tags as images_join on images_join.tag_id = tags.id
|
|
|
|
WHERE images_join.image_id = ?
|
|
|
|
GROUP BY tags.id
|
|
|
|
`
|
2021-03-11 11:17:37 +00:00
|
|
|
query += qb.getDefaultTagSort()
|
2020-10-12 23:12:46 +00:00
|
|
|
args := []interface{}{imageID}
|
2021-01-18 01:23:20 +00:00
|
|
|
return qb.queryTags(query, args)
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *tagQueryBuilder) FindByGalleryID(galleryID int) ([]*models.Tag, error) {
|
2020-10-12 23:12:46 +00:00
|
|
|
query := `
|
|
|
|
SELECT tags.* FROM tags
|
|
|
|
LEFT JOIN galleries_tags as galleries_join on galleries_join.tag_id = tags.id
|
|
|
|
WHERE galleries_join.gallery_id = ?
|
|
|
|
GROUP BY tags.id
|
|
|
|
`
|
2021-03-11 11:17:37 +00:00
|
|
|
query += qb.getDefaultTagSort()
|
2020-10-12 23:12:46 +00:00
|
|
|
args := []interface{}{galleryID}
|
2021-01-18 01:23:20 +00:00
|
|
|
return qb.queryTags(query, args)
|
2020-10-12 23:12:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *tagQueryBuilder) FindBySceneMarkerID(sceneMarkerID int) ([]*models.Tag, error) {
|
2019-02-09 12:30:49 +00:00
|
|
|
query := `
|
|
|
|
SELECT tags.* FROM tags
|
|
|
|
LEFT JOIN scene_markers_tags as scene_markers_join on scene_markers_join.tag_id = tags.id
|
2020-05-11 05:19:11 +00:00
|
|
|
WHERE scene_markers_join.scene_marker_id = ?
|
2019-02-09 12:30:49 +00:00
|
|
|
GROUP BY tags.id
|
|
|
|
`
|
2021-03-11 11:17:37 +00:00
|
|
|
query += qb.getDefaultTagSort()
|
2019-02-09 12:30:49 +00:00
|
|
|
args := []interface{}{sceneMarkerID}
|
2021-01-18 01:23:20 +00:00
|
|
|
return qb.queryTags(query, args)
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *tagQueryBuilder) FindByName(name string, nocase bool) (*models.Tag, error) {
|
2020-05-24 06:19:22 +00:00
|
|
|
query := "SELECT * FROM tags WHERE name = ?"
|
|
|
|
if nocase {
|
|
|
|
query += " COLLATE NOCASE"
|
|
|
|
}
|
|
|
|
query += " LIMIT 1"
|
2019-02-09 12:30:49 +00:00
|
|
|
args := []interface{}{name}
|
2021-01-18 01:23:20 +00:00
|
|
|
return qb.queryTag(query, args)
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *tagQueryBuilder) FindByNames(names []string, nocase bool) ([]*models.Tag, error) {
|
2020-05-24 06:19:22 +00:00
|
|
|
query := "SELECT * FROM tags 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.queryTags(query, args)
|
2019-02-09 12:30:49 +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 (qb *tagQueryBuilder) FindByParentTagID(parentID int) ([]*models.Tag, error) {
|
|
|
|
query := `
|
|
|
|
SELECT tags.* FROM tags
|
|
|
|
INNER JOIN tags_relations ON tags_relations.child_id = tags.id
|
|
|
|
WHERE tags_relations.parent_id = ?
|
|
|
|
`
|
|
|
|
query += qb.getDefaultTagSort()
|
|
|
|
args := []interface{}{parentID}
|
|
|
|
return qb.queryTags(query, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *tagQueryBuilder) FindByChildTagID(parentID int) ([]*models.Tag, error) {
|
|
|
|
query := `
|
|
|
|
SELECT tags.* FROM tags
|
|
|
|
INNER JOIN tags_relations ON tags_relations.parent_id = tags.id
|
|
|
|
WHERE tags_relations.child_id = ?
|
|
|
|
`
|
|
|
|
query += qb.getDefaultTagSort()
|
|
|
|
args := []interface{}{parentID}
|
|
|
|
return qb.queryTags(query, args)
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *tagQueryBuilder) Count() (int, error) {
|
|
|
|
return qb.runCountQuery(qb.buildCountQuery("SELECT tags.id FROM tags"), nil)
|
2019-02-11 20:36:10 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *tagQueryBuilder) All() ([]*models.Tag, error) {
|
2021-03-11 11:17:37 +00:00
|
|
|
return qb.queryTags(selectAll("tags")+qb.getDefaultTagSort(), nil)
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2021-04-26 02:51:31 +00:00
|
|
|
func (qb *tagQueryBuilder) QueryForAutoTag(words []string) ([]*models.Tag, error) {
|
|
|
|
// TODO - Query needs to be changed to support queries of this type, and
|
|
|
|
// this method should be removed
|
|
|
|
query := selectAll(tagTable)
|
2021-05-26 04:36:05 +00:00
|
|
|
query += " LEFT JOIN tag_aliases ON tag_aliases.tag_id = tags.id"
|
2021-04-26 02:51:31 +00:00
|
|
|
|
|
|
|
var whereClauses []string
|
|
|
|
var args []interface{}
|
|
|
|
|
|
|
|
for _, w := range words {
|
2021-06-08 00:47:22 +00:00
|
|
|
ww := w + "%"
|
2021-05-26 04:36:05 +00:00
|
|
|
whereClauses = append(whereClauses, "tags.name like ?")
|
|
|
|
args = append(args, ww)
|
|
|
|
|
|
|
|
// include aliases
|
|
|
|
whereClauses = append(whereClauses, "tag_aliases.alias like ?")
|
|
|
|
args = append(args, ww)
|
2021-04-26 02:51:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
where := strings.Join(whereClauses, " OR ")
|
|
|
|
return qb.queryTags(query+" WHERE "+where, args)
|
|
|
|
}
|
|
|
|
|
2021-03-11 11:17:37 +00:00
|
|
|
func (qb *tagQueryBuilder) validateFilter(tagFilter *models.TagFilterType) error {
|
|
|
|
const and = "AND"
|
|
|
|
const or = "OR"
|
|
|
|
const not = "NOT"
|
|
|
|
|
|
|
|
if tagFilter.And != nil {
|
|
|
|
if tagFilter.Or != nil {
|
|
|
|
return illegalFilterCombination(and, or)
|
|
|
|
}
|
|
|
|
if tagFilter.Not != nil {
|
|
|
|
return illegalFilterCombination(and, not)
|
|
|
|
}
|
|
|
|
|
|
|
|
return qb.validateFilter(tagFilter.And)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tagFilter.Or != nil {
|
|
|
|
if tagFilter.Not != nil {
|
|
|
|
return illegalFilterCombination(or, not)
|
|
|
|
}
|
|
|
|
|
|
|
|
return qb.validateFilter(tagFilter.Or)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tagFilter.Not != nil {
|
|
|
|
return qb.validateFilter(tagFilter.Not)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *tagQueryBuilder) makeFilter(tagFilter *models.TagFilterType) *filterBuilder {
|
|
|
|
query := &filterBuilder{}
|
|
|
|
|
|
|
|
if tagFilter.And != nil {
|
|
|
|
query.and(qb.makeFilter(tagFilter.And))
|
|
|
|
}
|
|
|
|
if tagFilter.Or != nil {
|
|
|
|
query.or(qb.makeFilter(tagFilter.Or))
|
|
|
|
}
|
|
|
|
if tagFilter.Not != nil {
|
|
|
|
query.not(qb.makeFilter(tagFilter.Not))
|
|
|
|
}
|
|
|
|
|
2021-06-21 05:48:28 +00:00
|
|
|
query.handleCriterion(stringCriterionHandler(tagFilter.Name, tagTable+".name"))
|
|
|
|
query.handleCriterion(tagAliasCriterionHandler(qb, tagFilter.Aliases))
|
|
|
|
|
|
|
|
query.handleCriterion(tagIsMissingCriterionHandler(qb, tagFilter.IsMissing))
|
|
|
|
query.handleCriterion(tagSceneCountCriterionHandler(qb, tagFilter.SceneCount))
|
|
|
|
query.handleCriterion(tagImageCountCriterionHandler(qb, tagFilter.ImageCount))
|
|
|
|
query.handleCriterion(tagGalleryCountCriterionHandler(qb, tagFilter.GalleryCount))
|
|
|
|
query.handleCriterion(tagPerformerCountCriterionHandler(qb, tagFilter.PerformerCount))
|
2021-08-04 03:39:24 +00:00
|
|
|
query.handleCriterion(tagMarkerCountCriterionHandler(qb, tagFilter.MarkerCount))
|
2021-10-01 01:50:06 +00:00
|
|
|
query.handleCriterion(tagParentsCriterionHandler(qb, tagFilter.Parents))
|
|
|
|
query.handleCriterion(tagChildrenCriterionHandler(qb, tagFilter.Children))
|
|
|
|
query.handleCriterion(tagParentCountCriterionHandler(qb, tagFilter.ParentCount))
|
|
|
|
query.handleCriterion(tagChildCountCriterionHandler(qb, tagFilter.ChildCount))
|
2021-03-11 11:17:37 +00:00
|
|
|
|
|
|
|
return query
|
2020-04-19 02:03:51 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *tagQueryBuilder) Query(tagFilter *models.TagFilterType, findFilter *models.FindFilterType) ([]*models.Tag, int, error) {
|
2020-07-07 00:35:43 +00:00
|
|
|
if tagFilter == nil {
|
2021-01-18 01:23:20 +00:00
|
|
|
tagFilter = &models.TagFilterType{}
|
2020-07-07 00:35:43 +00:00
|
|
|
}
|
2019-11-07 12:49:08 +00:00
|
|
|
if findFilter == nil {
|
2021-01-18 01:23:20 +00:00
|
|
|
findFilter = &models.FindFilterType{}
|
2019-11-07 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
query := qb.newQuery()
|
2021-10-25 00:40:13 +00:00
|
|
|
distinctIDs(&query, tagTable)
|
2020-07-08 22:42:07 +00:00
|
|
|
|
2019-11-07 12:49:08 +00:00
|
|
|
if q := findFilter.Q; q != nil && *q != "" {
|
2021-05-26 04:36:05 +00:00
|
|
|
query.join(tagAliasesTable, "", "tag_aliases.tag_id = tags.id")
|
|
|
|
searchColumns := []string{"tags.name", "tag_aliases.alias"}
|
2021-11-22 03:59:22 +00:00
|
|
|
query.parseQueryString(searchColumns, *q)
|
2020-07-07 00:35:43 +00:00
|
|
|
}
|
|
|
|
|
2021-03-11 11:17:37 +00:00
|
|
|
if err := qb.validateFilter(tagFilter); err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
filter := qb.makeFilter(tagFilter)
|
|
|
|
|
|
|
|
query.addFilter(filter)
|
|
|
|
|
|
|
|
query.sortAndPagination = qb.getTagSort(&query, findFilter) + getPagination(findFilter)
|
|
|
|
idsResult, countResult, err := query.executeFind()
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
2019-11-07 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
2021-03-11 11:17:37 +00:00
|
|
|
var tags []*models.Tag
|
|
|
|
for _, id := range idsResult {
|
|
|
|
tag, err := qb.Find(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
2020-07-07 00:35:43 +00:00
|
|
|
}
|
2021-03-11 11:17:37 +00:00
|
|
|
tags = append(tags, tag)
|
2020-07-07 00:35:43 +00:00
|
|
|
}
|
|
|
|
|
2021-03-11 11:17:37 +00:00
|
|
|
return tags, countResult, nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 04:36:05 +00:00
|
|
|
func tagAliasCriterionHandler(qb *tagQueryBuilder, alias *models.StringCriterionInput) criterionHandlerFunc {
|
|
|
|
h := stringListCriterionHandlerBuilder{
|
|
|
|
joinTable: tagAliasesTable,
|
|
|
|
stringColumn: tagAliasColumn,
|
|
|
|
addJoinTable: func(f *filterBuilder) {
|
|
|
|
qb.aliasRepository().join(f, "", "tags.id")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return h.handler(alias)
|
|
|
|
}
|
|
|
|
|
2021-03-11 11:17:37 +00:00
|
|
|
func tagIsMissingCriterionHandler(qb *tagQueryBuilder, isMissing *string) criterionHandlerFunc {
|
|
|
|
return func(f *filterBuilder) {
|
|
|
|
if isMissing != nil && *isMissing != "" {
|
|
|
|
switch *isMissing {
|
|
|
|
case "image":
|
|
|
|
qb.imageRepository().join(f, "", "tags.id")
|
|
|
|
f.addWhere("tags_image.tag_id IS NULL")
|
|
|
|
default:
|
|
|
|
f.addWhere("(tags." + *isMissing + " IS NULL OR TRIM(tags." + *isMissing + ") = '')")
|
|
|
|
}
|
2021-03-10 01:25:51 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-11 11:17:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func tagSceneCountCriterionHandler(qb *tagQueryBuilder, sceneCount *models.IntCriterionInput) criterionHandlerFunc {
|
|
|
|
return func(f *filterBuilder) {
|
|
|
|
if sceneCount != nil {
|
2021-12-06 01:30:40 +00:00
|
|
|
f.addLeftJoin("scenes_tags", "", "scenes_tags.tag_id = tags.id")
|
2021-08-12 00:24:16 +00:00
|
|
|
clause, args := getIntCriterionWhereClause("count(distinct scenes_tags.scene_id)", *sceneCount)
|
2021-03-11 11:17:37 +00:00
|
|
|
|
|
|
|
f.addHaving(clause, args...)
|
2021-03-10 01:25:51 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-11 11:17:37 +00:00
|
|
|
}
|
2021-03-10 01:25:51 +00:00
|
|
|
|
2021-03-11 11:17:37 +00:00
|
|
|
func tagImageCountCriterionHandler(qb *tagQueryBuilder, imageCount *models.IntCriterionInput) criterionHandlerFunc {
|
|
|
|
return func(f *filterBuilder) {
|
|
|
|
if imageCount != nil {
|
2021-12-06 01:30:40 +00:00
|
|
|
f.addLeftJoin("images_tags", "", "images_tags.tag_id = tags.id")
|
2021-08-12 00:24:16 +00:00
|
|
|
clause, args := getIntCriterionWhereClause("count(distinct images_tags.image_id)", *imageCount)
|
2021-03-11 11:17:37 +00:00
|
|
|
|
|
|
|
f.addHaving(clause, args...)
|
2021-03-10 01:25:51 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-11 11:17:37 +00:00
|
|
|
}
|
2021-03-10 01:25:51 +00:00
|
|
|
|
2021-03-11 11:17:37 +00:00
|
|
|
func tagGalleryCountCriterionHandler(qb *tagQueryBuilder, galleryCount *models.IntCriterionInput) criterionHandlerFunc {
|
|
|
|
return func(f *filterBuilder) {
|
|
|
|
if galleryCount != nil {
|
2021-12-06 01:30:40 +00:00
|
|
|
f.addLeftJoin("galleries_tags", "", "galleries_tags.tag_id = tags.id")
|
2021-08-12 00:24:16 +00:00
|
|
|
clause, args := getIntCriterionWhereClause("count(distinct galleries_tags.gallery_id)", *galleryCount)
|
2021-03-11 11:17:37 +00:00
|
|
|
|
|
|
|
f.addHaving(clause, args...)
|
|
|
|
}
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
2021-03-11 11:17:37 +00:00
|
|
|
}
|
2019-11-07 12:49:08 +00:00
|
|
|
|
2021-03-11 11:17:37 +00:00
|
|
|
func tagPerformerCountCriterionHandler(qb *tagQueryBuilder, performerCount *models.IntCriterionInput) criterionHandlerFunc {
|
|
|
|
return func(f *filterBuilder) {
|
|
|
|
if performerCount != nil {
|
2021-12-06 01:30:40 +00:00
|
|
|
f.addLeftJoin("performers_tags", "", "performers_tags.tag_id = tags.id")
|
2021-08-12 00:24:16 +00:00
|
|
|
clause, args := getIntCriterionWhereClause("count(distinct performers_tags.performer_id)", *performerCount)
|
2021-03-11 11:17:37 +00:00
|
|
|
|
|
|
|
f.addHaving(clause, args...)
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
2019-11-07 12:49:08 +00:00
|
|
|
}
|
2021-03-11 11:17:37 +00:00
|
|
|
}
|
2019-11-07 12:49:08 +00:00
|
|
|
|
2021-08-04 03:39:24 +00:00
|
|
|
func tagMarkerCountCriterionHandler(qb *tagQueryBuilder, markerCount *models.IntCriterionInput) criterionHandlerFunc {
|
|
|
|
return func(f *filterBuilder) {
|
|
|
|
if markerCount != nil {
|
2021-12-06 01:30:40 +00:00
|
|
|
f.addLeftJoin("scene_markers_tags", "", "scene_markers_tags.tag_id = tags.id")
|
|
|
|
f.addLeftJoin("scene_markers", "", "scene_markers_tags.scene_marker_id = scene_markers.id OR scene_markers.primary_tag_id = tags.id")
|
2021-08-12 00:24:16 +00:00
|
|
|
clause, args := getIntCriterionWhereClause("count(distinct scene_markers.id)", *markerCount)
|
2021-08-04 03:39:24 +00:00
|
|
|
|
|
|
|
f.addHaving(clause, args...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-01 01:50:06 +00:00
|
|
|
func tagParentsCriterionHandler(qb *tagQueryBuilder, tags *models.HierarchicalMultiCriterionInput) criterionHandlerFunc {
|
|
|
|
return func(f *filterBuilder) {
|
2021-11-06 22:34:33 +00:00
|
|
|
if tags != nil {
|
|
|
|
if tags.Modifier == models.CriterionModifierIsNull || tags.Modifier == models.CriterionModifierNotNull {
|
|
|
|
var notClause string
|
|
|
|
if tags.Modifier == models.CriterionModifierNotNull {
|
|
|
|
notClause = "NOT"
|
|
|
|
}
|
|
|
|
|
2021-12-06 01:30:40 +00:00
|
|
|
f.addLeftJoin("tags_relations", "parent_relations", "tags.id = parent_relations.child_id")
|
2021-11-06 22:34:33 +00:00
|
|
|
|
|
|
|
f.addWhere(fmt.Sprintf("parent_relations.parent_id IS %s NULL", notClause))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(tags.Value) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-01 01:50:06 +00:00
|
|
|
var args []interface{}
|
|
|
|
for _, val := range tags.Value {
|
|
|
|
args = append(args, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
depthVal := 0
|
|
|
|
if tags.Depth != nil {
|
|
|
|
depthVal = *tags.Depth
|
|
|
|
}
|
|
|
|
|
|
|
|
var depthCondition string
|
|
|
|
if depthVal != -1 {
|
|
|
|
depthCondition = fmt.Sprintf("WHERE depth < %d", depthVal)
|
|
|
|
}
|
|
|
|
|
|
|
|
query := `parents AS (
|
|
|
|
SELECT parent_id AS root_id, child_id AS item_id, 0 AS depth FROM tags_relations WHERE parent_id IN` + getInBinding(len(tags.Value)) + `
|
|
|
|
UNION
|
|
|
|
SELECT root_id, child_id, depth + 1 FROM tags_relations INNER JOIN parents ON item_id = parent_id ` + depthCondition + `
|
|
|
|
)`
|
|
|
|
|
|
|
|
f.addRecursiveWith(query, args...)
|
|
|
|
|
2021-12-06 01:30:40 +00:00
|
|
|
f.addLeftJoin("parents", "", "parents.item_id = tags.id")
|
2021-10-01 01:50:06 +00:00
|
|
|
|
|
|
|
addHierarchicalConditionClauses(f, tags, "parents", "root_id")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func tagChildrenCriterionHandler(qb *tagQueryBuilder, tags *models.HierarchicalMultiCriterionInput) criterionHandlerFunc {
|
|
|
|
return func(f *filterBuilder) {
|
2021-11-06 22:34:33 +00:00
|
|
|
if tags != nil {
|
|
|
|
if tags.Modifier == models.CriterionModifierIsNull || tags.Modifier == models.CriterionModifierNotNull {
|
|
|
|
var notClause string
|
|
|
|
if tags.Modifier == models.CriterionModifierNotNull {
|
|
|
|
notClause = "NOT"
|
|
|
|
}
|
|
|
|
|
2021-12-06 01:30:40 +00:00
|
|
|
f.addLeftJoin("tags_relations", "child_relations", "tags.id = child_relations.parent_id")
|
2021-11-06 22:34:33 +00:00
|
|
|
|
|
|
|
f.addWhere(fmt.Sprintf("child_relations.child_id IS %s NULL", notClause))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(tags.Value) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-01 01:50:06 +00:00
|
|
|
var args []interface{}
|
|
|
|
for _, val := range tags.Value {
|
|
|
|
args = append(args, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
depthVal := 0
|
|
|
|
if tags.Depth != nil {
|
|
|
|
depthVal = *tags.Depth
|
|
|
|
}
|
|
|
|
|
|
|
|
var depthCondition string
|
|
|
|
if depthVal != -1 {
|
|
|
|
depthCondition = fmt.Sprintf("WHERE depth < %d", depthVal)
|
|
|
|
}
|
|
|
|
|
|
|
|
query := `children AS (
|
|
|
|
SELECT child_id AS root_id, parent_id AS item_id, 0 AS depth FROM tags_relations WHERE child_id IN` + getInBinding(len(tags.Value)) + `
|
|
|
|
UNION
|
|
|
|
SELECT root_id, parent_id, depth + 1 FROM tags_relations INNER JOIN children ON item_id = child_id ` + depthCondition + `
|
|
|
|
)`
|
|
|
|
|
|
|
|
f.addRecursiveWith(query, args...)
|
|
|
|
|
2021-12-06 01:30:40 +00:00
|
|
|
f.addLeftJoin("children", "", "children.item_id = tags.id")
|
2021-10-01 01:50:06 +00:00
|
|
|
|
|
|
|
addHierarchicalConditionClauses(f, tags, "children", "root_id")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func tagParentCountCriterionHandler(qb *tagQueryBuilder, parentCount *models.IntCriterionInput) criterionHandlerFunc {
|
|
|
|
return func(f *filterBuilder) {
|
|
|
|
if parentCount != nil {
|
2021-12-06 01:30:40 +00:00
|
|
|
f.addLeftJoin("tags_relations", "parents_count", "parents_count.child_id = tags.id")
|
2021-10-01 01:50:06 +00:00
|
|
|
clause, args := getIntCriterionWhereClause("count(distinct parents_count.parent_id)", *parentCount)
|
|
|
|
|
|
|
|
f.addHaving(clause, args...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func tagChildCountCriterionHandler(qb *tagQueryBuilder, childCount *models.IntCriterionInput) criterionHandlerFunc {
|
|
|
|
return func(f *filterBuilder) {
|
|
|
|
if childCount != nil {
|
2021-12-06 01:30:40 +00:00
|
|
|
f.addLeftJoin("tags_relations", "children_count", "children_count.parent_id = tags.id")
|
2021-10-01 01:50:06 +00:00
|
|
|
clause, args := getIntCriterionWhereClause("count(distinct children_count.child_id)", *childCount)
|
|
|
|
|
|
|
|
f.addHaving(clause, args...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 11:17:37 +00:00
|
|
|
func (qb *tagQueryBuilder) getDefaultTagSort() string {
|
|
|
|
return getSort("name", "ASC", "tags")
|
2019-11-07 12:49:08 +00:00
|
|
|
}
|
|
|
|
|
2021-03-11 11:17:37 +00:00
|
|
|
func (qb *tagQueryBuilder) getTagSort(query *queryBuilder, 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-03-11 11:17:37 +00:00
|
|
|
|
|
|
|
if findFilter.Sort != nil {
|
|
|
|
switch *findFilter.Sort {
|
|
|
|
case "scenes_count":
|
2021-10-01 06:24:58 +00:00
|
|
|
return getCountSort(tagTable, scenesTagsTable, tagIDColumn, direction)
|
2021-08-04 03:39:24 +00:00
|
|
|
case "scene_markers_count":
|
2021-10-01 06:24:58 +00:00
|
|
|
return getCountSort(tagTable, "scene_markers_tags", tagIDColumn, direction)
|
2021-03-11 11:17:37 +00:00
|
|
|
case "images_count":
|
2021-10-01 06:24:58 +00:00
|
|
|
return getCountSort(tagTable, imagesTagsTable, tagIDColumn, direction)
|
2021-03-11 11:17:37 +00:00
|
|
|
case "galleries_count":
|
2021-10-01 06:24:58 +00:00
|
|
|
return getCountSort(tagTable, galleriesTagsTable, tagIDColumn, direction)
|
2021-03-11 11:17:37 +00:00
|
|
|
case "performers_count":
|
2021-10-01 06:24:58 +00:00
|
|
|
return getCountSort(tagTable, performersTagsTable, tagIDColumn, direction)
|
2021-03-11 11:17:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-09 12:30:49 +00:00
|
|
|
return getSort(sort, direction, "tags")
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *tagQueryBuilder) queryTag(query string, args []interface{}) (*models.Tag, error) {
|
|
|
|
results, err := qb.queryTags(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 *tagQueryBuilder) queryTags(query string, args []interface{}) ([]*models.Tag, error) {
|
|
|
|
var ret models.Tags
|
|
|
|
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.Tag(ret), nil
|
2019-02-14 22:53:32 +00:00
|
|
|
}
|
2020-07-07 00:35:43 +00:00
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *tagQueryBuilder) imageRepository() *imageRepository {
|
|
|
|
return &imageRepository{
|
|
|
|
repository: repository{
|
|
|
|
tx: qb.tx,
|
|
|
|
tableName: "tags_image",
|
|
|
|
idColumn: tagIDColumn,
|
|
|
|
},
|
|
|
|
imageColumn: "image",
|
2020-07-07 00:35:43 +00:00
|
|
|
}
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
2020-07-07 00:35:43 +00:00
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *tagQueryBuilder) GetImage(tagID int) ([]byte, error) {
|
|
|
|
return qb.imageRepository().get(tagID)
|
2020-07-07 00:35:43 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *tagQueryBuilder) HasImage(tagID int) (bool, error) {
|
|
|
|
return qb.imageRepository().exists(tagID)
|
|
|
|
}
|
2020-07-07 00:35:43 +00:00
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *tagQueryBuilder) UpdateImage(tagID int, image []byte) error {
|
|
|
|
return qb.imageRepository().replace(tagID, image)
|
2020-07-07 00:35:43 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *tagQueryBuilder) DestroyImage(tagID int) error {
|
|
|
|
return qb.imageRepository().destroy([]int{tagID})
|
2020-07-07 00:35:43 +00:00
|
|
|
}
|
2021-05-26 04:36:05 +00:00
|
|
|
|
|
|
|
func (qb *tagQueryBuilder) aliasRepository() *stringRepository {
|
|
|
|
return &stringRepository{
|
|
|
|
repository: repository{
|
|
|
|
tx: qb.tx,
|
|
|
|
tableName: tagAliasesTable,
|
|
|
|
idColumn: tagIDColumn,
|
|
|
|
},
|
|
|
|
stringColumn: tagAliasColumn,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *tagQueryBuilder) GetAliases(tagID int) ([]string, error) {
|
|
|
|
return qb.aliasRepository().get(tagID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *tagQueryBuilder) UpdateAliases(tagID int, aliases []string) error {
|
|
|
|
return qb.aliasRepository().replace(tagID, aliases)
|
|
|
|
}
|
2021-06-16 04:33:54 +00:00
|
|
|
|
|
|
|
func (qb *tagQueryBuilder) Merge(source []int, destination int) error {
|
|
|
|
if len(source) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
inBinding := getInBinding(len(source))
|
|
|
|
|
|
|
|
args := []interface{}{destination}
|
|
|
|
for _, id := range source {
|
|
|
|
if id == destination {
|
|
|
|
return errors.New("cannot merge where source == destination")
|
|
|
|
}
|
|
|
|
args = append(args, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
tagTables := map[string]string{
|
|
|
|
scenesTagsTable: sceneIDColumn,
|
|
|
|
"scene_markers_tags": "scene_marker_id",
|
|
|
|
galleriesTagsTable: galleryIDColumn,
|
|
|
|
imagesTagsTable: imageIDColumn,
|
|
|
|
"performers_tags": "performer_id",
|
|
|
|
}
|
|
|
|
|
2021-10-20 05:34:19 +00:00
|
|
|
args = append(args, destination)
|
2021-06-16 04:33:54 +00:00
|
|
|
for table, idColumn := range tagTables {
|
|
|
|
_, err := qb.tx.Exec(`UPDATE `+table+`
|
|
|
|
SET tag_id = ?
|
|
|
|
WHERE tag_id IN `+inBinding+`
|
|
|
|
AND NOT EXISTS(SELECT 1 FROM `+table+` o WHERE o.`+idColumn+` = `+table+`.`+idColumn+` AND o.tag_id = ?)`,
|
2021-10-20 05:34:19 +00:00
|
|
|
args...,
|
2021-06-16 04:33:54 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := qb.tx.Exec("UPDATE "+sceneMarkerTable+" SET primary_tag_id = ? WHERE primary_tag_id IN "+inBinding, args...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = qb.tx.Exec("INSERT INTO "+tagAliasesTable+" (tag_id, alias) SELECT ?, name FROM "+tagTable+" WHERE id IN "+inBinding, args...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = qb.tx.Exec("UPDATE "+tagAliasesTable+" SET tag_id = ? WHERE tag_id IN "+inBinding, args...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, id := range source {
|
|
|
|
err = qb.Destroy(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
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 (qb *tagQueryBuilder) UpdateParentTags(tagID int, parentIDs []int) error {
|
|
|
|
tx := qb.tx
|
|
|
|
if _, err := tx.Exec("DELETE FROM tags_relations WHERE child_id = ?", tagID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(parentIDs) > 0 {
|
|
|
|
var args []interface{}
|
|
|
|
var values []string
|
|
|
|
for _, parentID := range parentIDs {
|
|
|
|
values = append(values, "(? , ?)")
|
|
|
|
args = append(args, parentID, tagID)
|
|
|
|
}
|
|
|
|
|
|
|
|
query := "INSERT INTO tags_relations (parent_id, child_id) VALUES " + strings.Join(values, ", ")
|
|
|
|
if _, err := tx.Exec(query, args...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *tagQueryBuilder) UpdateChildTags(tagID int, childIDs []int) error {
|
|
|
|
tx := qb.tx
|
|
|
|
if _, err := tx.Exec("DELETE FROM tags_relations WHERE parent_id = ?", tagID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(childIDs) > 0 {
|
|
|
|
var args []interface{}
|
|
|
|
var values []string
|
|
|
|
for _, childID := range childIDs {
|
|
|
|
values = append(values, "(? , ?)")
|
|
|
|
args = append(args, tagID, childID)
|
|
|
|
}
|
|
|
|
|
|
|
|
query := "INSERT INTO tags_relations (parent_id, child_id) VALUES " + strings.Join(values, ", ")
|
|
|
|
if _, err := tx.Exec(query, args...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-06 22:33:46 +00:00
|
|
|
// FindAllAncestors returns a slice of TagPath objects, representing all
|
|
|
|
// ancestors of the tag with the provided id.
|
|
|
|
func (qb *tagQueryBuilder) FindAllAncestors(tagID int, excludeIDs []int) ([]*models.TagPath, error) {
|
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
|
|
|
inBinding := getInBinding(len(excludeIDs) + 1)
|
|
|
|
|
|
|
|
query := `WITH RECURSIVE
|
|
|
|
parents AS (
|
2021-11-06 22:33:46 +00:00
|
|
|
SELECT t.id AS parent_id, t.id AS child_id, t.name as path FROM tags t WHERE t.id = ?
|
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
|
|
|
UNION
|
2021-11-06 22:33:46 +00:00
|
|
|
SELECT tr.parent_id, tr.child_id, t.name || '->' || p.path as path FROM tags_relations tr INNER JOIN parents p ON p.parent_id = tr.child_id JOIN tags t ON t.id = tr.parent_id WHERE tr.parent_id NOT IN` + inBinding + `
|
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
|
|
|
)
|
2021-11-06 22:33:46 +00:00
|
|
|
SELECT t.*, p.path FROM tags t INNER JOIN parents p ON t.id = p.parent_id
|
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
|
|
|
`
|
|
|
|
|
2021-11-06 22:33:46 +00:00
|
|
|
var ret models.TagPaths
|
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
|
|
|
excludeArgs := []interface{}{tagID}
|
|
|
|
for _, excludeID := range excludeIDs {
|
|
|
|
excludeArgs = append(excludeArgs, excludeID)
|
|
|
|
}
|
|
|
|
args := []interface{}{tagID}
|
|
|
|
args = append(args, append(append(excludeArgs, excludeArgs...), excludeArgs...)...)
|
|
|
|
if err := qb.query(query, args, &ret); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2021-11-06 22:33:46 +00:00
|
|
|
// FindAllDescendants returns a slice of TagPath objects, representing all
|
|
|
|
// descendants of the tag with the provided id.
|
|
|
|
func (qb *tagQueryBuilder) FindAllDescendants(tagID int, excludeIDs []int) ([]*models.TagPath, error) {
|
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
|
|
|
inBinding := getInBinding(len(excludeIDs) + 1)
|
|
|
|
|
|
|
|
query := `WITH RECURSIVE
|
|
|
|
children AS (
|
2021-11-06 22:33:46 +00:00
|
|
|
SELECT t.id AS parent_id, t.id AS child_id, t.name as path FROM tags t WHERE t.id = ?
|
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
|
|
|
UNION
|
2021-11-06 22:33:46 +00:00
|
|
|
SELECT tr.parent_id, tr.child_id, c.path || '->' || t.name as path FROM tags_relations tr INNER JOIN children c ON c.child_id = tr.parent_id JOIN tags t ON t.id = tr.child_id WHERE tr.child_id NOT IN` + inBinding + `
|
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
|
|
|
)
|
2021-11-06 22:33:46 +00:00
|
|
|
SELECT t.*, c.path FROM tags t INNER JOIN children c ON t.id = c.child_id
|
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
|
|
|
`
|
|
|
|
|
2021-11-06 22:33:46 +00:00
|
|
|
var ret models.TagPaths
|
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
|
|
|
excludeArgs := []interface{}{tagID}
|
|
|
|
for _, excludeID := range excludeIDs {
|
|
|
|
excludeArgs = append(excludeArgs, excludeID)
|
|
|
|
}
|
|
|
|
args := []interface{}{tagID}
|
|
|
|
args = append(args, append(append(excludeArgs, excludeArgs...), excludeArgs...)...)
|
|
|
|
if err := qb.query(query, args, &ret); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|