2021-03-02 00:27:36 +00:00
|
|
|
package sqlite
|
|
|
|
|
|
|
|
import (
|
2022-05-19 07:49:32 +00:00
|
|
|
"context"
|
2021-03-02 00:27:36 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/stashapp/stash/pkg/models"
|
|
|
|
)
|
|
|
|
|
2024-06-11 01:34:38 +00:00
|
|
|
func illegalFilterCombination(type1, type2 string) error {
|
|
|
|
return fmt.Errorf("cannot have %s and %s in the same filter", type1, type2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateFilterCombination[T any](sf models.OperatorFilter[T]) error {
|
|
|
|
const and = "AND"
|
|
|
|
const or = "OR"
|
|
|
|
const not = "NOT"
|
|
|
|
|
|
|
|
if sf.And != nil {
|
|
|
|
if sf.Or != nil {
|
|
|
|
return illegalFilterCombination(and, or)
|
|
|
|
}
|
|
|
|
if sf.Not != nil {
|
|
|
|
return illegalFilterCombination(and, not)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if sf.Or != nil {
|
|
|
|
if sf.Not != nil {
|
|
|
|
return illegalFilterCombination(or, not)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleSubFilter[T any](ctx context.Context, handler criterionHandler, f *filterBuilder, subFilter models.OperatorFilter[T]) {
|
|
|
|
subQuery := &filterBuilder{}
|
|
|
|
handler.handle(ctx, subQuery)
|
|
|
|
|
|
|
|
if subFilter.And != nil {
|
|
|
|
f.and(subQuery)
|
|
|
|
}
|
|
|
|
if subFilter.Or != nil {
|
|
|
|
f.or(subQuery)
|
|
|
|
}
|
|
|
|
if subFilter.Not != nil {
|
|
|
|
f.not(subQuery)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-02 00:27:36 +00:00
|
|
|
type sqlClause struct {
|
|
|
|
sql string
|
|
|
|
args []interface{}
|
|
|
|
}
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
func (c sqlClause) not() sqlClause {
|
|
|
|
return sqlClause{
|
|
|
|
sql: "NOT (" + c.sql + ")",
|
|
|
|
args: c.args,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-02 00:27:36 +00:00
|
|
|
func makeClause(sql string, args ...interface{}) sqlClause {
|
|
|
|
return sqlClause{
|
|
|
|
sql: sql,
|
|
|
|
args: args,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-16 04:45:08 +00:00
|
|
|
func joinClauses(joinType string, clauses ...sqlClause) sqlClause {
|
2022-07-13 06:30:54 +00:00
|
|
|
var ret []string
|
|
|
|
var args []interface{}
|
|
|
|
|
|
|
|
for _, clause := range clauses {
|
|
|
|
ret = append(ret, "("+clause.sql+")")
|
|
|
|
args = append(args, clause.args...)
|
|
|
|
}
|
|
|
|
|
2022-09-16 04:45:08 +00:00
|
|
|
return sqlClause{sql: strings.Join(ret, " "+joinType+" "), args: args}
|
|
|
|
}
|
|
|
|
|
|
|
|
func orClauses(clauses ...sqlClause) sqlClause {
|
|
|
|
return joinClauses("OR", clauses...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func andClauses(clauses ...sqlClause) sqlClause {
|
|
|
|
return joinClauses("AND", clauses...)
|
2022-07-13 06:30:54 +00:00
|
|
|
}
|
|
|
|
|
2021-03-02 00:27:36 +00:00
|
|
|
type join struct {
|
|
|
|
table string
|
|
|
|
as string
|
|
|
|
onClause string
|
2021-12-06 01:30:40 +00:00
|
|
|
joinType string
|
2021-03-02 00:27:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// equals returns true if the other join alias/table is equal to this one
|
|
|
|
func (j join) equals(o join) bool {
|
|
|
|
return j.alias() == o.alias()
|
|
|
|
}
|
|
|
|
|
|
|
|
// alias returns the as string, or the table if as is empty
|
|
|
|
func (j join) alias() string {
|
|
|
|
if j.as == "" {
|
|
|
|
return j.table
|
|
|
|
}
|
|
|
|
|
|
|
|
return j.as
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j join) toSQL() string {
|
|
|
|
asStr := ""
|
2021-12-06 01:30:40 +00:00
|
|
|
joinStr := j.joinType
|
2021-03-02 00:27:36 +00:00
|
|
|
if j.as != "" && j.as != j.table {
|
|
|
|
asStr = " AS " + j.as
|
|
|
|
}
|
2021-12-06 01:30:40 +00:00
|
|
|
if j.joinType == "" {
|
|
|
|
joinStr = "LEFT"
|
|
|
|
}
|
2021-03-02 00:27:36 +00:00
|
|
|
|
2021-12-06 01:30:40 +00:00
|
|
|
return fmt.Sprintf("%s JOIN %s%s ON %s", joinStr, j.table, asStr, j.onClause)
|
2021-03-02 00:27:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type joins []join
|
|
|
|
|
|
|
|
func (j *joins) add(newJoins ...join) {
|
|
|
|
// only add if not already joined
|
|
|
|
for _, newJoin := range newJoins {
|
2021-06-21 05:48:28 +00:00
|
|
|
found := false
|
2021-03-02 00:27:36 +00:00
|
|
|
for _, jj := range *j {
|
|
|
|
if jj.equals(newJoin) {
|
2021-06-21 05:48:28 +00:00
|
|
|
found = true
|
|
|
|
break
|
2021-03-02 00:27:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 05:48:28 +00:00
|
|
|
if !found {
|
|
|
|
*j = append(*j, newJoin)
|
|
|
|
}
|
2021-03-02 00:27:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *joins) toSQL() string {
|
2021-10-25 00:40:13 +00:00
|
|
|
if len(*j) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2021-03-02 00:27:36 +00:00
|
|
|
var ret []string
|
|
|
|
for _, jj := range *j {
|
|
|
|
ret = append(ret, jj.toSQL())
|
|
|
|
}
|
|
|
|
|
2021-10-25 00:40:13 +00:00
|
|
|
return " " + strings.Join(ret, " ")
|
2021-03-02 00:27:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type filterBuilder struct {
|
|
|
|
subFilter *filterBuilder
|
|
|
|
subFilterOp string
|
|
|
|
|
|
|
|
joins joins
|
|
|
|
whereClauses []sqlClause
|
|
|
|
havingClauses []sqlClause
|
2021-06-03 10:52:19 +00:00
|
|
|
withClauses []sqlClause
|
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
|
|
|
recursiveWith bool
|
2021-03-02 00:27:36 +00:00
|
|
|
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2024-06-11 01:34:38 +00:00
|
|
|
func (f *filterBuilder) empty() bool {
|
|
|
|
return f == nil || (len(f.whereClauses) == 0 && len(f.joins) == 0 && len(f.havingClauses) == 0 && f.subFilter == nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func filterBuilderFromHandler(ctx context.Context, handler criterionHandler) *filterBuilder {
|
|
|
|
f := &filterBuilder{}
|
|
|
|
handler.handle(ctx, f)
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2021-09-27 23:29:45 +00:00
|
|
|
var errSubFilterAlreadySet = errors.New(`sub-filter already set`)
|
2021-03-02 00:27:36 +00:00
|
|
|
|
|
|
|
// sub-filter operator values
|
|
|
|
var (
|
|
|
|
andOp = "AND"
|
|
|
|
orOp = "OR"
|
|
|
|
notOp = "AND NOT"
|
|
|
|
)
|
|
|
|
|
|
|
|
// and sets the sub-filter that will be ANDed with this one.
|
|
|
|
// Sets the error state if sub-filter is already set.
|
|
|
|
func (f *filterBuilder) and(a *filterBuilder) {
|
|
|
|
if f.subFilter != nil {
|
|
|
|
f.setError(errSubFilterAlreadySet)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
f.subFilter = a
|
|
|
|
f.subFilterOp = andOp
|
|
|
|
}
|
|
|
|
|
|
|
|
// or sets the sub-filter that will be ORed with this one.
|
|
|
|
// Sets the error state if a sub-filter is already set.
|
|
|
|
func (f *filterBuilder) or(o *filterBuilder) {
|
|
|
|
if f.subFilter != nil {
|
|
|
|
f.setError(errSubFilterAlreadySet)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
f.subFilter = o
|
|
|
|
f.subFilterOp = orOp
|
|
|
|
}
|
|
|
|
|
|
|
|
// not sets the sub-filter that will be AND NOTed with this one.
|
|
|
|
// Sets the error state if a sub-filter is already set.
|
|
|
|
func (f *filterBuilder) not(n *filterBuilder) {
|
|
|
|
if f.subFilter != nil {
|
|
|
|
f.setError(errSubFilterAlreadySet)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
f.subFilter = n
|
|
|
|
f.subFilterOp = notOp
|
|
|
|
}
|
|
|
|
|
2021-12-06 01:30:40 +00:00
|
|
|
// addLeftJoin adds a left join to the filter. The join is expressed in SQL as:
|
2021-03-02 00:27:36 +00:00
|
|
|
// LEFT JOIN <table> [AS <as>] ON <onClause>
|
|
|
|
// The AS is omitted if as is empty.
|
|
|
|
// This method does not add a join if it its alias/table name is already
|
|
|
|
// present in another existing join.
|
2021-12-06 01:30:40 +00:00
|
|
|
func (f *filterBuilder) addLeftJoin(table, as, onClause string) {
|
|
|
|
newJoin := join{
|
|
|
|
table: table,
|
|
|
|
as: as,
|
|
|
|
onClause: onClause,
|
|
|
|
joinType: "LEFT",
|
|
|
|
}
|
|
|
|
|
|
|
|
f.joins.add(newJoin)
|
|
|
|
}
|
|
|
|
|
|
|
|
// addInnerJoin adds an inner join to the filter. The join is expressed in SQL as:
|
|
|
|
// INNER JOIN <table> [AS <as>] ON <onClause>
|
|
|
|
// The AS is omitted if as is empty.
|
|
|
|
// This method does not add a join if it its alias/table name is already
|
|
|
|
// present in another existing join.
|
|
|
|
func (f *filterBuilder) addInnerJoin(table, as, onClause string) {
|
2021-03-02 00:27:36 +00:00
|
|
|
newJoin := join{
|
|
|
|
table: table,
|
|
|
|
as: as,
|
|
|
|
onClause: onClause,
|
2021-12-06 01:30:40 +00:00
|
|
|
joinType: "INNER",
|
2021-03-02 00:27:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
f.joins.add(newJoin)
|
|
|
|
}
|
|
|
|
|
|
|
|
// addWhere adds a where clause and arguments to the filter. Where clauses
|
|
|
|
// are ANDed together. Does not add anything if the provided string is empty.
|
|
|
|
func (f *filterBuilder) addWhere(sql string, args ...interface{}) {
|
|
|
|
if sql == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
f.whereClauses = append(f.whereClauses, makeClause(sql, args...))
|
|
|
|
}
|
|
|
|
|
|
|
|
// addHaving adds a where clause and arguments to the filter. Having clauses
|
|
|
|
// are ANDed together. Does not add anything if the provided string is empty.
|
|
|
|
func (f *filterBuilder) addHaving(sql string, args ...interface{}) {
|
|
|
|
if sql == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
f.havingClauses = append(f.havingClauses, makeClause(sql, args...))
|
|
|
|
}
|
|
|
|
|
2021-06-03 10:52:19 +00:00
|
|
|
// addWith adds a with clause and arguments to the filter
|
|
|
|
func (f *filterBuilder) addWith(sql string, args ...interface{}) {
|
|
|
|
if sql == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
f.withClauses = append(f.withClauses, makeClause(sql, args...))
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// addRecursiveWith adds a with clause and arguments to the filter, and sets it to recursive
|
2022-09-06 05:12:59 +00:00
|
|
|
//
|
2021-09-23 07:15:50 +00:00
|
|
|
//nolint:unused
|
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 (f *filterBuilder) addRecursiveWith(sql string, args ...interface{}) {
|
|
|
|
if sql == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
f.addWith(sql, args...)
|
|
|
|
f.recursiveWith = true
|
|
|
|
}
|
|
|
|
|
2021-03-02 00:27:36 +00:00
|
|
|
func (f *filterBuilder) getSubFilterClause(clause, subFilterClause string) string {
|
|
|
|
ret := clause
|
|
|
|
|
|
|
|
if subFilterClause != "" {
|
|
|
|
var op string
|
|
|
|
if len(ret) > 0 {
|
|
|
|
op = " " + f.subFilterOp + " "
|
Enable gocritic (#1848)
* Don't capitalize local variables
ValidCodecs -> validCodecs
* Capitalize deprecation markers
A deprecated marker should be capitalized.
* Use re.MustCompile for static regexes
If the regex fails to compile, it's a programmer error, and should be
treated as such. The regex is entirely static.
* Simplify else-if constructions
Rewrite
else { if cond {}}
to
else if cond {}
* Use a switch statement to analyze formats
Break an if-else chain. While here, simplify code flow.
Also introduce a proper static error for unsupported image formats,
paving the way for being able to check against the error.
* Rewrite ifElse chains into switch statements
The "Effective Go" https://golang.org/doc/effective_go#switch document
mentions it is more idiomatic to write if-else chains as switches when
it is possible.
Find all the plain rewrite occurrences in the code base and rewrite.
In some cases, the if-else chains are replaced by a switch scrutinizer.
That is, the code sequence
if x == 1 {
..
} else if x == 2 {
..
} else if x == 3 {
...
}
can be rewritten into
switch x {
case 1:
..
case 2:
..
case 3:
..
}
which is clearer for the compiler: it can decide if the switch is
better served by a jump-table then a branch-chain.
* Rewrite switches, introduce static errors
Introduce two new static errors:
* `ErrNotImplmented`
* `ErrNotSupported`
And use these rather than forming new generative errors whenever the
code is called. Code can now test on the errors (since they are static
and the pointers to them wont change).
Also rewrite ifElse chains into switches in this part of the code base.
* Introduce a StashBoxError in configuration
Since all stashbox errors are the same, treat them as such in the code
base. While here, rewrite an ifElse chain.
In the future, it might be beneifical to refactor configuration errors
into one error which can handle missing fields, which context the error
occurs in and so on. But for now, try to get an overview of the error
categories by hoisting them into static errors.
* Get rid of an else-block in transaction handling
If we succesfully `recover()`, we then always `panic()`. This means the
rest of the code is not reachable, so we can avoid having an else-block
here.
It also solves an ifElse-chain style check in the code base.
* Use strings.ReplaceAll
Rewrite
strings.Replace(s, o, n, -1)
into
strings.ReplaceAll(s, o, n)
To make it consistent and clear that we are doing an all-replace in the
string rather than replacing parts of it. It's more of a nitpick since
there are no implementation differences: the stdlib implementation is
just to supply -1.
* Rewrite via gocritic's assignOp
Statements of the form
x = x + e
is rewritten into
x += e
where applicable.
* Formatting
* Review comments handled
Stash-box is a proper noun.
Rewrite a switch into an if-chain which returns on the first error
encountered.
* Use context.TODO() over context.Background()
Patch in the same vein as everything else: use the TODO() marker so we
can search for it later and link it into the context tree/tentacle once
it reaches down to this level in the code base.
* Tell the linter to ignore a section in manager_tasks.go
The section is less readable, so mark it with a nolint for now. Because
the rewrite enables a ifElseChain, also mark that as nolint for now.
* Use strings.ReplaceAll over strings.Replace
* Apply an ifElse rewrite
else { if .. { .. } } rewrite into else if { .. }
* Use switch-statements over ifElseChains
Rewrite chains of if-else into switch statements. Where applicable,
add an early nil-guard to simplify case analysis. Also, in
ScanTask's Start(..), invert the logic to outdent the whole block, and
help the reader: if it's not a scene, the function flow is now far more
local to the top of the function, and it's clear that the rest of the
function has to do with scene management.
* Enable gocritic on the code base.
Disable appendAssign for now since we aren't passing that check yet.
* Document the nolint additions
* Document StashBoxBatchPerformerTagInput
2021-10-18 03:12:40 +00:00
|
|
|
} else if f.subFilterOp == notOp {
|
|
|
|
op = "NOT "
|
2021-03-02 00:27:36 +00:00
|
|
|
}
|
|
|
|
|
2021-03-18 10:45:18 +00:00
|
|
|
ret += op + "(" + subFilterClause + ")"
|
2021-03-02 00:27:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// generateWhereClauses generates the SQL where clause for this filter.
|
|
|
|
// All where clauses within the filter are ANDed together. This is combined
|
|
|
|
// with the sub-filter, which will use the applicable operator (AND/OR/AND NOT).
|
|
|
|
func (f *filterBuilder) generateWhereClauses() (clause string, args []interface{}) {
|
|
|
|
clause, args = f.andClauses(f.whereClauses)
|
|
|
|
|
|
|
|
if f.subFilter != nil {
|
|
|
|
c, a := f.subFilter.generateWhereClauses()
|
|
|
|
if c != "" {
|
|
|
|
clause = f.getSubFilterClause(clause, c)
|
|
|
|
if len(a) > 0 {
|
|
|
|
args = append(args, a...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// generateHavingClauses generates the SQL having clause for this filter.
|
|
|
|
// All having clauses within the filter are ANDed together. This is combined
|
|
|
|
// with the sub-filter, which will use the applicable operator (AND/OR/AND NOT).
|
|
|
|
func (f *filterBuilder) generateHavingClauses() (string, []interface{}) {
|
|
|
|
clause, args := f.andClauses(f.havingClauses)
|
|
|
|
|
|
|
|
if f.subFilter != nil {
|
|
|
|
c, a := f.subFilter.generateHavingClauses()
|
|
|
|
if c != "" {
|
2021-03-18 10:45:18 +00:00
|
|
|
clause = f.getSubFilterClause(clause, c)
|
2021-03-02 00:27:36 +00:00
|
|
|
if len(a) > 0 {
|
|
|
|
args = append(args, a...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return clause, args
|
|
|
|
}
|
|
|
|
|
2021-06-03 10:52:19 +00:00
|
|
|
func (f *filterBuilder) generateWithClauses() (string, []interface{}) {
|
|
|
|
var clauses []string
|
|
|
|
var args []interface{}
|
|
|
|
for _, w := range f.withClauses {
|
|
|
|
clauses = append(clauses, w.sql)
|
|
|
|
args = append(args, w.args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(clauses) > 0 {
|
|
|
|
return strings.Join(clauses, ", "), args
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2021-03-02 00:27:36 +00:00
|
|
|
// getAllJoins returns all of the joins in this filter and any sub-filter(s).
|
|
|
|
// Redundant joins will not be duplicated in the return value.
|
|
|
|
func (f *filterBuilder) getAllJoins() joins {
|
|
|
|
var ret joins
|
|
|
|
ret.add(f.joins...)
|
|
|
|
if f.subFilter != nil {
|
|
|
|
subJoins := f.subFilter.getAllJoins()
|
|
|
|
if len(subJoins) > 0 {
|
|
|
|
ret.add(subJoins...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// getError returns the error state on this filter, or on any sub-filter(s) if
|
|
|
|
// the error state is nil.
|
|
|
|
func (f *filterBuilder) getError() error {
|
|
|
|
if f.err != nil {
|
|
|
|
return f.err
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.subFilter != nil {
|
|
|
|
return f.subFilter.getError()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleCriterion calls the handle function on the provided criterionHandler,
|
|
|
|
// providing itself.
|
2022-05-19 07:49:32 +00:00
|
|
|
func (f *filterBuilder) handleCriterion(ctx context.Context, handler criterionHandler) {
|
|
|
|
handler.handle(ctx, f)
|
2021-03-02 00:27:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *filterBuilder) setError(e error) {
|
|
|
|
if f.err == nil {
|
|
|
|
f.err = e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *filterBuilder) andClauses(input []sqlClause) (string, []interface{}) {
|
|
|
|
var clauses []string
|
|
|
|
var args []interface{}
|
|
|
|
for _, w := range input {
|
|
|
|
clauses = append(clauses, w.sql)
|
|
|
|
args = append(args, w.args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(clauses) > 0 {
|
2021-07-14 08:29:59 +00:00
|
|
|
c := "(" + strings.Join(clauses, ") AND (") + ")"
|
2021-03-18 10:45:18 +00:00
|
|
|
if len(clauses) > 1 {
|
|
|
|
c = "(" + c + ")"
|
|
|
|
}
|
2021-03-02 00:27:36 +00:00
|
|
|
return c, args
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", nil
|
|
|
|
}
|