This commit is contained in:
Iron_Felix 2025-11-25 04:42:56 +03:00
parent 3aafab36c2
commit dbdb52269a
7 changed files with 76 additions and 52 deletions

View file

@ -236,7 +236,7 @@ SELECT
i.image_path as image_path
FROM users as t
LEFT JOIN images as i ON (t.avatar_id = i.id)
WHERE id = $1::bigint
WHERE t.id = $1::bigint
`
type GetUserByIDRow struct {
@ -658,43 +658,45 @@ LEFT JOIN tags as g ON (tt.tag_id = g.id)
LEFT JOIN studios as s ON (t.studio_id = s.id)
WHERE
u.user_id = $1::bigint
AND
CASE
WHEN $1::boolean THEN
WHEN $2::boolean THEN
-- forward: greater than cursor (next page)
CASE $2::text
CASE $3::text
WHEN 'year' THEN
($3::int IS NULL) OR
(t.release_year > $3::int) OR
(t.release_year = $3::int AND t.id > $4::bigint)
($4::int IS NULL) OR
(t.release_year > $4::int) OR
(t.release_year = $4::int AND t.id > $5::bigint)
WHEN 'rating' THEN
($5::float IS NULL) OR
(t.rating > $5::float) OR
(t.rating = $5::float AND t.id > $4::bigint)
($6::float IS NULL) OR
(t.rating > $6::float) OR
(t.rating = $6::float AND t.id > $5::bigint)
WHEN 'id' THEN
($4::bigint IS NULL) OR
(t.id > $4::bigint)
($5::bigint IS NULL) OR
(t.id > $5::bigint)
ELSE true -- fallback
END
ELSE
-- backward: less than cursor (prev page)
CASE $2::text
CASE $3::text
WHEN 'year' THEN
($3::int IS NULL) OR
(t.release_year < $3::int) OR
(t.release_year = $3::int AND t.id < $4::bigint)
($4::int IS NULL) OR
(t.release_year < $4::int) OR
(t.release_year = $4::int AND t.id < $5::bigint)
WHEN 'rating' THEN
($5::float IS NULL) OR
(t.rating < $5::float) OR
(t.rating = $5::float AND t.id < $4::bigint)
($6::float IS NULL) OR
(t.rating < $6::float) OR
(t.rating = $6::float AND t.id < $5::bigint)
WHEN 'id' THEN
($4::bigint IS NULL) OR
(t.id < $4::bigint)
($5::bigint IS NULL) OR
(t.id < $5::bigint)
ELSE true
END
@ -702,7 +704,7 @@ WHERE
AND (
CASE
WHEN $6::text IS NOT NULL THEN
WHEN $7::text IS NOT NULL THEN
(
SELECT bool_and(
EXISTS (
@ -714,7 +716,7 @@ WHERE
FROM unnest(
ARRAY(
SELECT '%' || trim(w) || '%'
FROM unnest(string_to_array($6::text, ' ')) AS w
FROM unnest(string_to_array($7::text, ' ')) AS w
WHERE trim(w) <> ''
)
) AS pattern
@ -724,49 +726,50 @@ WHERE
)
AND (
$7::title_status_t[] IS NULL
OR array_length($7::title_status_t[], 1) IS NULL
OR array_length($7::title_status_t[], 1) = 0
OR t.title_status = ANY($7::title_status_t[])
$8::title_status_t[] IS NULL
OR array_length($8::title_status_t[], 1) IS NULL
OR array_length($8::title_status_t[], 1) = 0
OR t.title_status = ANY($8::title_status_t[])
)
AND (
$8::usertitle_status_t[] IS NULL
OR array_length($8::usertitle_status_t[], 1) IS NULL
OR array_length($8::usertitle_status_t[], 1) = 0
OR u.status = ANY($8::usertitle_status_t[])
$9::usertitle_status_t[] IS NULL
OR array_length($9::usertitle_status_t[], 1) IS NULL
OR array_length($9::usertitle_status_t[], 1) = 0
OR u.status = ANY($9::usertitle_status_t[])
)
AND ($9::int IS NULL OR u.rate >= $9::int)
AND ($10::float IS NULL OR t.rating >= $10::float)
AND ($11::int IS NULL OR t.release_year = $11::int)
AND ($12::release_season_t IS NULL OR t.release_season = $12::release_season_t)
AND ($10::int IS NULL OR u.rate >= $10::int)
AND ($11::float IS NULL OR t.rating >= $11::float)
AND ($12::int IS NULL OR t.release_year = $12::int)
AND ($13::release_season_t IS NULL OR t.release_season = $13::release_season_t)
GROUP BY
t.id, i.id, s.id
t.id, u.user_id, u.status, u.rate, u.review_id, u.ctime, i.id, s.id
ORDER BY
CASE WHEN $1::boolean THEN
CASE WHEN $2::boolean THEN
CASE
WHEN $2::text = 'id' THEN t.id
WHEN $2::text = 'year' THEN t.release_year
WHEN $2::text = 'rating' THEN t.rating
WHEN $2::text = 'rate' THEN u.rate
WHEN $3::text = 'id' THEN t.id
WHEN $3::text = 'year' THEN t.release_year
WHEN $3::text = 'rating' THEN t.rating
WHEN $3::text = 'rate' THEN u.rate
END
END ASC,
CASE WHEN NOT $1::boolean THEN
CASE WHEN NOT $2::boolean THEN
CASE
WHEN $2::text = 'id' THEN t.id
WHEN $2::text = 'year' THEN t.release_year
WHEN $2::text = 'rating' THEN t.rating
WHEN $2::text = 'rate' THEN u.rate
WHEN $3::text = 'id' THEN t.id
WHEN $3::text = 'year' THEN t.release_year
WHEN $3::text = 'rating' THEN t.rating
WHEN $3::text = 'rate' THEN u.rate
END
END DESC,
CASE WHEN $2::text <> 'id' THEN t.id END ASC
CASE WHEN $3::text <> 'id' THEN t.id END ASC
LIMIT COALESCE($13::int, 100)
LIMIT COALESCE($14::int, 100)
`
type SearchUserTitlesParams struct {
UserID int64 `json:"user_id"`
Forward bool `json:"forward"`
SortBy string `json:"sort_by"`
CursorYear *int32 `json:"cursor_year"`
@ -808,6 +811,7 @@ type SearchUserTitlesRow struct {
// 100 is default limit
func (q *Queries) SearchUserTitles(ctx context.Context, arg SearchUserTitlesParams) ([]SearchUserTitlesRow, error) {
rows, err := q.db.Query(ctx, searchUserTitles,
arg.UserID,
arg.Forward,
arg.SortBy,
arg.CursorYear,