mirror of https://github.com/stashapp/stash.git
Fix filter building with sub-filters (#1212)
* Fix bracketing on sub-filters * Add vscode to gitignore
This commit is contained in:
parent
d93011a828
commit
960f843259
|
@ -35,6 +35,7 @@ ui/v2.5/src/core/generated-*.tsx
|
||||||
.idea/**/usage.statistics.xml
|
.idea/**/usage.statistics.xml
|
||||||
.idea/**/dictionaries
|
.idea/**/dictionaries
|
||||||
.idea/**/shelf
|
.idea/**/shelf
|
||||||
|
.vscode
|
||||||
|
|
||||||
# Generated files
|
# Generated files
|
||||||
.idea/**/contentModel.xml
|
.idea/**/contentModel.xml
|
||||||
|
|
|
@ -182,7 +182,7 @@ func (f *filterBuilder) getSubFilterClause(clause, subFilterClause string) strin
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ret += op + subFilterClause
|
ret += op + "(" + subFilterClause + ")"
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
@ -216,7 +216,7 @@ func (f *filterBuilder) generateHavingClauses() (string, []interface{}) {
|
||||||
if f.subFilter != nil {
|
if f.subFilter != nil {
|
||||||
c, a := f.subFilter.generateHavingClauses()
|
c, a := f.subFilter.generateHavingClauses()
|
||||||
if c != "" {
|
if c != "" {
|
||||||
clause += " " + f.subFilterOp + " " + c
|
clause = f.getSubFilterClause(clause, c)
|
||||||
if len(a) > 0 {
|
if len(a) > 0 {
|
||||||
args = append(args, a...)
|
args = append(args, a...)
|
||||||
}
|
}
|
||||||
|
@ -284,7 +284,10 @@ func (f *filterBuilder) andClauses(input []sqlClause) (string, []interface{}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(clauses) > 0 {
|
if len(clauses) > 0 {
|
||||||
c := "(" + strings.Join(clauses, " AND ") + ")"
|
c := strings.Join(clauses, " AND ")
|
||||||
|
if len(clauses) > 1 {
|
||||||
|
c = "(" + c + ")"
|
||||||
|
}
|
||||||
return c, args
|
return c, args
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -227,14 +227,14 @@ func TestGenerateWhereClauses(t *testing.T) {
|
||||||
// ensure single where clause is generated correctly
|
// ensure single where clause is generated correctly
|
||||||
f.addWhere(clause1)
|
f.addWhere(clause1)
|
||||||
r, rArgs := f.generateWhereClauses()
|
r, rArgs := f.generateWhereClauses()
|
||||||
assert.Equal("("+clause1+")", r)
|
assert.Equal(clause1, r)
|
||||||
assert.Len(rArgs, 0)
|
assert.Len(rArgs, 0)
|
||||||
|
|
||||||
// ensure multiple where clauses are surrounded with parenthesis and
|
// ensure multiple where clauses are surrounded with parenthesis and
|
||||||
// ANDed together
|
// ANDed together
|
||||||
f.addWhere(clause2, arg1, arg2)
|
f.addWhere(clause2, arg1, arg2)
|
||||||
r, rArgs = f.generateWhereClauses()
|
r, rArgs = f.generateWhereClauses()
|
||||||
assert.Equal("("+clause1+" AND "+clause2+")", r)
|
assert.Equal(fmt.Sprintf("(%s AND %s)", clause1, clause2), r)
|
||||||
assert.Len(rArgs, 2)
|
assert.Len(rArgs, 2)
|
||||||
|
|
||||||
// ensure empty subfilter is not added to generated where clause
|
// ensure empty subfilter is not added to generated where clause
|
||||||
|
@ -242,13 +242,13 @@ func TestGenerateWhereClauses(t *testing.T) {
|
||||||
f.and(sf)
|
f.and(sf)
|
||||||
|
|
||||||
r, rArgs = f.generateWhereClauses()
|
r, rArgs = f.generateWhereClauses()
|
||||||
assert.Equal("("+clause1+" AND "+clause2+")", r)
|
assert.Equal(fmt.Sprintf("(%s AND %s)", clause1, clause2), r)
|
||||||
assert.Len(rArgs, 2)
|
assert.Len(rArgs, 2)
|
||||||
|
|
||||||
// ensure sub-filter is generated correctly
|
// ensure sub-filter is generated correctly
|
||||||
sf.addWhere(clause3, arg3)
|
sf.addWhere(clause3, arg3)
|
||||||
r, rArgs = f.generateWhereClauses()
|
r, rArgs = f.generateWhereClauses()
|
||||||
assert.Equal("("+clause1+" AND "+clause2+") AND ("+clause3+")", r)
|
assert.Equal(fmt.Sprintf("(%s AND %s) AND (%s)", clause1, clause2, clause3), r)
|
||||||
assert.Len(rArgs, 3)
|
assert.Len(rArgs, 3)
|
||||||
|
|
||||||
// ensure OR sub-filter is generated correctly
|
// ensure OR sub-filter is generated correctly
|
||||||
|
@ -258,7 +258,7 @@ func TestGenerateWhereClauses(t *testing.T) {
|
||||||
f.or(sf)
|
f.or(sf)
|
||||||
|
|
||||||
r, rArgs = f.generateWhereClauses()
|
r, rArgs = f.generateWhereClauses()
|
||||||
assert.Equal("("+clause1+" AND "+clause2+") OR ("+clause3+")", r)
|
assert.Equal(fmt.Sprintf("(%s AND %s) OR (%s)", clause1, clause2, clause3), r)
|
||||||
assert.Len(rArgs, 3)
|
assert.Len(rArgs, 3)
|
||||||
|
|
||||||
// ensure NOT sub-filter is generated correctly
|
// ensure NOT sub-filter is generated correctly
|
||||||
|
@ -268,7 +268,7 @@ func TestGenerateWhereClauses(t *testing.T) {
|
||||||
f.not(sf)
|
f.not(sf)
|
||||||
|
|
||||||
r, rArgs = f.generateWhereClauses()
|
r, rArgs = f.generateWhereClauses()
|
||||||
assert.Equal("("+clause1+" AND "+clause2+") AND NOT ("+clause3+")", r)
|
assert.Equal(fmt.Sprintf("(%s AND %s) AND NOT (%s)", clause1, clause2, clause3), r)
|
||||||
assert.Len(rArgs, 3)
|
assert.Len(rArgs, 3)
|
||||||
|
|
||||||
// ensure empty filter with ANDed sub-filter does not include AND
|
// ensure empty filter with ANDed sub-filter does not include AND
|
||||||
|
@ -276,7 +276,7 @@ func TestGenerateWhereClauses(t *testing.T) {
|
||||||
f.and(sf)
|
f.and(sf)
|
||||||
|
|
||||||
r, rArgs = f.generateWhereClauses()
|
r, rArgs = f.generateWhereClauses()
|
||||||
assert.Equal("("+clause3+")", r)
|
assert.Equal(fmt.Sprintf("(%s)", clause3), r)
|
||||||
assert.Len(rArgs, 1)
|
assert.Len(rArgs, 1)
|
||||||
|
|
||||||
// ensure empty filter with ORed sub-filter does not include OR
|
// ensure empty filter with ORed sub-filter does not include OR
|
||||||
|
@ -284,7 +284,7 @@ func TestGenerateWhereClauses(t *testing.T) {
|
||||||
f.or(sf)
|
f.or(sf)
|
||||||
|
|
||||||
r, rArgs = f.generateWhereClauses()
|
r, rArgs = f.generateWhereClauses()
|
||||||
assert.Equal("("+clause3+")", r)
|
assert.Equal(fmt.Sprintf("(%s)", clause3), r)
|
||||||
assert.Len(rArgs, 1)
|
assert.Len(rArgs, 1)
|
||||||
|
|
||||||
// ensure empty filter with NOTed sub-filter does not include AND
|
// ensure empty filter with NOTed sub-filter does not include AND
|
||||||
|
@ -292,8 +292,19 @@ func TestGenerateWhereClauses(t *testing.T) {
|
||||||
f.not(sf)
|
f.not(sf)
|
||||||
|
|
||||||
r, rArgs = f.generateWhereClauses()
|
r, rArgs = f.generateWhereClauses()
|
||||||
assert.Equal("NOT ("+clause3+")", r)
|
assert.Equal(fmt.Sprintf("NOT (%s)", clause3), r)
|
||||||
assert.Len(rArgs, 1)
|
assert.Len(rArgs, 1)
|
||||||
|
|
||||||
|
// (clause1) AND ((clause2) OR (clause3))
|
||||||
|
f = &filterBuilder{}
|
||||||
|
f.addWhere(clause1)
|
||||||
|
sf2 := &filterBuilder{}
|
||||||
|
sf2.addWhere(clause2, arg1, arg2)
|
||||||
|
f.and(sf2)
|
||||||
|
sf2.or(sf)
|
||||||
|
r, rArgs = f.generateWhereClauses()
|
||||||
|
assert.Equal(fmt.Sprintf("%s AND (%s OR (%s))", clause1, clause2, clause3), r)
|
||||||
|
assert.Len(rArgs, 3)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGenerateHavingClauses(t *testing.T) {
|
func TestGenerateHavingClauses(t *testing.T) {
|
||||||
|
@ -312,7 +323,7 @@ func TestGenerateHavingClauses(t *testing.T) {
|
||||||
// ensure single Having clause is generated correctly
|
// ensure single Having clause is generated correctly
|
||||||
f.addHaving(clause1)
|
f.addHaving(clause1)
|
||||||
r, rArgs := f.generateHavingClauses()
|
r, rArgs := f.generateHavingClauses()
|
||||||
assert.Equal("("+clause1+")", r)
|
assert.Equal(clause1, r)
|
||||||
assert.Len(rArgs, 0)
|
assert.Len(rArgs, 0)
|
||||||
|
|
||||||
// ensure multiple Having clauses are surrounded with parenthesis and
|
// ensure multiple Having clauses are surrounded with parenthesis and
|
||||||
|
|
Loading…
Reference in New Issue