2021-01-18 01:23:20 +00:00
|
|
|
package sqlite
|
|
|
|
|
2021-02-01 20:57:56 +00:00
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
|
|
|
|
"github.com/stashapp/stash/pkg/models"
|
|
|
|
)
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
type queryBuilder struct {
|
|
|
|
repository *repository
|
|
|
|
|
|
|
|
body string
|
|
|
|
|
|
|
|
whereClauses []string
|
|
|
|
havingClauses []string
|
|
|
|
args []interface{}
|
|
|
|
|
|
|
|
sortAndPagination string
|
2021-02-01 20:57:56 +00:00
|
|
|
|
|
|
|
err error
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (qb queryBuilder) executeFind() ([]int, int, error) {
|
2021-02-01 20:57:56 +00:00
|
|
|
if qb.err != nil {
|
|
|
|
return nil, 0, qb.err
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
return qb.repository.executeFindQuery(qb.body, qb.args, qb.sortAndPagination, qb.whereClauses, qb.havingClauses)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *queryBuilder) addWhere(clauses ...string) {
|
|
|
|
for _, clause := range clauses {
|
|
|
|
if len(clause) > 0 {
|
|
|
|
qb.whereClauses = append(qb.whereClauses, clause)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *queryBuilder) addHaving(clauses ...string) {
|
|
|
|
for _, clause := range clauses {
|
|
|
|
if len(clause) > 0 {
|
|
|
|
qb.havingClauses = append(qb.havingClauses, clause)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *queryBuilder) addArg(args ...interface{}) {
|
|
|
|
qb.args = append(qb.args, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *queryBuilder) handleIntCriterionInput(c *models.IntCriterionInput, column string) {
|
|
|
|
if c != nil {
|
|
|
|
clause, count := getIntCriterionWhereClause(column, *c)
|
|
|
|
qb.addWhere(clause)
|
|
|
|
if count == 1 {
|
|
|
|
qb.addArg(c.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *queryBuilder) handleStringCriterionInput(c *models.StringCriterionInput, column string) {
|
|
|
|
if c != nil {
|
|
|
|
if modifier := c.Modifier; c.Modifier.IsValid() {
|
|
|
|
switch modifier {
|
|
|
|
case models.CriterionModifierIncludes:
|
|
|
|
clause, thisArgs := getSearchBinding([]string{column}, c.Value, false)
|
|
|
|
qb.addWhere(clause)
|
|
|
|
qb.addArg(thisArgs...)
|
|
|
|
case models.CriterionModifierExcludes:
|
|
|
|
clause, thisArgs := getSearchBinding([]string{column}, c.Value, true)
|
|
|
|
qb.addWhere(clause)
|
|
|
|
qb.addArg(thisArgs...)
|
|
|
|
case models.CriterionModifierEquals:
|
|
|
|
qb.addWhere(column + " LIKE ?")
|
|
|
|
qb.addArg(c.Value)
|
|
|
|
case models.CriterionModifierNotEquals:
|
|
|
|
qb.addWhere(column + " NOT LIKE ?")
|
|
|
|
qb.addArg(c.Value)
|
2021-02-01 20:57:56 +00:00
|
|
|
case models.CriterionModifierMatchesRegex:
|
|
|
|
if _, err := regexp.Compile(c.Value); err != nil {
|
|
|
|
qb.err = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
qb.addWhere(column + " regexp ?")
|
|
|
|
qb.addArg(c.Value)
|
|
|
|
case models.CriterionModifierNotMatchesRegex:
|
|
|
|
if _, err := regexp.Compile(c.Value); err != nil {
|
|
|
|
qb.err = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
qb.addWhere(column + " NOT regexp ?")
|
|
|
|
qb.addArg(c.Value)
|
2021-01-18 01:23:20 +00:00
|
|
|
default:
|
|
|
|
clause, count := getSimpleCriterionClause(modifier, "?")
|
|
|
|
qb.addWhere(column + " " + clause)
|
|
|
|
if count == 1 {
|
|
|
|
qb.addArg(c.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|