2019-02-09 12:30:49 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2020-07-19 01:59:18 +00:00
|
|
|
"fmt"
|
2019-02-09 12:30:49 +00:00
|
|
|
"strings"
|
2019-08-15 07:32:57 +00:00
|
|
|
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
"github.com/stashapp/stash/pkg/database"
|
2019-02-09 12:30:49 +00:00
|
|
|
)
|
|
|
|
|
2020-05-11 05:19:11 +00:00
|
|
|
const sceneTable = "scenes"
|
|
|
|
|
|
|
|
var scenesForPerformerQuery = selectAll(sceneTable) + `
|
2019-02-09 12:30:49 +00:00
|
|
|
LEFT JOIN performers_scenes as performers_join on performers_join.scene_id = scenes.id
|
2020-05-11 05:19:11 +00:00
|
|
|
WHERE performers_join.performer_id = ?
|
2019-02-09 12:30:49 +00:00
|
|
|
GROUP BY scenes.id
|
|
|
|
`
|
|
|
|
|
2020-05-11 05:19:11 +00:00
|
|
|
var countScenesForPerformerQuery = `
|
|
|
|
SELECT performer_id FROM performers_scenes as performers_join
|
|
|
|
WHERE performer_id = ?
|
|
|
|
GROUP BY scene_id
|
|
|
|
`
|
|
|
|
|
|
|
|
var scenesForStudioQuery = selectAll(sceneTable) + `
|
2019-02-09 12:30:49 +00:00
|
|
|
JOIN studios ON studios.id = scenes.studio_id
|
|
|
|
WHERE studios.id = ?
|
|
|
|
GROUP BY scenes.id
|
|
|
|
`
|
2020-05-11 05:19:11 +00:00
|
|
|
var scenesForMovieQuery = selectAll(sceneTable) + `
|
2020-03-10 03:28:15 +00:00
|
|
|
LEFT JOIN movies_scenes as movies_join on movies_join.scene_id = scenes.id
|
2020-05-11 05:19:11 +00:00
|
|
|
WHERE movies_join.movie_id = ?
|
2020-03-10 03:28:15 +00:00
|
|
|
GROUP BY scenes.id
|
|
|
|
`
|
2019-02-09 12:30:49 +00:00
|
|
|
|
2020-05-24 06:18:02 +00:00
|
|
|
var countScenesForTagQuery = `
|
|
|
|
SELECT tag_id AS id FROM scenes_tags
|
|
|
|
WHERE scenes_tags.tag_id = ?
|
|
|
|
GROUP BY scenes_tags.scene_id
|
2019-02-09 12:30:49 +00:00
|
|
|
`
|
|
|
|
|
2020-08-06 01:21:14 +00:00
|
|
|
var countScenesForMissingChecksumQuery = `
|
|
|
|
SELECT id FROM scenes
|
|
|
|
WHERE scenes.checksum is null
|
|
|
|
`
|
|
|
|
|
|
|
|
var countScenesForMissingOSHashQuery = `
|
|
|
|
SELECT id FROM scenes
|
|
|
|
WHERE scenes.oshash is null
|
|
|
|
`
|
|
|
|
|
2019-02-14 22:53:32 +00:00
|
|
|
type SceneQueryBuilder struct{}
|
2019-02-09 12:30:49 +00:00
|
|
|
|
2019-02-14 22:53:32 +00:00
|
|
|
func NewSceneQueryBuilder() SceneQueryBuilder {
|
|
|
|
return SceneQueryBuilder{}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2019-02-14 22:53:32 +00:00
|
|
|
func (qb *SceneQueryBuilder) Create(newScene Scene, tx *sqlx.Tx) (*Scene, error) {
|
2019-02-09 12:30:49 +00:00
|
|
|
ensureTx(tx)
|
|
|
|
result, err := tx.NamedExec(
|
2020-08-06 01:21:14 +00:00
|
|
|
`INSERT INTO scenes (oshash, checksum, path, title, details, url, date, rating, o_counter, size, duration, video_codec,
|
2020-06-22 23:19:19 +00:00
|
|
|
audio_codec, format, width, height, framerate, bitrate, studio_id, created_at, updated_at)
|
2020-08-06 01:21:14 +00:00
|
|
|
VALUES (:oshash, :checksum, :path, :title, :details, :url, :date, :rating, :o_counter, :size, :duration, :video_codec,
|
2020-06-22 23:19:19 +00:00
|
|
|
:audio_codec, :format, :width, :height, :framerate, :bitrate, :studio_id, :created_at, :updated_at)
|
2019-02-09 12:30:49 +00:00
|
|
|
`,
|
|
|
|
newScene,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sceneID, err := result.LastInsertId()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := tx.Get(&newScene, `SELECT * FROM scenes WHERE id = ? LIMIT 1`, sceneID); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &newScene, nil
|
|
|
|
}
|
|
|
|
|
2019-10-14 21:57:53 +00:00
|
|
|
func (qb *SceneQueryBuilder) Update(updatedScene ScenePartial, tx *sqlx.Tx) (*Scene, error) {
|
2019-02-09 12:30:49 +00:00
|
|
|
ensureTx(tx)
|
|
|
|
_, err := tx.NamedExec(
|
2019-10-14 21:57:53 +00:00
|
|
|
`UPDATE scenes SET `+SQLGenKeysPartial(updatedScene)+` WHERE scenes.id = :id`,
|
2019-02-09 12:30:49 +00:00
|
|
|
updatedScene,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-10-14 23:54:05 +00:00
|
|
|
return qb.find(updatedScene.ID, tx)
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2020-09-20 08:36:02 +00:00
|
|
|
func (qb *SceneQueryBuilder) UpdateFull(updatedScene Scene, tx *sqlx.Tx) (*Scene, error) {
|
|
|
|
ensureTx(tx)
|
|
|
|
_, err := tx.NamedExec(
|
|
|
|
`UPDATE scenes SET `+SQLGenKeys(updatedScene)+` WHERE scenes.id = :id`,
|
|
|
|
updatedScene,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return qb.find(updatedScene.ID, tx)
|
|
|
|
}
|
|
|
|
|
2020-02-03 00:17:28 +00:00
|
|
|
func (qb *SceneQueryBuilder) IncrementOCounter(id int, tx *sqlx.Tx) (int, error) {
|
|
|
|
ensureTx(tx)
|
|
|
|
_, err := tx.Exec(
|
|
|
|
`UPDATE scenes SET o_counter = o_counter + 1 WHERE scenes.id = ?`,
|
|
|
|
id,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
scene, err := qb.find(id, tx)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return scene.OCounter, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *SceneQueryBuilder) DecrementOCounter(id int, tx *sqlx.Tx) (int, error) {
|
|
|
|
ensureTx(tx)
|
|
|
|
_, err := tx.Exec(
|
|
|
|
`UPDATE scenes SET o_counter = o_counter - 1 WHERE scenes.id = ? and scenes.o_counter > 0`,
|
|
|
|
id,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
scene, err := qb.find(id, tx)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return scene.OCounter, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *SceneQueryBuilder) ResetOCounter(id int, tx *sqlx.Tx) (int, error) {
|
|
|
|
ensureTx(tx)
|
|
|
|
_, err := tx.Exec(
|
|
|
|
`UPDATE scenes SET o_counter = 0 WHERE scenes.id = ?`,
|
|
|
|
id,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
scene, err := qb.find(id, tx)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return scene.OCounter, nil
|
|
|
|
}
|
|
|
|
|
2019-08-15 07:32:57 +00:00
|
|
|
func (qb *SceneQueryBuilder) Destroy(id string, tx *sqlx.Tx) error {
|
2020-04-01 01:07:43 +00:00
|
|
|
_, err := tx.Exec("DELETE FROM movies_scenes WHERE scene_id = ?", id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-15 07:32:57 +00:00
|
|
|
return executeDeleteQuery("scenes", id, tx)
|
|
|
|
}
|
2019-02-14 22:53:32 +00:00
|
|
|
func (qb *SceneQueryBuilder) Find(id int) (*Scene, error) {
|
2019-10-14 23:54:05 +00:00
|
|
|
return qb.find(id, nil)
|
|
|
|
}
|
|
|
|
|
2020-07-19 01:59:18 +00:00
|
|
|
func (qb *SceneQueryBuilder) FindMany(ids []int) ([]*Scene, error) {
|
|
|
|
var scenes []*Scene
|
|
|
|
for _, id := range ids {
|
|
|
|
scene, err := qb.Find(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if scene == nil {
|
|
|
|
return nil, fmt.Errorf("scene with id %d not found", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
scenes = append(scenes, scene)
|
|
|
|
}
|
|
|
|
|
|
|
|
return scenes, nil
|
|
|
|
}
|
|
|
|
|
2019-10-14 23:54:05 +00:00
|
|
|
func (qb *SceneQueryBuilder) find(id int, tx *sqlx.Tx) (*Scene, error) {
|
2020-05-11 05:19:11 +00:00
|
|
|
query := selectAll(sceneTable) + "WHERE id = ? LIMIT 1"
|
2019-02-09 12:30:49 +00:00
|
|
|
args := []interface{}{id}
|
2019-10-14 23:54:05 +00:00
|
|
|
return qb.queryScene(query, args, tx)
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2019-02-14 22:53:32 +00:00
|
|
|
func (qb *SceneQueryBuilder) FindByChecksum(checksum string) (*Scene, error) {
|
2019-02-09 12:30:49 +00:00
|
|
|
query := "SELECT * FROM scenes WHERE checksum = ? LIMIT 1"
|
|
|
|
args := []interface{}{checksum}
|
|
|
|
return qb.queryScene(query, args, nil)
|
|
|
|
}
|
|
|
|
|
2020-08-06 01:21:14 +00:00
|
|
|
func (qb *SceneQueryBuilder) FindByOSHash(oshash string) (*Scene, error) {
|
|
|
|
query := "SELECT * FROM scenes WHERE oshash = ? LIMIT 1"
|
|
|
|
args := []interface{}{oshash}
|
|
|
|
return qb.queryScene(query, args, nil)
|
|
|
|
}
|
|
|
|
|
2019-02-14 22:53:32 +00:00
|
|
|
func (qb *SceneQueryBuilder) FindByPath(path string) (*Scene, error) {
|
2020-05-11 05:19:11 +00:00
|
|
|
query := selectAll(sceneTable) + "WHERE path = ? LIMIT 1"
|
2019-02-09 12:30:49 +00:00
|
|
|
args := []interface{}{path}
|
|
|
|
return qb.queryScene(query, args, nil)
|
|
|
|
}
|
|
|
|
|
2019-05-27 19:34:26 +00:00
|
|
|
func (qb *SceneQueryBuilder) FindByPerformerID(performerID int) ([]*Scene, error) {
|
2019-02-09 12:30:49 +00:00
|
|
|
args := []interface{}{performerID}
|
|
|
|
return qb.queryScenes(scenesForPerformerQuery, args, nil)
|
|
|
|
}
|
|
|
|
|
2019-02-14 22:53:32 +00:00
|
|
|
func (qb *SceneQueryBuilder) CountByPerformerID(performerID int) (int, error) {
|
2019-02-09 12:30:49 +00:00
|
|
|
args := []interface{}{performerID}
|
2020-05-11 05:19:11 +00:00
|
|
|
return runCountQuery(buildCountQuery(countScenesForPerformerQuery), args)
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 19:34:26 +00:00
|
|
|
func (qb *SceneQueryBuilder) FindByStudioID(studioID int) ([]*Scene, error) {
|
2019-02-09 12:30:49 +00:00
|
|
|
args := []interface{}{studioID}
|
|
|
|
return qb.queryScenes(scenesForStudioQuery, args, nil)
|
|
|
|
}
|
|
|
|
|
2020-03-10 03:28:15 +00:00
|
|
|
func (qb *SceneQueryBuilder) FindByMovieID(movieID int) ([]*Scene, error) {
|
|
|
|
args := []interface{}{movieID}
|
|
|
|
return qb.queryScenes(scenesForMovieQuery, args, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *SceneQueryBuilder) CountByMovieID(movieID int) (int, error) {
|
|
|
|
args := []interface{}{movieID}
|
|
|
|
return runCountQuery(buildCountQuery(scenesForMovieQuery), args)
|
|
|
|
}
|
|
|
|
|
2019-02-14 22:53:32 +00:00
|
|
|
func (qb *SceneQueryBuilder) Count() (int, error) {
|
2019-02-11 20:36:10 +00:00
|
|
|
return runCountQuery(buildCountQuery("SELECT scenes.id FROM scenes"), nil)
|
|
|
|
}
|
|
|
|
|
2020-10-19 23:11:15 +00:00
|
|
|
func (qb *SceneQueryBuilder) Size() (uint64, error) {
|
|
|
|
return runSumQuery("SELECT SUM(size) as sum FROM scenes", nil)
|
2020-04-03 02:44:17 +00:00
|
|
|
}
|
|
|
|
|
2019-02-14 22:53:32 +00:00
|
|
|
func (qb *SceneQueryBuilder) CountByStudioID(studioID int) (int, error) {
|
2019-02-09 12:30:49 +00:00
|
|
|
args := []interface{}{studioID}
|
|
|
|
return runCountQuery(buildCountQuery(scenesForStudioQuery), args)
|
|
|
|
}
|
|
|
|
|
2019-02-14 22:53:32 +00:00
|
|
|
func (qb *SceneQueryBuilder) CountByTagID(tagID int) (int, error) {
|
2019-02-09 12:30:49 +00:00
|
|
|
args := []interface{}{tagID}
|
2020-05-24 06:18:02 +00:00
|
|
|
return runCountQuery(buildCountQuery(countScenesForTagQuery), args)
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2020-08-06 01:21:14 +00:00
|
|
|
// CountMissingChecksum returns the number of scenes missing a checksum value.
|
|
|
|
func (qb *SceneQueryBuilder) CountMissingChecksum() (int, error) {
|
|
|
|
return runCountQuery(buildCountQuery(countScenesForMissingChecksumQuery), []interface{}{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// CountMissingOSHash returns the number of scenes missing an oshash value.
|
|
|
|
func (qb *SceneQueryBuilder) CountMissingOSHash() (int, error) {
|
|
|
|
return runCountQuery(buildCountQuery(countScenesForMissingOSHashQuery), []interface{}{})
|
|
|
|
}
|
|
|
|
|
2019-05-27 19:34:26 +00:00
|
|
|
func (qb *SceneQueryBuilder) Wall(q *string) ([]*Scene, error) {
|
2019-02-09 12:30:49 +00:00
|
|
|
s := ""
|
|
|
|
if q != nil {
|
|
|
|
s = *q
|
|
|
|
}
|
2020-05-11 05:19:11 +00:00
|
|
|
query := selectAll(sceneTable) + "WHERE scenes.details LIKE '%" + s + "%' ORDER BY RANDOM() LIMIT 80"
|
2019-02-09 12:30:49 +00:00
|
|
|
return qb.queryScenes(query, nil, nil)
|
|
|
|
}
|
|
|
|
|
2019-05-27 19:34:26 +00:00
|
|
|
func (qb *SceneQueryBuilder) All() ([]*Scene, error) {
|
2020-05-11 05:19:11 +00:00
|
|
|
return qb.queryScenes(selectAll(sceneTable)+qb.getSceneSort(nil), nil, nil)
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 19:34:26 +00:00
|
|
|
func (qb *SceneQueryBuilder) Query(sceneFilter *SceneFilterType, findFilter *FindFilterType) ([]*Scene, int) {
|
2019-02-09 12:30:49 +00:00
|
|
|
if sceneFilter == nil {
|
|
|
|
sceneFilter = &SceneFilterType{}
|
|
|
|
}
|
|
|
|
if findFilter == nil {
|
|
|
|
findFilter = &FindFilterType{}
|
|
|
|
}
|
|
|
|
|
2020-05-11 05:19:11 +00:00
|
|
|
query := queryBuilder{
|
|
|
|
tableName: sceneTable,
|
|
|
|
}
|
|
|
|
|
|
|
|
query.body = selectDistinctIDs(sceneTable)
|
|
|
|
query.body += `
|
2019-02-09 12:30:49 +00:00
|
|
|
left join scene_markers on scene_markers.scene_id = scenes.id
|
|
|
|
left join performers_scenes as performers_join on performers_join.scene_id = scenes.id
|
2020-03-10 03:28:15 +00:00
|
|
|
left join movies_scenes as movies_join on movies_join.scene_id = scenes.id
|
2019-02-09 12:30:49 +00:00
|
|
|
left join studios as studio on studio.id = scenes.studio_id
|
|
|
|
left join galleries as gallery on gallery.scene_id = scenes.id
|
|
|
|
left join scenes_tags as tags_join on tags_join.scene_id = scenes.id
|
|
|
|
`
|
|
|
|
|
|
|
|
if q := findFilter.Q; q != nil && *q != "" {
|
2020-08-06 01:21:14 +00:00
|
|
|
searchColumns := []string{"scenes.title", "scenes.details", "scenes.path", "scenes.oshash", "scenes.checksum", "scene_markers.title"}
|
2020-03-02 22:18:14 +00:00
|
|
|
clause, thisArgs := getSearchBinding(searchColumns, *q, false)
|
2020-05-11 05:19:11 +00:00
|
|
|
query.addWhere(clause)
|
|
|
|
query.addArg(thisArgs...)
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-11 21:19:51 +00:00
|
|
|
query.handleStringCriterionInput(sceneFilter.Path, "scenes.path")
|
|
|
|
query.handleIntCriterionInput(sceneFilter.Rating, "scenes.rating")
|
|
|
|
query.handleIntCriterionInput(sceneFilter.OCounter, "scenes.o_counter")
|
2020-02-03 00:17:28 +00:00
|
|
|
|
2020-01-13 16:43:14 +00:00
|
|
|
if durationFilter := sceneFilter.Duration; durationFilter != nil {
|
|
|
|
clause, thisArgs := getDurationWhereClause(*durationFilter)
|
2020-05-11 05:19:11 +00:00
|
|
|
query.addWhere(clause)
|
|
|
|
query.addArg(thisArgs...)
|
2020-01-13 16:43:14 +00:00
|
|
|
}
|
|
|
|
|
2019-02-09 12:30:49 +00:00
|
|
|
if resolutionFilter := sceneFilter.Resolution; resolutionFilter != nil {
|
|
|
|
if resolution := resolutionFilter.String(); resolutionFilter.IsValid() {
|
|
|
|
switch resolution {
|
|
|
|
case "LOW":
|
2020-05-11 05:19:11 +00:00
|
|
|
query.addWhere("scenes.height < 480")
|
2019-02-09 12:30:49 +00:00
|
|
|
case "STANDARD":
|
2020-05-11 05:19:11 +00:00
|
|
|
query.addWhere("(scenes.height >= 480 AND scenes.height < 720)")
|
2019-02-09 12:30:49 +00:00
|
|
|
case "STANDARD_HD":
|
2020-05-11 05:19:11 +00:00
|
|
|
query.addWhere("(scenes.height >= 720 AND scenes.height < 1080)")
|
2019-02-09 12:30:49 +00:00
|
|
|
case "FULL_HD":
|
2020-05-11 05:19:11 +00:00
|
|
|
query.addWhere("(scenes.height >= 1080 AND scenes.height < 2160)")
|
2019-02-09 12:30:49 +00:00
|
|
|
case "FOUR_K":
|
2020-05-11 05:19:11 +00:00
|
|
|
query.addWhere("scenes.height >= 2160")
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasMarkersFilter := sceneFilter.HasMarkers; hasMarkersFilter != nil {
|
|
|
|
if strings.Compare(*hasMarkersFilter, "true") == 0 {
|
2020-05-11 05:19:11 +00:00
|
|
|
query.addHaving("count(scene_markers.scene_id) > 0")
|
2019-02-09 12:30:49 +00:00
|
|
|
} else {
|
2020-05-11 05:19:11 +00:00
|
|
|
query.addWhere("scene_markers.id IS NULL")
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if isMissingFilter := sceneFilter.IsMissing; isMissingFilter != nil && *isMissingFilter != "" {
|
|
|
|
switch *isMissingFilter {
|
|
|
|
case "gallery":
|
2020-05-11 05:19:11 +00:00
|
|
|
query.addWhere("gallery.scene_id IS NULL")
|
2019-02-09 12:30:49 +00:00
|
|
|
case "studio":
|
2020-05-11 05:19:11 +00:00
|
|
|
query.addWhere("scenes.studio_id IS NULL")
|
2020-03-10 03:28:15 +00:00
|
|
|
case "movie":
|
2020-05-11 05:19:11 +00:00
|
|
|
query.addWhere("movies_join.scene_id IS NULL")
|
2019-02-09 12:30:49 +00:00
|
|
|
case "performers":
|
2020-05-11 05:19:11 +00:00
|
|
|
query.addWhere("performers_join.scene_id IS NULL")
|
2019-09-15 21:52:02 +00:00
|
|
|
case "date":
|
2020-05-11 05:19:11 +00:00
|
|
|
query.addWhere("scenes.date IS \"\" OR scenes.date IS \"0001-01-01\"")
|
2020-05-09 02:21:08 +00:00
|
|
|
case "tags":
|
2020-05-11 05:19:11 +00:00
|
|
|
query.addWhere("tags_join.scene_id IS NULL")
|
2019-02-09 12:30:49 +00:00
|
|
|
default:
|
2020-10-16 00:14:48 +00:00
|
|
|
query.addWhere("scenes." + *isMissingFilter + " IS NULL OR TRIM(scenes." + *isMissingFilter + ") = ''")
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-27 13:05:54 +00:00
|
|
|
if tagsFilter := sceneFilter.Tags; tagsFilter != nil && len(tagsFilter.Value) > 0 {
|
|
|
|
for _, tagID := range tagsFilter.Value {
|
2020-05-11 05:19:11 +00:00
|
|
|
query.addArg(tagID)
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 05:19:11 +00:00
|
|
|
query.body += " LEFT JOIN tags on tags_join.tag_id = tags.id"
|
2020-06-15 11:34:39 +00:00
|
|
|
whereClause, havingClause := getMultiCriterionClause("scenes", "tags", "scenes_tags", "scene_id", "tag_id", tagsFilter)
|
2020-05-11 05:19:11 +00:00
|
|
|
query.addWhere(whereClause)
|
|
|
|
query.addHaving(havingClause)
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2019-10-27 13:05:54 +00:00
|
|
|
if performersFilter := sceneFilter.Performers; performersFilter != nil && len(performersFilter.Value) > 0 {
|
|
|
|
for _, performerID := range performersFilter.Value {
|
2020-05-11 05:19:11 +00:00
|
|
|
query.addArg(performerID)
|
2019-10-27 13:05:54 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 05:19:11 +00:00
|
|
|
query.body += " LEFT JOIN performers ON performers_join.performer_id = performers.id"
|
2020-06-15 11:34:39 +00:00
|
|
|
whereClause, havingClause := getMultiCriterionClause("scenes", "performers", "performers_scenes", "scene_id", "performer_id", performersFilter)
|
2020-05-11 05:19:11 +00:00
|
|
|
query.addWhere(whereClause)
|
|
|
|
query.addHaving(havingClause)
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2019-10-27 13:05:54 +00:00
|
|
|
if studiosFilter := sceneFilter.Studios; studiosFilter != nil && len(studiosFilter.Value) > 0 {
|
|
|
|
for _, studioID := range studiosFilter.Value {
|
2020-05-11 05:19:11 +00:00
|
|
|
query.addArg(studioID)
|
2019-10-27 13:05:54 +00:00
|
|
|
}
|
2019-11-07 12:49:08 +00:00
|
|
|
|
2020-06-15 11:34:39 +00:00
|
|
|
whereClause, havingClause := getMultiCriterionClause("scenes", "studio", "", "", "studio_id", studiosFilter)
|
2020-05-11 05:19:11 +00:00
|
|
|
query.addWhere(whereClause)
|
|
|
|
query.addHaving(havingClause)
|
2020-03-10 03:28:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if moviesFilter := sceneFilter.Movies; moviesFilter != nil && len(moviesFilter.Value) > 0 {
|
|
|
|
for _, movieID := range moviesFilter.Value {
|
2020-05-11 05:19:11 +00:00
|
|
|
query.addArg(movieID)
|
2020-03-10 03:28:15 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 05:19:11 +00:00
|
|
|
query.body += " LEFT JOIN movies ON movies_join.movie_id = movies.id"
|
2020-06-15 11:34:39 +00:00
|
|
|
whereClause, havingClause := getMultiCriterionClause("scenes", "movies", "movies_scenes", "scene_id", "movie_id", moviesFilter)
|
2020-05-11 05:19:11 +00:00
|
|
|
query.addWhere(whereClause)
|
|
|
|
query.addHaving(havingClause)
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 05:19:11 +00:00
|
|
|
query.sortAndPagination = qb.getSceneSort(findFilter) + getPagination(findFilter)
|
|
|
|
idsResult, countResult := query.executeFind()
|
2019-02-09 12:30:49 +00:00
|
|
|
|
2019-05-27 19:34:26 +00:00
|
|
|
var scenes []*Scene
|
2019-02-09 12:30:49 +00:00
|
|
|
for _, id := range idsResult {
|
|
|
|
scene, _ := qb.Find(id)
|
2019-05-27 19:34:26 +00:00
|
|
|
scenes = append(scenes, scene)
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return scenes, countResult
|
|
|
|
}
|
|
|
|
|
2019-10-27 13:05:54 +00:00
|
|
|
func appendClause(clauses []string, clause string) []string {
|
|
|
|
if clause != "" {
|
|
|
|
return append(clauses, clause)
|
|
|
|
}
|
|
|
|
|
|
|
|
return clauses
|
|
|
|
}
|
|
|
|
|
2020-01-13 16:43:14 +00:00
|
|
|
func getDurationWhereClause(durationFilter IntCriterionInput) (string, []interface{}) {
|
|
|
|
// special case for duration. We accept duration as seconds as int but the
|
|
|
|
// field is floating point. Change the equals filter to return a range
|
|
|
|
// between x and x + 1
|
|
|
|
// likewise, not equals needs to be duration < x OR duration >= x
|
|
|
|
var clause string
|
|
|
|
args := []interface{}{}
|
|
|
|
|
|
|
|
value := durationFilter.Value
|
|
|
|
if durationFilter.Modifier == CriterionModifierEquals {
|
|
|
|
clause = "scenes.duration >= ? AND scenes.duration < ?"
|
|
|
|
args = append(args, value)
|
|
|
|
args = append(args, value+1)
|
|
|
|
} else if durationFilter.Modifier == CriterionModifierNotEquals {
|
|
|
|
clause = "(scenes.duration < ? OR scenes.duration >= ?)"
|
|
|
|
args = append(args, value)
|
|
|
|
args = append(args, value+1)
|
|
|
|
} else {
|
|
|
|
var count int
|
|
|
|
clause, count = getIntCriterionWhereClause("scenes.duration", durationFilter)
|
|
|
|
if count == 1 {
|
|
|
|
args = append(args, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return clause, args
|
|
|
|
}
|
|
|
|
|
2019-12-01 16:18:44 +00:00
|
|
|
func (qb *SceneQueryBuilder) QueryAllByPathRegex(regex string) ([]*Scene, error) {
|
|
|
|
var args []interface{}
|
2020-01-06 18:02:25 +00:00
|
|
|
body := selectDistinctIDs("scenes") + " WHERE scenes.path regexp ?"
|
|
|
|
|
|
|
|
args = append(args, "(?i)"+regex)
|
2019-12-01 16:18:44 +00:00
|
|
|
|
|
|
|
idsResult, err := runIdsQuery(body, args)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var scenes []*Scene
|
|
|
|
for _, id := range idsResult {
|
|
|
|
scene, err := qb.Find(id)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
scenes = append(scenes, scene)
|
|
|
|
}
|
|
|
|
|
|
|
|
return scenes, nil
|
|
|
|
}
|
|
|
|
|
2019-10-30 13:37:21 +00:00
|
|
|
func (qb *SceneQueryBuilder) QueryByPathRegex(findFilter *FindFilterType) ([]*Scene, int) {
|
|
|
|
if findFilter == nil {
|
|
|
|
findFilter = &FindFilterType{}
|
|
|
|
}
|
|
|
|
|
|
|
|
var whereClauses []string
|
|
|
|
var havingClauses []string
|
|
|
|
var args []interface{}
|
|
|
|
body := selectDistinctIDs("scenes")
|
|
|
|
|
|
|
|
if q := findFilter.Q; q != nil && *q != "" {
|
2020-01-06 18:02:25 +00:00
|
|
|
whereClauses = append(whereClauses, "scenes.path regexp ?")
|
|
|
|
args = append(args, "(?i)"+*q)
|
2019-10-30 13:37:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sortAndPagination := qb.getSceneSort(findFilter) + getPagination(findFilter)
|
|
|
|
idsResult, countResult := executeFindQuery("scenes", body, args, sortAndPagination, whereClauses, havingClauses)
|
|
|
|
|
|
|
|
var scenes []*Scene
|
|
|
|
for _, id := range idsResult {
|
|
|
|
scene, _ := qb.Find(id)
|
|
|
|
scenes = append(scenes, scene)
|
|
|
|
}
|
|
|
|
|
|
|
|
return scenes, countResult
|
|
|
|
}
|
|
|
|
|
2019-02-14 22:53:32 +00:00
|
|
|
func (qb *SceneQueryBuilder) getSceneSort(findFilter *FindFilterType) string {
|
2019-02-09 12:30:49 +00:00
|
|
|
if findFilter == nil {
|
|
|
|
return " ORDER BY scenes.path, scenes.date ASC "
|
|
|
|
}
|
2019-02-14 22:53:32 +00:00
|
|
|
sort := findFilter.GetSort("title")
|
|
|
|
direction := findFilter.GetDirection()
|
|
|
|
return getSort(sort, direction, "scenes")
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
2019-02-14 22:53:32 +00:00
|
|
|
func (qb *SceneQueryBuilder) queryScene(query string, args []interface{}, tx *sqlx.Tx) (*Scene, error) {
|
2019-02-09 12:30:49 +00:00
|
|
|
results, err := qb.queryScenes(query, args, tx)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-05-27 19:34:26 +00:00
|
|
|
func (qb *SceneQueryBuilder) queryScenes(query string, args []interface{}, tx *sqlx.Tx) ([]*Scene, error) {
|
2019-02-09 12:30:49 +00:00
|
|
|
var rows *sqlx.Rows
|
|
|
|
var err error
|
|
|
|
if tx != nil {
|
|
|
|
rows, err = tx.Queryx(query, args...)
|
|
|
|
} else {
|
|
|
|
rows, err = database.DB.Queryx(query, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil && err != sql.ErrNoRows {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2019-05-27 19:34:26 +00:00
|
|
|
scenes := make([]*Scene, 0)
|
2019-02-09 12:30:49 +00:00
|
|
|
for rows.Next() {
|
2019-05-27 19:34:26 +00:00
|
|
|
scene := Scene{}
|
2019-02-09 12:30:49 +00:00
|
|
|
if err := rows.StructScan(&scene); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-05-27 19:34:26 +00:00
|
|
|
scenes = append(scenes, &scene)
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return scenes, nil
|
|
|
|
}
|
2020-04-09 22:38:34 +00:00
|
|
|
|
|
|
|
func (qb *SceneQueryBuilder) UpdateFormat(id int, format string, tx *sqlx.Tx) error {
|
|
|
|
ensureTx(tx)
|
|
|
|
_, err := tx.Exec(
|
|
|
|
`UPDATE scenes SET format = ? WHERE scenes.id = ? `,
|
|
|
|
format, id,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-06-22 23:19:19 +00:00
|
|
|
|
2020-08-06 01:21:14 +00:00
|
|
|
func (qb *SceneQueryBuilder) UpdateOSHash(id int, oshash string, tx *sqlx.Tx) error {
|
|
|
|
ensureTx(tx)
|
|
|
|
_, err := tx.Exec(
|
|
|
|
`UPDATE scenes SET oshash = ? WHERE scenes.id = ? `,
|
|
|
|
oshash, id,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *SceneQueryBuilder) UpdateChecksum(id int, checksum string, tx *sqlx.Tx) error {
|
|
|
|
ensureTx(tx)
|
|
|
|
_, err := tx.Exec(
|
|
|
|
`UPDATE scenes SET checksum = ? WHERE scenes.id = ? `,
|
|
|
|
checksum, id,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-22 23:19:19 +00:00
|
|
|
func (qb *SceneQueryBuilder) UpdateSceneCover(sceneID int, cover []byte, tx *sqlx.Tx) error {
|
|
|
|
ensureTx(tx)
|
|
|
|
|
|
|
|
// Delete the existing cover and then create new
|
|
|
|
if err := qb.DestroySceneCover(sceneID, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := tx.Exec(
|
|
|
|
`INSERT INTO scenes_cover (scene_id, cover) VALUES (?, ?)`,
|
|
|
|
sceneID,
|
|
|
|
cover,
|
|
|
|
)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *SceneQueryBuilder) DestroySceneCover(sceneID int, tx *sqlx.Tx) error {
|
|
|
|
ensureTx(tx)
|
|
|
|
|
|
|
|
// Delete the existing joins
|
|
|
|
_, err := tx.Exec("DELETE FROM scenes_cover WHERE scene_id = ?", sceneID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qb *SceneQueryBuilder) GetSceneCover(sceneID int, tx *sqlx.Tx) ([]byte, error) {
|
|
|
|
query := `SELECT cover from scenes_cover WHERE scene_id = ?`
|
|
|
|
return getImage(tx, query, sceneID)
|
|
|
|
}
|