fix: search titles rewritten

This commit is contained in:
Iron_Felix 2025-11-22 02:58:51 +03:00
parent e16adcf6cd
commit 73547187be

View file

@ -287,8 +287,23 @@ func (q *Queries) InsertTitleTags(ctx context.Context, arg InsertTitleTagsParams
const searchTitles = `-- name: SearchTitles :many const searchTitles = `-- name: SearchTitles :many
SELECT SELECT
id, title_names, studio_id, poster_id, title_status, rating, rating_count, release_year, release_season, season, episodes_aired, episodes_all, episodes_len t.id, t.title_names, t.studio_id, t.poster_id, t.title_status, t.rating, t.rating_count, t.release_year, t.release_season, t.season, t.episodes_aired, t.episodes_all, t.episodes_len,
FROM titles i.storage_type as title_storage_type,
i.image_path as title_image_path,
g.tag_names as tag_names,
s.studio_name as studio_name,
s.illust_id as studio_illust_id,
s.studio_desc as studio_desc,
si.storage_type as studio_storage_type,
si.image_path as studio_image_path
FROM titles as t
LEFT JOIN images as i ON (t.image_id = i.id)
LEFT JOIN title_tags as tt ON (t.id = tt.title_id)
LEFT JOIN tags as g ON (tt.tag_id = g.id)
LEFT JOIN studios as s ON (t.studio_id = s.id)
LEFT JOIN images as si ON (s.illust_id = si.id)
WHERE WHERE
CASE CASE
WHEN $1::boolean THEN WHEN $1::boolean THEN
@ -296,17 +311,17 @@ WHERE
CASE $2::text CASE $2::text
WHEN 'year' THEN WHEN 'year' THEN
($3::int IS NULL) OR ($3::int IS NULL) OR
(release_year > $3::int) OR (t.release_year > $3::int) OR
(release_year = $3::int AND id > $4::bigint) (t.release_year = $3::int AND t.id > $4::bigint)
WHEN 'rating' THEN WHEN 'rating' THEN
($5::float IS NULL) OR ($5::float IS NULL) OR
(rating > $5::float) OR (t.rating > $5::float) OR
(rating = $5::float AND id > $4::bigint) (t.rating = $5::float AND t.id > $4::bigint)
WHEN 'id' THEN WHEN 'id' THEN
($4::bigint IS NULL) OR ($4::bigint IS NULL) OR
(id > $4::bigint) (t.id > $4::bigint)
ELSE true -- fallback ELSE true -- fallback
END END
@ -316,17 +331,17 @@ WHERE
CASE $2::text CASE $2::text
WHEN 'year' THEN WHEN 'year' THEN
($3::int IS NULL) OR ($3::int IS NULL) OR
(release_year < $3::int) OR (t.release_year < $3::int) OR
(release_year = $3::int AND id < $4::bigint) (t.release_year = $3::int AND t.id < $4::bigint)
WHEN 'rating' THEN WHEN 'rating' THEN
($5::float IS NULL) OR ($5::float IS NULL) OR
(rating < $5::float) OR (t.rating < $5::float) OR
(rating = $5::float AND id < $4::bigint) (t.rating = $5::float AND t.id < $4::bigint)
WHEN 'id' THEN WHEN 'id' THEN
($4::bigint IS NULL) OR ($4::bigint IS NULL) OR
(id < $4::bigint) (t.id < $4::bigint)
ELSE true ELSE true
END END
@ -339,7 +354,7 @@ WHERE
SELECT bool_and( SELECT bool_and(
EXISTS ( EXISTS (
SELECT 1 SELECT 1
FROM jsonb_each_text(title_names) AS t(key, val) FROM jsonb_each_text(t.title_names) AS t(key, val)
WHERE val ILIKE pattern WHERE val ILIKE pattern
) )
) )
@ -355,28 +370,28 @@ WHERE
END END
) )
AND ($7::title_status_t IS NULL OR title_status = $7::title_status_t) AND ($7::title_status_t IS NULL OR t.title_status = $7::title_status_t)
AND ($8::float IS NULL OR rating >= $8::float) AND ($8::float IS NULL OR t.rating >= $8::float)
AND ($9::int IS NULL OR release_year = $9::int) AND ($9::int IS NULL OR t.release_year = $9::int)
AND ($10::release_season_t IS NULL OR release_season = $10::release_season_t) AND ($10::release_season_t IS NULL OR t.release_season = $10::release_season_t)
ORDER BY ORDER BY
CASE WHEN $1::boolean THEN CASE WHEN $1::boolean THEN
CASE CASE
WHEN $2::text = 'id' THEN id WHEN $2::text = 'id' THEN t.id
WHEN $2::text = 'year' THEN release_year WHEN $2::text = 'year' THEN t.release_year
WHEN $2::text = 'rating' THEN rating WHEN $2::text = 'rating' THEN t.rating
END END
END ASC, END ASC,
CASE WHEN NOT $1::boolean THEN CASE WHEN NOT $1::boolean THEN
CASE CASE
WHEN $2::text = 'id' THEN id WHEN $2::text = 'id' THEN t.id
WHEN $2::text = 'year' THEN release_year WHEN $2::text = 'year' THEN t.release_year
WHEN $2::text = 'rating' THEN rating WHEN $2::text = 'rating' THEN t.rating
END END
END DESC, END DESC,
CASE WHEN $2::text <> 'id' THEN id END ASC CASE WHEN $2::text <> 'id' THEN t.id END ASC
LIMIT COALESCE($11::int, 100) LIMIT COALESCE($11::int, 100)
` `
@ -395,7 +410,31 @@ type SearchTitlesParams struct {
Limit *int32 `json:"limit"` Limit *int32 `json:"limit"`
} }
func (q *Queries) SearchTitles(ctx context.Context, arg SearchTitlesParams) ([]Title, error) { type SearchTitlesRow struct {
ID int64 `json:"id"`
TitleNames []byte `json:"title_names"`
StudioID int64 `json:"studio_id"`
PosterID *int64 `json:"poster_id"`
TitleStatus TitleStatusT `json:"title_status"`
Rating *float64 `json:"rating"`
RatingCount *int32 `json:"rating_count"`
ReleaseYear *int32 `json:"release_year"`
ReleaseSeason *ReleaseSeasonT `json:"release_season"`
Season *int32 `json:"season"`
EpisodesAired *int32 `json:"episodes_aired"`
EpisodesAll *int32 `json:"episodes_all"`
EpisodesLen []byte `json:"episodes_len"`
TitleStorageType NullStorageTypeT `json:"title_storage_type"`
TitleImagePath *string `json:"title_image_path"`
TagNames []byte `json:"tag_names"`
StudioName *string `json:"studio_name"`
StudioIllustID *int64 `json:"studio_illust_id"`
StudioDesc *string `json:"studio_desc"`
StudioStorageType NullStorageTypeT `json:"studio_storage_type"`
StudioImagePath *string `json:"studio_image_path"`
}
func (q *Queries) SearchTitles(ctx context.Context, arg SearchTitlesParams) ([]SearchTitlesRow, error) {
rows, err := q.db.Query(ctx, searchTitles, rows, err := q.db.Query(ctx, searchTitles,
arg.Forward, arg.Forward,
arg.SortBy, arg.SortBy,
@ -413,9 +452,9 @@ func (q *Queries) SearchTitles(ctx context.Context, arg SearchTitlesParams) ([]T
return nil, err return nil, err
} }
defer rows.Close() defer rows.Close()
items := []Title{} items := []SearchTitlesRow{}
for rows.Next() { for rows.Next() {
var i Title var i SearchTitlesRow
if err := rows.Scan( if err := rows.Scan(
&i.ID, &i.ID,
&i.TitleNames, &i.TitleNames,
@ -430,6 +469,14 @@ func (q *Queries) SearchTitles(ctx context.Context, arg SearchTitlesParams) ([]T
&i.EpisodesAired, &i.EpisodesAired,
&i.EpisodesAll, &i.EpisodesAll,
&i.EpisodesLen, &i.EpisodesLen,
&i.TitleStorageType,
&i.TitleImagePath,
&i.TagNames,
&i.StudioName,
&i.StudioIllustID,
&i.StudioDesc,
&i.StudioStorageType,
&i.StudioImagePath,
); err != nil { ); err != nil {
return nil, err return nil, err
} }