2021-01-18 01:23:20 +00:00
|
|
|
package sqlite
|
|
|
|
|
|
|
|
import (
|
2022-05-19 07:49:32 +00:00
|
|
|
"context"
|
2021-01-18 01:23:20 +00:00
|
|
|
"database/sql"
|
Errorlint sweep + minor linter tweaks (#1796)
* Replace error assertions with Go 1.13 style
Use `errors.As(..)` over type assertions. This enables better use of
wrapped errors in the future, and lets us pass some errorlint checks
in the process.
The rewrite is entirely mechanical, and uses a standard idiom for
doing so.
* Use Go 1.13's errors.Is(..)
Rather than directly checking for error equality, use errors.Is(..).
This protects against error wrapping issues in the future.
Even though something like sql.ErrNoRows doesn't need the wrapping, do
so anyway, for the sake of consistency throughout the code base.
The change almost lets us pass the `errorlint` Go checker except for
a missing case in `js.go` which is to be handled separately; it isn't
mechanical, like these changes are.
* Remove goconst
goconst isn't a useful linter in many cases, because it's false positive
rate is high. It's 100% for the current code base.
* Avoid direct comparison of errors in recover()
Assert that we are catching an error from recover(). If we are,
check that the error caught matches errStop.
* Enable the "errorlint" checker
Configure the checker to avoid checking for errorf wraps. These are
often false positives since the suggestion is to blanket wrap errors
with %w, and that exposes the underlying API which you might not want
to do.
The other warnings are good however, and with the current patch stack,
the code base passes all these checks as well.
* Configure rowserrcheck
The project uses sqlx. Configure rowserrcheck to include said package.
* Mechanically rewrite a large set of errors
Mechanically search for errors that look like
fmt.Errorf("...%s", err.Error())
and rewrite those into
fmt.Errorf("...%v", err)
The `fmt` package is error-aware and knows how to call err.Error()
itself.
The rationale is that this is more idiomatic Go; it paves the
way for using error wrapping later with %w in some sites.
This patch only addresses the entirely mechanical rewriting caught by
a project-side search/replace. There are more individual sites not
addressed by this patch.
2021-10-12 03:03:08 +00:00
|
|
|
"errors"
|
2021-01-18 01:23:20 +00:00
|
|
|
"fmt"
|
2021-04-26 02:51:31 +00:00
|
|
|
"strings"
|
2021-01-18 01:23:20 +00:00
|
|
|
|
2022-08-12 02:21:46 +00:00
|
|
|
"github.com/doug-martin/goqu/v9"
|
|
|
|
"github.com/jmoiron/sqlx"
|
2021-01-18 01:23:20 +00:00
|
|
|
"github.com/stashapp/stash/pkg/models"
|
2022-08-12 02:21:46 +00:00
|
|
|
"github.com/stashapp/stash/pkg/sliceutil/intslice"
|
2021-01-18 01:23:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const studioTable = "studios"
|
|
|
|
const studioIDColumn = "studio_id"
|
2021-09-09 08:13:42 +00:00
|
|
|
const studioAliasesTable = "studio_aliases"
|
|
|
|
const studioAliasColumn = "alias"
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
type studioQueryBuilder struct {
|
|
|
|
repository
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
var StudioReaderWriter = &studioQueryBuilder{
|
|
|
|
repository{
|
|
|
|
tableName: studioTable,
|
|
|
|
idColumn: idColumn,
|
|
|
|
},
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (qb *studioQueryBuilder) Create(ctx context.Context, newObject models.Studio) (*models.Studio, error) {
|
2021-01-18 01:23:20 +00:00
|
|
|
var ret models.Studio
|
2022-05-19 07:49:32 +00:00
|
|
|
if err := qb.insertObject(ctx, newObject, &ret); err != nil {
|
2021-01-18 01:23:20 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ret, nil
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (qb *studioQueryBuilder) Update(ctx context.Context, updatedObject models.StudioPartial) (*models.Studio, error) {
|
2021-01-18 01:23:20 +00:00
|
|
|
const partial = true
|
2022-05-19 07:49:32 +00:00
|
|
|
if err := qb.update(ctx, updatedObject.ID, updatedObject, partial); err != nil {
|
2021-01-18 01:23:20 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
return qb.Find(ctx, updatedObject.ID)
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (qb *studioQueryBuilder) UpdateFull(ctx context.Context, updatedObject models.Studio) (*models.Studio, error) {
|
2021-01-18 01:23:20 +00:00
|
|
|
const partial = false
|
2022-05-19 07:49:32 +00:00
|
|
|
if err := qb.update(ctx, updatedObject.ID, updatedObject, partial); err != nil {
|
2021-01-18 01:23:20 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
return qb.Find(ctx, updatedObject.ID)
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (qb *studioQueryBuilder) Destroy(ctx context.Context, id int) error {
|
2021-01-18 01:23:20 +00:00
|
|
|
// TODO - set null on foreign key in scraped items
|
|
|
|
// remove studio from scraped items
|
2022-05-19 07:49:32 +00:00
|
|
|
_, err := qb.tx.Exec(ctx, "UPDATE scraped_items SET studio_id = null WHERE studio_id = ?", id)
|
2021-01-18 01:23:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
return qb.destroyExisting(ctx, []int{id})
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (qb *studioQueryBuilder) Find(ctx context.Context, id int) (*models.Studio, error) {
|
2021-01-18 01:23:20 +00:00
|
|
|
var ret models.Studio
|
2022-05-19 07:49:32 +00:00
|
|
|
if err := qb.getByID(ctx, 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
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (qb *studioQueryBuilder) FindMany(ctx context.Context, ids []int) ([]*models.Studio, error) {
|
2022-08-12 02:21:46 +00:00
|
|
|
tableMgr := studioTableMgr
|
|
|
|
q := goqu.Select("*").From(tableMgr.table).Where(tableMgr.byIDInts(ids...))
|
|
|
|
unsorted, err := qb.getMany(ctx, q)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := make([]*models.Studio, len(ids))
|
|
|
|
|
|
|
|
for _, s := range unsorted {
|
|
|
|
i := intslice.IntIndex(ids, s.ID)
|
|
|
|
ret[i] = s
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range ret {
|
|
|
|
if ret[i] == nil {
|
|
|
|
return nil, fmt.Errorf("studio with id %d not found", ids[i])
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
2022-08-12 02:21:46 +00:00
|
|
|
}
|
2021-01-18 01:23:20 +00:00
|
|
|
|
2022-08-12 02:21:46 +00:00
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *studioQueryBuilder) getMany(ctx context.Context, q *goqu.SelectDataset) ([]*models.Studio, error) {
|
|
|
|
const single = false
|
|
|
|
var ret []*models.Studio
|
|
|
|
if err := queryFunc(ctx, q, single, func(r *sqlx.Rows) error {
|
|
|
|
var f models.Studio
|
|
|
|
if err := r.StructScan(&f); err != nil {
|
|
|
|
return err
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2022-08-12 02:21:46 +00:00
|
|
|
ret = append(ret, &f)
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2022-08-12 02:21:46 +00:00
|
|
|
return ret, nil
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (qb *studioQueryBuilder) FindChildren(ctx context.Context, id int) ([]*models.Studio, error) {
|
2021-01-18 01:23:20 +00:00
|
|
|
query := "SELECT studios.* FROM studios WHERE studios.parent_id = ?"
|
|
|
|
args := []interface{}{id}
|
2022-05-19 07:49:32 +00:00
|
|
|
return qb.queryStudios(ctx, query, args)
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (qb *studioQueryBuilder) FindBySceneID(ctx context.Context, sceneID int) (*models.Studio, error) {
|
2021-01-18 01:23:20 +00:00
|
|
|
query := "SELECT studios.* FROM studios JOIN scenes ON studios.id = scenes.studio_id WHERE scenes.id = ? LIMIT 1"
|
|
|
|
args := []interface{}{sceneID}
|
2022-05-19 07:49:32 +00:00
|
|
|
return qb.queryStudio(ctx, query, args)
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (qb *studioQueryBuilder) FindByName(ctx context.Context, name string, nocase bool) (*models.Studio, error) {
|
2021-01-18 01:23:20 +00:00
|
|
|
query := "SELECT * FROM studios WHERE name = ?"
|
|
|
|
if nocase {
|
|
|
|
query += " COLLATE NOCASE"
|
|
|
|
}
|
|
|
|
query += " LIMIT 1"
|
|
|
|
args := []interface{}{name}
|
2022-05-19 07:49:32 +00:00
|
|
|
return qb.queryStudio(ctx, query, args)
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (qb *studioQueryBuilder) FindByStashID(ctx context.Context, stashID models.StashID) ([]*models.Studio, error) {
|
2021-11-14 20:51:52 +00:00
|
|
|
query := selectAll("studios") + `
|
|
|
|
LEFT JOIN studio_stash_ids on studio_stash_ids.studio_id = studios.id
|
|
|
|
WHERE studio_stash_ids.stash_id = ?
|
|
|
|
AND studio_stash_ids.endpoint = ?
|
|
|
|
`
|
|
|
|
args := []interface{}{stashID.StashID, stashID.Endpoint}
|
2022-05-19 07:49:32 +00:00
|
|
|
return qb.queryStudios(ctx, query, args)
|
2021-11-14 20:51:52 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (qb *studioQueryBuilder) Count(ctx context.Context) (int, error) {
|
|
|
|
return qb.runCountQuery(ctx, qb.buildCountQuery("SELECT studios.id FROM studios"), nil)
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (qb *studioQueryBuilder) All(ctx context.Context) ([]*models.Studio, error) {
|
|
|
|
return qb.queryStudios(ctx, selectAll("studios")+qb.getStudioSort(nil), nil)
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (qb *studioQueryBuilder) QueryForAutoTag(ctx context.Context, words []string) ([]*models.Studio, error) {
|
2021-04-26 02:51:31 +00:00
|
|
|
// TODO - Query needs to be changed to support queries of this type, and
|
|
|
|
// this method should be removed
|
|
|
|
query := selectAll(studioTable)
|
2021-09-09 08:13:42 +00:00
|
|
|
query += " LEFT JOIN studio_aliases ON studio_aliases.studio_id = studios.id"
|
2021-04-26 02:51:31 +00:00
|
|
|
|
|
|
|
var whereClauses []string
|
|
|
|
var args []interface{}
|
|
|
|
|
|
|
|
for _, w := range words {
|
2021-09-09 08:13:42 +00:00
|
|
|
ww := w + "%"
|
|
|
|
whereClauses = append(whereClauses, "studios.name like ?")
|
|
|
|
args = append(args, ww)
|
|
|
|
|
|
|
|
// include aliases
|
|
|
|
whereClauses = append(whereClauses, "studio_aliases.alias like ?")
|
|
|
|
args = append(args, ww)
|
2021-04-26 02:51:31 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 10:03:39 +00:00
|
|
|
whereOr := "(" + strings.Join(whereClauses, " OR ") + ")"
|
|
|
|
where := strings.Join([]string{
|
|
|
|
"studios.ignore_auto_tag = 0",
|
|
|
|
whereOr,
|
|
|
|
}, " AND ")
|
2022-05-19 07:49:32 +00:00
|
|
|
return qb.queryStudios(ctx, query+" WHERE "+where, args)
|
2021-04-26 02:51:31 +00:00
|
|
|
}
|
|
|
|
|
2021-09-16 02:09:23 +00:00
|
|
|
func (qb *studioQueryBuilder) validateFilter(filter *models.StudioFilterType) error {
|
|
|
|
const and = "AND"
|
|
|
|
const or = "OR"
|
|
|
|
const not = "NOT"
|
|
|
|
|
|
|
|
if filter.And != nil {
|
|
|
|
if filter.Or != nil {
|
|
|
|
return illegalFilterCombination(and, or)
|
|
|
|
}
|
|
|
|
if filter.Not != nil {
|
|
|
|
return illegalFilterCombination(and, not)
|
|
|
|
}
|
|
|
|
|
|
|
|
return qb.validateFilter(filter.And)
|
|
|
|
}
|
|
|
|
|
|
|
|
if filter.Or != nil {
|
|
|
|
if filter.Not != nil {
|
|
|
|
return illegalFilterCombination(or, not)
|
|
|
|
}
|
|
|
|
|
|
|
|
return qb.validateFilter(filter.Or)
|
|
|
|
}
|
|
|
|
|
|
|
|
if filter.Not != nil {
|
|
|
|
return qb.validateFilter(filter.Not)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (qb *studioQueryBuilder) makeFilter(ctx context.Context, studioFilter *models.StudioFilterType) *filterBuilder {
|
2021-09-09 08:13:42 +00:00
|
|
|
query := &filterBuilder{}
|
|
|
|
|
2021-09-16 02:09:23 +00:00
|
|
|
if studioFilter.And != nil {
|
2022-05-19 07:49:32 +00:00
|
|
|
query.and(qb.makeFilter(ctx, studioFilter.And))
|
2021-09-16 02:09:23 +00:00
|
|
|
}
|
|
|
|
if studioFilter.Or != nil {
|
2022-05-19 07:49:32 +00:00
|
|
|
query.or(qb.makeFilter(ctx, studioFilter.Or))
|
2021-09-16 02:09:23 +00:00
|
|
|
}
|
|
|
|
if studioFilter.Not != nil {
|
2022-05-19 07:49:32 +00:00
|
|
|
query.not(qb.makeFilter(ctx, studioFilter.Not))
|
2021-09-16 02:09:23 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
query.handleCriterion(ctx, stringCriterionHandler(studioFilter.Name, studioTable+".name"))
|
|
|
|
query.handleCriterion(ctx, stringCriterionHandler(studioFilter.Details, studioTable+".details"))
|
|
|
|
query.handleCriterion(ctx, stringCriterionHandler(studioFilter.URL, studioTable+".url"))
|
2022-08-08 04:24:08 +00:00
|
|
|
query.handleCriterion(ctx, intCriterionHandler(studioFilter.Rating, studioTable+".rating", nil))
|
|
|
|
query.handleCriterion(ctx, boolCriterionHandler(studioFilter.IgnoreAutoTag, studioTable+".ignore_auto_tag", nil))
|
2021-09-09 08:13:42 +00:00
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
query.handleCriterion(ctx, criterionHandlerFunc(func(ctx context.Context, f *filterBuilder) {
|
2021-09-09 08:13:42 +00:00
|
|
|
if studioFilter.StashID != nil {
|
|
|
|
qb.stashIDRepository().join(f, "studio_stash_ids", "studios.id")
|
2022-05-19 07:49:32 +00:00
|
|
|
stringCriterionHandler(studioFilter.StashID, "studio_stash_ids.stash_id")(ctx, f)
|
2021-09-09 08:13:42 +00:00
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
query.handleCriterion(ctx, studioIsMissingCriterionHandler(qb, studioFilter.IsMissing))
|
|
|
|
query.handleCriterion(ctx, studioSceneCountCriterionHandler(qb, studioFilter.SceneCount))
|
|
|
|
query.handleCriterion(ctx, studioImageCountCriterionHandler(qb, studioFilter.ImageCount))
|
|
|
|
query.handleCriterion(ctx, studioGalleryCountCriterionHandler(qb, studioFilter.GalleryCount))
|
|
|
|
query.handleCriterion(ctx, studioParentCriterionHandler(qb, studioFilter.Parents))
|
|
|
|
query.handleCriterion(ctx, studioAliasCriterionHandler(qb, studioFilter.Aliases))
|
2021-09-09 08:13:42 +00:00
|
|
|
|
|
|
|
return query
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (qb *studioQueryBuilder) Query(ctx context.Context, studioFilter *models.StudioFilterType, findFilter *models.FindFilterType) ([]*models.Studio, int, error) {
|
2021-01-18 01:23:20 +00:00
|
|
|
if studioFilter == nil {
|
|
|
|
studioFilter = &models.StudioFilterType{}
|
|
|
|
}
|
|
|
|
if findFilter == nil {
|
|
|
|
findFilter = &models.FindFilterType{}
|
|
|
|
}
|
|
|
|
|
2021-04-09 05:05:11 +00:00
|
|
|
query := qb.newQuery()
|
2021-10-25 00:40:13 +00:00
|
|
|
distinctIDs(&query, studioTable)
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
if q := findFilter.Q; q != nil && *q != "" {
|
2021-09-09 08:13:42 +00:00
|
|
|
query.join(studioAliasesTable, "", "studio_aliases.studio_id = studios.id")
|
|
|
|
searchColumns := []string{"studios.name", "studio_aliases.alias"}
|
2021-01-18 01:23:20 +00:00
|
|
|
|
2021-11-22 03:59:22 +00:00
|
|
|
query.parseQueryString(searchColumns, *q)
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2021-09-16 02:09:23 +00:00
|
|
|
if err := qb.validateFilter(studioFilter); err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
2022-05-19 07:49:32 +00:00
|
|
|
filter := qb.makeFilter(ctx, studioFilter)
|
2021-04-09 05:05:11 +00:00
|
|
|
|
2021-09-09 08:13:42 +00:00
|
|
|
query.addFilter(filter)
|
2021-01-18 01:23:20 +00:00
|
|
|
|
2021-04-09 05:05:11 +00:00
|
|
|
query.sortAndPagination = qb.getStudioSort(findFilter) + getPagination(findFilter)
|
2022-05-19 07:49:32 +00:00
|
|
|
idsResult, countResult, err := query.executeFind(ctx)
|
2021-01-18 01:23:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
2022-08-12 02:21:46 +00:00
|
|
|
studios, err := qb.FindMany(ctx, idsResult)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return studios, countResult, nil
|
|
|
|
}
|
|
|
|
|
2021-09-09 08:13:42 +00:00
|
|
|
func studioIsMissingCriterionHandler(qb *studioQueryBuilder, isMissing *string) criterionHandlerFunc {
|
2022-05-19 07:49:32 +00:00
|
|
|
return func(ctx context.Context, f *filterBuilder) {
|
2021-09-09 08:13:42 +00:00
|
|
|
if isMissing != nil && *isMissing != "" {
|
|
|
|
switch *isMissing {
|
|
|
|
case "image":
|
2021-12-06 01:30:40 +00:00
|
|
|
f.addLeftJoin("studios_image", "", "studios_image.studio_id = studios.id")
|
2021-09-09 08:13:42 +00:00
|
|
|
f.addWhere("studios_image.studio_id IS NULL")
|
|
|
|
case "stash_id":
|
|
|
|
qb.stashIDRepository().join(f, "studio_stash_ids", "studios.id")
|
|
|
|
f.addWhere("studio_stash_ids.studio_id IS NULL")
|
|
|
|
default:
|
|
|
|
f.addWhere("(studios." + *isMissing + " IS NULL OR TRIM(studios." + *isMissing + ") = '')")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func studioSceneCountCriterionHandler(qb *studioQueryBuilder, sceneCount *models.IntCriterionInput) criterionHandlerFunc {
|
2022-05-19 07:49:32 +00:00
|
|
|
return func(ctx context.Context, f *filterBuilder) {
|
2021-09-09 08:13:42 +00:00
|
|
|
if sceneCount != nil {
|
2021-12-06 01:30:40 +00:00
|
|
|
f.addLeftJoin("scenes", "", "scenes.studio_id = studios.id")
|
2021-09-09 08:13:42 +00:00
|
|
|
clause, args := getIntCriterionWhereClause("count(distinct scenes.id)", *sceneCount)
|
|
|
|
|
|
|
|
f.addHaving(clause, args...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func studioImageCountCriterionHandler(qb *studioQueryBuilder, imageCount *models.IntCriterionInput) criterionHandlerFunc {
|
2022-05-19 07:49:32 +00:00
|
|
|
return func(ctx context.Context, f *filterBuilder) {
|
2021-09-09 08:13:42 +00:00
|
|
|
if imageCount != nil {
|
2021-12-06 01:30:40 +00:00
|
|
|
f.addLeftJoin("images", "", "images.studio_id = studios.id")
|
2021-09-09 08:13:42 +00:00
|
|
|
clause, args := getIntCriterionWhereClause("count(distinct images.id)", *imageCount)
|
|
|
|
|
|
|
|
f.addHaving(clause, args...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func studioGalleryCountCriterionHandler(qb *studioQueryBuilder, galleryCount *models.IntCriterionInput) criterionHandlerFunc {
|
2022-05-19 07:49:32 +00:00
|
|
|
return func(ctx context.Context, f *filterBuilder) {
|
2021-09-09 08:13:42 +00:00
|
|
|
if galleryCount != nil {
|
2021-12-06 01:30:40 +00:00
|
|
|
f.addLeftJoin("galleries", "", "galleries.studio_id = studios.id")
|
2021-09-09 08:13:42 +00:00
|
|
|
clause, args := getIntCriterionWhereClause("count(distinct galleries.id)", *galleryCount)
|
|
|
|
|
|
|
|
f.addHaving(clause, args...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func studioParentCriterionHandler(qb *studioQueryBuilder, parents *models.MultiCriterionInput) criterionHandlerFunc {
|
|
|
|
addJoinsFunc := func(f *filterBuilder) {
|
2021-12-06 01:30:40 +00:00
|
|
|
f.addLeftJoin("studios", "parent_studio", "parent_studio.id = studios.parent_id")
|
2021-09-09 08:13:42 +00:00
|
|
|
}
|
|
|
|
h := multiCriterionHandlerBuilder{
|
|
|
|
primaryTable: studioTable,
|
|
|
|
foreignTable: "parent_studio",
|
|
|
|
joinTable: "",
|
|
|
|
primaryFK: studioIDColumn,
|
|
|
|
foreignFK: "parent_id",
|
|
|
|
addJoinsFunc: addJoinsFunc,
|
|
|
|
}
|
|
|
|
return h.handler(parents)
|
|
|
|
}
|
|
|
|
|
|
|
|
func studioAliasCriterionHandler(qb *studioQueryBuilder, alias *models.StringCriterionInput) criterionHandlerFunc {
|
|
|
|
h := stringListCriterionHandlerBuilder{
|
|
|
|
joinTable: studioAliasesTable,
|
|
|
|
stringColumn: studioAliasColumn,
|
|
|
|
addJoinTable: func(f *filterBuilder) {
|
|
|
|
qb.aliasRepository().join(f, "", "studios.id")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return h.handler(alias)
|
|
|
|
}
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
func (qb *studioQueryBuilder) getStudioSort(findFilter *models.FindFilterType) string {
|
|
|
|
var sort string
|
|
|
|
var direction string
|
|
|
|
if findFilter == nil {
|
|
|
|
sort = "name"
|
|
|
|
direction = "ASC"
|
|
|
|
} else {
|
|
|
|
sort = findFilter.GetSort("name")
|
|
|
|
direction = findFilter.GetDirection()
|
|
|
|
}
|
2021-04-20 06:48:36 +00:00
|
|
|
|
|
|
|
switch sort {
|
2021-09-09 09:44:02 +00:00
|
|
|
case "scenes_count":
|
|
|
|
return getCountSort(studioTable, sceneTable, studioIDColumn, direction)
|
2021-04-20 06:48:36 +00:00
|
|
|
case "images_count":
|
|
|
|
return getCountSort(studioTable, imageTable, studioIDColumn, direction)
|
|
|
|
case "galleries_count":
|
|
|
|
return getCountSort(studioTable, galleryTable, studioIDColumn, direction)
|
|
|
|
default:
|
|
|
|
return getSort(sort, direction, "studios")
|
|
|
|
}
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (qb *studioQueryBuilder) queryStudio(ctx context.Context, query string, args []interface{}) (*models.Studio, error) {
|
|
|
|
results, err := qb.queryStudios(ctx, query, args)
|
2021-01-18 01:23:20 +00:00
|
|
|
if err != nil || len(results) < 1 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return results[0], nil
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (qb *studioQueryBuilder) queryStudios(ctx context.Context, query string, args []interface{}) ([]*models.Studio, error) {
|
2021-01-18 01:23:20 +00:00
|
|
|
var ret models.Studios
|
2022-05-19 07:49:32 +00:00
|
|
|
if err := qb.query(ctx, query, args, &ret); err != nil {
|
2021-01-18 01:23:20 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return []*models.Studio(ret), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *studioQueryBuilder) imageRepository() *imageRepository {
|
|
|
|
return &imageRepository{
|
|
|
|
repository: repository{
|
|
|
|
tx: qb.tx,
|
|
|
|
tableName: "studios_image",
|
|
|
|
idColumn: studioIDColumn,
|
|
|
|
},
|
|
|
|
imageColumn: "image",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (qb *studioQueryBuilder) GetImage(ctx context.Context, studioID int) ([]byte, error) {
|
|
|
|
return qb.imageRepository().get(ctx, studioID)
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (qb *studioQueryBuilder) HasImage(ctx context.Context, studioID int) (bool, error) {
|
|
|
|
return qb.imageRepository().exists(ctx, studioID)
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (qb *studioQueryBuilder) UpdateImage(ctx context.Context, studioID int, image []byte) error {
|
|
|
|
return qb.imageRepository().replace(ctx, studioID, image)
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (qb *studioQueryBuilder) DestroyImage(ctx context.Context, studioID int) error {
|
|
|
|
return qb.imageRepository().destroy(ctx, []int{studioID})
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *studioQueryBuilder) stashIDRepository() *stashIDRepository {
|
|
|
|
return &stashIDRepository{
|
|
|
|
repository{
|
|
|
|
tx: qb.tx,
|
|
|
|
tableName: "studio_stash_ids",
|
|
|
|
idColumn: studioIDColumn,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-12 02:21:46 +00:00
|
|
|
func (qb *studioQueryBuilder) GetStashIDs(ctx context.Context, studioID int) ([]models.StashID, error) {
|
2022-05-19 07:49:32 +00:00
|
|
|
return qb.stashIDRepository().get(ctx, studioID)
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2022-08-12 02:21:46 +00:00
|
|
|
func (qb *studioQueryBuilder) UpdateStashIDs(ctx context.Context, studioID int, stashIDs []models.StashID) error {
|
2022-05-19 07:49:32 +00:00
|
|
|
return qb.stashIDRepository().replace(ctx, studioID, stashIDs)
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
2021-09-09 08:13:42 +00:00
|
|
|
|
|
|
|
func (qb *studioQueryBuilder) aliasRepository() *stringRepository {
|
|
|
|
return &stringRepository{
|
|
|
|
repository: repository{
|
|
|
|
tx: qb.tx,
|
|
|
|
tableName: studioAliasesTable,
|
|
|
|
idColumn: studioIDColumn,
|
|
|
|
},
|
|
|
|
stringColumn: studioAliasColumn,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (qb *studioQueryBuilder) GetAliases(ctx context.Context, studioID int) ([]string, error) {
|
|
|
|
return qb.aliasRepository().get(ctx, studioID)
|
2021-09-09 08:13:42 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (qb *studioQueryBuilder) UpdateAliases(ctx context.Context, studioID int, aliases []string) error {
|
|
|
|
return qb.aliasRepository().replace(ctx, studioID, aliases)
|
2021-09-09 08:13:42 +00:00
|
|
|
}
|