search: add way to search array attributes by regex.

Add a `where_any_in_array_matches_regex` method and expose it to the API:

 * https://danbooru.donmai.us/artists?search[any_other_name_matches_regex]=^blah
 * https://danbooru.donmai.us/wiki_pages?search[any_other_name_matches_regex]=^blah
 * https://danbooru.donmai.us/saved_searches?search[any_label_matches_regex]=^blah

In SQL, this does `WHERE '^blah' ~<< ANY(other_names)`, where `~<<` is a
custom operator based on the `~` regex match operator, but with the
arguments reversed. This allows it to be used with the ANY(array) operator.

See also:

* https://stackoverflow.com/a/22101172
* https://www.postgresql.org/docs/current/sql-createfunction.html
* https://www.postgresql.org/docs/current/sql-createoperator.html
* https://www.postgresql.org/docs/current/functions-comparisons.html
This commit is contained in:
evazion
2021-01-09 20:19:49 -06:00
parent 65adcd09c2
commit 9759701071
4 changed files with 46 additions and 3 deletions

View File

@@ -352,6 +352,15 @@ CREATE FUNCTION public.favorites_insert_trigger() RETURNS trigger
$$;
--
-- Name: reverse_textregexeq(text, text); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.reverse_textregexeq(text, text) RETURNS boolean
LANGUAGE sql IMMUTABLE PARALLEL SAFE
AS $_$ SELECT textregexeq($2, $1); $_$;
--
-- Name: testprs_end(internal); Type: FUNCTION; Schema: public; Owner: -
--
@@ -388,6 +397,17 @@ CREATE FUNCTION public.testprs_start(internal, integer) RETURNS internal
AS '$libdir/test_parser', 'testprs_start';
--
-- Name: ~<<; Type: OPERATOR; Schema: public; Owner: -
--
CREATE OPERATOR public.~<< (
FUNCTION = public.reverse_textregexeq,
LEFTARG = text,
RIGHTARG = text
);
--
-- Name: testparser; Type: TEXT SEARCH PARSER; Schema: public; Owner: -
--
@@ -7849,6 +7869,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20210106212805'),
('20210108030722'),
('20210108030723'),
('20210108030724');
('20210108030724'),
('20210110015410');