SELECT record_maps.map, category, points, stars, mappers, release, width, height, tiles
    FROM record_maps JOIN record_mapinfo_array ON record_maps.map = record_mapinfo_array.map
        WHERE ($1::TEXT IS NULL OR record_maps.map ILIKE $1)
        AND (ARRAY_LENGTH(ARRAY[]::TEXT[], 1) > 0 IS TRUE OR $2::TEXT[] <@ mappers::TEXT[])
        AND ($3::TEXT IS NULL OR category = $3)
        AND ($4::SMALLINT[] IS NULL OR stars = ANY($4))
        AND ($5::TIMESTAMP IS NULL OR release >= $5)
        AND ($6::TIMESTAMP IS NULL OR release <= $6)
        AND (ARRAY_LENGTH(ARRAY[]::TEXT[], 1) > 0 IS TRUE OR $7::TEXT[] <@ tiles::TEXT[])
        ORDER BY -- what in the flying fuck
         (case when lower($8) = 'map' and lower($9) = 'asc' then record_maps.map end) asc,
         (case when lower($8) = 'map' and lower($9) = 'desc' then record_maps.map end) desc,
         (case when lower($8) = 'category' and lower($9) = 'asc' then category end) asc,
         (case when lower($8) = 'category' and lower($9) = 'desc' then category end) desc,
         (case when lower($8) = 'points' and lower($9) = 'asc' then points::INTEGER end) asc,
         (case when lower($8) = 'points' and lower($9) = 'desc' then points::INTEGER end) desc,
         (case when lower($8) = 'stars' and lower($9) = 'asc' then stars::INTEGER end) asc,
         (case when lower($8) = 'stars' and lower($9) = 'desc' then stars::INTEGER end) desc,
         (case when lower($8) = 'width' and lower($9) = 'asc' then width::INTEGER end) asc,
         (case when lower($8) = 'width' and lower($9) = 'desc' then width::INTEGER end) desc,
         (case when lower($8) = 'height' and lower($9) = 'asc' then height::INTEGER end) asc,
         (case when lower($8) = 'height' and lower($9) = 'desc' then height::INTEGER end) desc